-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtag2influx.py
executable file
·161 lines (129 loc) · 5.78 KB
/
tag2influx.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
import argparse
import requests
import json
from datetime import datetime, timedelta
import pytz
from calendar import timegm
from collections import defaultdict
import sys
## parse arguments
parser = argparse.ArgumentParser(description="Copy data from Wireless Tags API to InfluxDB", epilog="Available STAT types depend on the tag, but these are the current known types: temperature, dp (dew point), cap (humidity), batteryVolt, signal, motion, light")
parser.add_argument('--stat', metavar='STAT', nargs='*', default=["temperature"], help='stat type(s) to fetch (default: temperature)')
parser.add_argument('--last', metavar='N', type=int, default=30, help='fetch last N minutes of data (default: 30)')
parser.add_argument('--fromdate', metavar='YYYY-MM-DD[THH:MM]', help='fetch data starting from date (optionally time)')
parser.add_argument('--todate', metavar='YYYY-MM-DD', help='fetch data ending on date (default: now)')
parser.add_argument('--config', metavar='FILE', default="tag2influx.conf", help='path to configuration file (default: tag2influx.conf)')
parser.add_argument('-d', action='count', default=0, help=argparse.SUPPRESS)
args = parser.parse_args()
def _debug(msg, minlvl=1):
if args.d >= minlvl:
print("debug: " + msg)
_debug("debug level " + str(args.d) + " enabled")
## settings ##
_debug("using configuration file " + str(args.config))
with open(args.config) as json_conf_file:
conf = json.load(json_conf_file)
wtag_email = conf['wtag']['email']
wtag_password = conf['wtag']['password']
wtag_timezone = conf['wtag']['timezone']
wtag_tag_ids = conf['wtag'].get('tag_ids') or []
influx_write_url = conf['influx']['write_url']
influx_batch_size = conf['influx'].get('batch_size') or 1000
wtag_base_url = conf['wtag'].get('base_url') or "https://www.mytaglist.com"
wtag_signin_url = wtag_base_url + (conf['wtag'].get('signin_url') or "/ethAccount.asmx/SignIn")
wtag_getmultitagstatsraw_url = wtag_base_url + (conf['wtag'].get('getmultitagstatsraw_url') or "/ethLogs.asmx/GetMultiTagStatsRaw")
## settings end ##
def _batches(iterable, size):
for i in range(0, len(iterable), size):
yield iterable[i:i + size]
def _format_points(points):
measurement = "wtag"
try:
measurement = conf['influx']['schema'].get('measurement') or measurement
except KeyError:
pass
tag_key = "tag"
try:
tag_key = conf['influx']['schema'].get('tag_key') or tag_key
except KeyError:
pass
result = []
for time in sorted(points):
for tag in points[time]:
stats = []
for stat in points[time][tag]:
value = str(points[time][tag][stat])
try:
stat = conf['influx']['schema']['stat_map'].get(stat) or stat
except KeyError:
pass
stats.append("%s=%s" % (stat, value))
result.append("%s,%s=\"%s\" %s %s" % (measurement, tag_key, str(tag).replace(' ', r'\ '), ','.join(stats), str(time*1000000000)))
_debug(result[-1])
return result
def _fetch_wtag_data(rs, stat, local_tz, fromDate, toDate, points):
wtag_r = rs.post(wtag_getmultitagstatsraw_url, json = {"ids":wtag_tag_ids,"type":stat,"fromDate":fromDate.strftime("%Y-%m-%dT%H:%M"),"toDate":toDate.strftime("%Y-%m-%d")}, timeout=20)
assert wtag_r.ok, "GetMultiTagStatsRaw failed"
j = json.loads(wtag_r.content)
idmap = {}
for i,id in enumerate(j['d']['ids'],0):
idmap[id]=j['d']['names'][i]
for day in j['d']['stats']:
day_dt = datetime.strptime(day['date'],'%m/%d/%Y')
day_dt = local_tz.localize(day_dt)
for tag_index,tagvalues in enumerate(day['values'],0):
for i,value in enumerate(tagvalues,0):
value_dt = day_dt + timedelta(seconds=day['tods'][tag_index][i])
value_dt_utc = value_dt.astimezone(pytz.utc)
timestamp = timegm(value_dt_utc.timetuple())
tag = idmap[day['ids'][tag_index]]
points[timestamp][tag][stat] = value
return points
def _write_influx(points):
influx_rs = requests.Session()
for batch,data in enumerate(_batches(points, influx_batch_size),1):
print("WRITE batch " + str(batch) + ", " + str(len(data)) + " points")
if isinstance(data, str):
data = [data]
influx_r = influx_rs.post(influx_write_url, data = ('\n'.join(data) + '\n').encode('utf-8'), timeout=15)
assert influx_r.ok, "influx write failed: " + influx_r.text
print("WROTE " + str(len(points)) + " points in " + str(batch) + " batches")
def _main():
if args.todate and not args.fromdate:
parser.error('--todate can only be set with --fromdate')
wtag_local_tz = pytz.timezone(wtag_timezone)
if not args.fromdate:
toDate = datetime.now(wtag_local_tz)
fromDate = toDate - timedelta(minutes=args.last)
else:
try:
fromDate = datetime.strptime(args.fromdate,'%Y-%m-%dT%H:%M')
except ValueError:
fromDate = datetime.strptime(args.fromdate,'%Y-%m-%d')
fromDate = wtag_local_tz.localize(fromDate)
if args.todate:
toDate = datetime.strptime(args.todate,'%Y-%m-%d')
toDate = wtag_local_tz.localize(toDate)
else:
toDate = datetime.now(wtag_local_tz)
wtag_rs = requests.Session()
points = defaultdict(lambda: defaultdict(dict))
login = False
for stat in args.stat:
print("Requesting WTAG " + stat + " data, fromDate: " + str(fromDate.strftime("%Y-%m-%dT%H:%M")) + ", toDate: " + str(toDate.strftime("%Y-%m-%d")))
if not login:
wtag_r = wtag_rs.post(wtag_signin_url, json = {"email":wtag_email,"password":wtag_password}, timeout=5)
assert wtag_r.ok, "login failed"
login = True
points = _fetch_wtag_data(wtag_rs, stat, wtag_local_tz, fromDate, toDate, points)
if not points:
print("No data points received from API")
else:
if args.d < 2:
_write_influx(_format_points(points))
else:
_debug("influx write disabled, format only")
_format_points(points)
if __name__ == "__main__":
_main()