forked from IBMDeveloperNYC/Pi-Sense-HAT-Node-RED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushasync.py
executable file
·138 lines (111 loc) · 3.2 KB
/
pushasync.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
import sys
from requests_futures.sessions import FuturesSession
from time import sleep
from colorLetter8x8 import *
import requests
from sense_hat import SenseHat
import datetime as dt
from CsvHelper import CsvReader as ch
lol = ch().readCSV("./utils/users.csv")
from common import *
import time
sense = SenseHat()
def getSec():
sec = time.mktime(dt.datetime.now().timetuple())
return sec
def run(urls):
t = getTimeStamp()
start = getSec()
data = {"temp":getTemp(),
"humidity":getHumidity(),
"longitude":getLongitude(),
"latitude":getLatitude(),
"elevation":getElevation(),
"sensorID":getSensorID(),
"sensorLocalTime":getTimeStamp()}
with FuturesSession(max_workers=2) as session:
for url in urls:
#print(url)
session.post(url, data)
end = getSec()
timeran = end - start
print("script took {} seconds".format(timeran))
displayText("{}".format(timeran))
def displayText(txt):
sense.clear()
sense.show_message(txt)
sense.clear()
def callNodeRED(url, data):
try:
r = None
print("About to post to {}".format(url))
r = requests.post(url, data = data)
print(r.status_code)
print(r.json())
except Exception as rex:
print("error")
print(rex)
def getTimeStamp():
return dt.datetime.now().strftime("%Y-%m-%d %H:%M")
def mkLetter(C, X, O):
sense.clear()
if C == 'H':
letter = letterH(X, O)
elif C == 'M':
letter = letterM(X, O)
else:
letter = letterC(X, O)
sense.set_pixels(letter)
def getTemp():
sense.clear()
temp = sense.get_temperature()
sense.clear()
return temp
def displayTemp():
sense.clear()
temp = getTemp()
sense.clear()
bg = (0,0,0)
c = (255,255,255)
L = ''
if (temp >= 30.00):
print("hot!!")
L='H'
c = (255,0,0) #red
bg = (242,231,102) #yellow
elif (temp < 30 and temp > 15):
print("moderate")
L='M'
c= (3,255,53) #greenw
bg = (5,5,5) #dark grey`
else:
print("cold")
L='C'
c=(3,32,252)
bg=(225,225,230)
mkLetter(L,c,bg)
sleep(2)
sense.clear()
return temp
def getHumidity():
sense.clear()
return sense.get_humidity()
def createUrls(lol, urlList):
""" create urls off list of lists from users.csv"""
for user in lol:
firstName = user[0].strip()
lastName = user[1].strip()
url = "http://{}{}nodered.myblumeix.net/sense-hat".format(firstName[0].lower(), lastName[0].lower())
urlList.append(url)
return urlList
if __name__ == '__main__':
url_ = ["http://pmistrynoderedtest.mybluemix.net/sense-hat"]
#url_ = ["http://gsnodered.mybluemix.net/sense-hat"]
try:
lol = ch().readCSV("./utils/users.csv")
lol = createUrls(lol, url_)
except FileNotFoundError:
pass
lol.append("http://gsnodered.mybluemix.net/sense-hat")
displayTemp()
run(url_)