/
core.go
135 lines (114 loc) · 2.87 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Package core
// Created by RTT.
// Author: teocci@yandex.com on 2021-Sep-27
package core
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/aler9/gomavlib"
"github.com/aler9/gomavlib/pkg/dialects/ardupilotmega"
"github.com/teocci/go-mavlink-parser/src/csvmgr"
"github.com/teocci/go-mavlink-parser/src/datamgr"
"github.com/teocci/go-mavlink-parser/src/model"
"github.com/teocci/go-mavlink-parser/src/wsnet"
)
var (
initConf datamgr.InitConf
rtt *datamgr.RTT
ws *wsnet.Client
csvl *csvmgr.CSVLogger
dbl *DBLogger
headerSent = false
)
func Start(c datamgr.InitConf) error {
pid := os.Getpid()
fmt.Println("PID:", pid)
initConf = c
address := fmt.Sprintf("%s:%d", initConf.Host, initConf.Port)
// create a node which
// - communicates with a TCP endpoint in client mode
// - understands ardupilotmega dialect
// - writes messages with given system id
node, err := gomavlib.NewNode(gomavlib.NodeConf{
Endpoints: []gomavlib.EndpointConf{
gomavlib.EndpointTCPClient{Address: address},
},
Dialect: ardupilotmega.Dialect,
OutVersion: gomavlib.V2,
OutSystemID: 10,
})
if err != nil {
panic(err)
}
defer node.Close()
// Init db
db = model.Setup()
defer db.Close()
drone := &model.Drone{ID: initConf.DroneID}
droneExits := drone.Select(db)
if droneExits {
initConf.CompanyID = drone.CompanyID
log.Printf("Drone: %d exits. Get company id: %d", drone.ID, drone.CompanyID)
}
// Init websocket
ws = wsnet.NewClient(initConf)
// Init CSV logger
csvl = csvmgr.NewCSVLogger(initConf)
// Init CSV logger
dbl = NewDBLogger(initConf)
var seq int64 = 0
var trigger = 0
for event := range node.Events() {
if frm, ok := event.(*gomavlib.EventFrame); ok {
//fmt.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message())
if trigger == 0 {
rtt = &datamgr.RTT{
DroneID: initConf.DroneID,
FlightID: initConf.FlightID,
}
}
switch msg := frm.Message().(type) {
case *ardupilotmega.MessageHeartbeat:
//fmt.Printf("received heartbeat (type %d)\n", msg.Type)
case *ardupilotmega.MessageAttitude:
rtt.Roll = msg.Roll
rtt.Pitch = msg.Pitch
rtt.Yaw = msg.Yaw
trigger |= 1
case *ardupilotmega.MessageGlobalPositionInt:
rtt.TimeBootMs = msg.TimeBootMs
rtt.Lat = msg.Lat
rtt.Lon = msg.Lon
rtt.Alt = msg.Alt // convert to meters
rtt.LastUpdate = time.Now()
trigger |= 2
}
if trigger&2 == 2 {
rtt.Seq = seq
process(rtt)
trigger = 0
seq++
}
}
}
return nil
}
func process(rtt *datamgr.RTT) {
req := &datamgr.UpdateTelemetry{
CMD: wsnet.CMDUpdateTelemetry,
ToConnID: initConf.ConnID,
ModuleTag: initConf.ModuleTag,
DroneID: initConf.DroneID,
Record: *rtt,
}
jsonData, err := json.Marshal(req)
if err != nil {
log.Printf("process(): %v", err)
}
ws.Send <- jsonData
appendRecord(rtt)
insertRecord(rtt)
}