-
Notifications
You must be signed in to change notification settings - Fork 1
/
camar.go
274 lines (233 loc) · 8.25 KB
/
camar.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
// Package camar is main code file for this service.
// This package will contain every single business logic needed for the service
// benCAna Monitoring and AleRting
package camar
import (
"context"
"github.com/globalsign/mgo/bson"
"github.com/pamungkaski/camar/datamodel"
"github.com/pamungkaski/camar/grabber"
"github.com/pamungkaski/camar/notifier"
"github.com/pamungkaski/camar/recorder"
"github.com/pkg/errors"
"fmt"
)
// DisasterReporter is the business logic contract for camar service.
// The main idea of the interface is to record disaster into database then alert all service's device.
type DisasterReporter interface {
// ListenTheEarth is a function that Listen to any Earthquake happen.
// It is the main function of DisasterReporter Interface
ListenTheEarth() error
// RecordDisaster is a function to save Disaster into our database.
RecordDisaster(ctx context.Context, disaster datamodel.Event) error
//
GetEarthquakeList(ctx context.Context, limit, page int) ([]datamodel.CamarQuakeData, int, error)
// AlertDisastrousEvent is a function to alert service's device.
GetEarthquake(ctx context.Context, ID string) (datamodel.CamarQuakeData, error)
//
AlertDisastrousEvent(ctx context.Context, disaster interface{}, coor []float64) error
// NewDevice is a function to save new device device for alerting purpose.
NewDevice(ctx context.Context, device datamodel.Device) (datamodel.Device, error)
// GetDevice
GetDevice(ctx context.Context, deviceID string) (datamodel.Device, error)
// UpdateDevice is a function to update device latitude and longitude coordinate.
UpdateDevice(ctx context.Context, device datamodel.Device) (datamodel.Device, error)
//
GetAllDevice(ctx context.Context) ([]datamodel.Device, error)
// NewEvent is a function to save new event for alerting purpose.
NewEvent(ctx context.Context, event datamodel.Event) (datamodel.Event, error)
// GetEvent
GetEvent(ctx context.Context, eventID string) (datamodel.Event, error)
// UpdateEvent is a function to update event latitude and longitude coordinate.
UpdateEvent(ctx context.Context, event datamodel.Event) (datamodel.Event, error)
//
DeleteEvent(ctx context.Context, eventID string) (error)
//
GetAllEvent(ctx context.Context, limit, page int, eventType string) ([]datamodel.Event, int, error)
}
// Camar is the main struct of the service.
// Camar implements DisasterReporter interface.
// It contains alerting and recorder interface implementation.
type Camar struct {
grabber grabber.ResourceGrabber
recorder recorder.Recorder
notifer notifier.Notifier
}
//NewDisasterReporter is a function that creates an instance of DisasterReporter.
func NewDisasterReporter(grabber grabber.ResourceGrabber, recorder recorder.Recorder, notifier notifier.Notifier) DisasterReporter {
return &Camar{
recorder: recorder,
grabber: grabber,
notifer: notifier,
}
}
// ListenTheEarth is a function that Listen to any Earthquake happen.
// It is the main function of DisasterReporter Interface
func (c *Camar) ListenTheEarth() error {
latest, err := c.grabber.GetEarthquakes()
if err != nil {
return err
}
quakes, _, err := c.recorder.GetAllEvent(1, 1, "earthquake")
if err != nil {
return nil
}
if len(latest) != 0{
if len(quakes) != 0 {
if !c.verifyQuake(latest[0], quakes[0]) {
fmt.Println(latest[0].EventDetail)
if err := c.RecordDisaster(context.Background(), latest[0]); err != nil {
return err
}
if err := c.AlertDisastrousEvent(context.Background(), latest[0], latest[0].Location.Coordinates); err != nil {
return err
}
}
} else {
fmt.Println(latest[0].EventDetail)
if err := c.RecordDisaster(context.Background(), latest[0]); err != nil {
return err
}
if err := c.AlertDisastrousEvent(context.Background(), latest[0], latest[0].Location.Coordinates); err != nil {
return err
}
}
}
return nil
}
func (c *Camar) verifyQuake(first, second datamodel.Event) bool {
if first.Time != second.Time {
return false
}
if first.Location.Coordinates[0] != first.Location.Coordinates[0] {
return false
}
return first.Location.Coordinates[1] == first.Location.Coordinates[1]
}
func (c *Camar) GetEarthquakeList(ctx context.Context, limit, page int) ([]datamodel.CamarQuakeData, int, error) {
var snapList []datamodel.CamarQuakeData
snapList, count, err := c.recorder.GetEarthquakeList(limit, page)
if err != nil {
return nil, count, err
}
return snapList, count, nil
}
// RecordDisaster is a function to save Disaster into our database.
func (c *Camar) RecordDisaster(ctx context.Context, event datamodel.Event) error {
event.ID = bson.NewObjectId()
return c.recorder.NewEvent(event)
}
// GetEarthquake return CamarQuakeData with specified ID.
func (c *Camar) GetEarthquake(ctx context.Context, ID string) (datamodel.CamarQuakeData, error) {
return c.recorder.GetEarthquake(ID)
}
// AlertDisastrousEvent is a function to alert service's device.
func (c *Camar) AlertDisastrousEvent(ctx context.Context, disaster interface{}, coor []float64) error {
var errs []error
errc := make(chan error)
devices, err := c.recorder.GetDeviceInRadius([]float64{coor[0], coor[1]}, 1.36)
if err != nil {
return errors.Wrap(err, "AlertDisastrousEvent error on getting device in radius")
}
length := len(devices)
for _, device := range devices {
go c.notifer.SendAlert(disaster, device.Token, errc)
}
for i := 0; i < length; i++ {
errsx := <-errc
if errsx != nil {
errs = append(errs, errsx)
}
}
if len(errs) > 0 {
return errs[0]
}
return nil
}
// NewDevice is a function to save new device device for alerting purpose.
func (c *Camar) NewDevice(ctx context.Context, device datamodel.Device) (datamodel.Device, error) {
dev, err := c.recorder.GetDevice(device.DeviceID)
if err != nil {
device.ID = bson.NewObjectId()
if err := c.recorder.NewDevice(device); err != nil {
return datamodel.Device{}, err
}
return device, nil
}
return dev, nil
}
// UpdateDevice is a function to update device latitude and logitude coordinate.
func (c *Camar) GetDevice(ctx context.Context, deviceid string) (datamodel.Device, error) {
device, err := c.recorder.GetDevice(deviceid)
if err != nil {
return datamodel.Device{}, err
}
return device, nil
}
// UpdateDevice is a function to update device latitude and logitude coordinate.
func (c *Camar) GetAllDevice(ctx context.Context) ([]datamodel.Device, error) {
devices, err := c.recorder.GetAllDevice()
if err != nil {
return nil, err
}
return devices, nil
}
// UpdateDevice is a function to update device latitude and logitude coordinate.
func (c *Camar) UpdateDevice(ctx context.Context, device datamodel.Device) (datamodel.Device, error) {
dev, err := c.recorder.GetDevice(device.DeviceID)
if err != nil {
return dev, err
}
dev.Location = device.Location
if err := c.recorder.UpdateDevice(dev); err != nil {
return datamodel.Device{}, err
}
return dev, nil
}
func (c *Camar) NewEvent(ctx context.Context, event datamodel.Event) (datamodel.Event, error) {
event.ID = bson.NewObjectId()
if err := c.recorder.NewEvent(event); err != nil {
return datamodel.Event{}, err
}
go c.AlertDisastrousEvent(context.Background(), event, event.Location.Coordinates)
return event, nil
}
// GetEvent
func (c *Camar) GetEvent(ctx context.Context, eventID string) (datamodel.Event, error) {
event, err := c.recorder.GetEvent(eventID)
if err != nil {
return datamodel.Event{}, err
}
return event, nil
}
// UpdateEvent is a function to update event latitude and longitude coordinate.
func (c *Camar) UpdateEvent(ctx context.Context, event datamodel.Event) (datamodel.Event, error) {
eve, err := c.recorder.GetEvent(event.ID.String())
if err != nil {
return eve, err
}
if err := c.recorder.UpdateEvent(event); err != nil {
return datamodel.Event{}, err
}
return event, nil
}
//
func (c *Camar) DeleteEvent(ctx context.Context, eventID string) (error) {
eve, err := c.recorder.GetEvent(eventID)
if err != nil {
return err
}
if err := c.recorder.DeleteEvent(eve); err != nil {
return err
}
return nil
}
//
func (c *Camar) GetAllEvent(ctx context.Context, limit, page int, eventType string) ([]datamodel.Event, int, error) {
var snapList []datamodel.Event
snapList, count, err := c.recorder.GetAllEvent(limit, page, eventType)
if err != nil {
return nil, count, err
}
return snapList, count, nil
}