File: usbrelayd

package info (click to toggle)
usbrelay 1.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: ansic: 618; python: 100; makefile: 58; xml: 25; sh: 5
file content (87 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/env python3

# MQTT daemon for usbrelay
# Publishes a state topic for each connected usbrelay and subscribes to command topics  for each relay
# Topics are stat/SERIAL/RELAY or cmnd/SERIAL/RELAY
# eg stat/QWERT/2 or cmnd/QWERT/2

import paho.mqtt.client as mqtt
import usbrelay_py
import time
import re
import sys
import configparser

def publish_states(client):
    boards = usbrelay_py.board_details()
#    print("Boards: ",boards)
    for board in boards:
#        print("Board: ",board)
        relay = 1
        # determine the state of each relay and publish to the MQTT broker
        while(relay < board[1]+1):
            if ( board[2] & ( 1 << (relay -1) )):
                relay_state = "ON"
            else:
                relay_state = "OFF"
            
            
            topic = "{0}/{1}/{2}"
            topic_str = topic.format("stat",board[0],relay)
            print("State: ", topic_str, relay_state,flush=True)
            client.publish(topic_str, relay_state, qos=0, retain=True)
            topic_str = topic.format("cmnd",board[0],relay)
            print("Subscribed: ", topic_str,flush=True)
            client.subscribe(topic_str)
            relay += 1
        

def on_message(client, userdata, message):
    msg_state = str(message.payload.decode("utf-8"))
    print("received message: " ,message.topic, msg_state,flush=True)
    # any message other than ON is OFF
    if( msg_state == "ON" ):
        relay_cmd = 1
    else:
        relay_cmd = 0
    
    content = re.split("/",message.topic)
    result = usbrelay_py.board_control(content[1],int(content[2]),relay_cmd)
#    print("COntent: ", content , result)
    pub_str = "stat/{0}/{1}"
    client.publish(pub_str.format(content[1],content[2]), msg_state)

# read the server name or IP address from the command lin


config = configparser.ConfigParser()
config.read('/etc/usbrelayd.conf')
mqttBroker = config['MQTT']['BROKER']
clientName = config['MQTT']['CLIENTNAME']

print("MQTT Broker: ", mqttBroker)
print("MQTT Client: ", clientName)


#
# Count connected usbrelay modules, exit if none 
count = usbrelay_py.board_count()

if(count < 1):
    print("No usbrelay modules connected",flush=True)
    exit()
else:
    print("Modules Connected: ",count,flush=True)

# connect to the mqtt broker


client = mqtt.Client(clientName)
client.connect(mqttBroker) 
publish_states(client)
client.on_message=on_message 
while(True):
    client.loop_start()
    time.sleep(60)
    publish_states(client)