-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f991a0
commit a60558a
Showing
9 changed files
with
91 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
kubectl delete -f deployment.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,87 @@ | ||
import paho | ||
import paho.mqtt.client as mqtt | ||
import json | ||
from collections import defaultdict | ||
import threading | ||
import time | ||
import os | ||
|
||
from uuid import uuid4 | ||
|
||
print("Using paho-mqtt version", paho.__version__, flush=True) | ||
|
||
# Configuration | ||
MQTT_HOST = 'emqx-service' | ||
MQTT_PORT = 1883 | ||
MQTT_HOST = '192.168.1.55' #'emqx-service' | ||
MQTT_PORT = 31883 #1883 | ||
TOPIC = '/topic_root/subtopic' | ||
|
||
messages = defaultdict(int) | ||
|
||
def on_connect(client, userdata, flags, rc): | ||
"""Callback for when the client receives a CONNACK response from the server.""" | ||
if rc == 0: | ||
print(f"Connected successfully to {MQTT_HOST}:{MQTT_PORT}") | ||
client.subscribe(TOPIC) # Move subscription to here to ensure it's effective after a successful connect | ||
else: | ||
print(f"Failed to connect, return code {rc}") | ||
# Implement reconnection logic here if necessary | ||
|
||
def on_disconnect(client, userdata, rc): | ||
"""Callback for when the client disconnects from the broker.""" | ||
if rc != 0: | ||
print(f"Unexpected disconnection.") | ||
# Implement reconnection logic here | ||
|
||
def on_message(client, userdata, msg): | ||
"""Handle incoming MQTT messages.""" | ||
payload = json.loads(msg.payload) | ||
sender = payload['sender'] | ||
messages[sender] += int(payload['data'][0]) # Increment by the first character converted to int | ||
|
||
def display_stats(): | ||
"""Periodically display message stats.""" | ||
while True: | ||
for sender, sum_ in messages.items(): | ||
print(f"{sender} -> sum -> {sum_}") | ||
time.sleep(10) # Adjust as necessary | ||
|
||
|
||
class SimpleListener: | ||
def __init__(self): | ||
self.listener_id = str(uuid4())[:5] | ||
self.messages = defaultdict(int) | ||
self.client = mqtt.Client( | ||
client_id=self.listener_id, | ||
clean_session=True | ||
) | ||
# Assign the callbacks | ||
self.client.on_connect = self.on_connect | ||
self.client.on_disconnect = self.on_disconnect | ||
self.client.on_message = self.on_message | ||
return | ||
|
||
def on_connect(self, client, userdata, flags, rc): | ||
"""Callback for when the client receives a CONNACK response from the server.""" | ||
if rc == 0: | ||
print(f"Connected successfully to {MQTT_HOST}:{MQTT_PORT}") | ||
client.subscribe(TOPIC) # Move subscription to here to ensure it's effective after a successful connect | ||
else: | ||
print(f"Failed to connect, return code {rc}") | ||
# Implement reconnection logic here if necessary | ||
return | ||
|
||
def on_disconnect(self, client, userdata, rc): | ||
"""Callback for when the client disconnects from the broker.""" | ||
if rc != 0: | ||
print(f"Unexpected disconnection.") | ||
# Implement reconnection logic here | ||
return | ||
|
||
def on_message(self, client, userdata, msg): | ||
"""Handle incoming MQTT messages.""" | ||
payload = json.loads(msg.payload) | ||
sender = payload['sender'] | ||
self.messages[sender] += int(payload['data'][0]) # Increment by the first character converted to int | ||
return | ||
|
||
def display_stats(self, ): | ||
"""Periodically display message stats.""" | ||
while True: | ||
for sender, sum_ in self.messages.items(): | ||
print(f"{sender} -> sum -> {sum_}") | ||
time.sleep(10) # Adjust as necessary | ||
return | ||
|
||
def run(self): | ||
try: | ||
self.client.connect(MQTT_HOST, MQTT_PORT, 60) | ||
except Exception as e: | ||
print(f"Failed to connect to MQTT broker at {MQTT_HOST}:{MQTT_PORT}, error: {e}") | ||
exit(1) # Exiting if connection is not successful | ||
|
||
stats_thread = threading.Thread(target=self.display_stats) | ||
stats_thread.start() | ||
|
||
self.client.loop_forever() | ||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
for k, v in os.environ.items(): | ||
if k.startswith('MQTT') or k.startswith('EMQX'): | ||
print(f"{k}={v}") | ||
|
||
client = mqtt.Client() | ||
# Assign the callbacks | ||
client.on_connect = on_connect | ||
client.on_disconnect = on_disconnect | ||
client.on_message = on_message | ||
|
||
try: | ||
client.connect(MQTT_HOST, MQTT_PORT, 60) | ||
except Exception as e: | ||
print(f"Failed to connect to MQTT broker at {MQTT_HOST}:{MQTT_PORT}, error: {e}") | ||
exit(1) # Exiting if connection is not successful | ||
|
||
stats_thread = threading.Thread(target=display_stats) | ||
stats_thread.start() | ||
|
||
client.loop_forever() | ||
|
||
listener = SimpleListener() | ||
listener.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NAMESPACE=mqtt-test | ||
POD_NAME=$(kubectl get pods -n $NAMESPACE -o jsonpath="{.items[0].metadata.name}") | ||
kubectl logs -n $NAMESPACE $POD_NAME -f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: mqtt-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
kubectl get all -n mqtt-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
kubectl apply -f deployment.yaml |