/
oyster-guardian.go
58 lines (50 loc) · 1.3 KB
/
oyster-guardian.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
package service
import (
"encoding/json"
"time"
"github.com/IPreferWater/oyster-guardian/model"
log "github.com/sirupsen/logrus"
)
func HandleTopicDetected(payload string) {
var detected model.Detected
if err := json.Unmarshal([]byte(payload), &detected); err != nil {
log.Error(err)
return
}
typeDetected := getTypeDetected(detected.ImageUrl)
threatPoints, err := getThreatPoints(detected, typeDetected)
if err != nil {
log.Error(err)
return
}
if threatPoints > 20 {
Stream.PublishTopicThreat(payload)
}
Repository.InsertDetected(detected)
}
func HandleTopicThreat(payload string) {
log.Infof("ALERT ! There might be a threat on your farm on the location => %s", payload)
}
func getThreatPoints(detected model.Detected, typeDetected model.TypeDetected) (int, error) {
points := 0
switch typeDetected {
case model.Car, model.Truck:
//TODO vehicules
break
case model.Human, model.HumanPack:
t, err := time.Parse("2006-01-02 15:04:05", detected.Timestamp)
if err != nil {
return points, err
}
// Get the hour from the time.Time value.
hour := t.Hour()
// having people in this time slot is strange
if hour > 22 || hour < 6 {
points += 100
}else {
// people without abilitation is strange, but could be hikers at this time slot
points += 30
}
}
return points, nil
}