First commit with working implementation
This commit is contained in:
commit
1feabffe2b
5 changed files with 308 additions and 0 deletions
83
send_sms.py
Executable file
83
send_sms.py
Executable file
|
|
@ -0,0 +1,83 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pydbus import SystemBus
|
||||
from gi.repository import GLib
|
||||
|
||||
def assemble_sms():
|
||||
str_list = []
|
||||
str_list.append("Observium {}\n".format(os.environ.get('OBSERVIUM_TITLE')))
|
||||
str_list.append("{} [{}]\n".format(os.environ.get('OBSERVIUM_ENTITY_NAME'), os.environ.get('OBSERVIUM_ENTITY_DESCRIPTION')))
|
||||
str_list.append("{}".format(os.environ.get('OBSERVIUM_DURATION')))
|
||||
return ''.join(str_list)
|
||||
|
||||
# Setup logging
|
||||
logfile = "/opt/sms/sms.log"
|
||||
FORMAT = '%(asctime)-15s %(message)s'
|
||||
logging.basicConfig(format=FORMAT,filename=logfile,level=10)
|
||||
logger = logging.getLogger('observiumsms')
|
||||
|
||||
# Path to file with SMS numbers
|
||||
emergency_file = "/opt/sms/emergency_numbers.txt"
|
||||
|
||||
# Assemble SMS
|
||||
smsstring = assemble_sms()
|
||||
logger.info("Sending SMS: %s", smsstring)
|
||||
|
||||
# Contact ModemManager, unlock SIM and send SMS to emergency numbers
|
||||
with SystemBus() as bus:
|
||||
mm = bus.get('.ModemManager1')
|
||||
modems = mm.GetManagedObjects()
|
||||
modem_path = list(modems.keys())[0]
|
||||
|
||||
logger.info("Going with %s", modem_path)
|
||||
modem = bus.get(".ModemManager1", modem_path)
|
||||
|
||||
sim_path = modem.Sim
|
||||
if not sim_path:
|
||||
logger.critical("No SIM")
|
||||
quit()
|
||||
|
||||
# unlock SIM, if not alreadY
|
||||
props = modem.GetStatus()
|
||||
print(props)
|
||||
if props and props['state'] == 2:
|
||||
logger.info("Trying to unlock %s", sim_path)
|
||||
sim = bus.get('.ModemManager1', sim_path)
|
||||
sim.SendPin("1234", timeout=10000)
|
||||
props = modem.GetStatus()
|
||||
if props and props['state'] == 2:
|
||||
logger.critical("Wrong PIN!")
|
||||
quit()
|
||||
|
||||
# enable the modem
|
||||
modem.Enable(True)
|
||||
|
||||
# test sending SMS
|
||||
modem_msg = modem['org.freedesktop.ModemManager1.Modem.Messaging']
|
||||
smses = modem_msg.List()
|
||||
|
||||
print(modem.GetStatus()['signal-quality'])
|
||||
|
||||
raise Exception('TODO phone numbers')
|
||||
|
||||
for line in fnums:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
logger.info("Creating new SMS for %s", line)
|
||||
smstext = GLib.Variant('s', smsstring)
|
||||
smsnumber = GLib.Variant('s', line)
|
||||
|
||||
dictsms = { 'text': smstext, 'number': smsnumber }
|
||||
|
||||
msgpath = modem_msg.Create(dictsms)
|
||||
logger.info("Created new SMS %s", msgpath)
|
||||
|
||||
logger.info("Sending SMS %s", msgpath)
|
||||
sms = bus.get(".ModemManager1", msgpath)
|
||||
sms.Send(timeout=30000)
|
||||
|
||||
logger.info("SMS %s state %s", msgpath, sms.State)
|
||||
Loading…
Add table
Add a link
Reference in a new issue