-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
149 lines (127 loc) · 2.84 KB
/
sensor.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
package main
import (
"git.sapienzaapps.it/SeismoCloud/seismocloud-sensor-raspberrypi/config"
"git.sapienzaapps.it/seismocloud/seismocloud-client-go/scsclient"
"os"
"os/signal"
"syscall"
"time"
)
func sensor() {
var err error
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
log.Info("Logging started")
// Init LEDS
err = ledset.Init()
if err != nil {
log.Error(err.Error())
os.Exit(-1)
}
ledset.StartLoading()
// Load config if exists
log.Info("Loading config")
cfg, err = config.New()
if err != nil {
log.Error("error loading configuration: ", err)
os.Exit(-3)
}
log.Info("Device Id:", cfg.GetDeviceId(), "Sigma:", cfg.GetSigma())
// TODO: check for updates
// Setup client
log.Info("Connecting to MQTT Server", config.MQTT_SERVER)
scs, err = scsclient.New(scsclient.ClientOptions{
DeviceId: cfg.GetDeviceId(),
Model: config.MODEL,
Version: config.VERSION,
OnNewSigma: onNewSigma,
OnReboot: onReboot,
OnStreamCommand: nil,
OnProbeSpeedSet: nil,
OnTimeReceived: onTimeReceived,
SeismoCloudBroker: config.MQTT_SERVER,
Username: "embedded",
Password: "embedded",
})
if err != nil {
panic(err)
}
// Try to connect indefinitely
for {
err = scs.Connect()
if err != nil {
// Connect error - retry in 10s if not interrupted
ledset.StopLoading()
_ = ledset.Yellow(true)
log.Error(err.Error())
select {
case <-sigs:
os.Exit(0)
default:
time.Sleep(10 * time.Second)
}
} else {
break
}
}
// Connect OK
ledset.StartLoading()
// NTP sync
checkTime()
servertimeLastCheck := time.Now()
// Start local broadcaster
log.Info("Starting LAN interface")
StartLANInterface(cfg.GetDeviceId())
// Init seismometer
log.Info("Init Seismometer")
seismometer, err := CreateNewSeismometer(cfg.GetSigma())
if err != nil {
panic(err)
}
ledset.StopLoading()
_ = ledset.StartupBlink()
_ = ledset.Green(true)
log.Info("Ready")
seismometer.StartSeismometer()
running := true
for running {
// Check internet every X minutes
if !scs.IsConnected() {
_ = ledset.Green(false)
_ = ledset.Yellow(true)
_ = ledset.Red(false)
for {
err = scs.Connect()
if err != nil {
log.Error(err.Error())
select {
case <-sigs:
os.Exit(-2)
default:
time.Sleep(10 * time.Second)
}
} else {
break
}
}
_ = ledset.Green(true)
_ = ledset.Yellow(false)
_ = ledset.Red(false)
}
// Check server time
if time.Now().Sub(servertimeLastCheck).Hours() >= 24 {
checkTime()
servertimeLastCheck = time.Now()
}
// Check if termination is requested
select {
case <-sigs:
running = false
case <-time.After(1 * time.Second):
}
}
seismometer.StopSeismometer()
log.Info("Stopping LAN interface")
StopLANInterface()
log.Info("End")
}