-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
114 lines (101 loc) · 4.2 KB
/
client.py
File metadata and controls
114 lines (101 loc) · 4.2 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
import requests
from device import Device
from device import Traits as DeviceTraits
from room import Room
from room import Traits as RoomTraits
from urls import sengled_base_url, zigbee_url, customer_url, room_url, headers
class Client:
username = None
password = None
jsessionid = None
logged_in = False
rooms = None
devices = None
def __init__(self, username, password):
self.username = username
self.password = password
self.load()
def login(self):
if self.logged_in:
return
print("Logging In")
login_data = {'os_type': 'android', 'pwd': self.password, 'user': self.username, 'uuid': 'xxxxxx'}
resp = requests.post(sengled_base_url + zigbee_url + customer_url + 'login.json',
headers=headers, verify=False, json=login_data)
if resp.status_code == 200:
resp_json = resp.json()
if 'msg' in resp_json and resp_json['msg'] == 'success':
headers['Cookie'] = 'JSESSIONID={}'.format(resp_json['jsessionid'])
self.jsessionid = resp_json['jsessionid']
self.logged_in = True
else:
key = None
if 'msg' in resp_json:
key = 'msg'
elif 'info' in resp_json:
key = 'info'
if key is not None:
print('Login unsuccessful: {}'.format(resp_json[key]))
print('Make sure you change the "username" and "password" in /sengled/element/actions/urls.py')
else:
print('Could not login successfully')
else:
print('Could not login successfully')
def load(self):
self.login()
print("Loading Devices")
resp = requests.post(sengled_base_url + zigbee_url + room_url + 'getUserRoomsDetail.json',
headers=headers, verify=False, json={})
if resp.status_code == 200:
resp_json = resp.json()
if 'roomList' in resp_json:
self.parse_room_list(resp_json['roomList'])
else:
print('Could not get rooms: {}'.format(resp.status_code))
def update(self):
self.login()
print("Updating Devices")
# TODO figure out how to get around server delay
resp = requests.post(sengled_base_url + zigbee_url + room_url + 'getUserRoomsDetail.json',
headers=headers, verify=False, json={})
if resp.status_code == 200:
resp_json = resp.json()
if 'roomList' in resp_json:
for room_data in resp_json['roomList']:
room = self.get_room_by_id(room_data[RoomTraits.ID.value])
room.data = room_data
if 'deviceList' in room_data:
for device_data in room_data['deviceList']:
device = self.get_device_by_id(device_data[DeviceTraits.ID.value])
device.data = device_data
else:
print('Could not get rooms: {}'.format(resp.status_code))
def parse_room_list(self, room_list):
self.devices = []
self.rooms = []
for room_data in room_list:
devices = []
if 'deviceList' in room_data:
for device_data in room_data['deviceList']:
device = Device(device_data, self)
devices.append(device)
self.devices.append(device)
self.rooms.append(Room(room_data, devices))
def get_room_by_id(self, room_id):
for room in self.rooms:
if int(room.get_id()) == int(room_id):
return room
print("Room not found:", room_id)
return None
def get_device_by_id(self, device_id):
for device in self.devices:
if device.get_id() == device_id:
return device
print("Device not found:", device_id)
return None
def get_room_for_device(self, device):
for room in self.rooms:
if device in room.devices:
return room
print("Room not found for device:", device.get_id())
return None