Skip to content

Commit

Permalink
Fixing Paho.mqtt v2 support, adding Zabbix controller
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed Mar 8, 2024
1 parent bdebd7c commit b6c138b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 11 deletions.
6 changes: 5 additions & 1 deletion _C002_DomoMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ def controller_init(self,enablecontroller=None):
except:
self.useping = True
try:
self.mqttclient = DMQTTClient()
mqttcompatibility = mqtt.CallbackAPIVersion.VERSION1
except:
mqttcompatibility = None
try:
self.mqttclient = DMQTTClient(mqttcompatibility)
self.mqttclient.subscribechannel = self.outchannel
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
Expand Down
14 changes: 9 additions & 5 deletions _C014_GenMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def controller_init(self,enablecontroller=None):
pass
except:
self.globalretain = False
self.mqttclient = GMQTTClient()
try:
mqttcompatibility = mqtt.CallbackAPIVersion.VERSION1
except:
mqttcompatibility = None
self.mqttclient = GMQTTClient(mqttcompatibility)
self.mqttclient.subscribechannel = self.outch
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
Expand Down Expand Up @@ -174,7 +178,7 @@ def connect(self):
except:
self.keepalive = 60
try:
self.mqttclient.will_set(self.lwt_t, self.lwtdisconnmsg, qos=0, retain=True)
self.mqttclient.will_set(self.lwt_t, payload=self.lwtdisconnmsg, qos=0, retain=True)
self.mqttclient.connect(self.controllerip,int(self.controllerport),keepalive=self.keepalive) # connect_async() is faster but maybe not the best for user/pass method
self.mqttclient.loop_start()
except Exception as e:
Expand All @@ -184,7 +188,7 @@ def connect(self):

def disconnect(self):
try:
(mres,mid) = self.mqttclient.publish(self.lwt_t,self.lwtdisconnmsg, qos=0, retain=True)
(mres,mid) = self.mqttclient.publish(self.lwt_t,self.lwtdisconnmsg)
except Exception as e:
print(e)
try:
Expand Down Expand Up @@ -223,7 +227,7 @@ def isconnected(self,ForceCheck=True):
commands.rulesProcessing("GenMQTT#Disconnected",rpieGlobals.RULE_SYSTEM)
else:
try:
(mres,mid) = self.mqttclient.publish(self.lwt_t,self.lwtconnmsg, qos=0, retain=True)
(mres,mid) = self.mqttclient.publish(self.lwt_t,self.lwtconnmsg)
except:
pass
commands.rulesProcessing("GenMQTT#Connected",rpieGlobals.RULE_SYSTEM)
Expand Down Expand Up @@ -582,4 +586,4 @@ def on_disconnect(self, client, userdata, rc):
def on_message(self, mqttc, obj, msg):
if self.controllercb is not None:
self.controllercb(msg)


81 changes: 81 additions & 0 deletions _C017_Zabbix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
#############################################################################
##################### Zabbix controller for RPIEasy #########################
#############################################################################
#
# Zabbix controller
#
# Copyright (C) 2018-2024 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import controller
import misc
import rpieGlobals
import time
import webserver
import Settings
import socket
import struct
import json
import threading
import Settings

class Controller(controller.ControllerProto):
CONTROLLER_ID = 17
CONTROLLER_NAME = "Zabbix"

def __init__(self,controllerindex):
controller.ControllerProto.__init__(self,controllerindex)
self.usesID = False
self.onmsgcallbacksupported = False # use direct set_value() instead of generic callback to make sure that values setted anyway
self.controllerport = 10051

def controller_init(self,enablecontroller=None):
if enablecontroller != None:
self.enabled = enablecontroller
self.initialized = True
return True

def webform_load(self):
webserver.addFormNote("Value names will be transmitted as 'taskname-valuename' to Zabbix server!")

def sender(self,data):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect( (self.controllerip,int(self.controllerport)) )
s.send(data)
#resp_header = s.recv(5)
#if 'ZBXD' in str(resp_header):
# resp_header = s.recv(8)
# resp_header = resp_header[:4]
# response_len = struct.unpack('i', resp_header)[0]
# response_raw = s.recv(response_len)
# print(response_raw)
s.close()
except Exception as e:
print(e)

def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1): # called by plugin
if tasknum is None:
return False
if tasknum!=-1 and self.enabled:
if tasknum<len(Settings.Tasks):
if Settings.Tasks[tasknum] != False:
reply = {}
reply['request'] = 'sender data'
reply['data'] = []
hostname = Settings.Settings["Name"]
for u in range(Settings.Tasks[tasknum].valuecount):
treply = {}
treply["host"] = hostname
treply["key"] = str(Settings.Tasks[tasknum].taskname).strip() + "-" + str(Settings.Tasks[tasknum].valuenames[u]).strip()
treply["value"] = str(Settings.Tasks[tasknum].uservar[u])
reply["data"].append(treply)
HEADER = bytes('ZBXD','utf-8') + b'\x01'
dstr = bytes(json.dumps(reply),'utf-8')
dlen = len(dstr)
dheader = struct.pack('i', dlen) + b'\0\0\0\0'
datablob = HEADER + dheader + dstr
t = threading.Thread(target=self.sender, args=(datablob,))
t.daemon = True
t.start()
return True
9 changes: 7 additions & 2 deletions _C024_ADMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ def controller_init(self,enablecontroller=None):
self.lwt_t, state = commands.parseruleline(self.lwt_topic)
except:
self.lwt_topic = ""
self.mqttclient = GMQTTClient()
try:
mqttcompatibility = mqtt.CallbackAPIVersion.VERSION1
except:
mqttcompatibility = None
self.mqttclient = GMQTTClient(mqttcompatibility)
self.mqttclient.subscribechannel = self.outch
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
Expand Down Expand Up @@ -219,7 +223,8 @@ def isconnected(self,ForceCheck=True):

def webform_load(self): # create html page for settings
webserver.addFormTextBox("Discovery topic","dtopic",self.discoverytopic,255)
webserver.addHtml("</td></tr><tr><td></td><td><a href='/adconfig?cid="+ str(self.controllerindex) +"'>Open device configuration page</a></td></tr>")
if self.controllerindex >= 0:
webserver.addHtml("</td></tr><tr><td></td><td><a href='/adconfig?cid="+ str(self.controllerindex) +"'>Open device configuration page</a></td></tr>")
webserver.addFormNumericBox("Keepalive time","keepalive",self.keepalive,2,600)
webserver.addUnit("s")
try:
Expand Down
7 changes: 5 additions & 2 deletions _C025_ThingsboardM.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ def controller_init(self,enablecontroller=None):
self.connectinprogress = 0
self.inch = str(self.inchannel)
self.outch = str(self.outchannel)

self.mqttclient = GMQTTClient()
try:
mqttcompatibility = mqtt.CallbackAPIVersion.VERSION1
except:
mqttcompatibility = None
self.mqttclient = GMQTTClient(mqttcompatibility)
self.mqttclient.subscribechannel = self.outch
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
Expand Down
2 changes: 1 addition & 1 deletion rpieGlobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2018-2023 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
PROGNAME = "RPIEasy"
BUILD = 24025
BUILD = 24068
PROGVER = str(BUILD)[:1]+"."+str(BUILD)[1:2]+"."+str(BUILD)[2:]

gpMenu = []
Expand Down

0 comments on commit b6c138b

Please sign in to comment.