-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_template.py
118 lines (99 loc) · 5.13 KB
/
basic_template.py
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
113
114
115
116
117
118
# Basic example MQTT APP template for use with the HBNet Data Gateway
# More about the API used via MQTT can be found at https://github.com/kf7eel/hbnet_external_services/wiki
# If you are new to MQTT, I highle recommend that you read https://www.hivemq.com/mqtt-essentials/ to understand the principals of MQTT.
# Hope you can find this useful. - KF7EEL
# Import needed modules
import paho.mqtt.client as mqtt
import threading
import json
import time
# Shortcut of APP. This is what users will have to type into radio, keep to about 5 characters.
# Shortcut must be unique on each server, please check to make sure that shortcut is not already in use.
app_shortcut = 'EXAMPLE'
# URL where users can go to find out about this APP
app_url = 'http://example.org'
# Brief description about APP
app_description = 'This is a cool radio project'
# Contact email, so someone can contact you if there is a problem
app_contact = '[email protected]'
# MQTT server details. Only data gateways connected to the same MQTT server will be able to use this wonderful script.
mqtt_server = 'mqtt.hbnet.xyz'
mqtt_port = 1883
mqtt_user = ''
mqtt_password = ''
# This is where we process incoming SMS messages. You can specify a response based on what the user sent, or run some code and then respond.
# It is a good idea to write a function to execute code, if you are going to be doing something with the received SMS.
def process_message(payload):
# Turn JSON payload into Python dictionary
dict_payload = json.loads(payload)
# Create variables to shorten things...
dmr_id = dict_payload['dmr_id']
message = dict_payload['message']
network = dict_payload['network']
# Split the received message into a list, for example, "This is a test" becomes ['This', 'is', 'a', 'test']. This makes it easy to create commands with arguments.
msg_split = message.split(' ')
# Print for debugging, so you can see what is happening.
print(msg_split)
# Add condition to respond to message with no characters.
if '' == msg_split[0]:
print('Received blank SMS, responding.')
mqtt_reply(network, dmr_id, 'No input.')
# If HI is in the message, respond with Hello there.
elif 'HI' == msg_split[0]:
print('Sending response.')
mqtt_reply(network, dmr_id, 'Hello there.')
# Define MQTT instance. See https://pypi.org/project/paho-mqtt for more in depth info about module.
def mqtt_main(broker_url = 'localhost', broker_port = 1883):
global mqtt_client
mqtt_client = mqtt.Client()
# On connect, send announcement
def on_connect(client, userdata, flags, rc):
mqtt_announce()
print('Connected')
def on_disconnect(client, userdata, flags, rc):
print('Disconnected')
# Process received msg here
def on_message(client, userdata, message):
topic_list = str(message.topic).split('/')
print("Message Recieved: " + message.payload.decode())
if message.topic == 'ANNOUNCE/MQTT':
print('--------------------------------\nServer message:\n')
print(message.payload.decode())
print('\n--------------------------------')
else:
# Pass message payload into our function to process message.
process_message(message.payload.decode())
def mqtt_connect():
# Pass MQTT server details to instrance
if mqtt_user != '':
print('MQTT User/Pass specified')
mqtt_client.username_pw_set(mqtt_user, mqtt_password)
mqtt_client.connect(broker_url, broker_port, keepalive = 30)
# Last will and testament, this tells everyone that we are going offline.
mqtt_client.will_set("ANNOUNCE", json.dumps({app_shortcut:"LOST_CONNECTION"}), 0, False)
# Telling the MQTT instance what to do when these events happen.
mqtt_client.on_message = on_message
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
# Pass MQTT server details to instrance
mqtt_connect()
# Subscribe to topic for incoming messages. See https://github.com/kf7eel/hbnet_external_services/wiki for topic structure.
mqtt_client.subscribe('APP/' + app_shortcut, qos=0)
# Announcements for service/network discovery. Function that runs on a 5 minute loop.
mqtt_client.loop_start()
# Function to reply to messages. Already formatted for your convenience.
def mqtt_reply(shortcut, dmr_id, message):
mqtt_client.publish(topic="MSG/" + shortcut + '/' + str(dmr_id), payload=json.dumps({str(dmr_id):message, 'network':app_shortcut, 'sms_type':'unit'}, indent = 4), qos=0, retain=False)
def mqtt_announce():
mqtt_client.publish(topic="ANNOUNCE", payload=json.dumps({'shortcut':app_shortcut, 'type': 'app', 'url':app_url, 'description':app_description, 'contact':app_contact}, indent = 4), qos=0, retain=False)
def mqtt_announce_loop(ann_time):
while True:
ann_time = ann_time * 60
time.sleep(ann_time)
mqtt_announce()
if __name__ == '__main__':
# Start ANNOUNCE thread
mqtt_thread = threading.Thread(target=mqtt_announce_loop, args=(5,))
mqtt_thread.daemon = True
mqtt_thread.start()
mqtt_main(mqtt_server, mqtt_port)