-
Notifications
You must be signed in to change notification settings - Fork 2
/
dispatcher.go
73 lines (64 loc) · 2.16 KB
/
dispatcher.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
package main
import (
"errors"
"github.com/PiotrTopa/js8web/model"
)
// JS8Call uses the same event name for two different kinds of events.
// STATION.STATUS is used for both current RIG status (freq, selected callsign)
// and as an answer for "STATION.GET_STATUS" command with value of STATUS message.
// Here we set the former to "RIG.STATUS" and leave the latter untached.
func fixSameNameForDifferentStationStatusEvents(events <-chan model.Js8callEvent) <-chan model.Js8callEvent {
fixedEvents := make(chan model.Js8callEvent, 1)
go func() {
defer close(fixedEvents)
for event := range events {
if event.Type == model.EVENT_TYPE_STATION_STATUS {
if event.Params.Freq > 0 {
event.Type = model.EVENT_TYPE_RIG_STATUS
}
}
fixedEvents <- event
}
}()
return fixedEvents
}
func dispatchStateChangeEvents(events <-chan model.Js8callEvent) (<-chan model.WebsocketEvent, <-chan model.DbObj) {
events = fixSameNameForDifferentStationStatusEvents(events)
websocketEvents := make(chan model.WebsocketEvent, 1)
dbObjects := make(chan model.DbObj, 1)
go func() {
defer close(websocketEvents)
defer close(dbObjects)
f := defaultNotifier
for event := range events {
switch event.Type {
case model.EVENT_TYPE_RX_ACTIVITY, model.EVENT_TYPE_RX_DIRECTED, model.EVENT_TYPE_RX_DIRECTED_ME:
f = rxActivityNotifier
case model.EVENT_TYPE_RX_SPOT:
f = rxSpotNotifier
case model.EVENT_TYPE_TX_FRAME:
f = txFrameNotifier
case model.EVENT_TYPE_RIG_STATUS:
f = rigStatusNotifier
case model.EVENT_TYPE_STATION_CALLSIGN, model.EVENT_TYPE_STATION_GRID, model.EVENT_TYPE_STATION_INFO, model.EVENT_TYPE_STATION_STATUS:
f = stationInfoNotifier
case model.EVENT_TYPE_RIG_PTT:
f = rigPttNotifier
default:
f = defaultNotifier
}
err := f(&event, websocketEvents, dbObjects)
if err != nil {
logger.Sugar().Errorw(
"Error dispatching event",
"event", event,
"error", err,
)
}
}
}()
return websocketEvents, dbObjects
}
func defaultNotifier(event *model.Js8callEvent, websocketEvents chan<- model.WebsocketEvent, databaseObjects chan<- model.DbObj) error {
return errors.New("unrecognized event type")
}