-
Notifications
You must be signed in to change notification settings - Fork 0
/
influx_populator.py
349 lines (286 loc) · 11.3 KB
/
influx_populator.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
import random, datetime
# InfluxDB connection settings
url = 'http://localhost:8086'
token = 'cq6nclb3oF18TopO-KJ20Iq1U1b2tPhnl-KCaPiDXcs9RuidUxrkRNrGXpwkrmibyT4nkFmHTcPqEyGV8pJbfw=='
org = 'Tiba'
bucket = 'measurements'
measurements = ['humidity', 'temperature']
# Create InfluxDB client
client = InfluxDBClient(url=url, token=token, org=org)
write_api = client.write_api(write_options=SYNCHRONOUS)
# Function to generate sample data
def generate_data():
now = datetime.datetime.utcnow()
timestamp = now - datetime.timedelta(days=90)
while timestamp <= now:
for measurement in measurements:
unit = 'C'
value_1 = 0
value_2 = 0
if measurement == "humidity":
unit = "%"
value_1 = random.uniform(30, 60)
value_2 = random.uniform(30, 60)
else:
value_1 = random.uniform(-5, 5)
value_2 = random.uniform(30, 60)
point = Point(measurement).field("value", value_1).tag("deviceId", str(7)).tag("unit", unit).time(timestamp, WritePrecision.NS)
write_to_influxdb(point)
point = Point(measurement).field("value", value_2).tag("deviceId", str(8)).tag("unit", unit).time(timestamp, WritePrecision.NS)
write_to_influxdb(point)
timestamp += datetime.timedelta(seconds=5)
def generate_states_data():
now = datetime.datetime.utcnow()
timestamp = now - datetime.timedelta(days=90)
while timestamp <= now:
ac_point = generate_ac_data(timestamp,11)
write_to_influxdb(ac_point)
ac_point = generate_ac_data(timestamp,12)
write_to_influxdb(ac_point)
wm_point = generate_wm_data(timestamp,9)
write_to_influxdb(wm_point)
wm_point = generate_wm_data(timestamp,10)
write_to_influxdb(wm_point)
timestamp += datetime.timedelta(seconds=60)
def generate_data_2():
now = datetime.datetime.utcnow()
timestamp = now - datetime.timedelta(days=91)
limit = now
count = 0
while timestamp <= limit:
battery_point_1, battery_point_2 = generate_battery_data_3m(timestamp) # every 30 secs
write_to_influxdb(battery_point_1)
write_to_influxdb(battery_point_2)
if count % 720 == 0: # every 6 hrs
sps_point = generate_sps_data_3m(timestamp)
write_to_influxdb(sps_point)
if count % 360 == 0: # every 3 hrs
charger_point = generate_charger_data_3m(timestamp)
write_to_influxdb(charger_point)
if count % 2 == 0: # every min
consumption, production, balance = generate_power_data(timestamp)
write_to_influxdb(consumption)
write_to_influxdb(production)
write_to_influxdb(balance)
count += 1
timestamp += datetime.timedelta(seconds=30)
def generate_power_data(curr_timestamp):
id = 1
consumption = random.random() * 2
production = random.random() * 2
balance = production - consumption
consumption_point = Point("totalConsumption").field("value", consumption).time(curr_timestamp, WritePrecision.NS)
production_point = Point("totalProduction").field("value", production).time(curr_timestamp, WritePrecision.NS)
balance_point = Point("totalBalance").field("value", balance).time(curr_timestamp, WritePrecision.NS)
tags = {
"deviceId": str(id),
"unit": "kWh"
}
for key,value in tags.items():
consumption_point.tag(key, str(value))
production_point.tag(key, str(value))
balance_point.tag(key, str(value))
return consumption_point, production_point, balance_point
def generate_battery_data_3m(curr_timestamp):
id_1 = 5
id_2 = 6
user_id = 2
battery_kwh_1 = float(random.randint(0, 100))
battery_kwh_2 = float(random.randint(0, 100))
tags_1 = {
"userId" : str(user_id),
"deviceId": str(id_1)
}
tags_2 = {
"userId" : str(user_id),
"deviceId": str(id_2)
}
point_1 = Point("battery").field("value", battery_kwh_1).time(curr_timestamp, WritePrecision.NS)
point_2 = Point("battery").field("value", battery_kwh_2).time(curr_timestamp, WritePrecision.NS)
for key,value in tags_1.items():
point_1.tag(key,str(value))
for key,value in tags_2.items():
point_2.tag(key,str(value))
return point_1, point_2
def generate_charger_data_3m(curr_timestamp):
id = random.randint(3, 4)
user_id = 2
state = "START_CHARGE"
portNum = random.randint(1, 2)
tags = {
"userId" : str(user_id),
"deviceId": str(id),
"portNum": str(portNum)
}
if random.randint(0, 1) == 0:
state = "END_CHARGE"
carCharge = random.randint(20, 150)
spentEnergy = random.randint(1, 130)
tags['carCharge'] = str(carCharge)
tags['spentEnergy'] = str(spentEnergy)
else:
carCapacity = random.randint(20, 150)
carCharge = random.randint(0, carCapacity - 5)
tags['carCapacity'] = carCapacity
tags['carCharge'] = carCharge
point = Point("states").field("value", state).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_sps_data_3m(curr_timestamp):
id = random.randint(1, 2)
user_id = 2
val = random.randint(0, 1)
if val == 0:
val = "ON"
else:
val = "OFF"
tags = {
"userId" : str(user_id),
"deviceId": str(id)
}
point = Point("states").field("value", val).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_battery_data(curr_timestamp):
id = random.randint(900018, 1000017)
user_id = random.randint(1,1000002)
battery_kwh = random.randint(0, 100)
tags = {
"userId" : str(user_id),
"deviceId": str(id)
}
point = Point("battery").field("value", battery_kwh).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_charger_data(curr_timestamp):
id = random.randint(1000017, 1100017)
user_id = random.randint(1,1000002)
state = "START_CHARGE"
portNum = random.randint(1, 4)
tags = {
"userId" : str(user_id),
"deviceId": str(id),
"portNum": str(portNum)
}
if random.randint(0, 1) == 0:
state = "END_CHARGE"
carCharge = random.randint(20, 150)
spentEnergy = random.randint(1, 130)
tags['carCharge'] = str(carCharge)
tags['spentEnergy'] = str(spentEnergy)
else:
carCapacity = random.randint(20, 150)
carCharge = random.randint(0, carCapacity - 5)
tags['carCapacity'] = carCapacity
tags['carCharge'] = carCharge
point = Point("states").field("value", state).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_sps_data(curr_timestamp):
id = random.randint(1100017, 1200017)
user_id = random.randint(1,1000002)
val = random.randint(0, 1)
if val == 0:
val = "ON"
else:
val = "OFF"
tags = {
"userId" : str(user_id),
"deviceId": str(id)
}
point = Point("states").field("value", val).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_consumption_data(curr_timestamp):
id = random.randint(1100017, 1200017)
val = random.randint(0, 10)
tags = {
"deviceId": str(id),
"unit": "kWh"
}
point = Point("totalConsumption").field("value", val).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_production_data(curr_timestamp):
id = random.randint(1100017, 1200017)
val = random.randint(0, 10)
tags = {
"deviceId": str(id),
"unit": "kWh"
}
point = Point("totalProduction").field("value", val).time(curr_timestamp, WritePrecision.NS)
for key,value in tags.items():
point.tag(key,str(value))
return point
def generate_ac_data(curr_timestamp, ac_id):
user_id = 2
modes = ['HEAT_MODE', 'COOL_MODE', 'AUTO_MODE', 'DRY_MODE', 'FUNGUS_CHANGE', 'HEALTH_CHANGE', 'FAN_SPEED_CHANGE', 'TEMP_CHANGE',
'CANCEL_SCHEDULED', 'SCHEDULE', 'SCHEDULE_OFF', 'ON', 'OFF', 'SCHEDULE_ON', 'CHANGE']
state_idx = random.randint(8,14)
tags = {
"userId" : str(user_id),
"deviceId": str(ac_id)
}
val = modes[state_idx]
if val == 'CHANGE':
mode = modes[random.randint(0,7)]
if mode == "FUNGUS_CHANGE":
tags['isFungus'] = False if random.randint(0,1) == 0 else True
if mode == "HEALTH_CHANGE":
tags['isHealth'] = False if random.randint(0,1) == 0 else True
if mode == 'FAN_SPEED_CHANGE':
tags['fanSpeed'] = random.randint(1,3)
if mode == "TEMP_CHANGE":
tags['target'] = random.randint(16,40)
val = mode
if val == "SCHEDULE" or val == "CANCEL_SCHEDULED":
tags['from'] = round((curr_timestamp - datetime.timedelta(hours=1)).timestamp()) * 1000
tags['to'] = round((curr_timestamp - datetime.timedelta(minutes=15)).timestamp()) * 1000
tags['everyDay'] = False if random.randint(0,1) == 0 else True
point = Point("states").field("value", val).time(curr_timestamp, WritePrecision.NS)
for key,valu in tags.items():
point.tag(key,str(valu))
return point
def generate_wm_data(curr_timestamp, wm_id):
user_id = 2
states = ['ON', 'OFF', 'SCHEDULE_ON', 'SCHEDULE_OFF', 'SCHEDULE', 'CANCEL_SCHEDULED']
modes = ['COTTONS_MODE', 'SYNTHETICS_MODE', 'DAILY_EXPRESS_MODE', 'WOOL_MODE', 'DARK_WASH_MODE',
'OUTDOOR_MODE', 'SHIRTS_MODE', 'DUVET_MODE', 'MIXED_MODE', 'STEAM_MODE', 'RINSE_SPIN_MODE', 'SPIN_ONLY_MODE', 'HYGIENE_MODE']
centrifuge_speeds = [800, 900, 1000, 1100, 1200, 1300, 1400]
temps = [30, 40, 50, 60, 70, 80, 90]
state_idx = random.randint(0,5)
tags = {
"userId" : str(user_id),
"deviceId": str(wm_id)
}
if states[state_idx] == 'ON':
tags['mode'] = modes[random.randint(0,12)]
tags['temp'] = temps[random.randint(0,6)]
tags['centrifuge'] = centrifuge_speeds[random.randint(0,6)]
if states[state_idx] == "SCHEDULE" or states[state_idx] == "CANCEL_SCHEDULED":
tags['from'] = round((curr_timestamp - datetime.timedelta(hours=1)).timestamp()) * 1000
tags['to'] = round((curr_timestamp - datetime.timedelta(minutes=15)).timestamp()) * 1000
point = Point("states").field("value", states[state_idx]).time(curr_timestamp, WritePrecision.NS)
for key,val in tags.items():
point.tag(key,str(val))
return point
# Function to write data to InfluxDB
def write_to_influxdb(point):
try:
write_api.write(bucket=bucket, record=point)
except Exception as e:
print(f"Error writing to InfluxDB: {e}")
# Main execution
if __name__ == "__main__":
#generate_data()
generate_states_data()
#generate_data_2()
print('Done')