-
Notifications
You must be signed in to change notification settings - Fork 2
/
wirelesstag.py
147 lines (110 loc) · 4.03 KB
/
wirelesstag.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
import requests
import json
from decimal import Decimal
_USERNAME = ""
_PASSWORD = ""
_BASEURL = "https://www.mytaglist.com"
_SIGNIN = _BASEURL + "/ethAccount.asmx/SignIn"
_ISSIGNED = _BASEURL + "/ethAccount.asmx/IsSignedIn"
_GETTAGLIST = _BASEURL + "/ethClient.asmx/GetTagList"
_GETTEMPDATA = _BASEURL + "/ethLogShared.asmx/GetLatestTemperatureRawDataByUUID"
_HEADERS = {
"content-type": "application/json; charset=utf-8"
}
_DECIMALS = 1
class ClientAuth:
"""
Request authentication and return authentication cookie. If cookie requested and not already logged in, it will log in again.
"""
def __init__(self, username=_USERNAME,
password=_PASSWORD):
postParams = {
"email": username,
"password": password
}
r = requests.post(_SIGNIN, headers=_HEADERS, data=json.dumps(postParams))
self._accessCookie = r.cookies
self._username = username
self._password = password
r = requests.post(_ISSIGNED, headers=_HEADERS,cookies=self._accessCookie)
response = r.json()
if response['d']!=True:
raise ValueError('Incorrect Login operation')
@property
def accessCookie(self):
#if not signed in, sign in and return cookie
r = requests.post(_ISSIGNED, headers=_HEADERS)
response = r.json()
if response['d']=='TRUE':
return self._accessCookie
else:
postParams = {
"email": self._username,
"password": self._password
}
r = requests.post(_SIGNIN, headers=_HEADERS, data=json.dumps(postParams))
self._accessCookie = r.cookies
return self._accessCookie
return self._accessCookie
class WirelessTagData:
"""
Retrieves data from Wireless senors available
"""
def __init__(self,authData):
self.getAuthToken = authData.accessCookie
# self._TagList = self.getTagsList()
@property
def tagList(self):
self._tagList = {}
cookies = self.getAuthToken
r = requests.post(_GETTAGLIST, headers=_HEADERS, cookies=cookies)
response = r.json()
for i in response:
for tag in response[i]:
tag_id = tag["slaveId"]
tag_uuid = tag["uuid"]
tag_name = tag["name"]
tag_type = tag["tagType"]
self._tagList[tag_uuid] = {'tag_id' : tag_id, 'tag_name' : tag_name, 'tag_type' : tag_type}
return self._tagList
def getTemperature(self,uuid=""):
"""
If no UUID provided, it will take the first sensor discovered
"""
if uuid=="":
uuid = self.tagList.keys()[0]
data = {
"uuid": uuid
}
cookies = self.getAuthToken
r = requests.post(_GETTEMPDATA, headers=_HEADERS, cookies=cookies, data=json.dumps(data))
parsed_response = r.json()
temp = Decimal(float(parsed_response["d"]["temp_degC"]))
rounded_temp = round(temp,_DECIMALS)
return rounded_temp
def getHumidity(self,uuid=""):
"""
If no UUID provided, it will take the first sensor discovered
"""
if uuid=="":
uuid = self.tagList.keys()[0]
data = {
"uuid": uuid
}
cookies = self.getAuthToken
r = requests.post(_GETTEMPDATA, headers=_HEADERS, cookies=cookies, data=json.dumps(data))
parsed_response = r.json()
return parsed_response["d"]["cap"]
def getBatteryVolt(self,uuid=""):
"""
If no UUID provided, it will take the first sensor discovered
"""
if uuid=="":
uuid = self.tagList.keys()[0]
data = {
"uuid": uuid
}
cookies = self.getAuthToken
r = requests.post(_GETTEMPDATA, headers=_HEADERS, cookies=cookies, data=json.dumps(data))
parsed_response = r.json()
return parsed_response["d"]["battery_volts"]