From 7ff580a6fe60f7e3ea160f7b181ff0fa934453ce Mon Sep 17 00:00:00 2001 From: yvesf Date: Sun, 15 Aug 2010 22:54:27 +0200 Subject: So 15. Aug 22:54:27 CEST 2010 --- ebus/__init__.py | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'ebus/__init__.py') diff --git a/ebus/__init__.py b/ebus/__init__.py index 832e9ff..58192b9 100644 --- a/ebus/__init__.py +++ b/ebus/__init__.py @@ -8,13 +8,16 @@ import os from lxml import objectify from lxml import etree +class UnknownPacketException(Exception): + pass + class EbusPacket(object): EBUS_SPECIFICATION = os.path.join(os.path.dirname(__file__), "ebus_specification.xml") ebus_xml = objectify.parse(open(EBUS_SPECIFICATION)) @staticmethod def address_to_type(address): - d=[dev.get("type") for dev in EbusXMLMixin.ebus_xml.xpath("/ebus/devices/device[@address=$address]", + d=[dev.get("type") for dev in EbusPacket.ebus_xml.xpath("/ebus/devices/device[@address=$address]", address="#x%.2x"%address)] return len(d)>0 and d[0] or None @@ -26,29 +29,28 @@ class EbusPacket(object): def __str__(self): #XXX self.length only in subclasses - return "<%-18s name=\"%s\" source=\"0x%.2x\" destination=\"0x%.2x\" primary=0x%x secondary=0x%x length=0x%x %s>" % \ + return "<%-18s name=\"%s\" source=\"0x%.2x\" destination=\"0x%.2x\" primary=0x%x secondary=0x%x length=0x%x>" % \ (self.__class__.__name__, self.name(), self.source, self.destination, \ - self.primary_command, self.secondary_command, self.length, \ - " ".join(map(lambda name: "%s=%s" % (name, self.values()[name]),self.values()))) + self.primary_command, self.secondary_command, self.length) def get_packet_description(self): - p=EbusXMLMixin.ebus_xml.xpath("/ebus/packets/packet[@primary=$primary and @secondary=$secondary]", + p=EbusPacket.ebus_xml.xpath("/ebus/packets/packet[@primary=$primary and @secondary=$secondary]", primary="#x%.2x"%self.primary_command, secondary="#x%.2x"%self.secondary_command) if len(p)>0 and p[0] is not None: return p[0] else: - raise "Unknown Packet, primary_command=#x%.2 secondary_command=#x%.2" % ( + raise UnknownPacketException("Unknown Packet, primary_command=#x%.2x secondary_command=#x%.2x" % ( self.primary_command, - self.secondardy_command) + self.secondary_command)) def _get_source_name(self): - s=EbusXMLMixin.ebus_xml.xpath("/ebus/devices/device[@address=$address]", + s=EbusPacket.ebus_xml.xpath("/ebus/devices/device[@address=$address]", address="#x%.2x"%self.source) return len(s)>0 and s[0] or None def _get_destination_name(self): - d=EbusXMLMixin.ebus_xml.xpath("/ebus/devices/device[@address=$address]", + d=EbusPacket.ebus_xml.xpath("/ebus/devices/device[@address=$address]", address="#x%.2x"%self.destination) return len(d)>0 and d[0] or None @@ -123,9 +125,9 @@ class EbusReader(asynchat.async_chat): print "GAGA" return source = ord(data[0]) - sourceType = EbusXMLMixin.address_to_type(source) + sourceType = EbusPacket.address_to_type(source) destination = ord(data[1]) - destinationType = EbusXMLMixin.address_to_type(destination) + destinationType = EbusPacket.address_to_type(destination) if len(data) < 9: print >>sys.stderr, "Unvollständige Daten" @@ -146,18 +148,21 @@ class EbusReader(asynchat.async_chat): " ".join(map(lambda byte: "%.2x"%ord(byte), payload))) - if sourceType == 'master' and destinationType == 'master': - p = EbusMasterMaster(source, destination, primaryCommand, secondaryCommand, payload) - self.handle_ebus(p) - elif sourceType == 'master' and destinationType == 'slave': - p = EbusMasterSlave(source, destination, primaryCommand, secondaryCommand, payload, None) #FIXME - print >>sys.stderr, "SKIP MASTER-SLAVE" - elif sourceType == 'master' and destinationType == 'broadcast': - p = EbusBroadcast(source, destination, primaryCommand, secondaryCommand, payload) - self.handle_ebus(p) - else: - print >>sys.stderr, "SKIP source=%s sourceType=%s destination=%s destType=%s" % \ - (source, sourceType, destination, destinationType) + try: + if sourceType == 'master' and destinationType == 'master': + p = EbusMasterMaster(source, destination, primaryCommand, secondaryCommand, payload) + self.handle_ebus(p) + elif sourceType == 'master' and destinationType == 'slave': + p = EbusMasterSlave(source, destination, primaryCommand, secondaryCommand, payload, None) #FIXME + print >>sys.stderr, "SKIP MASTER-SLAVE" + elif sourceType == 'master' and destinationType == 'broadcast': + p = EbusBroadcast(source, destination, primaryCommand, secondaryCommand, payload) + self.handle_ebus(p) + else: + print >>sys.stderr, "SKIP source=%s sourceType=%s destination=%s destType=%s" % \ + (source, sourceType, destination, destinationType) + except UnknownPacketException,e: + print e def handle_ebus(self,ebus_packet): print >>sys.stderr, "unhandled ebus_packet" -- cgit v1.2.1