-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpySwitcher.py
More file actions
83 lines (68 loc) · 3.75 KB
/
pySwitcher.py
File metadata and controls
83 lines (68 loc) · 3.75 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
import requests
import json
class pySwitcher(object):
def __init__(self):
self.token = ''
self.baseUri = 'http://server.switcher.co.il/Switcher/'
self.userAgent = 'Switcher/1.0 CFNetwork/758.5.3 Darwin/15.6.0'
self.headers = {'user-agent': self.userAgent}
self.postHeaders = {'user-agent': self.userAgent, 'Content-Type': 'application/x-www-form-urlencoded'}
self.switchId = ''
def getSwitch(self):
assert (self.token != ''), 'Not logged in!'
req = requests.get(self.baseUri + 'appServiceGetSwitches', params={'token': self.token}, headers=self.headers)
res = json.loads(req.text)
if (res['errorCode'] == 0):
switchId = res['switches'][0]
else:
switchId = ''
return switchId
def login(self, username, password):
assert (username != ''), 'Invalid Username!'
assert (password != ''), 'Invalid Password!'
payload = {'password': password, 'account_pid': username, 'item_id': 'iPhone_app_id',
'device_info': {'duid': '4B7299E2-2677-43AC-BE40-A3CC5E5F700B',
'versions': {'software': 'iphone_app'}}}
req = requests.post(self.baseUri + 'loginApp', data=json.dumps(payload), headers=self.postHeaders)
res = json.loads(req.text)
if (res['errorCode'] == 0):
self.token = res['token']
else:
self.token = ''
self.switchId = self.getSwitch()
def getState(self):
assert (self.token != ''), 'Not logged in!'
assert (self.switchId != ''), 'Cant get switch associated with the user!'
serviceParams = {'token': self.token, 'switchId': self.switchId}
req = requests.get(self.baseUri + 'appServiceGetSwitchState', params=serviceParams, headers=self.headers)
res = json.loads(req.text)
if (res['errorCode'] == 0):
state = res['state']
else:
state = ''
return state
def turnOn(self):
assert (self.token != ''), 'Not logged in!'
assert (self.switchId != ''), 'Cant get switch associated with the user!'
serviceParams = {'token': self.token, 'switchId': self.switchId, 'state': 'on'}
req = requests.get(self.baseUri + 'appServiceSetSwitchState', params=serviceParams, headers=self.headers)
res = json.loads(req.text)
return res['errorCode'] == 0 and res['state'] == 'on'
def turnOnWithDuration(self, durationMs):
assert (self.token != ''), 'Not logged in!'
assert (self.switchId != ''), 'Cant get switch associated with the user!'
assert (durationMs > 0), 'Time should be non-zero'
serviceParams = {'token': self.token, 'switchId': self.switchId, 'state': 'on', 'isManual': "true",
'duration': durationMs}
req = requests.get(self.baseUri + 'setSpontaneousEvent', params=serviceParams, headers=self.headers)
res = json.loads(req.text)
return res['errorCode'] == 0 and res['state'] == 'on'
# rp({ url: util.format(ENABLE_DURATION, switcher.token, switcher.switchID, duration_ms), json: true })
# ENABLE_DURATION = BASE_URL + "/setSpontaneousEvent?token=%s&switchId=%s&isManual=true&duration=%s"
def turnOff(self):
assert (self.token != ''), 'Not logged in!'
assert (self.switchId != ''), 'Cant get switch associated with the user!'
serviceParams = {'token': self.token, 'switchId': self.switchId, 'state': 'off'}
req = requests.get(self.baseUri + 'appServiceSetSwitchState', params=serviceParams, headers=self.headers)
res = json.loads(req.text)
return res['errorCode'] == 0 and res['state'] == 'off'