-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsendmessage.py
More file actions
193 lines (161 loc) · 6.81 KB
/
Copy pathsendmessage.py
File metadata and controls
193 lines (161 loc) · 6.81 KB
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
'''
Created on 6 Dec 2015
@author: steph
'''
#import requests
import logging
import binascii
import socket
import sys
import time
#import base64
from variables import Variables
from database import DbUtils
from webui import CreateUIPage
VAR = Variables()
class SendMessage(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
self.baseString = "000440000000"
def s_Command(self, rfAddress, roomID, thermostatMode, setpointTemperature):
"""
Base String 6 000440000000
RF Address 3 0FDAED
Room nr 1 01
Temperature & 1 66
Mode (similar to the L message)
Date Until 1 04 Note: Only in case of vacation mode setting. Otherwise this byte is omitted
hex: | 66 |
dual: | 0110 1100 |
|||| ||||
||++-++++-- temperature: 10 1100 -> 38 = temp * 2
|| (to get the temperature, the value must be divided by 2: 38/2 = 19)
||
||
++--------- mode: 00=auto/weekly program
01=manual
10=vacation
11=boost
"""
self.logger = logging.getLogger("main.sendmessage.s_Command")
self.logger.info("creating s command for %s %s %s %s" %(rfAddress, roomID, thermostatMode, setpointTemperature))
self.rfAddress = rfAddress
self.roomID = format(int(roomID), '02x')
self.setpointTemperature = int(float(setpointTemperature) * 2)
bits = '{0:06b}'.format(self.setpointTemperature)
self.logger.debug("setpoint temp in bits : %s" % bits)
if thermostatMode != 'none':
if thermostatMode == 'MANUAL':
bits = '01' + bits
elif thermostatMode == 'BOOST':
bits = '11' + bits
elif thermostatMode == 'VACATION':
bits = '10' + bits
else:
bits = '00' + bits
self.logger.debug("bits with mode added : %s" % bits)
bits = hex(int(bits, 2))[2:]
self.logger.debug("temp bits as Hex : %s" % bits)
if bits == '0':
bits = '00'
if thermostatMode == 'none':
commandString = self.baseString + self.rfAddress + self.roomID
else:
commandString = self.baseString + self.rfAddress + self.roomID + bits
self.logger.debug("command string : %s" % commandString)
commandString = self.hextoBase64(commandString)
self.logger.debug("Encoded string : %s" % commandString)
return commandString
def hextoBase64(self, hexnumber):
hexified = hexnumber.decode("hex")
base = binascii.b2a_base64(hexified)
return base
#Auto AARAAAAAEFHaAwA= 00 04 40 00 00 00 10 51 da 03 00
#manu AARAAAAAEFHWBAA= 00 04 40 00 00 00 10 51 d6 04 00
# AARAAAAAEWtjAgA= 00 04 40 00 00 00 11 6b 63 02 00
# AARAAAAAEWOlAWY= 00 04 40 00 00 00 11 63 A5 01 66
#('1163A5', 1, 'MANUAL', 19)
def sendMAX(self, commandString):
self.logger = logging.getLogger("main.sendmessage.sendMAX")
sendString = 's:{}\r\n'.format(commandString)
Max_IP, Max_Port = VAR.readVariables(['MaxIP', 'MaxPort'])
self.logger.info("Sending s command to MAX on : %s %s" % (Max_IP, Max_Port))
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
self.logger.debug("Max Socket Created")
except socket.error:
self.logger.exception("Failed to create MAX socket")
s.close()
sys.exit()
#Connect to remote server
try:
s.connect((Max_IP, int(Max_Port)))
self.logger.debug("Connected to Max on ip %s" % Max_IP)
except Exception, err:
s.close()
self.logger.exception("Unable to make MAX connection Trying later %s" % err)
return
message = ""
try:
while 1:
reply = s.recv(1024)
message += reply
except:
pass
if message != "":
validData = True
self.logger.info("Sending message to MAX : %s" % sendString)
s.send(sendString)
message = ""
try:
while 1:
reply = s.recv(1024)
message += reply
except:
pass
s.close()
self.logger.debug("MAX send message reply %s" % message)
return message
def updateRoom(self, roomData):
self.logger = logging.getLogger("main.sendmessage.updateRoom")
self.logger.info("Updating rooms")
roomList = DbUtils().getRooms()
self.logger.debug("roomList %s" % roomList)
self.logger.debug("roomData %s" % roomData)
roomSplit = roomData.split('?')
mode = roomSplit[1].replace('%20', ' ')
room = roomSplit[2].replace('%20', ' ')
temp = roomSplit[3].replace('%20', ' ')
self.logger.debug("room %s mode %s temp %s" % (room, mode, temp))
if mode == 'eco':
for rooms in roomList:
sCommand = self.s_Command(rooms[2], rooms[0], 'none', 0.0)
self.logger.debug("eco sCommand %s" % sCommand)
self.sendMAX(sCommand)
time.sleep(0.8)
if mode == 'ECO':
for rooms in roomList:
if room == rooms[1]:
sCommand = self.s_Command(rooms[2], rooms[0], 'none', 0.0)
self.logger.debug("ECO sCommand %s" % sCommand)
self.sendMAX(sCommand)
elif mode == 'auto':
for rooms in roomList:
sCommand = self.s_Command(rooms[2], rooms[0], 'AUTO', 0.0)
self.logger.debug("auto sCommand %s" % sCommand)
self.sendMAX(sCommand)
time.sleep(0.8)
else:
# For standard room change
for rooms in roomList:
if room == rooms[1]:
sCommand = self.s_Command(rooms[2], rooms[0], mode, temp)# rf address,room Number, mode, temp
self.logger.debug("standard sCommand %s" % sCommand)
self.sendMAX(sCommand)
CreateUIPage().updateWebUI()