-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeterlogger.py
More file actions
39 lines (32 loc) · 1.21 KB
/
Copy pathmeterlogger.py
File metadata and controls
39 lines (32 loc) · 1.21 KB
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
import requests
import json
from concurrent.futures import ProcessPoolExecutor
import time
from pathlib import Path
import random
directory = "meter_data"
meterfiles = sorted(Path(directory).glob("readings*"))
for i in meterfiles:
print(i)
APIURL = "http://localhost:5000/meter"
APIURL2GETREADINGS = "http://localhost:5000/getmeterdata"
def packetreader(packet):
response = requests.post(APIURL, json = packet)
while response.status_code == 201:
time.sleep(0.5)
response = requests.post(APIURL, json = packet)
print(response.text, "| Status Code:", response.status_code)
time.sleep((random.randrange(1, 5)/10))
if __name__ == "__main__":
filecounter = 0
for filepath in meterfiles:
with open(filepath) as f:
jsonfile = json.load(f)
reading_list = jsonfile["readings"]
with ProcessPoolExecutor(max_workers = 8) as executor:
futures = [executor.submit(packetreader, i) for i in reading_list]
filecounter += 1
print(f"Readings for {filepath} all uploaded, this is file {filecounter}")
time.sleep(5) # Simulates 1/2 hour sending
response2 = requests.get(APIURL2GETREADINGS)
print(response2.text)