forked from davorf/BlackBeanControl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebControl.py
125 lines (100 loc) · 4.03 KB
/
WebControl.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
#!python2
import broadlink, configparser
import sys, getopt
import time, binascii
import netaddr
import Settings
import web
from os import path
from Crypto.Cipher import AES
SettingsFile = configparser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(Settings.BlackBeanControlSettings)
DeviceName=''
DeviceIPAddress = ''
DevicePort = ''
DeviceMACAddres = ''
DeviceTimeout = ''
WebPort = ''
GlobalDevice = None
urls = (
'/', 'WebHook',
)
class WebHook:
def GET(self):
global GlobalDevice
input = web.input(device="", command="")
DeviceName = input.device
if DeviceName.strip() == '':
return 'ERROR: Device name parameter is mandatory'
if SettingsFile.has_section(DeviceName.strip()):
if SettingsFile.has_option(DeviceName.strip(), 'IPAddress'):
DeviceIPAddress = SettingsFile.get(DeviceName.strip(), 'IPAddress')
else:
DeviceIPAddress = ''
if SettingsFile.has_option(DeviceName.strip(), 'Port'):
DevicePort = SettingsFile.get(DeviceName.strip(), 'Port')
else:
DevicePort = ''
if SettingsFile.has_option(DeviceName.strip(), 'MACAddress'):
DeviceMACAddress = SettingsFile.get(DeviceName.strip(), 'MACAddress')
else:
DeviceMACAddress = ''
if SettingsFile.has_option(DeviceName.strip(), 'Timeout'):
DeviceTimeout = SettingsFile.get(DeviceName.strip(), 'Timeout')
else:
DeviceTimeout = ''
else:
return 'ERROR: Device does not exist in BlackBeanControl.ini'
if (DeviceName.strip() != '') and (DeviceIPAddress.strip() == ''):
return 'ERROR: IP address must exist in BlackBeanControl.ini for the selected device'
if (DeviceName.strip() != '') and (DevicePort.strip() == ''):
return 'ERROR: Port must exist in BlackBeanControl.ini for the selected device'
if (DeviceName.strip() != '') and (DeviceMACAddress.strip() == ''):
return 'ERROR: MAC address must exist in BlackBeanControl.ini for the selected device'
if (DeviceName.strip() != '') and (DeviceTimeout.strip() == ''):
return 'ERROR: Timeout must exist in BlackBeanControl.ini for the selected device'
if DeviceName.strip() != '':
RealTimeout = DeviceTimeout.strip()
else:
RealTimeout = Settings.Timeout
if RealTimeout.strip() == '':
return 'ERROR: Timeout must exist in BlackBeanControl.ini'
else:
RealTimeout = int(RealTimeout.strip())
ParsedMAC = netaddr.EUI(DeviceMACAddress)
ParsedPort = int(DevicePort)
RM3Device = broadlink.rm((DeviceIPAddress, ParsedPort), ParsedMAC)
RM3Device.auth()
if SettingsFile.has_option('Commands', input.command):
CommandFromSettings = SettingsFile.get('Commands', input.command)
else:
CommandFromSettings = ''
if CommandFromSettings.strip() != '':
DecodedCommand = CommandFromSettings.decode('hex')
RM3Device.send_data(DecodedCommand)
return "OKAY"
else:
return "ERROR: Couldn't find %s" % (input.command)
if __name__ == '__main__':
try:
Options, args = getopt.getopt(sys.argv[1:], 'w:h', ['webport=','help'])
except getopt.GetoptError:
print('WebControl.py -w <Web port>')
sys.exit(2)
for Option, Argument in Options:
if Option in ('-h', '--help'):
print('WebControl.py -w <Web port>')
sys.exit()
elif Option in ('-w', '--webport'):
WebPort = Argument
try:
RealPort = int(WebPort.strip())
except:
print("WebPort must be an integer")
sys.exit(2)
print("Starting up WebControl on port %s" % (RealPort))
app = web.application(urls, globals())
app.internalerror = web.debugerror
web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", RealPort))
print("Exiting")