Received SMS with python

Colin Helliwell colin.helliwell at ln-systems.com
Thu Sep 21 10:54:47 UTC 2017


> On 19 September 2017 at 17:39 Aleksander Morgado <aleksander at aleksander.es> wrote:
> 
...
> 
> Another (better?) option would be to listen to updates in the
> "Messages" property itself. E.g. just
> msging.connect('notify::messages',...) and setup a callback that will
> get called any time the Messages property is updated (and you can call
> list_sync() inside the callback). If you get a clean example with that
> logic we could add it to the examples/ directory in git ;)
> 

A first attempt attached - any critique welcome! (especially w.r.t. cleanup or race hazards etc)

I notice that I get the callback if the modem is disabled, but not when it is re-enabled? Perhaps I'm missing something there, or perhaps it doesn't matter?
Also, I'm seeing a message come and go - somewhere around deleting a message and/or disabling/reenabling the modem. At first I thought it was being deleted, but it does seem to reappear at a later point. I've not yet pinned down a repeatable sequence (or even 100% convinced myself I'm not imagining it).

Next step would be to copy the messages and then delete from the modem (to avoid filling the me storage) - since the actual processing of them may take some time. I'd prefer to copy the whole Sms object rather than just, say, the text - what's a safe way to do that, across all eventualites?
-------------- next part --------------
#!/usr/bin/env python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2014 Aleksander Morgado <aleksander at aleksander.es>
# Copyright (C) 2017 LN Systems Limited <enquiries at ln-systems.com>
#

import os, sys, gi, signal

gi.require_version('ModemManager', '1.0')
from gi.repository import GLib, GObject, Gio, ModemManager


"""
The ModemSms class is responsible for monitoring receipt of SMS messages
"""
class ModemSms:

    """
    Constructor
    """
    def __init__(self, device):
        # Flag for initial logs
        self.initializing = True
        # Setup DBus monitoring
        self.connection = Gio.bus_get_sync (Gio.BusType.SYSTEM, None)
        self.manager = ModemManager.Manager.new_sync (self.connection,
                                                      Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START,
                                                      None)

        self.device = device
        self.obj = None

        # IDs for added/removed signals
        self.object_added_id = 0
        self.object_removed_id = 0

        # ID for notify signal
        self.msging_notify_id = 0

        # Follow availability of the ModemManager process
        self.available = False
        self.manager.connect('notify::name-owner', self.on_name_owner)
        self.on_name_owner(self.manager, None)

        # Finish initialization
        self.initializing = False


    """
    ModemManager is now available
    """
    def set_available(self):
        if self.available == False or self.initializing == True:
            print 'ModemManager service is available in bus'
        self.object_added_id = self.manager.connect('object-added', self.on_object_added)
        self.object_removed_id = self.manager.connect('object-removed', self.on_object_removed)
        self.available = True

        # Initial scan
        if self.initializing == True:
            for obj in self.manager.get_objects():
                self.on_object_added(self.manager, obj)


    """
    ModemManager is now unavailable
    """
    def set_unavailable(self):
        if self.available == True or self.initializing == True:
            print 'ModemManager service not available in bus'
            self.obj = None

        if self.object_added_id:
            self.manager.disconnect(self.object_added_id)
            self.object_added_id = 0
        if self.object_removed_id:
            self.manager.disconnect(self.object_removed_id)
            self.object_removed_id = 0

        if self.msging_notify_id:
            self.msging.disconnect(self.msging_notify_id)
            self.msging_notify_id = 0

        self.available = False


    """
    Name owner updates
    """
    def on_name_owner(self, manager, prop):
        if self.manager.get_name_owner():
            self.set_available()
        else:
            self.set_unavailable()


    """
    Object added
    """
    def on_object_added(self, manager, obj):
        if self.obj is None:
            modem = obj.get_modem()
            if modem.get_state() == ModemManager.ModemState.FAILED:
                print '%s ignoring failed modem' % modem_index(obj.get_object_path())
                pass
            else:
                if modem.get_device() == self.device:
                    print 'Found our modem'
                    self.obj = obj
                    self.msging = obj.get_modem_messaging()
                    self.msging_notify_id = self.msging.connect('notify::messages', self.on_messaging_notify)
                    self.list_msgs (self.msging)
                else:
                    #print 'Not our modem'
                    pass


    """
    Object removed
    """
    def on_object_removed(self, manager, obj):
        print 'modem unmanaged by ModemManager: %s' % obj.get_object_path()

        self.msging.disconnect(self.msging_notify_id)
        self.msging_notify_id = 0

        self.obj = None
        self.msging = None


    """
    Messaging callback
    """
    def on_messaging_notify(self, manager, obj):
        if self.obj:
            msgs = self.obj.get_modem_messaging()
            self.list_msgs(msgs)


    def list_msgs(self, msging):
        if msging:
            rcvd =  msging.list_sync()
            for m in rcvd:
                print m.get_number(), m.get_timestamp(), '"%s"' % m.get_text()


def signal_handler(data):
    main_loop.quit()


if __name__ == "__main__":

    mm = ModemSms('MyTty', None)

    # Main loop
    main_loop = GLib.MainLoop()
    GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, None)
    GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, None)

    try:
        main_loop.run()
    except KeyboardInterrupt:
        pass




More information about the ModemManager-devel mailing list