This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
mediolamanager.py
executable file
·246 lines (213 loc) · 8.75 KB
/
mediolamanager.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
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
#!/usr/bin/env python
import sys
from PyQt5 import QtWidgets, uic, QtGui
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot, QTimer
from PyQt5.QtWidgets import QTableWidgetItem, QDialog
import requests
import json
class addDevice(QtWidgets.QDialog):
def __init__(self, parent):
super(addDevice, self).__init__(parent)
uic.loadUi('adddevice.ui', self)
self.hideAllGroups()
self.findChild(QObject, 'comboDeviceType').currentTextChanged.connect(self.deviceTypeChanged)
self.findChild(QObject, 'btnClose').clicked.connect(self.hide)
self.deviceTypeChanged(self.findChild(QObject, 'comboDeviceType').currentText())
def hideAllGroups(self):
self.findChild(QObject, 'groupElero').setVisible(False)
self.findChild(QObject, 'groupSomfy').setVisible(False)
self.findChild(QObject, 'groupIntertechno').setVisible(False)
def deviceTypeChanged(self, text):
self.hideAllGroups()
if text == 'Elero':
self.findChild(QObject, 'groupElero').setVisible(True)
elif text == 'Intertechno':
self.findChild(QObject, 'groupIntertechno').setVisible(True)
elif text == 'Somfy':
self.findChild(QObject, 'groupSomfy').setVisible(True)
class eleroManager(QtWidgets.QDialog):
def __init__(self, parent):
super(eleroManager, self).__init__(parent)
self.ui = parent
uic.loadUi('eleromanager.ui', self)
self.findChild(QObject, 'btnClose').clicked.connect(self.hide)
self.findChild(QObject, 'btnUp').clicked.connect(self.btnUpClicked)
self.findChild(QObject, 'btnDown').clicked.connect(self.btnDownClicked)
self.findChild(QObject, 'btnStop').clicked.connect(self.btnStopClicked)
self.findChild(QObject, 'btnLearn').clicked.connect(self.btnLearnClicked)
def getChannel(self):
idx = self.findChild(QObject, 'comboChannel').currentIndex()
channel = format(idx+1, 'x')
return channel
def btnUpClicked(self):
channel = self.getChannel()
data = '0' + channel + '01'
payload = {
'type' : 'ER',
'data' : data
}
self.ui.sendRequest('SendSC', payload)
def btnStopClicked(self):
channel = self.getChannel()
data = '0' + channel + '02'
payload = {
'type' : 'ER',
'data' : data
}
self.ui.sendRequest('SendSC', payload)
def btnDownClicked(self):
channel = self.getChannel()
data = '0' + channel + '00'
payload = {
'type' : 'ER',
'data' : data
}
self.ui.sendRequest('SendSC', payload)
def btnLearnClicked(self, checked):
if checked:
cmd = 'LearnSC'
else:
cmd = 'stopLearn'
channel = self.getChannel()
adr = '0' + channel
payload = {
'type' : 'ER',
'adr' : adr
}
self.ui.sendRequest(cmd, payload)
def closeEvent(self, event):
self.hide()
event.ignore()
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('mediolamanager.ui', self)
btnConnect = self.findChild(QObject, 'btnConnect')
btnConnect.clicked.connect(self.connect)
btnEleroManager = self.findChild(QObject, 'btnEleroManager')
btnEleroManager.clicked.connect(self.eleroManager)
btnAddDevice = self.findChild(QObject, 'btnAddDevice')
btnAddDevice.clicked.connect(self.addDevice)
btnDelDevice = self.findChild(QObject, 'btnDeleteDevice')
btnDelDevice.clicked.connect(self.delDevice)
self.version = 4
self.url = ''
self.devices = []
self.show()
self.eleroManager = eleroManager(self)
self.addDevice = addDevice(self)
self.gatewayDisconnected()
def gatewayDisconnected(self):
self.findChild(QObject, 'btnEleroManager').setEnabled(False)
self.findChild(QObject, 'btnAddDevice').setEnabled(False)
self.findChild(QObject, 'btnDeleteDevice').setEnabled(False)
self.findChild(QObject, 'btnConnect').setChecked(False)
self.findChild(QObject, 'editHostname').setEnabled(True)
self.findChild(QObject, 'tblDevices').clear()
def gatewayConnected(self):
self.findChild(QObject, 'btnEleroManager').setEnabled(True)
self.findChild(QObject, 'btnAddDevice').setEnabled(True)
self.findChild(QObject, 'btnDeleteDevice').setEnabled(True)
self.findChild(QObject, 'statusbar').showMessage('Connected.')
self.findChild(QObject, 'editHostname').setEnabled(False)
def addDevice(self):
self.addDevice.show()
def delDevice(self):
pass
def eleroManager(self):
self.eleroManager.show()
def parseResponse(self, text):
ret = {}
res = False
if self.version == 4:
if text.startswith('{XC_SUC}'):
text = text.replace('{XC_SUC}', '')
if len(text) > 0:
ret = json.loads(text)
res = True
elif text.startswith('{XC_ERR}'):
text = text.replace('{XC_ERR}', '')
ret = text
res = False
elif self.version == 5:
ret = json.loads(text)
if 'XC_SUC' in ret:
ret = ret['XC_SUC']
res = True
else:
ret = ret['XC_ERR']
res = False
return res, ret
def connect(self, checked):
if checked:
hostname = self.findChild(QObject, 'editHostname').text()
if hostname == '':
self.findChild(QObject, 'btnConnect').setChecked(False)
return
version = self.findChild(QObject, 'comboVersion').currentText()
if version == 'v4/v4+':
self.version = 4
self.url = 'http://' + hostname + '/command'
elif version == 'v5/v5+':
self.version = 5
self.url = 'http://' + hostname + '/cmd'
status, message = self.sendRequest('GetSI')
if status:
self.gatewayConnected()
print('Connected.')
info = 'MAC: ' + message['MAC'] + '\n'
info += 'HW: ' + message['HWV'] + '\n'
info += 'SW: ' + message['VER'] + '\n'
self.findChild(QObject, 'textInformation').clear()
self.findChild(QObject, 'textInformation').append(info)
self.getDevices()
else:
print('Error connecting to Gateway')
self.findChild(QObject, 'statusbar').showMessage('Error connecting to Gateway')
self.gatewayDisconnected()
else:
self.findChild(QObject, 'textInformation').clear()
self.findChild(QObject, 'statusbar').showMessage('Disconnected.')
self.gatewayDisconnected()
def sendRequest(self, command, data = None):
print('sendRequest')
print(command)
print(data)
payload = { 'XC_FNC' : command }
if data:
payload.update(data)
print(payload)
try:
response = requests.get(self.url, params=payload, headers={'Connection':'close'})
except:
return False, ''
message = ''
if response.status_code == 200:
res, message = self.parseResponse(response.text)
else:
res = False
return res, message
def getDevices(self):
status, message = self.sendRequest('GetStates')
if status:
self.devices = message
count = len(message)
if count > 0:
offset = 0
if message[0]['type'] == 'EVENT':
print('EVENT')
count = count-1
offset = 1
tab = self.findChild(QObject, 'tblDevices')
tab.setRowCount(count)
tab.setColumnCount(3)
tab.setHorizontalHeaderItem(0, QTableWidgetItem('Type'))
tab.setHorizontalHeaderItem(1, QTableWidgetItem('Address'))
tab.setHorizontalHeaderItem(2, QTableWidgetItem('State'))
for ii in range(offset, count+offset):
tab.setItem(ii-offset, 0, QTableWidgetItem(message[ii]['type']))
tab.setItem(ii-offset, 1, QTableWidgetItem(message[ii]['adr']))
tab.setItem(ii-offset, 2, QTableWidgetItem(message[ii]['state']))
app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.exit(app.exec_())