-
Notifications
You must be signed in to change notification settings - Fork 1
/
alpha-ess-web_mqtt.py
188 lines (140 loc) · 5.25 KB
/
alpha-ess-web_mqtt.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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import signal
import time
import _thread
import traceback
from queue import Queue
import json
import paho.mqtt.publish as mqtt_publish
import paho.mqtt.client as mqtt_client
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import shutil
# external files/classes
import logger
import serviceReport
import settings
import AlphaEssMonitor
sendQueue = Queue(maxsize=0)
oldTimeout = 0
exitThread = False
chromeDriver = None
monitor = None
alphaEssStatus = {}
def current_sec_time():
return int(round(time.time()))
def current_milli_time():
return int(round(time.time() * 1000))
def signal_handler(_signal, frame):
global exitThread
print('You pressed Ctrl+C!')
exitThread = True
# The callback for when the client receives a CONNACK response from the server.
def on_connect(_client, userdata, flags, rc):
if rc == 0:
print("MQTT Client connected successfully")
_client.subscribe([(settings.MQTT_TOPIC_OUT, 1), (settings.MQTT_TOPIC_CHECK, 1)])
else:
print(("ERROR: MQTT Client connected with result code %s " % str(rc)))
# The callback for when a published message is received from the server
def on_message(_client, userdata, msg):
print(('ERROR: Received ' + msg.topic + ' in on_message function' + str(msg.payload)))
def on_message_homelogic(_client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
# topics = msg.topic.split("/")
# deviceName = topics[2] #huis/RFXtrx/KaKu-12/out
# cmnd = deviceName.split("-") #KaKu-12
def alphaEssThread():
global oldTimeout
global chromeDriver
global monitor
global exitThread
oldTimeout = current_sec_time()
try:
print("Start Chrome driver")
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.binary_location = shutil.which("chromium-browser")
chromeDriver = webdriver.Chrome(options=chrome_options, executable_path=shutil.which("chromedriver"))
# Handle other exceptions and print the error
except Exception as arg:
print("%s" % str(arg))
traceback.print_exc()
# Report failure to Home Logic system check
serviceReport.sendFailureToHomeLogic(serviceReport.ACTION_NOTHING, 'Failed to open Chrome driver')
exitThread = True
return
print("Init AlphaEss monitor")
monitor = AlphaEssMonitor.AlphaEssMonitor(settings.ALPHAESS_USERNAME, settings.ALPHAESS_PASSWORD)
print("Start AlphaEss monitor")
monitor.start(chromeDriver)
# Delay after monitor start, otherwise url errors when getting data
time.sleep(5)
while not exitThread:
try:
# print("Get monitor data")
monitor_data = monitor.get_data()
for key in monitor_data:
# print("Monitor data key: %s" % key)
alphaEssStatus[key] = monitor_data[key]
serviceReport.systemWatchTimer = current_sec_time()
mqtt_publish.single("huis/AlphaEss/Web/solar", json.dumps(alphaEssStatus, separators=(', ', ':')), qos=1, hostname=settings.MQTT_ServerIP, retain=True)
# print("Waiting %d seconds" % settings.ALPHAESS_WAIT)
time.sleep(settings.ALPHAESS_WAIT)
# Check if there is any message to send to AlphaEss.com
if not sendQueue.empty():
sendMsg = sendQueue.get_nowait()
# print "SendMsg:", sendMsg
if sendMsg != "":
print("SendMsg: Not implemented yet")
# In case the message contains unusual data
except ValueError as arg:
print(arg)
traceback.print_exc()
time.sleep(1)
# Quit the program by Ctrl-C
except KeyboardInterrupt:
print("Program aborted by Ctrl-C")
exit()
# Handle other exceptions and print the error
except Exception as arg:
print("%s" % str(arg))
traceback.print_exc()
time.sleep(10)
def print_time(delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print("%s" % (time.ctime(time.time())))
###
# Initalisation ####
###
logger.initLogger(settings.LOG_FILENAME)
# Init signal handler, because otherwise Ctrl-C does not work
signal.signal(signal.SIGINT, signal_handler)
# Give Home Assistant and Mosquitto the time to startup
time.sleep(2)
# First start the MQTT client
client = mqtt_client.Client()
client.message_callback_add(settings.MQTT_TOPIC_OUT, on_message_homelogic)
client.message_callback_add(settings.MQTT_TOPIC_CHECK, serviceReport.on_message_check)
client.on_connect = on_connect
client.on_message = on_message
client.connect(settings.MQTT_ServerIP, settings.MQTT_ServerPort, 60)
client.loop_start()
# Create the alphaEssThread
try:
# thread.start_new_thread( print_time, (60, ) )
_thread.start_new_thread(alphaEssThread, ())
except Exception:
print("Error: unable to start the alphaEssThread")
while not exitThread:
time.sleep(6) # 60s
if monitor is not None:
monitor.stop()
print("Stopped AlphaEss monitor")
print("Clean exit!")