-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathambient.py
More file actions
105 lines (97 loc) · 4.05 KB
/
Copy pathambient.py
File metadata and controls
105 lines (97 loc) · 4.05 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
class Ambient:
def __init__(self, channelId, writeKey, readKey=None, userKey=None, ssl=False, debug=False):
try:
import urequests
self.requests = urequests
self.micro = True
except ImportError:
import requests
self.requests = requests
self.micro = False
import time
self.time = time
self.channelId = channelId
self.writeKey = writeKey
self.readKey = readKey
self.userKey = userKey
self.ssl = ssl
self.debug = debug
if self.debug:
self.url = 'http://192.168.33.13/api/v2/channels/' + str(channelId)
else:
if self.ssl and not self.micro:
self.url = 'https://ambidata.io/api/v2/channels/' + str(channelId)
else:
self.url = 'http://ambidata.io/api/v2/channels/' + str(channelId)
self.lastsend = 0
def send(self, data, timeout = 30.0):
millis = self.time.time() * 1000.0 if not self.micro else self.time.ticks_ms()
if self.lastsend != 0 and (millis - self.lastsend ) < 4999:
if self.micro:
r = self.requests.Response(None)
else:
r = self.requests.Response()
r.status_code = 403
return r
if isinstance(data, list):
__d = data
else:
__d = [data]
if self.micro:
r = self.requests.post(self.url + '/dataarray', json = {'writeKey': self.writeKey, 'data': __d}, headers = {'Content-Type' : 'application/json'})
else:
r = self.requests.post(self.url + '/dataarray', json = {'writeKey': self.writeKey, 'data': __d}, headers = {}, timeout = timeout)
millis = self.time.time() * 1000.0 if not self.micro else self.time.ticks_ms()
self.lastsend = millis
return r
def read(self, **args):
url = self.url + '/data'
__o = []
if hasattr(self, 'readKey'):
__o.append('readKey=' + self.readKey)
if 'date' in args:
__o.append('date=' + args['date'])
else:
if 'start' in args and 'end' in args:
__o.append('start=' + args['start'])
__o.append('end=' + args['end'])
else:
if 'n' in args:
__o.append('n=' + str(args['n']))
if 'skip' in args:
__o.append('skip=' + str(args['skip']))
if len(__o) > 0:
url = url + '?' + '&'.join(__o)
timeout = 30.0
if 'timeout' in args:
timeout = args['timeout']
if self.micro:
self.r = self.requests.get(url)
else:
self.r = self.requests.get(url, timeout = timeout)
return list(reversed(self.r.json()))
def getprop(self, **args):
url = self.url
if hasattr(self, 'readKey'):
url = url + '?' + 'readKey=' + self.readKey
timeout = 30.0
if 'timeout' in args:
timeout = args['timeout']
if self.micro:
self.r = self.requests.get(url)
else:
self.r = self.requests.get(url, timeout = timeout)
self.prop = self.r.json()
return self.prop
def putcmnt(self, t, cmnt, timeout = 30.0):
if self.micro:
r = self.requests.put(self.url + '/data', json = {'writeKey': self.writeKey, 'created': t, 'cmnt': cmnt}, headers = {'Content-Type' : 'application/json'})
else:
r = self.requests.put(self.url + '/data', json = {'writeKey': self.writeKey, 'created': t, 'cmnt': cmnt}, headers = {}, timeout = timeout)
return r
def sethide(self, t, hide, timeout = 30.0):
if self.micro:
r = self.requests.put(self.url + '/data', json = {'writeKey': self.writeKey, 'created': t, 'hide': hide}, headers = {'Content-Type' : 'application/json'})
else:
r = self.requests.put(self.url + '/data', json = {'writeKey': self.writeKey, 'created': t, 'hide': hide}, headers = {}, timeout = timeout)
return r