-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·89 lines (71 loc) · 2.49 KB
/
main.py
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
import mqttcom
import configparser
import math
import time
import syslog
print("Starting Rollershutter IO Translator")
hpConfig = configparser.ConfigParser()
hpConfig.read("config.ini")
preshutter_names = hpConfig["mqtt"]["shutter_names"].strip().split(",")
shutter_names = []
for sn in preshutter_names:
shutter_names.append(sn.strip())
def slog(msg):
syslog.syslog(msg)
print(msg)
def connecit():
doinit = True
while doinit:
try:
mqttClient = mqttcom.MQTTComm(hpConfig["mqtt"]["server_address"], hpConfig["mqtt"]["real_topic"],
hpConfig["mqtt"]["virtual_topic"], shutter_names)
doinit = False
mqttClient.ping()
return mqttClient
except BaseException as error:
slog('An exception occurred during init') #: {}'.format(error))
slog('{}: {}'.format(type(error).__name__, error))
if type(error) == KeyboardInterrupt:
exit(0)
slog("restarting after 5 secs")
time.sleep(5)
return None
mqttClient = connecit()
lctime = math.trunc(time.time() * 1000)
pollPeriod = 100
check_period = 5000
tele_period = 60000
lstateCounter = 0
ltime = 0
lteletime = 0
spamltime = 0
onon = True
while onon:
try:
while True:
currtime = math.trunc(time.time() * 1000) # time in microseconds
if currtime - pollPeriod > ltime:
delta = currtime - ltime
mqttClient.ping_time(delta) # ping the client to do timebased events
ltime = currtime
if currtime - check_period > lctime:
if not mqttClient.client.is_connected():
slog("not connected retrying")
mqttClient = connecit()
lctime = currtime
if currtime - tele_period > lteletime:
mqttClient.send_tele(currtime, mqttClient.client.is_connected())
lteletime = currtime
if lstateCounter != mqttClient.stateCounter:
lstateCounter = mqttClient.stateCounter
time.sleep(0.01)
except BaseException as error:
slog('An exception occurred during onon') #: {}'.format(error))
slog('{}: {}'.format(type(error).__name__, error))
if type(error) == KeyboardInterrupt:
exit(0)
slog("restarting after 5 secs")
time.sleep(5)
except:
slog("exception occurred restarting after 1 secs")
time.sleep(1)