def datagramReceived(self, data, handler=None, timedelta=None): if len(data) < 2: raise WrongDatagram(data) if self.last_dgram is not None and timedelta is not None and self.last_dgram == data and timedelta < 0.15: return self.last_dgram = data fcode = ord(data[1]) if fcode & 0x20: if len(data) != 3: raise WrongDatagram(data) ext = ord(data[2]) else: if len(data) != 2: raise WrongDatagram(data) ext = None dc = ord(data[0]) try: dev = self.devs[dc] except KeyError: simple_event( Context(), "fs20", "unknown", "device", to_hc(self.code), to_dev(dc), "".join("%02x" % ord(x) for x in data), ) return try: fn = switch_names[fcode & 0x1F] except KeyError: simple_event( Context(), "fs20", "unknown", "function", to_hc(self.code), fcode & 0x1F, "".join("%02x" % ord(x) for x in data), ) return if fcode & 0x80: hdl = dev.getReply else: hdl = dev.get if ext is not None: hdl(fn, ext, handler=handler) else: hdl(fn, handler=handler) data = chr(dc) + chr(fcode | 0x80) + data[2:] self.send(data, handler)
def add_switch(self, switch): dc = to_hc(switch.code) # if dc % 100 == 44 or dc // 100 == 44: # raise SyntaxError("Devices cannot have group or master codes") if switch.code in self.devs: raise RuntimeError("Device exists (%s in %s)" % (unicode(self.codes[switch.code]), unicode(self))) self.devs[switch.code] = switch
def __init__(self, code, name): self.code = code self.name = SName(name) self.devs = {} self.last_dgram = None if self.code in codes: raise RuntimeError("Device exists (%s)" % (to_hc(self.code),)) super(SwitchGroup, self).__init__() codes[self.code] = self
def list(self): yield ("name", self.name) yield ("code", to_dev(self.code)) yield ("parent", self.parent.name) yield ("parentcode", to_hc(self.parent.code)) for d in self.does: yield ("does", d) if self.state is not None: yield ("state", self.state) if self.handler: yield ("via", self.handler.name)
def run(self,ctx,**k): event = self.params(self.ctx) if self.new_hc is None: raise SyntaxError(u"‹fs20 switch› without sub-statements does nothing!") if self.hc is None: if self.code is None: raise SyntaxError(u"A new ‹fs20 switch› needs a ‹code› sub-statement!") self.hc = SwitchGroup(self.code, SName(event)) elif self.code is not None: self.hc.code = self.code ## update for s in self.old_sw: if s.parent != self.hc: raise RuntimeError("The named device has house code %d, not %d" % (to_hc(s.parent.code),to_hc(self.hc.code))) s.delete() for s in self.new_sw: if s.code in self.hc.devs: raise RuntimeError(u"The code ‹%d› is already known in ‹%d›" % (to_dev(s.code),to_hc(self.hc.code))) s.parent = self.hc s.add()
def dataReceived(self, ctx, data, handler=None, timedelta=None): if len(data) < 4: return # obviously way too short qs = 0 for d in data: qs += ord(d) qs -= ord(data[-1]) # the above loop added it, that's nonsense qs = (ord(data[-1]) - qs) & 0xFF # we want the actual difference code = ord(data[0])*256+ord(data[1]) try: g = groups[(code,qs)] except KeyError: simple_event(ctx, "fs20","unknown",to_hc(code),qs,"".join("%02x" % ord(x) for x in data)) else: return g.datagramReceived(data[2:-1], handler, timedelta=timedelta)
def __repr__(self): try: return u"‹%s:%s:%s›" % (self.__class__.__name__, to_hc(self.code), self.name) except Exception: return "‹" + self.__class__.__name__ + "›"
def list(self): yield ("name", self.name) yield ("code", to_hc(self.code)) for d in self.devs.itervalues(): yield ("device", d.name)
def info(self): return str(to_hc(self.code))