-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate-sensor-data.py
78 lines (74 loc) · 2.37 KB
/
populate-sensor-data.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
# -*- coding: utf-8 -*-
# Mutated from code examples for Aerospike Modeling: IoT Sensors
# https://dev.to/aerospike/aerospike-modeling-iot-sensors-453a
# https://github.com/aerospike-examples/modeling-iot-sensors
from args import options
import aerospike
from aerospike import exception as ex
from aerospike_helpers.operations import list_operations as lh
import sys
if options.set == "None":
options.set = None
config = {"hosts": [(options.host, options.port)]}
if options.alternate:
config["use_services_alternate"] = True
try:
client = aerospike.client(config).connect(options.username, options.password)
policy = {"key": aerospike.POLICY_KEY_SEND}
except ex.ClientError as e:
if not options.quiet:
print("Error: {0} [{1}]".format(e.msg, e.code))
sys.exit(2)
sensor_id = 1
spacer = "=" * 30
minute = 0
f = open("./sensor.csv", "r")
for line in f:
try:
try:
_, h, t = line.split(",")
try:
prev_hour
except:
prev_temp = int(float(t.strip()[1:-1]) * 10)
prev_day = h[1:6]
prev_hour = int(h[7:9])
continue
temp = int(float(t.strip()[1:-1]) * 10)
day = h[1:6]
hour = int(h[7:9])
readings = []
step = (temp - prev_temp) / 60.0
for i in range(0, 60):
prev_temp = prev_temp + step
readings.append([minute, int(prev_temp)])
minute = minute + 1
key = (
options.namespace,
options.set,
"sensor{}-{}".format(sensor_id, prev_day),
)
print(spacer)
print("Day {0} hour {1}".format(prev_day, prev_hour))
print(readings)
client.operate(key, [lh.list_append_items("t", readings)], policy=policy)
if day != prev_day:
minute = 0
prev_temp = temp
prev_day = day
prev_hour = hour
except ValueError as e:
print(e)
pass
except IndexError:
pass
f.close()
print(spacer)
print("Sensor {} data for December 31".format(sensor_id))
k, m, b = client.get(
(options.namespace, options.set, "sensor{}-12-31".format(sensor_id))
)
print(b)
print("Above is Sensor {} data for December 31".format(sensor_id))
print(spacer)
client.close()