-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
131 lines (99 loc) · 3.37 KB
/
message.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
package models
import (
"fmt"
"log"
"time"
gongsim_models "github.com/fullstack-lang/gongsim/go/models"
geo "github.com/kellydunn/golang-geo"
)
// Message
// swagger:model message
type Message struct {
MovingObject // concept
// Agent
// swagger:ignore
gongsim_models.Agent
State MessageStateEnum
Name string
// Control of the aircraft
TargetLocationLat float64
TargetLocationLng float64
DistanceToTarget float64
// time stamps of the message
timestamp time.Time
Timestampstring string
// time stamps of the message at the beginning
timestampstart time.Time
DurationSinceSimulationStart time.Duration
Timestampstartstring string
From LatLngAgentInterface `gorm:"-"`
Source string // string version of from
To LatLngAgentInterface `gorm:"-"`
Destination string // string version of to
Content string
About_string string
Display bool
}
func (*Message) GetLayerGroupName() (name string) { return string(Aircraft_) }
func (Message *Message) Register(engine *gongsim_models.Engine) (res *Message) {
gongsim_models.AppendToSingloton(engine, Message)
res = Message
return
}
// MessageStateEnum ..
// swagger:enum MessageStateEnum
type MessageStateEnum string
// state
const (
MESSAGE_EN_ROUTE MessageStateEnum = "MESSAGE_EN_ROUTE"
MESSAGE_ARRIVED MessageStateEnum = "MESSAGE_ARRIVED"
)
// FireNextEvent fire next Event
func (message *Message) FireNextEvent() {
event, _ := message.GetNextEventAndRemoveIt()
switch event.(type) {
case *gongsim_models.UpdateState:
checkStateEvent := event.(*gongsim_models.UpdateState)
// post next event
checkStateEvent.SetFireTime(checkStateEvent.GetFireTime().Add(checkStateEvent.Period))
message.QueueEvent(checkStateEvent)
//
// Update Speed & Heading
//
// Change heading toward target location
messagePoint := geo.NewPoint(message.Lat, message.Lng)
targetLocation := geo.NewPoint(message.TargetLocationLat, message.TargetLocationLng)
message.Heading = messagePoint.BearingTo(targetLocation)
// Update speed
message.DistanceToTarget = targetLocation.GreatCircleDistance(messagePoint)
if message.DistanceToTarget < 10.0 { // 10 km.
message.Speed = 0.0
message.Display = false
message.State = MESSAGE_ARRIVED
}
// update state vector of the message
orig := geo.NewPoint(message.Lat, message.Lng)
elapsedTimeInHours := checkStateEvent.Period.Hours()
distance := message.Speed * elapsedTimeInHours // since speed is in km/h, the distance in km
nextPoint := orig.PointAtDistanceAndBearing(
distance,
message.Heading)
message.Lat = nextPoint.Lat()
message.Lng = nextPoint.Lng()
default:
err := fmt.Sprintf("unkown event type %T", event)
log.Panic(err)
}
}
// functions to satisty the visual interface for track
func (message *Message) GetLat() float64 { return message.Lat }
func (message *Message) GetLng() float64 { return message.Lng }
func (message *Message) GetHeading() float64 { return message.Heading }
// speed is displayed in tens of nautical miles
func (message *Message) GetSpeed() float64 {
return message.Speed / 18.52
}
func (message *Message) GetVerticalSpeed() float64 { return 0 }
func (message *Message) GetLevel() float64 { return 0 }
func (message *Message) GetDisplay() bool { return message.Display }
func (message *Message) GetVisualLayerName() string { return "" }