-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
156 lines (135 loc) · 4.05 KB
/
messages.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
package main
import (
"errors"
"geeo.io/GeeoServer/quad"
)
// JSONChangeMessage tags update messages sent to clients
type JSONChangeMessage interface{}
// JSONCommand holds WS messages
type JSONCommand struct {
AgentPosition *quad.Point `json:"agentPosition"`
AgentPublicData map[string]interface{} `json:"publicData"`
ViewPosition *quad.Rect `json:"viewPosition"`
CreatePOI *JSONPOI `json:"createPOI"`
RemovePOI *JSONPOI `json:"removePOI"`
CreateAirBeacon *JSONAirBeacon `json:"createAirBeacon"`
RemoveAirBeacon *JSONAirBeacon `json:"removeAirBeacon"`
// TODO add messages and geo events
//SendMessage *JSONMessage `json:"sendMessage"`
//SendEvent *JSONEvent `json:"sendMessage"`
}
func (j *JSONCommand) check() error {
if j.AgentPosition != nil && !j.AgentPosition.IsValid() {
return errors.New("Invalid agentPosition")
}
if j.ViewPosition != nil {
// TODO we don't support wrapping around yet
if j.ViewPosition[0] >= j.ViewPosition[2] ||
j.ViewPosition[2] <= j.ViewPosition[0] {
j.ViewPosition[0] = -180
j.ViewPosition[2] = 180
}
if !j.ViewPosition.IsValid() {
return errors.New("Invalid viewPosition")
}
}
if j.CreatePOI != nil && !j.CreatePOI.Pos.IsValid() {
return errors.New("Invalid POI position")
}
if j.RemovePOI != nil && j.RemovePOI.ID == nil {
return errors.New("Invalid POI ID")
}
if j.CreateAirBeacon != nil && !j.CreateAirBeacon.Pos.IsValid() {
return errors.New("Invalid AirBeacon position")
}
if j.RemoveAirBeacon != nil && j.RemoveAirBeacon.ID == nil {
return errors.New("Invalid air beacon ID")
}
return nil
}
func (j *JSONCommand) clear() {
j.AgentPosition = nil
j.AgentPublicData = nil
j.ViewPosition = nil
j.CreatePOI = nil
j.RemovePOI = nil
j.CreateAirBeacon = nil
j.RemoveAirBeacon = nil
}
// EnteredLeft is used to provide additional enter/leave information
type EnteredLeft struct {
Entered bool `json:"entered,omitempty"`
Left bool `json:"left,omitempty"`
}
func (e *EnteredLeft) clearEnteredLeft() {
e.Entered = false
e.Left = false
}
// JSONPOI holds POI messages
type JSONPOI struct {
ID *string `json:"poi_id"`
Pos *quad.Point `json:"pos,omitempty"`
PublicData map[string]interface{} `json:"publicData,omitempty"`
Creator *string `json:"creator,omitempty"`
}
func (p *JSONPOI) clear() {
p.ID = nil
p.Pos = nil
p.PublicData = nil
p.Creator = nil
}
// JSONAirBeacon holds AirBeacon messages
type JSONAirBeacon struct {
ID *string `json:"ab_id"`
Pos *quad.Rect `json:"pos,omitempty"`
PublicData map[string]interface{} `json:"publicData,omitempty"`
Creator *string `json:"creator,omitempty"`
}
// JSONAgent describes the public view on agents
type JSONAgent struct {
ID *string `json:"agent_id"`
Pos *quad.Point `json:"pos,omitempty"`
PublicData map[string]interface{} `json:"publicData,omitempty"`
}
func (a *JSONAgent) clear() {
a.ID = nil
a.Pos = nil
a.PublicData = nil
}
// JSONPOIEnteredLeft holds POI enter/leave messages
type JSONPOIEnteredLeft struct {
JSONChangeMessage `json:"JSONChangeMessage,omitempty"`
JSONPOI
EnteredLeft
}
func (poi *POI) enterLeaveMessage(enter bool) JSONChangeMessage {
message := &JSONPOIEnteredLeft{}
message.ID = poi.id
if enter {
message.Pos = poi.GetPoint()
message.PublicData = poi.publicData
message.Creator = poi.creator
message.Entered = true
} else {
message.Left = true
}
return message
}
// JSONAgentEnteredLeft is sent through the WS when an agent enters/leaves a view
type JSONAgentEnteredLeft struct {
JSONChangeMessage `json:"JSONChangeMessage,omitempty"`
JSONAgent
EnteredLeft
}
func (a *Agent) enterLeaveMessage(enter bool) JSONChangeMessage {
message := &JSONAgentEnteredLeft{}
message.ID = a.ID
if enter {
message.Pos = a.GetPoint()
message.PublicData = a.publicData
message.Entered = true
} else {
message.Left = true
}
return message
}