Skip to content

Commit

Permalink
Support connection to Mosquitto through unix socket (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
KraPete authored Sep 23, 2022
1 parent 6cfc21a commit f0bd204
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-mqtt-mbgate (1.3.0) stable; urgency=medium

* Support connection to Mosquitto through unix socket

-- Petr Krasnoshchekov <[email protected]> Tue, 20 Sep 2022 15:05:48 +0500

wb-mqtt-mbgate (1.2.0) stable; urgency=medium

* Add python3 and bullseye support
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Homepage: https://github.com/wirenboard/wb-mqtt-mbgate

Package: wb-mqtt-mbgate
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, python3 (>= 3.5), python3-paho-mqtt
Depends: ${shlibs:Depends}, ${misc:Depends}, python3 (>= 3.5), python3-paho-mqtt, python3-paho-socket
Breaks: wb-mqtt-homeui (<< 1.7)
Description: Wiren Board MQTT to Modbus gateway
wb-mqtt-mbgate is a service which acts as Modbus slave on
Expand Down
25 changes: 17 additions & 8 deletions utils/generate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import random
import sys
import time

import urllib.parse
import paho.mqtt.client as mqtt
import paho_socket


# Salt for address hashtable in case of match
ADDR_SALT = 7079
Expand All @@ -19,6 +21,10 @@
# Unit IDs reserved by Modbus
RESERVED_UNIT_IDS += list(range(247, 256)) + [0]

DEFAULT_MOSQUITTO_SOCKET_PATH = "unix:///var/run/mosquitto/mosquitto.sock"
DEFAULT_MOSQUITTO_SERVER = "localhost"
DEFAULT_MOSQUITTO_PORT = 1883

client = None
table = dict()
old_config = None
Expand Down Expand Up @@ -249,8 +255,8 @@ def main(args=None):
type=str, default="")
parser.add_argument("-f", "--force-create", help="force creating new config file",
action="store_true")
parser.add_argument("-s", "--server", help="MQTT server hostname", type=str, default="localhost")
parser.add_argument("-p", "--port", help="MQTT server port", type=int, default=1883)
parser.add_argument("-s", "--server", help="MQTT server hostname", type=str, default=DEFAULT_MOSQUITTO_SOCKET_PATH)
parser.add_argument("-p", "--port", help="MQTT server port", type=int, default=DEFAULT_MOSQUITTO_PORT)

args = parser.parse_args()

Expand All @@ -273,13 +279,16 @@ def main(args=None):
config_file = open(args.config, "w")

client_id = str(time.time()) + str(random.randint(0, 100000))

client = mqtt.Client(client_id)

hostname = args.server
port = args.port

client.connect(args.server, args.port)

url = urllib.parse.urlparse(args.server)
if url.scheme == 'unix':
client = paho_socket.Client(client_id)
client.sock_connect(url.path)
else:
client = mqtt.Client(client_id)
client.connect(args.server, args.port)

client.on_message = mqtt_on_message

Expand Down

0 comments on commit f0bd204

Please sign in to comment.