Skip to content

Commit

Permalink
let the service check if device is already monitored
Browse files Browse the repository at this point in the history
- update languages
  • Loading branch information
Yann Büchau committed Nov 28, 2016
1 parent 6057e53 commit 323ab0d
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 112 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ The folder structure (```etc```,```lib```,```usr```,```var```) is based on the [
- Hendryk Plötz on [hackaday.io](https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor) for the device interaction
- Mike Kazantsev on his blog on [fraggod.net](http://blog.fraggod.net/2012/06/16/proper-ish-way-to-start-long-running-systemd-service-on-udev-event-device-hotplug.html) for the systemd integration
- Ascot on [StackOverflow.com](http://stackoverflow.com/a/26457317/5433146) for a workaround on ```signal.signal(signal, handler)``` when using a ```GLib.MainLoop```
- don_crissti on [StackOveflow.com](http://unix.stackexchange.com/a/203678) for
getting a list of dbus objects
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
co2monitor (0.0.10) unstable; urgency=medium

* better hotplug handling

-- Yann Büchau <[email protected]> Mon, 28 Nov 2016 11:24:14 +0100

co2monitor (0.0.9) unstable; urgency=medium

* fix multiple co2monitor-service invocation bug
Expand Down
2 changes: 1 addition & 1 deletion usr/lib/co2monitor/python/co2monitor/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def device(self):
def device(self, device):
try:
if not os.path.exists(device):
raise TypeError(_("Device file '{}' does not exist.").format(
raise OSError(_("Device file '{}' does not exist.").format(
device))
except: raise
# set the new device file
Expand Down
73 changes: 56 additions & 17 deletions usr/lib/co2monitor/python/co2monitor/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dbus
import dbus.service
import dbus.mainloop.glib
from xml.etree import ElementTree

import logging
import configparser
Expand Down Expand Up @@ -43,9 +44,9 @@ def __init__(self):

self.loop = GLib.MainLoop() # create mainloop

systembus = dbus.SystemBus() # the system bus
systembus.request_name(CO2MONITOR_BUSNAME) # request the bus name
bus_name = dbus.service.BusName(CO2MONITOR_BUSNAME, systembus) # create bus name
self.systembus = dbus.SystemBus() # the system bus
self.systembus.request_name(CO2MONITOR_BUSNAME) # request the bus name
bus_name = dbus.service.BusName(CO2MONITOR_BUSNAME, self.systembus) # create bus name
# register the object on the bus name
dbus.service.Object.__init__(self, bus_name, CO2MONITOR_OBJECTPATH)

Expand Down Expand Up @@ -89,19 +90,55 @@ def run(self):
self.loop.run()
self.logger.info(_("Service stopped"))

@dbus.service.method(CO2MONITOR_INTERFACE, in_signature='s', out_signature='')
# get a list of exported objects under a path (only one layer deep)
# thanks to don_crissti on http://unix.stackexchange.com/a/203678
def get_monitored_devices_objects(self):
res = []
# iface = dbus.Interface(self, 'org.freedesktop.DBus.Introspectable')
xml_string = self.Introspect(CO2MONITOR_OBJECTPATH,self.connection)
for child in ElementTree.fromstring(xml_string):
if child.tag == 'node':
new_path = '/'.join((CO2MONITOR_OBJECTPATH, child.attrib['name']))
res.append(new_path)
return res

@dbus.service.method(CO2MONITOR_INTERFACE,
in_signature='s', out_signature='b')
def start_device_logging(self, devicefile):
self.logger.info(_("received request to start logging on device {}").format(
devicefile))
thread = LogThread(devicefile = devicefile) # logger object
self.logger.info(_("starting logging thread for device {}").format(
devicefile))
# same logger as service
thread.set_logger(self.logger)
# same config as service
thread.set_config(self.config)
thread.daemon = True # let this thread be a daemon thread
thread.start()
# create the object path name on the bus name
objectpath = "/".join([CO2MONITOR_OBJECTPATH,
utils.devicefile2objectname(devicefile)])
# check if device is already monitored
monitored_devices = self.get_monitored_devices_objects()
if objectpath in monitored_devices:
self.logger.warning(" ".join([
_("There is already a logging thread for device {}."),
_("This is odd... I better do nothing.")
]).format(devicefile))
return False
else:
try:
thread = LogThread(devicefile = devicefile) # logger object
self.logger.info(_("starting logging thread for device {}").format(
devicefile))
# same logger as service
thread.set_logger(self.logger)
# same config as service
thread.set_config(self.config)
thread.daemon = True # let this thread be a daemon thread
thread.start()
except OSError:
self.logger.critical(_("Could not access the device file '{}'."
).format(devicefile))
return False
except:
self.logger.critical(_(
"Something went wrong with device file '{}'."
).format(devicefile))
return False
return True


@dbus.service.method(CO2MONITOR_INTERFACE, in_signature='', out_signature='s')
Expand Down Expand Up @@ -136,9 +173,9 @@ def __init__(self, devicefile):
dbus.mainloop.glib.threads_init()
GLib.threads_init()

systembus = dbus.SystemBus() # the system bus
systembus.request_name(CO2MONITOR_BUSNAME) # request the bus name
bus_name = dbus.service.BusName(CO2MONITOR_BUSNAME, systembus) # create bus name
self.systembus = dbus.SystemBus() # the system bus
self.systembus.request_name(CO2MONITOR_BUSNAME) # request the bus name
bus_name = dbus.service.BusName(CO2MONITOR_BUSNAME, self.systembus) # create bus name

self.devicefile = devicefile

Expand All @@ -147,7 +184,7 @@ def __init__(self, devicefile):

# register the object on the bus name
objectpath = "/".join([CO2MONITOR_OBJECTPATH,
os.path.basename(self.devicefile)])
utils.devicefile2objectname(self.devicefile)])
dbus.service.Object.__init__(self, bus_name, objectpath)
self.update_status(_("idle"))
threading.Thread.__init__(self)
Expand Down Expand Up @@ -198,6 +235,8 @@ def quit(self):
self.logger.debug(_("Shutting down logging thread of device {}").format(
self.devicefile))
self.remove_from_connection()
self.logger.debug(_("logging thread for device {} was shut down properly.").format(
self.devicefile))
sys.exit()

# logloop
Expand Down
6 changes: 4 additions & 2 deletions usr/lib/co2monitor/python/co2monitor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def splitpath(path):

# convert a device file path to a DBus object name last part
def devicefile2objectname(devicefile):
p = [x.replace("-","--") for x in splitpath(devicefile)]
return "-".join(p)
return "_".join(splitpath(os.path.realpath(devicefile)))


# # convert a DBus object name last part to a device file path
Expand All @@ -45,3 +44,6 @@ def devicefile2objectname(devicefile):
# c = True
# print("---------------")
# return os.path.join(*res)



117 changes: 71 additions & 46 deletions usr/share/co2monitor/lang/de/LC_MESSAGES/co2monitor.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-11-23 17:30+0100\n"
"PO-Revision-Date: 2016-11-23 17:36+0100\n"
"POT-Creation-Date: 2016-11-28 13:54+0100\n"
"PO-Revision-Date: 2016-11-28 14:04+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de\n"
Expand All @@ -18,6 +18,22 @@ msgstr ""
"X-Generator: Poedit 1.8.7.1\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: usr/lib/co2monitor/python/co2monitor/gui.py:76
msgid "Welcome to Co2monitor!"
msgstr "Willkommen bei Co2monitor!"

#: usr/lib/co2monitor/python/co2monitor/gui.py:84
msgid "Starting GLib main loop..."
msgstr "starte GLib Hautpschleife..."

#: usr/lib/co2monitor/python/co2monitor/gui.py:86
msgid "GLib main loop ended."
msgstr "GLib Hauptschleife beendet."

#: usr/lib/co2monitor/python/co2monitor/gui.py:90
msgid "Received quitting signal."
msgstr "Anhaltesignal erhalten."

#: usr/lib/co2monitor/python/co2monitor/device.py:31
msgid "Device file '{}' does not exist."
msgstr "Gerätedatei '{}' existiert nicht."
Expand Down Expand Up @@ -58,133 +74,138 @@ msgstr "Versuche Neuverbindung..."
msgid "Could not reconnect. Aborting."
msgstr "Konnte nicht wiederverbinden. Abbruch."

#: usr/lib/co2monitor/python/co2monitor/gui.py:76
msgid "Welcome to Co2monitor!"
msgstr "Willkommen bei Co2monitor!"

#: usr/lib/co2monitor/python/co2monitor/gui.py:84
msgid "Starting GLib main loop..."
msgstr "starte GLib Hautpschleife..."

#: usr/lib/co2monitor/python/co2monitor/gui.py:86
msgid "GLib main loop ended."
msgstr "GLib Hauptschleife beendet."

#: usr/lib/co2monitor/python/co2monitor/gui.py:90
msgid "Received quitting signal."
msgstr "Anhaltesignal erhalten."

#: usr/lib/co2monitor/python/co2monitor/service.py:82
#: usr/lib/co2monitor/python/co2monitor/service.py:84
msgid "received stop signal {},"
msgstr "Anhaltesignal {} erhalten,"

#: usr/lib/co2monitor/python/co2monitor/service.py:83
#: usr/lib/co2monitor/python/co2monitor/service.py:85
msgid "will terminate soon..."
msgstr "werde bald anhalten..."

#: usr/lib/co2monitor/python/co2monitor/service.py:87
#: usr/lib/co2monitor/python/co2monitor/service.py:89
msgid "Service running..."
msgstr "Service läuft..."

#: usr/lib/co2monitor/python/co2monitor/service.py:89
#: usr/lib/co2monitor/python/co2monitor/service.py:91
msgid "Service stopped"
msgstr "Service beendet"

#: usr/lib/co2monitor/python/co2monitor/service.py:93
#: usr/lib/co2monitor/python/co2monitor/service.py:108
msgid "received request to start logging on device {}"
msgstr "Anweisung erhalten, Daten von Gerät {} zu loggen"

#: usr/lib/co2monitor/python/co2monitor/service.py:96
#: usr/lib/co2monitor/python/co2monitor/service.py:117
msgid "There is already a logging thread for device {}."
msgstr "Es läuft bereits ein Datenaufzeichnungsthread für Gerät {}."

#: usr/lib/co2monitor/python/co2monitor/service.py:118
msgid "This is odd... I better do nothing."
msgstr "Das ist seltsam... Ich tue besser nichts."

#: usr/lib/co2monitor/python/co2monitor/service.py:124
msgid "starting logging thread for device {}"
msgstr "starte Datenaufzeichnungs-Thread auf Gerät {}"

#: usr/lib/co2monitor/python/co2monitor/service.py:109
#: usr/lib/co2monitor/python/co2monitor/service.py:133
msgid "Could not access the device file '{}'."
msgstr "Konnte Gerätedatei '{}' nicht erreichen."

#: usr/lib/co2monitor/python/co2monitor/service.py:138
msgid "Something went wrong with device file '{}'."
msgstr "Etwas hat mit Gerätedatei '{}' nicht funktioniert."

#: usr/lib/co2monitor/python/co2monitor/service.py:147
msgid "running"
msgstr "aktiv"

#: usr/lib/co2monitor/python/co2monitor/service.py:111
#: usr/lib/co2monitor/python/co2monitor/service.py:149
msgid "stopped"
msgstr "inaktiv"

#: usr/lib/co2monitor/python/co2monitor/service.py:116
#: usr/lib/co2monitor/python/co2monitor/service.py:154
msgid "stopping co2monitor..."
msgstr "stoppe co2monitor..."

#: usr/lib/co2monitor/python/co2monitor/service.py:118
#: usr/lib/co2monitor/python/co2monitor/service.py:156
msgid "stopped co2monitor"
msgstr "co2monitor angehalten"

#: usr/lib/co2monitor/python/co2monitor/service.py:151
#: usr/lib/co2monitor/python/co2monitor/service.py:189
msgid "idle"
msgstr "untätig"

#: usr/lib/co2monitor/python/co2monitor/service.py:188
#: usr/lib/co2monitor/python/co2monitor/service.py:226
msgid "Request to stop logging on device {} - will terminate soon..."
msgstr ""
"Anfrage, die Datenaufzeichnung auf Gerät {} zu beenden - werde bald "
"anhalten..."

#: usr/lib/co2monitor/python/co2monitor/service.py:197
#: usr/lib/co2monitor/python/co2monitor/service.py:235
msgid "Shutting down logging thread of device {}"
msgstr "Fahre Datenaufzeichnungs-Thread auf Gerät {} herunter"

#: usr/lib/co2monitor/python/co2monitor/service.py:204
#: usr/lib/co2monitor/python/co2monitor/service.py:238
msgid "logging thread for device {} was shut down properly."
msgstr ""
"Datenaufzeichnungsthread für Gerät {} wurde ordentlich heruntergefahren."

#: usr/lib/co2monitor/python/co2monitor/service.py:244
msgid "Starting data logging on device {}"
msgstr "Beginne Datenaufzeichnung auf Gerät {}"

#: usr/lib/co2monitor/python/co2monitor/service.py:208
#: usr/lib/co2monitor/python/co2monitor/service.py:248
msgid "creating data directory"
msgstr "erstelle Datenordner"

#: usr/lib/co2monitor/python/co2monitor/service.py:211
#: usr/lib/co2monitor/python/co2monitor/service.py:251
msgid "created data directory"
msgstr "Datenordner erstellt"

#: usr/lib/co2monitor/python/co2monitor/service.py:225
#: usr/lib/co2monitor/python/co2monitor/service.py:265
msgid "device warmup"
msgstr "Geräteaufwärmung"

#: usr/lib/co2monitor/python/co2monitor/service.py:227
#: usr/lib/co2monitor/python/co2monitor/service.py:267
msgid "giving the device another {} seconds of warmup time..."
msgstr "warte noch {} Sekunden, bis das Gerät bereit ist..."

#: usr/lib/co2monitor/python/co2monitor/service.py:233
#: usr/lib/co2monitor/python/co2monitor/service.py:273
msgid "opening data file"
msgstr "öffne Aufzeichnungsdatei"

#: usr/lib/co2monitor/python/co2monitor/service.py:235
#: usr/lib/co2monitor/python/co2monitor/service.py:275
msgid "opened data file"
msgstr "Aufzeichnungsdatei geöffnet"

#: usr/lib/co2monitor/python/co2monitor/service.py:236
#: usr/lib/co2monitor/python/co2monitor/service.py:276
msgid "opened {} for data logging."
msgstr "Datei {} zur Datenaufzeichnung geöffnet."

#: usr/lib/co2monitor/python/co2monitor/service.py:240
#: usr/lib/co2monitor/python/co2monitor/service.py:280
msgid "writing header to file"
msgstr "Schreibe Kopfzeile in Aufzeichnungsdatei"

#: usr/lib/co2monitor/python/co2monitor/service.py:245
#: usr/lib/co2monitor/python/co2monitor/service.py:285
msgid "reading from device"
msgstr "lese vom Gerät"

#: usr/lib/co2monitor/python/co2monitor/service.py:249
#: usr/lib/co2monitor/python/co2monitor/service.py:289
msgid "postprocessing raw data"
msgstr "Nachbearbeitung der Rohdaten"

#: usr/lib/co2monitor/python/co2monitor/service.py:272
#: usr/lib/co2monitor/python/co2monitor/service.py:312
msgid "writing data to file"
msgstr "schreibe Daten in Aufzeichnungsdatei"

#: usr/lib/co2monitor/python/co2monitor/service.py:283
#: usr/lib/co2monitor/python/co2monitor/service.py:323
msgid "stopped logging."
msgstr "Datenaufzeichnung beendet."

#: usr/lib/co2monitor/python/co2monitor/service.py:284
#: usr/lib/co2monitor/python/co2monitor/service.py:324
msgid "closed data logging file '{}'"
msgstr "Datenaufzeichnungsdatei '{}' geschlossen"

#: usr/lib/co2monitor/python/co2monitor/service.py:286
#: usr/lib/co2monitor/python/co2monitor/service.py:326
msgid "Stopped data logging on device {}"
msgstr "beende Datenaufzeichnung auf Gerät {}"

Expand Down Expand Up @@ -250,6 +271,10 @@ msgstr "DEVNAME Umgebungsvariable: {}"
msgid "DEVNAME environment variable '{}' is a non-existant file. Aborting."
msgstr "DEVNAME Umgebungsvariable '{}' ist keine existierende Datei. Abbruch."

#, fuzzy
#~ msgid "getting monitored_devices..."
#~ msgstr "stoppe co2monitor..."

#~ msgid "co2monitor was started by {}"
#~ msgstr "co2monitor wurde von {} gestartet"

Expand Down
Loading

0 comments on commit 323ab0d

Please sign in to comment.