def __init__(self, glade_file, testing=False): self.testing=testing self.builder = gtk.Builder() self.builder.add_from_file(glade_file) self.dialog = self.builder.get_object("config_dialog") self.dialog._testing=testing self.dialog._builder=self.builder self.mb_proxy_detected=False self.lb_proxy_detected=False signals={"visibility-notify-event": self.on_show, "on_show": self.on_show, "on_close_clicked": self.on_close_clicked, "on_config_dialog_destroy": self.on_config_dialog_destroy} self.builder.connect_signals(signals, self) Bus.subscribe("Config", "musicbrainz_proxy_detected", self.on_musicbrainz_proxy_detected)
def __init__ (self): rb.Plugin.__init__ (self) self.active=None self.current_entry=None self.dbcount=0 self.load_complete=False self.done=False self.db=None self.type_song=None self.start_phase=True self.current_entries_count=0 self.previous_entries_count=0 self.song_entries={} self.sourcescb=[] Bus.subscribe(self.BUSNAME, "devmode?", self.hq_devmode) Bus.subscribe(self.BUSNAME, "appname?", self.hq_appname) Bus.subscribe(self.BUSNAME, "__tick__", self.h_tick)
def _hlogpath(self, name, path): self._path=path self._name=name self._setup() def _hlog(self, *arg): if self._shutdown: return if self._logger is None: self._setup() if len(arg) == 1: self._logger.log(logging.INFO, arg[0]) else: level=self.mlevel.get(arg[0], logging.INFO) self._logger.log(level, arg[1]) def _hshutdown(self, *arg): self._shutdown=True self._logger=None logging.shutdown([self.fhdlr]) _log=Logger() Bus.subscribe("%log", _log._hlog) Bus.subscribe("%logpath", _log._hlogpath) Bus.subscribe("shutdown", _log._hshutdown)
except: Bus.publish(self, "%log", "warning", "Cannot find previsouly registered device, serial(%s)" % serial) def _hdevice(self, details): """ %device - local message handler """ dtype=details.get("type", None) if dtype!="PhidgetInterfaceKit": return serial=details.get("serial", None) entry=self._devices.get(serial, {}) if not entry: Bus.publish(self, "%log", "Found IFK device, serial(%s)" % serial) Bus.publish(self, "%attached", details) try: device=IfkAgent(self._q, serial) except: device=None if device: self._devices[serial] = device ## ===================================================================== _ifkManager=IfkManager() Bus.subscribe("%device", _ifkManager._hdevice) Bus.subscribe("%detached", _ifkManager._hdetached) Bus.subscribe("%poll", _ifkManager._hpoll)
return self.validateConfig(config) def validateConfig(self, config): """ Validates (as much as possible) the configuration information before handing it off Categories: "States", "Devices" Pins: integer """ for param in self.PARAMS: try: _value=config[param] except: self.log("warning", "Missing '%s' entry in configuration file(%s)" % (param, self.cpath)) self.usedefaults() return self.config=config self.log("info", "Successfully validated configuration file(%s)" % self.cpath) def _qconfig_amqp(self, *_): """ Answers the question `%config-amqp` """ Bus.publish(self, "%config-amqp", self.config) _ac=AmqpConfig() Bus.subscribe("%poll", _ac._hpoll) Bus.subscribe("%config-amqp?", _ac._qconfig_amqp)
break def gMsg(self): try: msg=self.iq.get_nowait() except Empty: msg=(None, None) return msg def processMsg(self, data): try: devices=json.loads(data) except Exception,e: Bus.publish(self, "%llog", "%json-decode", "warning", "Error(%s) decoding json object: %s" % (e, data)) return ## We need to itemize the devices list ## for the benefit of the other agents sitting on the Bus try: for device in devices: Bus.publish(self, "%device", device) except Exception, e: Bus.publish(self, "%llog", "%json-decode", "warning", "Error(%s) accessing json list object: %s" % (e, devices)) _ifkm=IfkManagerAgent(qToManagerConsumer, qFromManagerConsumer) Bus.subscribe("%config-amqp", _ifkm._hConfigAmqp) Bus.subscribe("%poll", _ifkm._hpoll) Bus.subscribe("%quit", _ifkm._hquit)
self._q.put(["%device-detached", details], block=True) def _onError(self, e): try: details=self._getDeviceDetails(e.device) self._q.put(["%device-error", details], block=True) Bus.publish(self, "%log", "warning", "Device error: %s" % details) except Exception,e: Bus.publish(self, "%log", "error", "exception whilst attempting to report Phidgets.onError (%s)" % e) def _getDeviceDetails(self, device): details={} ## should at least have serial number try: details["serial"] = device.getSerialNum() except: pass try: details["name"] = device.getDeviceName() details["type"] = device.getDeviceType() details["version"] = device.getDeviceVersion() #details["label"] = device.getDeviceLabel() # crashes DBus except: pass return details _manager=ManagerAgent() Bus.subscribe("%poll", _manager._hpoll)
def __init__ (self): rb.Plugin.__init__ (self) self.main=PluginAgent() Bus.subscribe(self.BUSNAME, "devmode?", self.hq_devmode) Bus.subscribe(self.BUSNAME, "appname?", self.hq_appname)
try: self.chan.exchange_declare(exchange=self.EXCH, type="topic", durable=False, auto_delete=True,) except Exception,e: Bus.publish(self, "%conn-error") self.log("%conn-error", "error", "Failed to declare exchange on connection to AMQP broker. Exception(%s)" % e) try: self.chan.close() self.conn.close() self.chan=None self.conn=None except: pass return self.log("%conn-open", "info", "Connection to AMQP broker opened") def log(self, ltype, level, msg): Bus.publish(self, "%llog", ltype, level, msg) _handler=APIHandler() Bus.subscribe("%devices", _handler.Devices) Bus.subscribe("%device-attached", _handler.Attached) Bus.subscribe("%device-detached", _handler.Detached) Bus.subscribe("%device-error", _handler.Error) Bus.subscribe("%config-amqp", _handler._hconfig) Bus.subscribe("%poll", _handler._hpoll)
pinnames.extend([pname]) try: _ipin=int(pin) except: self.log("warning", "Expecting 'integer' value for pin entry, device(%s)" % device) return ## stringify for less headache: normalize type m="%s.%s" % (device_name, pin) pinmap[m] = pname except: self.log("warning", "Error whilst validating 'Devices' section of configuration file") return #print "pinnames: ",pinnames try: for pinname in states: if not pinname in pinnames: self.log("warning", "Pin name(%s) not found in any 'Device' definition" % pinname) except: self.log("warning", "Error whilst validating 'States' section of configuration file") return self.config=config self.log("info", "Successfully validated configuration file(%s)" % self.cpath) Bus.publish(self, "%config-sensors", self.config) Bus.publish(self, "%pin-map", pinmap) _ca=ConfigAgent() Bus.subscribe("%poll", _ca._hpoll)
## ============================================================================ ## SIGNAL GENERATORS ## ============================================================================ @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="a{sv}") def Error(self, dic): """Generated when an error on a device is detected""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="sii") def Din(self, serial, pin, value): """Generated when the state of a digital input changes""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="sii") def Dout(self, serial, pin, value): """Generated when the state of a digital output changes""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="sii") def Ain(self, serial, pin, value): """Generated when the state of an analog input changes""" _handler=DBusAPIHandler() Bus.subscribe("%device-error", _handler.Error) Bus.subscribe("%device-din", _handler.Din) Bus.subscribe("%device-dout", _handler.Dout) Bus.subscribe("%device-ain", _handler.Ain)
@dbus.service.method('com.phidgets.Phidgets', in_signature="s") def EchoString(self, original): return original @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="aa{sv}") def Devices(self, liste): """Generated when a device is attached to the host""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="a{sv}") def Attached(self, dic): """Generated when a device is attached to the host""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="a{sv}") def Detached(self, dic): """Generated when a device is detached to the host""" @dbus.service.signal(dbus_interface="com.phidgets.Phidgets", signature="a{sv}") def Error(self, dic): """Generated when an error on a device is detected""" _handler=DBusAPIHandler() Bus.subscribe("%devices", _handler.Devices) Bus.subscribe("%device-attached", _handler.Attached) Bus.subscribe("%device-detached", _handler.Detached) Bus.subscribe("%device-error", _handler.Error)
Bus.publish(self, "%conn-error", "warning", "Connection to AMQP broker failed") del self.currentWorker self.currentWorker = None if self.currentWorker is not None: self.processMsgQueue() def processMsgQueue(self): while True: msg=self.currentWorker.rxFromWorker() if msg is None: break try: mtype=msg.pop(0) mdata=msg.pop(0) except: Bus.publish(self, "%llog", "%msg-error", "error", "Error whilst decoding message from AMQP exchange 'org.sensors' ") continue ## e.g. "state.io.din" Bus.publish(self, mtype, mdata) def _hquit(self): self.update() _mng=Manager() Bus.subscribe("%config-amqp", _mng._hconfig) Bus.subscribe("%poll", _mng._hpoll) Bus.subscribe("%quit", _mng._hquit)
try: msg = p[0] except: raise RuntimeError("LogLimiter: invalid usage") cdelta=self.map.get(ltype, self.DEFAULT_CDELTA) lpoll=self.st.get(ltype, 0) delta=self.cpoll - lpoll #print "cpoll(%s) ltype(%s) delta(%s) cdelta(%s) msg(%s)" % (self.cpoll, ltype, delta, cdelta, msg) if delta >= cdelta or lpoll==0: self.st[ltype] = self.cpoll Bus.publish(self, "%log", level, "(%s:%s) %s" % (ltype, c, msg)) _ll=LogLimiter() Bus.subscribe("%llog", _ll._hllog) Bus.subscribe("%llconfig", _ll._hllconfig) Bus.subscribe("%poll", _ll._hpoll) ## ====================================================================== ## ====================================================================== if __name__=="__main__": Bus.debug=True Bus.publish(None, "%llog", "l1", "info", "msg1") Bus.publish(None, "%llog", "l2", "warning", "msg2") Bus.publish(None, "%llog", "l3", "msg3")
Bus.publish(self, "%state-changed", serial, "dout:%s" % pin, mval) def _hAin(self, serial, pin, value): pname, mval=self.domap(serial, pin, value) if mval is not None: Bus.publish(self, "%state-changed", serial, pname, mval) else: Bus.publish(self, "%state-changed", serial, "ain:%s" % pin, mval) ## ================================================== def domap(self, serial, pin, value): pn=self.pmap(serial, pin) pstates=self.states.get(pn, {}) mvalue=pstates.get(value, None) return (pn, mvalue) def pmap(self, serial, pin): key="%s.%s" % (serial, pin) return self.map.get(key, None) _sa=SensorsAgent() Bus.subscribe("%config-sensors", _sa.hConfig) Bus.subscribe("%pin-map", _sa.hPinMap) Bus.subscribe("%din", _sa._hDin) Bus.subscribe("%dout", _sa._hDout) Bus.subscribe("%ain", _sa._hAin)
PATH = "/State" BUS_NAME= "org.sensors" INTERF = "org.sensors" def __init__(self): self.bus=dbus.SessionBus() bus_name = dbus.service.BusName(self.BUS_NAME, self.bus) dbus.service.Object.__init__(self, bus_name, self.PATH) @dbus.service.signal(dbus_interface="org.sensors", signature="ssv") def State(self, device_id, sensor_name, sensor_state): """Generated when a sensor changes state""" _shandler=DBus_State() Bus.subscribe("%state-changed", _shandler.State) class DBus_Config(dbus.service.Object): """ DBus signals handler """ PATH = "/Config" BUS_NAME= "org.sensors" INTERF = "org.sensors" def __init__(self): bus_name = dbus.service.BusName(self.BUS_NAME, bus=dbus.SessionBus()) dbus.service.Object.__init__(self, bus_name, self.PATH) @dbus.service.signal(dbus_interface="org.sensors", signature="a{sv}")
Bus.publish(self, "%config-amqp?") if not self.comm: self.comm=AMQPCommTx(self.config, self.EXCH) self.comm.connect() if not self.comm.isOk(): del self.comm self.comm=None def StateChanged(self, sensor_type, dic): """Generated when a sensor changes state""" if self.comm: self.comm.publish("state.io.%s" % sensor_type, dic) def ConfigSensors(self, config): """Generated when sensor configuration changess and also periodically """ if self.comm: self.comm.publish("config", config) _shandler=SensorsHandlerAgent() Bus.subscribe("%config-amqp", _shandler._hConfig) Bus.subscribe("%state-changed", _shandler.StateChanged) Bus.subscribe("%config-sensors", _shandler.ConfigSensors) Bus.subscribe("%poll", _shandler._hPoll)
"""Generated when the state of an analog input changes""" if self.comm: self.comm.publish("device.io.ain", dic) def _hpoll(self, pc): if self.config is None: Bus.publish(self, "%config-amqp?") if self.comm is not None: if not self.comm.isOk(): del self.comm self.comm=None if self.comm is None: self.comm=AMQPCommTx(self.config, self.EXCH) self.comm.connect() _handler=IfkAmqpAgent() Bus.subscribe("%device-error", _handler.Error) Bus.subscribe("%device-din", _handler.Din) Bus.subscribe("%device-dout", _handler.Dout) Bus.subscribe("%device-ain", _handler.Ain) Bus.subscribe("%poll", _handler._hpoll) Bus.subscribe("%config-amqp", _handler._hconfig)