-
Notifications
You must be signed in to change notification settings - Fork 32
Destinations: MQTT update #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tim-quix
wants to merge
1
commit into
main
Choose a base branch
from
destinations/mqtt-template-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,95 +1,28 @@ | ||
from quixstreams import Application, context | ||
import paho.mqtt.client as paho | ||
from paho import mqtt | ||
import json | ||
from mqtt import MQTTSink | ||
from quixstreams import Application | ||
import os | ||
|
||
# Load environment variables (useful when working locally) | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
# from dotenv import load_dotenv | ||
# load_dotenv() | ||
|
||
def mqtt_protocol_version(): | ||
if os.environ["mqtt_version"] == "3.1": | ||
print("Using MQTT version 3.1") | ||
return paho.MQTTv31 | ||
if os.environ["mqtt_version"] == "3.1.1": | ||
print("Using MQTT version 3.1.1") | ||
return paho.MQTTv311 | ||
if os.environ["mqtt_version"] == "5": | ||
print("Using MQTT version 5") | ||
return paho.MQTTv5 | ||
print("Defaulting to MQTT version 3.1.1") | ||
return paho.MQTTv311 | ||
app = Application(consumer_group="mqtt_consumer_group", auto_offset_reset="earliest") | ||
input_topic = app.topic(os.environ["input"], value_deserializer="double") | ||
|
||
def configure_authentication(mqtt_client): | ||
mqtt_username = os.getenv("mqtt_username", "") | ||
if mqtt_username != "": | ||
mqtt_password = os.getenv("mqtt_password", "") | ||
if mqtt_password == "": | ||
raise ValueError('mqtt_password must set when mqtt_username is set') | ||
print("Using username & password authentication") | ||
mqtt_client.username_pw_set(os.environ["mqtt_username"], os.environ["mqtt_password"]) | ||
return | ||
print("Using anonymous authentication") | ||
sink = MQTTSink( | ||
client_id=os.environ["MQTT_CLIENT_ID"], | ||
server=os.environ["MQTT_SERVER"], | ||
port=int(os.environ["MQTT_PORT"]), | ||
topic_root=os.environ["MQTT_TOPIC_ROOT"], | ||
username=os.environ["MQTT_USERNAME"], | ||
password=os.environ["MQTT_PASSWORD"], | ||
version=os.environ["MQTT_VERSION"], | ||
tls_enabled=os.environ["MQTT_USE_TLS"].lower() == "true" | ||
) | ||
|
||
mqtt_port = os.environ["mqtt_port"] | ||
# Validate the config | ||
if not mqtt_port.isnumeric(): | ||
raise ValueError('mqtt_port must be a numeric value') | ||
sdf = app.dataframe(topic=input_topic) | ||
sdf.sink(sink) | ||
|
||
client_id = os.getenv("Quix__Deployment__Id", "default") | ||
mqtt_client = paho.Client(callback_api_version=paho.CallbackAPIVersion.VERSION2, | ||
client_id = client_id, userdata = None, protocol = mqtt_protocol_version()) | ||
mqtt_client.tls_set(tls_version = mqtt.client.ssl.PROTOCOL_TLS) # we'll be using tls | ||
mqtt_client.reconnect_delay_set(5, 60) | ||
configure_authentication(mqtt_client) | ||
|
||
# Create a Quix platform-specific application instead | ||
app = Application(consumer_group="mqtt_consumer_group", auto_offset_reset='earliest') | ||
# initialize the topic, this will combine the topic name with the environment details to produce a valid topic identifier | ||
input_topic = app.topic(os.environ["input"]) | ||
|
||
# setting callbacks for different events to see if it works, print the message etc. | ||
def on_connect_cb(client: paho.Client, userdata: any, connect_flags: paho.ConnectFlags, | ||
reason_code: paho.ReasonCode, properties: paho.Properties): | ||
if reason_code == 0: | ||
print("CONNECTED!") # required for Quix to know this has connected | ||
else: | ||
print(f"ERROR! - ({reason_code.value}). {reason_code.getName()}") | ||
|
||
def on_disconnect_cb(client: paho.Client, userdata: any, disconnect_flags: paho.DisconnectFlags, | ||
reason_code: paho.ReasonCode, properties: paho.Properties): | ||
print(f"DISCONNECTED! Reason code ({reason_code.value}) {reason_code.getName()}!") | ||
|
||
mqtt_client.on_connect = on_connect_cb | ||
mqtt_client.on_disconnect = on_disconnect_cb | ||
|
||
mqtt_topic_root = os.environ["mqtt_topic_root"] | ||
|
||
# connect to MQTT Cloud on port 8883 (default for MQTT) | ||
mqtt_client.connect(os.environ["mqtt_server"], int(mqtt_port)) | ||
|
||
# Hook up to termination signal (for docker image) and CTRL-C | ||
print("Listening to streams. Press CTRL-C to exit.") | ||
|
||
sdf = app.dataframe(input_topic) | ||
|
||
def publish_to_mqtt(data, key, timestamp, headers): | ||
json_data = json.dumps(data) | ||
message_key_string = key.decode('utf-8') # Convert to string using utf-8 encoding | ||
# publish to MQTT | ||
mqtt_client.publish(mqtt_topic_root + "/" + message_key_string, payload = json_data, qos = 1) | ||
|
||
sdf = sdf.apply(publish_to_mqtt, metadata=True) | ||
|
||
|
||
# start the background process to handle MQTT messages | ||
mqtt_client.loop_start() | ||
|
||
print("Starting application") | ||
# run the data processing pipeline | ||
app.run(sdf) | ||
|
||
# stop handling MQTT messages | ||
mqtt_client.loop_stop() | ||
print("Exiting") | ||
if __name__ == '__main__': | ||
app.run() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Properly import MQTT Sink from Quix Streams once proper version is available