-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
140 lines (110 loc) · 4.16 KB
/
consumer.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
import datetime as dt
import json
from argparse import Namespace
import paho.mqtt.client as mqtt
from functions.encryption import init_rsa_security, verify_and_decrypt_data
from functions.parse import get_params
from functions.typing_extras import FileClient, MQTTClient, istypedinstance
PORT = 1883
# MQTT callback functions
def on_connect(self: mqtt.Client, userdata, flags, rc):
"""
MQTT callback function for handling the connect event.
Args:
userdata: User-specific data passed to the callback.
flags: Response flags from the broker.
rc: The connection result code.
Examples:
>>> obj = mqtt.Client()
>>> usr = Namespace(topic=["my_topic"])
>>> on_connect(mqtt.Client(), usr, None, 0)
Connected with result code 0
"""
print("Connected with result code " + str(rc))
self.subscribe([(topic, 0) for topic in userdata.topic])
def on_message(self, userdata, msg):
"""
MQTT callback function for handling incoming messages.
Args:
userdata: User-specific data passed to the callback.
msg: The message received from the broker.
Examples:
>>> obj = mqtt.Client()
>>> usr = Namespace(topic=["my_topic"])
>>> msg = mqtt.MQTTMessage(); msg.payload = b'Hello'
>>> on_message(obj, usr, msg)
Received message at 1970-01-01 ...: Hello
"""
if isinstance(userdata, Namespace) and "receiver" in userdata:
item = verify_and_decrypt_data(
json.loads(msg.payload.decode()), userdata.receiver
)
item = json.dumps(item)
else:
item = msg.payload.decode()
t = dt.datetime.fromtimestamp(msg.timestamp).replace(microsecond=0)
print(f"Received message at {t}: {item}")
def query_file(config: FileClient, **kwargs):
"""
Query a JSON file based on the command-line arguments and print the
closest past item.
Args:
config (dict): The configuration dictionary.
args (Namespace): Parsed command-line arguments.
Examples:
>>> config = {"output": "tests/sample.json"}
>>> query_file(config)
{'time': datetime.datetime(2023, 1, 1, 0, 0), 'anomaly': 0, ...}
"""
# Load the JSON file as a list of dictionaries
with open(config.get("output", ""), encoding="utf-8") as f:
data: list[dict] = [json.loads(line) for line in f]
# Convert the time strings to datetime objects
for item in data:
if "receiver" in kwargs and not item["time"].isascii():
item = verify_and_decrypt_data(item, kwargs["receiver"])
item["time"] = dt.datetime.strptime(item["time"], "%Y-%m-%d %H:%M:%S")
# Sort the data by time in descending order
data.sort(key=lambda x: x["time"], reverse=True)
# Find the closest past item
for item in data:
if item["time"] <= dt.datetime.strptime(
dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
"%Y-%m-%d %H:%M:%S",
):
closest_item = item
break
# Print the closest past item
print(closest_item)
def query_mqtt(config: MQTTClient):
"""
Create an MQTT client instance and connect to the MQTT broker.
Args:
config (dict): The configuration dictionary.
args (Namespace): Parsed command-line arguments.
Returns:
mqtt.Client: MQTT client instance.
Examples:
>>> config = {"host": "mqtt.eclipseprojects.io"}
>>> args = Namespace()
>>> client = query_mqtt(config)
>>> isinstance(client, mqtt.Client)
True
"""
# Create MQTT client instance
client = mqtt.Client()
# Assign callback functions
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker
client.connect(config["host"], PORT, 60)
return client
if __name__ == "__main__":
config = get_params()
if "key_path" in config["setup"]:
_, receiver = init_rsa_security(config["setup"]["key_path"])
if istypedinstance(config["client"], FileClient):
query_file(config["client"])
elif istypedinstance(config["client"], MQTTClient):
client = query_mqtt(config["client"])
client.loop_forever()