-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
executable file
·157 lines (139 loc) · 5.86 KB
/
consumer.py
File metadata and controls
executable file
·157 lines (139 loc) · 5.86 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
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
#!/usr/bin/env python
import sys, json, datetime, calendar, time, socket, httpx
from argparse import ArgumentParser, FileType
from configparser import ConfigParser
from confluent_kafka import Consumer, OFFSET_BEGINNING
from threading import Thread
if __name__ == '__main__':
# Parse the command line.
parser = ArgumentParser()
parser.add_argument('--reset', action='store_true')
args = parser.parse_args()
def reset_offset(consumer, partitions):
if args.reset:
for p in partitions:
p.offset = OFFSET_BEGINNING
consumer.assign(partitions)
def power_consumer(name, house_range):
consumer = Consumer(
{
'bootstrap.servers': '172.16.250.13, 172.16.250.14',
'group.id': name,
'auto.offset.reset': 'earliest'
}
)
consumer.subscribe(["power"], on_assign=reset_offset)
try:
while True:
try:
msg = consumer.poll(1.0)
if msg is None:
print("Power Waiting...")
continue
elif msg.error():
print(msg.error())
print("ERROR: %s".format(msg.error()))
else:
value = json.loads(msg.value())
house_id = int(value.get("house_id"))
if house_id not in range(*house_range):
continue
power = value.get("kwh")
timestamp = value.get("timestamp", 0)
housedata = f'house_id="{house_id}"'
try:
time.sleep(0.1)
httpx.post(f"http://172.16.250.15:8428/api/v1/import/prometheus?extra_label=instance=RAPPERDAPPER", data=f'superpower{{{housedata}}} {float(power)} {timestamp}\n')
except Exception:
pass
except:
pass
except KeyboardInterrupt:
pass
finally:
consumer.close()
def water_consumer(name, house_range):
consumer = Consumer(
{
'bootstrap.servers': '172.16.250.13, 172.16.250.14',
'group.id': name,
'auto.offset.reset': 'earliest'
}
)
consumer.subscribe(["water"], on_assign=reset_offset)
try:
while True:
try:
msg = consumer.poll(1.0)
if msg is None:
print("Water Waiting...")
continue
elif msg.error():
print(msg.error())
print("ERROR: %s".format(msg.error()))
else:
value = json.loads(msg.value())
house_id = int(value.get("house_id"))
if house_id not in range(*house_range):
continue
power = value.get("m3")
timestamp = value.get("timestamp", 0)
housedata = f'house_id="{house_id}"'
try:
httpx.post(f"http://172.16.250.15:8428/api/v1/import/prometheus?extra_label=instance=RAPPERDAPPER", data=f'superwater{{{housedata}}} {float(power)} {timestamp}\n')
except Exception:
pass
except:
pass
except KeyboardInterrupt:
pass
finally:
consumer.close()
def heat_consumer(name, house_range):
consumer = Consumer(
{
'bootstrap.servers': '172.16.250.13, 172.16.250.14',
'group.id': name,
'auto.offset.reset': 'earliest'
}
)
consumer.subscribe(["heat"], on_assign=reset_offset)
try:
while True:
try:
msg = consumer.poll(1.0)
if msg is None:
print("Heat Waiting...")
continue
elif msg.error():
print(msg.error())
print("ERROR: %s".format(msg.error()))
else:
value = json.loads(msg.value())
house_id = int(value.get("house_id"))
if house_id not in range(*house_range):
continue
power = value.get("kwh")
timestamp = value.get("timestamp", 0)
housedata = f'house_id="{house_id}"'
try:
httpx.post(f"http://172.16.250.15:8428/api/v1/import/prometheus?extra_label=instance=RAPPERDAPPER", data=f'superheat{{{housedata}}} {float(power)} {timestamp}\n')
except Exception:
pass
except:
pass
except KeyboardInterrupt:
pass
finally:
consumer.close()
threads = []
threads.append(Thread(target=power_consumer, args=("PowerThread1", (1,50))))
threads.append(Thread(target=power_consumer, args=("PowerThread2", (51, 100))))
threads.append(Thread(target=water_consumer, args=("WaterThread1", (1,50))))
threads.append(Thread(target=water_consumer, args=("WaterThread2", (51,100))))
threads.append(Thread(target=heat_consumer, args=("HeatThread1", (1,50))))
threads.append(Thread(target=heat_consumer, args=("HeatThread2", (51,100))))
for thread in threads:
thread.start()
for thread in threads:
thread.join()