forked from johanmeijer/grott
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrottmqtt.py
More file actions
113 lines (81 loc) · 2.92 KB
/
grottmqtt.py
File metadata and controls
113 lines (81 loc) · 2.92 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import time
import datetime
import logging
import signal
from datetime import datetime
import logging.handlers as Handlers
import paho
import paho.mqtt.client as mqtt
#--------------------------------------------------
sig_int = False
mqtt_client = None
mqtt_status = 0
#============================================================================
# The callback for when the client receives a CONNACK response from the server.
def mqtt_on_connect(client, userdata, flags, rc, properties):
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
global sig_int, mqtt_status
if not rc.is_failure:
logging.info( f"MQTT connection successfull" )
# client.subscribe( userdata + "/#" )
else:
logging.error( f"MQTT connection error: {rc}" )
# client.disconnect()
mqtt_status = rc
def mqtt_on_disconnect(client, userdata, flags, rc, properties):
global sig_int, mqtt_status
if rc > 0 and not sig_int and mqtt_status == 0:
logging.warning( f"MQTT disconnected with result code {rc}" )
else:
logging.info( f"MQTT normal disconnection, exiting" )
sig_int = True
def signal_sigint_handler( sig, frame ):
global sig_int, mqtt_client
logging.info( "SIGINT received, exiting" )
sig_int = True
if mqtt_client is not None:
mqtt_client.loop_stop()
mqtt_client.disconnect()
class publish:
def single( topic: str,
payload: str,
qos: int = 0,
retain: bool = False,
hostname: str = 'localhost',
port: int = 1883,
client_id: str = '',
keepalive: int = 60,
auth: dict | None = None ) -> None:
global sig_int, mqtt_client, mqtt_status
if mqtt_client == None:
# set defaults
logging.basicConfig( stream = sys.stdout, level = logging.INFO, format = '[%(levelname)s] %(message)s', datefmt = '%d.%m.%Y %I:%M:%S' )
# logging.getLogger().setLevel( "INFO" )
logging.info( '============ GROTT MQTT Client starting ' + datetime.now().strftime('%d.%m.%Y %H:%M:%S') + ' =======================' )
# signal.signal( signal.SIGINT, signal_sigint_handler )
# signal.signal( signal.SIGTERM, signal_sigint_handler )
mqtt_client = mqtt.Client( mqtt.CallbackAPIVersion.VERSION2,
client_id=client_id,
transport="tcp" )
mqtt_client.on_connect = mqtt_on_connect
mqtt_client.on_disconnect = mqtt_on_disconnect
mqtt_client.username_pw_set( auth[ 'username' ], auth[ 'password' ] )
if mqtt_client != None:
if not mqtt_client.is_connected():
logging.info( 'MQTT (re)starting the connection' )
mqtt_client.loop_stop()
mqtt_client.connect( hostname, port, keepalive )
mqtt_client.loop_start()
mqtt_client.publish( topic=topic, payload=payload, qos=qos, retain=retain )
# logging.info( 'MQTT message published' )
if sig_int:
mqtt_client.loop_stop()
mqtt_client.disconnect()
if mqtt_status == 0:
exit( 0 )
else:
exit( mqtt_status.pack()[ 0 ] )