This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateHolder.go
83 lines (71 loc) · 2.21 KB
/
stateHolder.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
package main
import (
"encoding/json"
"github.com/gswly/gomavlib"
"github.com/gswly/gomavlib/dialects/common"
)
const GlobalPositionInt = 33
const AttitudeInt = 30
const RawGps = 24
const Hud = 74
const SysStatus = 1
type stateHolder struct {
stateData stateData
db DbWrapper
}
type stateData struct {
GpsData *GpsData
TelemetryData *TelemetryData
GlobalPosition *common.MessageGlobalPositionInt
Attitude *common.MessageAttitude
GpsRaw *common.MessageGpsRawInt
HudData *common.MessageVfrHud
}
func (s *stateHolder) startStateHolder(packetChan chan *gomavlib.EventFrame, dbFilename string) {
s.stateData = stateData{}
s.stateData.TelemetryData = &TelemetryData{}
s.stateData.GpsData = &GpsData{}
s.db = DbWrapper{}
s.db.initialize(dbFilename)
var packet *gomavlib.EventFrame
for {
packet = <-packetChan
s.processPacket(packet)
}
}
func (s *stateHolder) processPacket(packet *gomavlib.EventFrame) {
if gps, ok := packet.Message().(*common.MessageGlobalPositionInt); ok {
s.stateData.GlobalPosition = gps
s.stateData.TelemetryData.SetGlobalPosition(gps)
s.stateData.GpsData.SetGlobalPositionInt(gps)
s.insertIntoDb(s.stateData.GlobalPosition, GlobalPositionInt)
}
if att, ok := packet.Message().(*common.MessageAttitude); ok {
s.stateData.Attitude = att
s.stateData.TelemetryData.SetAttitude(att)
s.insertIntoDb(s.stateData.Attitude, AttitudeInt)
}
if mess, ok := packet.Message().(*common.MessageGpsRawInt); ok {
s.stateData.GpsData.SetRawGps(mess)
s.stateData.TelemetryData.SetRawGps(mess)
s.stateData.GpsRaw = mess
s.insertIntoDb(s.stateData.GpsRaw, RawGps)
}
if mess, ok := packet.Message().(*common.MessageVfrHud); ok {
s.stateData.HudData = mess
s.stateData.TelemetryData.SetAirSpeed(mess)
s.insertIntoDb(s.stateData.HudData, Hud)
}
if status, ok := packet.Message().(*common.MessageSysStatus); ok {
s.stateData.TelemetryData.SetBatteryStatus(status)
s.insertIntoDb(status, SysStatus)
}
//TODO add information about last update time
}
func (s *stateHolder) insertIntoDb(object interface{}, dataType int) {
jsonEncoded, err := json.Marshal(s.stateData.TelemetryData)
if err != nil {
panic(err)
}
s.db.insert(dataType, string(jsonEncoded))
}