def do_off(self, devId): """off name Sends off command to named device""" devKey = devices.FindDev(devId) if devKey != None: database.NewEvent(devKey, "SwitchOff", "UICmd") devcmds.SwitchOff(devKey)
def do_toggle(self, devId): """toggle name Sends toggle on/off command to named device""" devKey = devices.FindDev(devId) if devKey != None: database.NewEvent(devKey, "Toggle", "UICmd") devcmds.Toggle(devKey)
def do_setSchedule(self, line): """setSchedule id name Sets schedule on device using name (eg Heating, etc.)""" argList = line.split() if len(argList) >= 2: devKey = devices.FindDev(argList[0]) scheduleName = argList[1] if devKey != None: heating.SetSchedule(devKey, scheduleName)
def do_rptTemp(self, line): # NB As a version of SetSourceTemp """rptTemp id temp Reports the temperature to a device""" argList = line.split() if len(argList) >= 2: devKey = devices.FindDev(argList[0]) temp = argList[1] if devKey != None: heating.RptTemp(devKey, temp)
def do_devat(self, line): """devat name cmd Sends AT command to named device""" argList = line.split() if len(argList) >= 2: cmd = argList[1] devKey = devices.FindDev(argList[0]) if devKey != None: queue.EnqueueCmd(devKey, ["AT" + cmd, "OK"])
def MakeText(id): if weather.forecastPeriod=="": return None; reportDict = dict() reportDict["target"] = id minutes = datetime.now().minute #if minutes % 2 == 0: # reportDict["display"] = "off" #else: reportDict["display"] = "on" # For now at least reportDict["period"] = weather.forecastPeriod reportDict["icon"] = str(weather.symSym)[4:] reportDict["cloudText"] = weather.cloudText reportDict["maxTemp"] = str(round(weather.maxTemp))+"C" reportDict["minTemp"] = str(round(weather.minTemp))+"C" reportDict["windSpeed"] = str(round(weather.maxWind)) reportDict["windDir"] = str(weather.windDir) reportDict["windText"] = weather.windText now = datetime.now() reportDict["timeDigits"] = str(now.strftime("%H:%M")) reportDict["timeText"] = GetTimeInWords() reportDict["dayOfWeekText"] = str(now.strftime("%A")) reportDict["dayOfMonthText"] = str(int(now.strftime("%d"))) # Use int() to remove leading zero reportDict["monthText"] = str(now.strftime("%B")) powerMonitorName = config.Get("PowerMonitor") # House power monitoring device if powerMonitorName != None: devKey = devices.FindDev(powerMonitorName) if devKey != None: powerW = database.GetLatestLoggedItem(devKey, "PowerReadingW") if powerW != None: reportDict["powerNow"] = str(powerW[0]) energyToday = variables.Get("energyToday_kWh") if energyToday: reportDict["energyToday"] = energyToday tempMonitorName = config.Get("HouseTempDevice") # House temperature monitoring device if tempMonitorName != None: devKey = devices.FindDev(tempMonitorName) if devKey != None: houseTemp = database.GetLatestLoggedItem(devKey, "TemperatureCelsius") reportDict["houseTemp"] = '%.1f' % (houseTemp[0]) targetTemp = variables.Get("TargetTemp") if targetTemp != None: reportDict["TargetTemp"] = targetTemp #log.debug("ReportDict for:" +id + "=" + str(reportDict)) # Already reported in WiFiServer.py return (str(reportDict))
def do_setTargetTemp(self, line): """setTargetTemp id temp durationS Sets the target temperature on the thermostat device for durationS""" argList = line.split() if len(argList) >= 3: devKey = devices.FindDev(argList[0]) temp = argList[1] timeoutS = argList[2] if devKey != None: schedule.Override(devKey, temp, timeoutS)
def do_identify(self, line): """identify name seconds Sends identify command to named device. Use 0 seconds to stop immediately""" argList = line.split() if len(argList) >= 2: devKey = devices.FindDev(argList[0]) if devKey != None: timeS = int(argList[1]) devcmds.Identify(devKey, timeS) else: log.fault("Insufficient Args")
def do_sat(self, line): """sat name sat Sends ColorCtrl command to named device, where 0<sat<100""" argList = line.split() if len(argList) >= 2: sat = int(argList[1]) devKey = devices.FindDev(argList[0]) if devKey != None: database.NewEvent(devKey, "Sat", "UICmd") devcmds.Sat(devKey, sat) else: log.fault("Insufficient Args")
def do_hue(self, line): """hue name hue Sends ColorCtrl command to named device, where 0<hue<360""" argList = line.split() if len(argList) >= 2: hue = int(argList[1]) devKey = devices.FindDev(argList[0]) if devKey != None: database.NewEvent(devKey, "Hue", "UICmd") devcmds.Hue(devKey, hue) else: log.fault("Insufficient Args")
def do_dim(self, line): """dim name percentage Sends level command to named device""" argList = line.split() if len(argList) >= 2: percentage = int(argList[1]) devKey = devices.FindDev(argList[0]) if devKey != None: database.NewEvent(devKey, "Dim", "UICmd") devcmds.Dim(devKey, percentage) else: log.fault("Insufficient Args")
def do_getAttr(self, line): """getAttr name clstr attr Get attribute on cluster from named device""" argList = line.split() if len(argList) >= 3: devKey = devices.FindDev(argList[0]) clstr = argList[1] attr = argList[2] if devKey != None: telegesis.TxReadDevAttr( devKey, clstr, attr) # WOrk out details and send command via queue
def EventHandler(eventId, eventArg): global overrideTimeoutMins, currentTargetTemp if eventId == events.ids.MINUTES: name = config.Get("HeatingDevice") if name != None: heatingDevKey = devices.FindDev( name ) # Get heating device every minute to allow for it changing if heatingDevKey != None: # Check that we have a real heating device schedule = config.Get("HeatingSchedule") if schedule != None: scheduledTargetTemp = GetTarget(schedule) if scheduledTargetTemp != None: # We now have a heating device and a schedule to follow or override if overrideTimeoutMins > 0: overrideTimeoutMins = overrideTimeoutMins - 1 if overrideTimeoutMins <= 0: # Just finished override database.NewEvent( heatingDevKey, "Resume " + str(scheduledTargetTemp) + "'C") # For ActivityLog on web page currentTargetTemp = heating.SetTargetTemp( heatingDevKey, scheduledTargetTemp ) # Resume schedule here else: # Still overriding if scheduledTargetTemp != currentTargetTemp: # Check whether schedule in heating device is about to change target database.NewEvent( heatingDevKey, "Overriding scheduled " + str(scheduledTargetTemp) + "'C with " + str(currentTargetTemp) + "C") # For ActivityLog on web page # Un-indent following line to force override temp once/min while overriding, rather than just at change heating.SetTargetTemp( heatingDevKey, currentTargetTemp ) # Re-Set target in heating device (since it's also running the schedule) else: # Not overriding if scheduledTargetTemp != currentTargetTemp: database.NewEvent( heatingDevKey, "Scheduled " + str(scheduledTargetTemp) + "'C") # For ActivityLog on web page #heating.SetTargetTemp(heatingDevKey, scheduledTargetTemp) # Set target in heating device here. (Not needed since it's running the schedule directly) currentTargetTemp = scheduledTargetTemp # else: No scheduled target # else: No HeatingSchedule # else: Despite having a name, there's no associated device variables.Set("TargetTemp", str(currentTargetTemp)) else: # Ignore schedules and overrides if no named heating device synopsis.problem( "NoHeatingDevice", "No HeatingDevice entry in config, needed to resume after override" )
def do_removeDevice(self, devId): """removeDevice id Tells device to leave the network and remove it from the database""" devKey = devices.FindDev(devId) if devKey != None: devices.Remove(devKey)
def do_getTime(self, devId): """getTime id Gets the time from the device using the time cluster""" devKey = devices.FindDev(devId) if devKey != None: iottime.GetTime(devKey)
def do_setTime(self, devId): """setTime id Sets the time on the device from Linux time""" devKey = devices.FindDev(devId) if devKey != None: devcmds.SetTime(devKey)
def do_getSourceTemp(self, devId): """getSourceTemp id Gets the source temperature from a thermostat device""" devKey = devices.FindDev(devId) if devKey != None: heating.GetSourceTemp(devKey)
def Action(actList, ruleId): log.debug("Action with: " + str(actList)) action = actList[0].lower() if action == "Log".lower(): log.debug("Rule says Log event for " + ' '.join(actList[1:])) elif action == "Play".lower(): call(["omxplayer", "-o", actList[1], actList[2]]) elif action == "Event".lower(): if actList[1].lower() == "TimeOfDay".lower(): events.IssueEvent(events.ids.TIMEOFDAY, actList[2]) elif actList[1].lower() == "Alarm".lower(): events.IssueEvent(events.ids.ALARM, actList[2]) # Could have other events here... elif action == "synopsis": # Was status emailAddress = config.Get("emailAddress") log.debug("About to send synopsis to " + emailAddress) if emailAddress != None: synopsis.BuildPage() # Create synopsis page on demand with open("synopsis.txt", "r") as fh: # Plain text of email emailText = fh.readlines() text = ''.join(emailText) with open("synopsis.html", "r") as fh: # HTML of email emailHtml = fh.readlines() html = ''.join(emailHtml) sendmail.email("Vesta Status", text, html) # See sendmail.py else: synopsis.problem( "NoEmail", "No emailAddress entry in config, needed to send synopsis") elif action == "email": # All args are body of the text. Fixed subject and email address emailAddress = config.Get("emailAddress") if emailAddress != None: emailBody = [] for item in actList[1:]: emailBody.append(item) plainText = " ".join(emailBody) log.debug("Sending email with '" + plainText + "'") result = sendmail.email("Vesta Alert!", plainText, None) if result != 0: synopsis.problem( "Email", "sendmail.email() failed with code " + str(result) + " when trying to send:" + plainText) else: synopsis.problem("NoEmail", "No emailAddress entry in config") elif action == "override": # Syntax is "Override <targetDevice> <targetDegC> <durationSecs>" devKey = devices.FindDev(actList[1]) target = actList[2] timeSecs = actList[3] if devKey != None: schedule.Override(devKey, target, timeSecs) elif action == "set": # Set a named variable to a value expression = "".join( actList[1:] ) # First recombine actList[1] onwards, with no spaces. Now expression should be of the form "<var>=<val>" if "--" in expression: sep = expression.index("--") varName = expression[:sep] varVal = variables.Get(varName) if isNumber(varVal): newVal = str(eval(varVal + "-1")) variables.Set(varName, newVal) Run( varName + "==" + newVal ) # Recurse! to see if any rules need running now that we've set a variable else: log.fault(varName + " not a number at " + expression) elif "++" in expression: sep = expression.index("++") varName = expression[:sep] varVal = variables.Get(varName) if isNumber(varVal): newVal = str(eval(varVal + "+1")) variables.Set(varName, newVal) Run( varName + "==" + newVal ) # Recurse! to see if any rules need running now that we've set a variable else: log.fault(varName + " not a number at " + expression) elif "=" in expression: sep = expression.index("=") varName = expression[:sep] varVal = expression[sep + 1:] variables.Set(varName, varVal) Run( varName + "==" + varVal ) # Recurse! to see if any rules need running now that we've set a variable else: log.fault("Badly formatted rule at " + expression) elif action == "unset": # Remove a named variable variables.Del(actList[1]) else: # Must be a command for a device, or group of devices if len(actList) >= 2: # Check that we have a second arg... name = actList[1] # Second arg is name if database.IsGroupName(name): # Check if name is a groupName devKeyList = GetGroupDevs(name) for devKey in devKeyList: CommandDev(action, devKey, actList, ruleId) # Command each device in list else: devKey = database.GetDevKey("userName", name) CommandDev(action, devKey, actList, ruleId) # Command one device
def do_getSchedule(self, devId): """getSchedule id Gets heating schedule from device""" devKey = devices.FindDev(devId) if devKey != None: heating.GetSchedule(devKey)