-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessor.py
106 lines (92 loc) · 2.92 KB
/
processor.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
import logging
logger = logging.getLogger(__name__)
logger.debug("processor.py loaded")
import gviz_api
import json
from datetime import datetime, timedelta
from time import mktime
from smartthings import SmartThings
def update_range(old, new):
if "max" in new:
if old["max"] is None:
old["max"] = new["max"]
else:
if new["max"] > old["max"]:
old["max"] = new["max"]
if "min" in new:
if old["min"] is None:
old["min"] = new["min"]
else:
if new["min"] < old["min"]:
old["min"] = new["min"]
def things(st):
return 1
def results(token):
logger.debug("results(%s)" % token)
st = SmartThings(token)
dates = {
"bound": {
"min": None,
"max": None,
},
"default": {
"min": datetime(2016, 4, 20),
"max": datetime(2016, 4, 25),
},
}
description = { "Date": "datetime" }
data = []
columns_order=["Date"]
things = st.things("temperature")
for thing in things[:9]:
description[thing["label"]] = "number"
columns_order.append(thing["label"])
source_data = st.states(
thing_id=thing["id"],
state="temperature",
since=dates["default"]["min"],
until=dates["default"]["max"],
)
update_range(
dates["bound"],
st.states_range(
thing_id=thing["id"],
state="temperature",
),
)
logger.debug(
"range is {0} to {1}"
.format(dates["bound"]["min"], dates["bound"]["max"])
)
#logger.debug("Processing thing: {0}".format(thing))
logger.debug("Found rows: {0}".format(source_data.count()))
for row in source_data[:]:
#logger.debug("Processing row: {0}".format(row))
if row["date"] is not None and row["value"] is not None:
value = int(float(row["value"]))
if value < 150:
data.append({
"Date": row["date"],
thing["label"]: value,
})
# logger.debug("Data: {0}".format(data))
# Load it into gviz_api.DataTable
data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
# Create JavaScript code string
jscode = data_table.ToJSCode(
"jscode_data",
columns_order=columns_order,
order_by=columns_order[0],
)
jsdates = {
"bound": {
"min": int(mktime(dates["bound"]["min"].timetuple())) * 1000,
"max": int(mktime(dates["bound"]["max"].timetuple())) * 1000,
},
"default": {
"min": int(mktime(dates["default"]["min"].timetuple())) * 1000,
"max": int(mktime(dates["default"]["max"].timetuple())) * 1000,
},
}
return {"jscode": jscode, "dates": jsdates}