forked from Domochip/sms2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms2mqtt.py
276 lines (236 loc) · 9.78 KB
/
sms2mqtt.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import logging
import time
from datetime import datetime
import os
import signal
import paho.mqtt.client as mqtt
import gammu
import json
# callback when the broker responds to our connection request.
def on_mqtt_connect(client, userdata, flags, rc):
logging.info("Connected to MQTT host")
client.publish(f"{mqttprefix}/connected", "1", 0, True)
client.subscribe(f"{mqttprefix}/send")
# callback when the client disconnects from the broker.
def on_mqtt_disconnect(client, userdata, rc):
logging.info("Disconnected from MQTT host")
logging.info("Exit")
exit()
# callback when a message has been received on a topic that the client subscribes to.
def on_mqtt_message(client, userdata, msg):
try:
logging.info(f'MQTT received : {msg.payload}')
payload = msg.payload.decode("utf-8")
data = json.loads(payload, strict=False)
except Exception as e:
feedback = {"result":f'error : failed to decode JSON ({e})', "payload":payload}
client.publish(f"{mqttprefix}/sent", json.dumps(feedback, ensure_ascii=False))
logging.error(f'failed to decode JSON ({e}), payload: {msg.payload}')
return
for key, value in data.items():
if key.lower() == 'number':
number=value
if key.lower() == 'text':
text=value
if 'number' not in locals() or not isinstance(number, str) or not number:
feedback = {"result":'error : no number to send to', "payload":payload}
client.publish(f"{mqttprefix}/sent", json.dumps(feedback, ensure_ascii=False))
logging.error('no number to send to')
return False
if 'text' not in locals() or not isinstance(text, str):
feedback = {"result":'error : no text body to send', "payload":payload}
client.publish(f"{mqttprefix}/sent", json.dumps(feedback, ensure_ascii=False))
logging.error('no text body to send')
return False
for num in (number.split(";")):
num = num.replace(' ','')
if num == '':
continue
smsinfo = {
'Class': -1,
'Entries': [{
'ID': 'ConcatenatedAutoTextLong',
'Buffer' : text
}]
}
try:
logging.info(f'Sending SMS To {num} containing {text}')
encoded = gammu.EncodeSMS(smsinfo)
for message in encoded:
message['SMSC'] = {'Location': 1}
message['Number'] = num
gammusm.SendSMS(message)
feedback = {"result":"success", "datetime":time.strftime("%Y-%m-%d %H:%M:%S"), "number":num, "text":text}
client.publish(f"{mqttprefix}/sent", json.dumps(feedback, ensure_ascii=False))
logging.info(f'SMS sent to {num}')
except Exception as e:
feedback = {"result":f'error : {e}', "datetime":time.strftime("%Y-%m-%d %H:%M:%S"), "number":num, "text":text}
client.publish(f"{mqttprefix}/sent", json.dumps(feedback, ensure_ascii=False))
logging.error(feedback['result'])
# function used to parse received sms
def loop_sms_receive():
# process Received SMS
allsms = []
start=True
while True:
try:
if start:
sms = gammusm.GetNextSMS(Folder=0, Start=True)
start=False
else:
sms = gammusm.GetNextSMS(Folder=0, Location=sms[0]['Location'])
allsms.append(sms)
except gammu.ERR_EMPTY as e:
break
if not len(allsms):
return
alllinkedsms=gammu.LinkSMS(allsms)
for sms in alllinkedsms:
if sms[0]['UDH']['Type'] == 'NoUDH':
message = {"datetime":str(sms[0]['DateTime']), "number":sms[0]['Number'], "text":sms[0]['Text']}
payload = json.dumps(message, ensure_ascii=False)
client.publish(f"{mqttprefix}/received", payload)
logging.info(payload)
try:
gammusm.DeleteSMS(Folder=0, Location=sms[0]['Location'])
except Exception as e:
logging.error(f'ERROR: Unable to delete SMS: {e}')
elif sms[0]['UDH']['AllParts'] != -1:
if len(sms) == sms[0]['UDH']['AllParts']:
decodedsms = gammu.DecodeSMS(sms)
message = {"datetime":str(sms[0]['DateTime']), "number":sms[0]['Number'], "text":decodedsms['Entries'][0]['Buffer']}
payload = json.dumps(message, ensure_ascii=False)
client.publish(f"{mqttprefix}/received", payload)
logging.info(payload)
for part in sms:
gammusm.DeleteSMS(Folder=0, Location=part['Location'])
else:
logging.info(f"Incomplete Multipart SMS ({len(sms)}/{sms[0]['UDH']['AllParts']}): waiting for parts")
else:
logging.info('***************** Unsupported SMS type *****************')
logging.info('===============sms=================')
logging.info(sms)
logging.info('===============decodedsms=================')
decodedsms = gammu.DecodeSMS(sms)
logging.info(decodedsms)
logging.info('================================')
gammusm.DeleteSMS(Folder=0, Location=sms[0]['Location'])
# function used to obtain signal quality
def get_signal_info():
global old_signal_info
try:
signal_info = gammusm.GetSignalQuality()
if signal_info != old_signal_info:
signal_payload = json.dumps(signal_info)
client.publish(f"{mqttprefix}/signal", signal_payload)
old_signal_info = signal_info
except Exception as e:
logging.error(f'ERROR: Unable to check signal quality: {e}')
old_signal_info = ""
# function used to obtain battery charge
def get_battery_charge():
global old_battery_charge
try:
battery_charge = gammusm.GetBatteryCharge()
if battery_charge != old_battery_charge:
battery_payload = json.dumps(battery_charge)
client.publish(f"{mqttprefix}/battery", battery_payload)
old_battery_charge = battery_charge
except Exception as e:
logging.error(f'ERROR: Unable to check battery charge: {e}')
old_battery_charge = ""
# function used to obtain network info
def get_network_info():
global old_network_info
try:
network_info = gammusm.GetNetworkInfo()
if network_info != old_network_info:
network_payload = json.dumps(network_info)
client.publish(f"{mqttprefix}/network", network_payload)
old_network_info = network_info
except Exception as e:
logging.error(f'ERROR: Unable to check network info: {e}')
old_network_info = ""
# function used to obtain datetime
def get_datetime():
global old_time
try:
now = gammusm.GetDateTime().timestamp()
if (now - old_time) > 60:
client.publish(f"{mqttprefix}/datetime", now)
old_time = now
except Exception as e:
logging.error(f'ERROR: Unable to check datetime: {e}')
old_time = time.time()
def shutdown(signum=None, frame=None):
client.publish(f"{mqttprefix}/connected", "0", 0, True)
client.disconnect()
if __name__ == "__main__":
logging.basicConfig( format="%(asctime)s: %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S")
versionnumber='1.4.6'
logging.info(f'===== sms2mqtt v{versionnumber} =====')
# devmode is used to start container but not the code itself, then you can connect interactively and run this script by yourself
# docker exec -it sms2mqtt /bin/sh
if os.getenv("DEVMODE",0) == "1":
logging.info('DEVMODE mode : press Enter to continue')
try:
input()
logging.info('')
except EOFError as e:
# EOFError means we're not in interactive so loop forever
while 1:
time.sleep(3600)
device = os.getenv("DEVICE","/dev/mobile")
pincode = os.getenv("PIN")
gammuoption = os.getenv("GAMMUOPTION","")
moreinfo = bool(os.getenv("MOREINFO"))
heartbeat = bool(os.getenv("HEARTBEAT"))
mqttprefix = os.getenv("PREFIX","sms2mqtt")
mqtthost = os.getenv("HOST","localhost")
mqttport = int(os.getenv("PORT",1883))
mqttclientid = os.getenv("CLIENTID","sms2mqtt")
mqttuser = os.getenv("USER")
mqttpassword = os.getenv("PASSWORD")
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
gammurcfile = open("/app/gammurc", 'w')
gammurcfile.write(f"""
[gammu]
device = {device}
connection = at
{gammuoption}
""")
gammurcfile.close()
gammusm = gammu.StateMachine()
gammusm.ReadConfig(Filename="/app/gammurc")
gammusm.Init()
if gammusm.GetSecurityStatus() == 'PIN':
gammusm.EnterSecurityCode('PIN',pincode)
versionTuple = gammu.Version()
logging.info(f'Gammu runtime: v{versionTuple[0]}')
logging.info(f'Python-gammu runtime: v{versionTuple[1]}')
logging.info(f'Manufacturer: {gammusm.GetManufacturer()}')
logging.info(f'IMEI: {gammusm.GetIMEI()}')
logging.info(f'SIMIMSI: {gammusm.GetSIMIMSI()}')
if heartbeat:
gammusm.SetDateTime(datetime.now())
logging.info('Gammu initialized')
# https://stackoverflow.com/a/77985329
# client = mqtt.Client(mqttclientid)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, mqttclientid)
client.username_pw_set(mqttuser, mqttpassword)
client.on_connect = on_mqtt_connect
client.on_disconnect = on_mqtt_disconnect
client.on_message = on_mqtt_message
client.will_set(f"{mqttprefix}/connected", "0", 0, True)
client.connect(mqtthost, mqttport)
while True:
time.sleep(1)
loop_sms_receive()
get_signal_info()
if moreinfo:
get_battery_charge()
get_network_info()
if heartbeat:
get_datetime()
client.loop()