-
Notifications
You must be signed in to change notification settings - Fork 1
/
staticscm.go
285 lines (260 loc) · 7 KB
/
staticscm.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package eddnc
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"git.fractalqb.de/fractalqb/daq"
"git.fractalqb.de/fractalqb/eloc"
)
//go:generate versioner -bno build_no -pkg eddnc ./VERSION ./version.go
type Header struct {
UploaderID string
SoftwareName string
SoftwareVersion string
GatewayTimestamp time.Time `json:",omitempty"`
daq.DictAny
}
func (h *Header) Wrap(hdr daq.DictAny) {
h.UploaderID = daq.Must(hdr.AsString("uploaderID"))
h.SoftwareName = daq.Must(hdr.AsString("softwareName"))
h.SoftwareVersion = daq.Must(hdr.AsString("softwareVersion"))
h.GatewayTimestamp = hdr.TimeOr("gatewayTimestamp", time.Time{})
h.DictAny = hdr
}
type Message interface {
Timestamp() time.Time
SystemName() string
}
func trim(s string) string {
if s == "" {
return ""
}
if s[0] == '$' {
s = s[1:]
if s == "" {
return ""
}
}
if l := len(s); s[l-1] == ';' {
s = s[:l-1]
}
return s
}
type msg struct {
T time.Time
System string
}
func (m *msg) Timestamp() time.Time { return m.T }
func (m *msg) SystemName() string { return m.System }
func (m *msg) Wrap(msg daq.DictAny) {
m.T = daq.Must(msg.AsTime("timestamp"))
}
type atStation struct {
msg
StationName string
}
func (m *atStation) Wrap(msg daq.DictAny) {
m.msg.Wrap(msg)
m.System = daq.Must(msg.AsString("systemName"))
m.StationName = daq.Must(msg.AsString("stationName"))
}
type Event struct {
SchemaRef string
Header Header
Message Message
}
func (e *Event) Parse(txt []byte) error {
var gen daq.DictAny
if err := json.Unmarshal(txt, &gen); err != nil {
return err
}
return e.Wrap(gen)
}
func (e *Event) Wrap(evt daq.DictAny) (err error) {
defer eloc.RecoverAs(&err)
e.SchemaRef = daq.Must(evt.AsString("$schemaRef"))
e.Header.Wrap(daq.Must(evt.AsDictAny("header")))
switch e.SchemaRef {
case ScmURLs[Sjournal], ScmURLs[Snavbeaconscan], ScmURLs[Sscanbarycentre]:
// TODO put non-journal schemas into specific messages
jm := new(JournalMsg)
if err = jm.Wrap(daq.Must(evt.AsDictAny("message"))); err != nil {
return err
}
e.Message = jm
case ScmURLs[Scommodity]:
cm := new(CommodityMsg)
if err = cm.Wrap(daq.Must(evt.AsDictAny("message"))); err != nil {
return err
}
e.Message = cm
case ScmURLs[Sfssdiscoveryscan]:
fds := new(FSSDiscoScanMsg)
if err = fds.Wrap(daq.Must(evt.AsDictAny("message"))); err != nil {
return err
}
e.Message = fds
case ScmURLs[Scodexentry]:
cdx := new(CodexMsg)
if err = cdx.Wrap(daq.Must(evt.AsDictAny("message"))); err != nil {
return err
}
e.Message = cdx
default:
if _, ok := ScmMap[e.SchemaRef]; !ok {
return fmt.Errorf("unknown schema: '%s'", e.SchemaRef)
}
}
return err
}
// TODO
// blackmarket [ "timestamp", "systemName", "stationName", "name", "sellPrice", "prohibited" ]
// outfit [ "timestamp", "systemName", "stationName", "marketId", "modules" ]
// shipyard [ "timestamp", "systemName", "stationName", "marketId", "ships" ]
type JournalMsg struct {
msg
SystemAddr int64
StarPos [3]float64
Event string
daq.DictAny
}
func (je *JournalMsg) Wrap(msg daq.DictAny) error {
je.msg.Wrap(msg)
je.System = daq.Must(msg.AsString("StarSystem"))
je.SystemAddr = daq.Must(msg.AsInt64("SystemAddress"))
je.Event = daq.Must(msg.AsString("event"))
spos := daq.Must(msg.AsSliceAny("StarPos"))
je.StarPos = [3]float64{
daq.Must(spos.AsFloat64(0)),
daq.Must(spos.AsFloat64(1)),
daq.Must(spos.AsFloat64(2)),
}
je.DictAny = msg
return nil
}
type CommodityMsg struct {
atStation
MarketID int64
Commodities []*Commodity
}
type Commodity struct {
Name string
MeanPrice int
BuyPrice int
Stock int
StockBracket int
SellPrice int
Demand int
DemandBracket int
StatusFlags []string
}
func (cm *CommodityMsg) Wrap(msg daq.DictAny) error {
cm.atStation.Wrap(msg)
cm.MarketID = daq.Must(msg.AsInt64("marketId"))
cmdts := daq.Must(msg.AsSliceAny("commodities"))
if l := len(cmdts); cap(cm.Commodities) >= l {
cm.Commodities = cm.Commodities[:l]
} else {
cm.Commodities = make([]*Commodity, l)
}
fromIntOrStr := func(obj daq.DictAny, att string) (res int) {
val := obj[att]
if res, err := daq.ToInt(val); err == nil {
return res
}
if txt, err := daq.ToString(val); err != nil {
res, _ = strconv.Atoi(txt)
}
return
}
for i, e := range cmdts {
var src daq.DictAny = e.(map[string]any)
dst := cm.Commodities[i]
if dst == nil {
dst = new(Commodity)
cm.Commodities[i] = dst
}
dst.Name = daq.Must(src.AsString("name"))
dst.MeanPrice = daq.Must(src.AsInt("meanPrice"))
dst.BuyPrice = daq.Must(src.AsInt("buyPrice"))
dst.Stock = daq.Must(src.AsInt("stock"))
dst.StockBracket = fromIntOrStr(src, "stockBracket")
dst.SellPrice = daq.Must(src.AsInt("sellPrice"))
dst.Demand = daq.Must(src.AsInt("demand"))
dst.DemandBracket = fromIntOrStr(src, "demandBracket")
if arr, _ := src.AsSliceAny("statusFlags"); arr == nil {
dst.StatusFlags = nil
} else {
dst.StatusFlags = make([]string, len(arr))
for i, v := range arr {
dst.StatusFlags[i] = daq.Must(daq.ToString(v))
}
}
}
return nil
}
type CodexMsg struct {
msg
EntryID int64
SystemAddress int64
StarPos [3]float32
Region int16
Name string
Category string
SubCategory string
BodyID int16
BodyName string
Latitude float32
Longitude float32
}
func (cdx *CodexMsg) Wrap(msg daq.DictAny) error {
cdx.msg.Wrap(msg)
cdx.System = daq.Must(msg.AsString("System"))
cdx.EntryID = daq.Must(msg.AsInt64("EntryID"))
cdx.SystemAddress = daq.Must(msg.AsInt64("SystemAddress"))
coos := daq.Must(msg.AsSliceAny("StarPos"))
cdx.StarPos[0] = daq.Must(coos.AsFloat32(0))
cdx.StarPos[1] = daq.Must(coos.AsFloat32(1))
cdx.StarPos[2] = daq.Must(coos.AsFloat32(2))
if reg := trim(daq.Must(msg.AsString("Region"))); !strings.HasPrefix(reg, "Codex_RegionName_") {
return fmt.Errorf("illegal region name format: '%s'", reg)
} else {
reg = reg[17:]
rid, err := strconv.Atoi(reg)
if err != nil {
return err
}
cdx.Region = int16(rid)
}
cdx.Name = trim(daq.Must(msg.AsString("Name")))
cdx.Category = trim(daq.Must(msg.AsString("Category")))
cdx.SubCategory = trim(daq.Must(msg.AsString("SubCategory")))
cdx.BodyID = msg.Int16Or("BodyID", -1)
if cdx.BodyID >= 0 {
cdx.BodyName = daq.Must(msg.AsString("BodyName"))
cdx.Latitude = daq.Must(msg.AsFloat32("Latitude"))
cdx.Longitude = daq.Must(msg.AsFloat32("Longitude"))
}
return nil
}
type FSSDiscoScanMsg struct {
msg
SystemAddress int64
StarPos [3]float32
BodyCount int16
NonBodyCount int16
}
func (fds *FSSDiscoScanMsg) Wrap(msg daq.DictAny) error {
fds.msg.Wrap(msg)
fds.System = daq.Must(msg.AsString("SystemName"))
fds.SystemAddress = daq.Must(msg.AsInt64("SystemAddress"))
coos := daq.Must(msg.AsSliceAny("StarPos"))
fds.StarPos[0] = daq.Must(coos.AsFloat32(0))
fds.StarPos[1] = daq.Must(coos.AsFloat32(1))
fds.StarPos[2] = daq.Must(coos.AsFloat32(2))
fds.BodyCount = daq.Must(msg.AsInt16("BodyCount"))
fds.NonBodyCount = daq.Must(msg.AsInt16("NonBodyCount"))
return nil
}