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 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)" % (six.text_type(self.codes[switch.code]), six.text_type(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 __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 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)" % (six.text_type(self.codes[switch.code]), six.text_type(self))) self.devs[switch.code] = switch
def list(self): yield super(Switch,self) 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 list(self): yield super(Switch, self) 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("fs20","unknown","hc", hc=to_hc(code),checksum=qs,data=data) else: return g.datagramReceived(data[2:-1], handler, timedelta=timedelta)
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("fs20", "unknown", "hc", hc=to_hc(code), checksum=qs, data=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 super(SwitchGroup,self) yield("code",to_hc(self.code)) for d in self.devs.values(): yield("device",d.name)
def info(self): return str(to_hc(self.code))
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 super(SwitchGroup, self) yield ("code", to_hc(self.code)) for d in self.devs.values(): yield ("device", d.name)