def databaseComplete(self, db): iofun.out("database complete, analyzing...") if not self.dev: iofun.out("no valid device set, aborting!") return linkType = 0 searchRec = { "offset": 0, "addr": InsteonAddress("00.00.00"), "type": linkType, "group": 0, "data": [0, 0, 0] } records = db.findAllRecords(searchRec, False, False, False) if not records: iofun.out("no matching record found, no action taken!") return iofun.out("found active records: ") i = 0 for rec in reversed(records): if self.numRecords > 0 and i >= self.numRecords: break db.dumpRecord(rec, " removing record:") self.dev.setRecord(rec["offset"], InsteonAddress("00.00.00"), 0, 0, [0, 0, 0]) time.sleep(5.0) i = i + 1 time.sleep(1) # wait for one second iofun.out("complete")
def setRampRateOn(self, level=0xFF, rate=0x1f): """setRampRateOn(level, rate) set future ramp rate and switch on to level (0-255) at rate (0-31)""" cmd2 = (level & 0xf0) | ((rate & 0x1f) >> 1) iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x2e, cmd2, -1))
def setRampRateOff(self, rate=0x1f): """setRampRateOff(rate) set future ramp rate and switch off at rate (0-31)""" cmd2 = (rate & 0x1f) >> 1 iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x2f, cmd2, -1))
def sendOff(self, group): """sendOff(group) sends ALLLink broadcast OFF message to group "group" """ msg = message.createStdMsg(InsteonAddress("00.00.00"), 0x0f, 0x13, 0xFF, group) iofun.writeMsg(msg) iofun.out("sent msg: " + msg.toString())
def getIMConfig(self): """getIMConfig() get modem configuration flags byte""" self.querier = Querier(InsteonAddress("00.00.00")) self.querier.setMsgHandler(IMConfigMsgHandler("getIMConfig")) msg = Msg.s_makeMessage("GetIMConfig") self.querier.sendMsg(msg)
def getid(self): """getid() get modem id data""" self.querier = Querier(InsteonAddress("00.00.00")) self.querier.setMsgHandler(IMInfoMsgHandler("getid")) msg = Msg.s_makeMessage("GetIMInfo") self.querier.sendMsg(msg)
def processMsg(self, msg): tmp = msg.getByte("command1") & 0xFF if (tmp != 0x2e): out(self.name + " got unexpected msg: " + msg.toString()) return 0 if msg.isExtended(): out(self.name + " existing schedule:") printScheduleMsg(msg) idx = 3 * self.period data = message.getMsgData(msg) data = [(data[k] & 0xFF) for k in range(len(data))] data[idx] = self.time & 0xFF data[idx + 1] = self.cool & 0xFF data[idx + 2] = self.heat & 0xFF nmsg = message.createExtendedMsg2( InsteonAddress(self.thermostat.address), 0x2e, (0x03 + self.day) & 0xFF, data) out(self.name + " new schedule:") printScheduleMsg(nmsg) iofun.writeMsg(nmsg) out(self.name + " sent new schedule: " + nmsg.toString()) return 1 else: out(self.name + " got ack, waiting for ext msg!") return 0
def addStopRecord(self): rec = {"offset" : self.getAppendRecordOffset(), "addr" : InsteonAddress("00.00.00"), "type" : 0, "group" : 0, "data" : [0, 0 , 0]} self.addRecord(rec, allowDuplicates= True)
def msgReceived(self, msg): self.restartTimer() if msg.isPureNack(): # out("got pure NACK") return if msg.getByte("Cmd") == 0x62: # out("query msg acked!") return elif msg.getByte("Cmd") == 0x50: # out("got ack of direct!") return elif msg.getByte("Cmd") == 0x51: off = (msg.getByte("userData3") & 0xFF) << 8 | ( msg.getByte("userData4") & 0xFF) rb = msg.getBytes("userData6", 8) # ctrl + group + [data1,data2,data3] + whatever ltype = rb[0] & 0xFF group = rb[1] & 0xFF data = rb[5:8] addr = InsteonAddress(rb[2] & 0xff, rb[3] & 0xff, rb[4] & 0xff) rec = { "offset": off, "addr": addr, "type": ltype, "group": group, "data": data } self.db.addRecord(rec, False) outchars(" " + format(self.db.getNumberOfRecords(), 'd')) if (ltype & 0x02 == 0): # out("last record: " + msg.toString()) self.done() return else: out("got unexpected msg: " + msg.toString())
def databaseComplete(self, db): iofun.out("database complete, removing last record") above, stopaddr, below = db.findStopRecordAddresses() if (above == 0): iofun.out("db already empty!") else: self.dev.setRecord(above, InsteonAddress("00.00.00"), 0x00, 0x00, [0, 0, 0])
def __bcast(self, group, cmd1, cmd2): msg = Msg.s_makeMessage("SendStandardMessage") flags = 0xcf adr = InsteonAddress(0x00, 0x00, group & 0xFF) msg.setAddress("toAddress", adr) msg.setByte("messageFlags", flags) msg.setByte("command1", cmd1) msg.setByte("command2", cmd2) iofun.writeMsg(msg)
def createStdMsg(adr, flags, cmd1, cmd2, group): msg = Msg.s_makeMessage("SendStandardMessage") if group != -1: flags |= 0xc0 adr = InsteonAddress(0x00, 0x00, group & 0xFF) msg.setAddress("toAddress", adr) msg.setByte("messageFlags", flags) msg.setByte("command1", cmd1) msg.setByte("command2", cmd2) return msg
def __makeModMsg(self, addr, group, controlCode, recordFlags, data, txt): msg = Msg.s_makeMessage("ManageALLLinkRecord") msg.setByte("controlCode", controlCode) # mod. first ctrl found or add msg.setByte("recordFlags", recordFlags) msg.setByte("ALLLinkGroup", group) msg.setAddress("linkAddress", InsteonAddress(addr)) msg.setByte("linkData1", data[0] & 0xFF) msg.setByte("linkData2", data[1] & 0xFF) msg.setByte("linkData3", data[2] & 0xFF) return msg
def load(self, filename): r = re.compile('[ \t\n\r:]+') with open(filename, 'r') as f: for line in f: (offset, addr, typ, group, d1, d2, d3, dummy) = r.split(line) rec = {"offset" : int(offset, 16), "addr" : InsteonAddress(addr), "type" : int(typ, 16), "group" : int(group, 16), "data" : [int(d1, 16), int(d2, 16), int(d3, 16)]} self.addRecord(rec, True)
def start(self): self.db.clear() iofun.addListener(self) msg = message.createExtendedMsg(InsteonAddress(self.addr), 0x2f, 0, []) msg.setByte("userData1", 0) msg.setByte("userData2", 0) msg.setByte("userData3", 0) msg.setByte("userData4", 0) msg.setByte("userData5", 0) iofun.writeMsg(msg) outchars("sent db query msg, incoming records: ") self.timer = Timer(20.0, self.giveUp) self.timer.start()
def databaseComplete(self, db): iofun.out("database complete, moving...") mask = 0x02 searchRec = { "offset": self.oldOffset, "addr": InsteonAddress("00.00.00"), "type": (1 << 1), "group": 0, "data": [] } recs = db.findRecord(searchRec, mask, False, False, False, True) for rec in recs: if rec["offset"] == self.oldOffset: db.dumpRecord(rec, "oldRecord: ") oldRec = rec searchRec = { "offset": self.newOffset, "addr": InsteonAddress("00.00.00"), "type": (1 << 1), "group": 0, "data": [] } recs = db.findRecord(searchRec, mask, False, False, False, True) for rec in recs: if rec["offset"] == self.newOffset: db.dumpRecord(rec, "newRecord: ") newRec = rec if not oldRec: iofun.out("no source record found, nothing to do!") return if not newRec: iofun.out("no target record found, nothing to do!") return self.dev.setRecord(newRec["offset"], oldRec["addr"], oldRec["group"], oldRec["type"], oldRec["data"]) time.sleep(1) # wait for one second iofun.out("complete")
def msgReceived(self, msg): self.restartTimer() if msg.isPureNack(): #out("got pure NACK") return if msg.getByte("Cmd") == 0x62: #out("query msg acked!") return if msg.getByte("Cmd") == 0x50 and msg.getByte("command1") == 0x2F: #out("std reply received!") return elif msg.getByte("Cmd") == 0x51: off = (((msg.getByte("userData3") & 0xFF) << 8) | (msg.getByte("userData4") & 0xFF)) linkType = msg.getByte("userData6") & 0xFF group = msg.getByte("userData7") & 0xFF linkAddr = InsteonAddress( msg.getByte("userData8") & 0xFF, msg.getByte("userData9") & 0xFF, msg.getByte("userData10") & 0xFF) data = [ msg.getByte("userData11") & 0xFF, msg.getByte("userData12") & 0xFF, msg.getByte("userData13") & 0xFF ] if (self.db.hasOffset(off)): return rec = { "offset": off, "addr": linkAddr, "type": linkType, "group": group, "data": data } self.db.addRecord(rec, False) #self.db.dumpRecord(rec, "got record: "); outchars(" " + format(self.db.getNumberOfRecords(), 'd')) if (linkType & 0x02 == 0): # has end-of-list marker self.done() return else: out("got unexpected msg: " + msg.toString())
def makeMsg(self, offset, laddr, group, linkType, data): msg = Msg.s_makeMessage("SendExtendedMessage") msg.setAddress("toAddress", InsteonAddress(self.getAddress())) msg.setByte("messageFlags", 0x1f) msg.setByte("command1", 0x2f) msg.setByte("command2", 0x00) msg.setByte("userData1", 0x00) # don't care info msg.setByte("userData2", 0x02) # set database msg.setByte("userData3", offset >> 8) # high byte msg.setByte("userData4", offset & 0xff) # low byte msg.setByte("userData5", 8) # number of bytes set: 1...8 msg.setByte("userData6", linkType) msg.setByte("userData7", group) msg.setByte("userData8", laddr.getHighByte()) msg.setByte("userData9", laddr.getMiddleByte()) msg.setByte("userData10", laddr.getLowByte()) # depends on mode: could be e.g. trigger point msg.setByte("userData11", data[0]) msg.setByte("userData12", data[1]) # unused? msg.setByte("userData13", data[2]) # unused? rb = msg.getBytes("command1", 15); checksum = (~sum(rb) + 1) & 0xFF msg.setByte("userData14", checksum) return msg
def incrementalDim(self): """incrementalDim() dim light incrementally""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x16, 0x00, -1))
def incrementalBright(self): """incrementalBright() brighten light incrementally""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x15, 0x00, -1))
def offFast(self): """offFast() switch light off fast""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x14, 0x00, -1))
def onFast(self, level=0xFF): """onFast(level) switch light on fast, to given level""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x12, level, -1))
def beep(self): """beep() make device beep""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x30, 0x00, -1))
def off(self): """off() switch off""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x13, 0x00, -1))
def on(self, level=0xFF): """on(level) switch on to given light level""" iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x11, level, -1))
def addDev(dev): devNameMap[dev.getName()] = dev addr = InsteonAddress(dev.getAddress()).toString() devAddressMap[addr] = dev
def removeAddress(self, addr): searchRec = {"offset" : 0, "addr": InsteonAddress(addr), "type" : 0, "group" : 0, "data" : [0,0,0]} remove = self.findActiveRecords(searchRec, True, False, False) for i in remove: del self.records[i["offset"]]
def startManualChangeDown(self): """startManualChangeDown() start manual change, dim down""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x17, 0x00, -1))
def stopManualChange(self): """stopManualChange() stop manual change""" iofun.writeMsg( message.createStdMsg(InsteonAddress(self.getAddress()), 0x0F, 0x18, 0x00, -1))
def rampRateOn(self, level=0xFF, rate = 0x1f): """rampRateOn(level, rate) switch on to given light level (0-255) at given rate (0-31)""" cmd2 = (level & 0xf0) | ((rate & 0x1f) >> 1); iofun.writeMsg(message.createStdMsg( InsteonAddress(self.getAddress()), 0x0F, 0x2e, cmd2, -1))