-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgs.py
More file actions
executable file
·86 lines (51 loc) · 1.99 KB
/
gs.py
File metadata and controls
executable file
·86 lines (51 loc) · 1.99 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
#!/usr/bin/env python
import time
import datetime
import httplib
import json
import random
import urllib
import logging
class GSU(object):
def __init__ (self):
self.api_key = "0e7249e2-7420-3a97-8987-d7a6f248d5de" #Change This!!!
self.component_id = "GreenEyeMonitor"
self.base_url = "/api/feed?"
self.nextSendTime = 0.0
self.headers = {"Connection" : "close", "Content-type": "application/json",
"Cookie" : "api_key="+ self.api_key}
def sendData (self, data):
if self.nextSendTime < time():
return
self.nextSendTime = time.time() + 15*60
logging.debug ("Next time to send is in 15 minutes")
try:
conn = httplib.HTTPConnection('www.grovestreams.com')
url = self.base_url + urllib.urlencode (data)
logging.debug ("Uploading data = " + url)
logging.debug ("headers = " + json.dumps(self.headers))
conn.request ("PUT", url, "", self.headers)
response = conn.getresponse()
status = response.status
if status != 200 and status != 201:
try:
if (response.reason != None):
logging.debug('HTTP Failure Reason: ' + response.reason + ' body: ' + response.read())
else:
logging.debug('HTTP Failure Body: ' + response.read())
except Exception:
logging.debug('HTTP Failure Status: %d' % (status) )
except Exception as e:
logging.debug('HTTP Failure: ' + str(e))
finally:
if conn != None:
conn.close()
if __name__ == "__main__":
logging.debug ("main in gs.py")
g = GSU()
while True:
dp1 = random.randrange (-10, 40)
dp2 = random.randrange (0, 100)
g.sendData ({ 'compId': 'GreenEyeMonitor', 'dp1' : dp1,
'dp2' : dp2})
time.sleep(10)