-
Notifications
You must be signed in to change notification settings - Fork 2
/
jablotron.go
479 lines (408 loc) · 11.3 KB
/
jablotron.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var t1 time.Time
var oktimer time.Time
var JabloPIN string
var JStates map[string]string
var JPG map[string]string
var JCommands map[time.Time]string
func TestPub(jd map[int]bool, client mqtt.Client, token mqtt.Token) {
// publish to topic
for key, value := range jd {
TOP := "jablotron/device/" + fmt.Sprintf("%d", key)
var STA string
if value == true {
STA = "ON"
} else {
STA = "OFF"
}
token = client.Publish(TOP, byte(0), false, STA)
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to publish, %v", token.Error())
}
}
}
func PubPG(pgid string, pgstate string, client mqtt.Client, token mqtt.Token) {
// publish to topic
TOP := "jablotron/pg/" + pgid
pgstate = strings.TrimSpace(pgstate)
fmt.Println("Wysylam do ", TOP, "wartosc ", pgstate)
token = client.Publish(TOP, byte(0), false, pgstate)
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to publish, %v", token.Error())
}
}
type HASupervisorConfig struct {
Result string `json:"result"`
Data struct {
Host string `json:"host"`
Port int `json:"port"`
Ssl bool `json:"ssl"`
Protocol string `json:"protocol"`
Username string `json:"username"`
Password string `json:"password"`
Addon string `json:"addon"`
ClientID string `json:"clientid"`
} `json:"data"`
}
func GetMqttConfigFromHA(token string) (HASupervisorConfig, error) {
var haconfig HASupervisorConfig
url := "http://supervisor/services/mqtt"
// Create a Bearer string by appending string access token
var bearer = "Bearer " + token
// Create a new request using http
req, err := http.NewRequest("GET", url, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
return haconfig, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println("---------------------------")
log.Println(string([]byte(body)))
fmt.Println("---------------------------")
if err != nil {
log.Println("Error while reading the response bytes:", err)
return haconfig, err
}
err = json.Unmarshal(body, &haconfig)
if err != nil {
log.Println("Error while reading the response bytes:", err)
return haconfig, err
}
log.Println(string([]byte(body)))
return haconfig, nil
}
func PublishAlarm(AlarmType string, AlarmZone string, AlarmState string, client mqtt.Client, token mqtt.Token) {
AlarmState = strings.Trim(strings.TrimSpace(AlarmState), " ")
// publish to topic
AlarmType = strings.TrimSpace(AlarmType)
TOP := "jablotron/alert/" + AlarmType + "/" + AlarmZone
fmt.Println("Wysylam do ", TOP, "wartosc ", AlarmState)
token = client.Publish(TOP, byte(0), false, AlarmState)
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to publish, %v", token.Error())
}
}
var err error
func main() {
var stok string
var hacfg HASupervisorConfig
cf, err := ShowOptionsFile()
stok = os.Getenv("SUPERVISOR_TOKEN")
if len(stok) < 2 {
oa := os.Args
if len(oa) > 1 {
stok = os.Args[1]
hacfg, err = GetMqttConfigFromHA(stok)
} else {
hacfg.Data.Host = cf.MQTTHost
hacfg.Data.Password = cf.MQTTPassword
hacfg.Data.Username = cf.MQTTUser
hacfg.Data.Port = cf.MQTTPort
hacfg.Data.ClientID = cf.MQTTClientID
}
} else {
hacfg, err = GetMqttConfigFromHA(stok)
}
log.Println(stok)
fmt.Println(os.Environ())
if err != nil {
fmt.Println("Cant Read Config From Supervisor")
hacfg.Data.Host = cf.MQTTHost
hacfg.Data.Password = cf.MQTTPassword
hacfg.Data.Username = cf.MQTTUser
hacfg.Data.Port = cf.MQTTPort
hacfg.Data.ClientID = cf.MQTTClientID
}
JabloPIN = cf.JablotronPIN
JStates = make(map[string]string)
JPG = make(map[string]string)
JCommands = make(map[time.Time]string)
for {
mainloop(hacfg, cf)
}
}
func mainloop(hacfg HASupervisorConfig, cf ConfigFile) {
client, token := MakeMQTTConn(hacfg)
for {
if token.Wait() && token.Error() != nil {
fmt.Println("Wjechal Error Polaczenie MQTT resetuje", token.Error())
return
}
GetFromJablo(client, token, cf)
time.Sleep(2 * time.Second)
}
}
func connLostHandler(c mqtt.Client, err error) {
fmt.Printf("Connection lost, reason: %v\n", err)
//Perform additional action...
}
func startsub(c mqtt.Client) {
c.Subscribe("jablotron/+/+/set", 2, HandleMSGfromMQTT)
//Perform additional action...
}
func HandleMSGfromMQTT(client mqtt.Client, msg mqtt.Message) {
s := strings.Split(msg.Topic(), "/")
if len(s) > 2 {
switch s[1] {
case "pg":
if fmt.Sprintf("%s", msg.Payload()) == "ON" {
JCommands[time.Now()] = JabloPIN + " PGON " + s[2]
} else {
JCommands[time.Now()] = JabloPIN + " PGOFF " + s[2]
}
case "state":
if fmt.Sprintf("%s", msg.Payload()) == "ON" {
JCommands[time.Now()] = JabloPIN + " SET " + s[2]
} else {
JCommands[time.Now()] = JabloPIN + " UNSET " + s[2]
}
}
}
fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
fmt.Printf(".")
}
type ConfigFile struct {
JablotronPIN string `json:"JablotronPIN"`
JablotronIP string `json:"JablotronIP"`
JablotronPort int `json:"JablotronPort"`
MQTTHost string `json:"MQTTHost"`
MQTTPort int `json:"MQTTPort"`
MQTTUser string `json:"MQTTUser"`
MQTTPassword string `json:"MQTTPassword"`
MQTTProtocol string `json:"MQTTProtocol"`
MQTTClientID string `json:"MQTTClientID"`
}
func ShowOptionsFile() (ConfigFile, error) {
var cf ConfigFile
file, err := os.Open("/data/options.json")
if err != nil {
log.Println(err)
return cf, err
}
defer func() {
if err = file.Close(); err != nil {
log.Println(err)
}
}()
b, err := ioutil.ReadAll(file)
if err != nil {
return cf, err
}
err = json.Unmarshal(b, &cf)
if err != nil {
return cf, err
}
fmt.Println("---------------------------")
fmt.Println(string(b))
fmt.Println("---------------------------")
return cf, nil
}
func MakeMQTTConn(hacfg HASupervisorConfig) (mqtt.Client, mqtt.Token) {
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("%s://%s:%d", "tcp", hacfg.Data.Host, hacfg.Data.Port))
opts.SetPassword(hacfg.Data.Username)
opts.SetUsername(hacfg.Data.Password)
fmt.Println("Connstring", fmt.Sprintf("%s://%s:%d", "tcp", hacfg.Data.Host, hacfg.Data.Port), "usernamee", hacfg.Data.Username, "password", hacfg.Data.Password)
opts.SetClientID(hacfg.Data.ClientID)
opts.SetKeepAlive(time.Second * time.Duration(60))
opts.SetOnConnectHandler(startsub)
opts.SetConnectionLostHandler(connLostHandler)
if hacfg.Data.Protocol == "3.1.1" {
fmt.Println("Proto v4")
opts.SetProtocolVersion(4)
}
opts.SetProtocolVersion(4)
// connect to broker
client := mqtt.NewClient(opts)
//defer client.Disconnect(uint(2))
token := client.Connect()
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to connect broker, %v", token.Error())
}
return client, token
}
func GetFromJablo(client mqtt.Client, token mqtt.Token, cf ConfigFile) {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", cf.JablotronIP, cf.JablotronPort))
if err != nil {
return
}
t1 = time.Now()
go TouchJablo(conn)
reader := bufio.NewReader(conn)
for {
message, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
if strings.HasPrefix(message, "OK\r") {
// fmt.Print("PONG FROM JABLOTRON: " + message)
oktimer = time.Now()
} else {
s := strings.Split(message, " ")
if len(s) > 1 {
switchstr := strings.Trim(strings.TrimSpace(s[0]), " ")
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
fmt.Print(err)
}
switchstr = reg.ReplaceAllString(switchstr, "")
switch switchstr {
case "PRFSTATE":
fmt.Printf("Przyszedl status jablotron\n")
t1 = time.Now()
str := strings.TrimSpace(s[1])
JDEV, err := ParseJablotronDevices(str)
if err != nil {
fmt.Println(err)
return
} else {
TestPub(JDEV, client, token)
}
case "PG":
fmt.Printf("Przyszedl Status Wyjscia PG - %s\n", message)
JPG[s[1]] = s[2]
PubPG(s[1], s[2], client, token)
case "STATE":
fmt.Printf("Przyszedl Status Strefy - %s\n", message)
JStates[s[1]] = s[2]
PublishStates(client, token)
case "ENTRY":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "EXIT":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "INTERNALWARNING":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "EXTERNALWARNING":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "INTRUDERALARM":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "PANICALARM":
PublishAlarm(switchstr, s[1], s[2], client, token)
case "FIREALARM":
PublishAlarm(switchstr, s[1], s[2], client, token)
default:
fmt.Println("nieobsluzona wiadomosc: ", message, err)
}
}
}
//
}
return
}
func PublishStates(client mqtt.Client, token mqtt.Token) {
for key, value := range JStates {
fmt.Println("Key:", key, "Value:", value)
TOP := "jablotron/state/" + fmt.Sprintf("%s", key)
fmt.Println("Publikuje do ", TOP, "warosc", value)
value = strings.TrimSpace(value)
token = client.Publish(TOP, byte(0), false, value)
if token.Wait() && token.Error() != nil {
fmt.Printf("Fail to publish, %v", token.Error())
}
}
}
func TouchJablo(conn net.Conn) {
for {
for key, value := range JCommands {
fmt.Println("Wysylam komende ", value)
//conn.Write([]byte("\n\r"))
_, err := conn.Write([]byte(value + "\n\r"))
time.Sleep(1 * time.Second)
if err != nil {
fmt.Println("NIe udalo sie wyslac polaczenie zepsute ", err)
return
} else {
// conn.Write([]byte("\n\r"))
delete(JCommands, key)
}
}
// diff := time.Now().Sub(t1)
if len(JStates) < 1 {
fmt.Println("NIe mam tabeli statusow wszystkich stref pytam")
JCommands[time.Now()] = "STATE"
}
if len(JPG) < 1 {
fmt.Println("NIe mam tabeli statusow wszystkich PG pytam")
JCommands[time.Now()] = "PGSTATE"
}
// if diff.Seconds() > 4 {
// fmt.Printf("Nie bylo STANU OD %f pytam \n", diff.Seconds())
// JCommands[time.Now()] = "PRFSTATE"
// }
time.Sleep(1 * time.Second)
}
}
func utb(a uint64) bool {
if a == uint64(1) {
return true
}
return false
}
func ParseJablotronDevices(instr string) (map[int]bool, error) {
marker := 0
m := make(map[int]bool)
inmarker := 0
var err error
for {
end := marker + 2
if end <= len(instr) {
first2 := instr[marker:end]
//fmt.Println(first2)
i, err := strconv.ParseUint(first2, 16, 32)
if err != nil {
return m, err
}
iarr := asBits(i)
m[inmarker] = iarr[0]
m[inmarker+1] = iarr[1]
m[inmarker+2] = iarr[2]
m[inmarker+3] = iarr[3]
m[inmarker+4] = iarr[4]
m[inmarker+5] = iarr[5]
m[inmarker+6] = iarr[6]
m[inmarker+7] = iarr[7]
// fmt.Println(iarr)
inmarker = inmarker + 8
marker = end
} else {
break
}
}
return m, err
}
func asBits(val uint64) []bool {
bits := []bool{}
//bits2 := []uint64{}
for i := 0; i < 8; i++ {
//bits = append([]uint64{val & 0x1}, bits...)
// or
bits = append(bits, utb(val&0x1))
// bits2 = append(bits2, val&0x1)
// depending on the order you want
val = val >> 1
}
/// fmt.Println(bits2)
return bits
}