def current(self): c = SmapClient() latest = c.latest(self.select, streamlimit=1000) test = self.get_test() levels = [] for v in latest: if len(v['Readings']): level = test(v['Readings'][0][1])[0] v['level'] = { "priority": level.priority, "description": level.description, } levels.append(level) return latest, max(levels)
class Alert(SmapDriver): def setup(self, opts): url = opts.get('url', 'http://new.openbms.org/backend') self.client = SmapClient(url) self.limit = float(opts.get('limit', 300)) # Seconds self.rate = float(opts.get('rate', 300)) self.alert_interval = float(opts.get( 'alert_interval', 86400)) # Minimum seconds between alerts smtp_server = opts.get('smtp_server') self.server = smtplib.SMTP(smtp_server, 587) self.server.starttls() self.email = opts.get('email_address') pw = opts.get('password') self.server.login(self.email, pw) self.restrict = opts.get('restrict') self.recipient = opts.get('recipient') self.carrier = opts.get('carrier') def start(self): self.process = periodicSequentialCall(self.read) self.process.start(self.rate) def read(self): data = self.client.latest(self.restrict, streamlimit=-1) for d in data: uuid = d["uuid"] latest = d["Readings"][0][0] / 1000 now = time.time() gap = now - latest if gap > self.limit: self.alert(uuid) self.process.stop() reactor.callLater(self.alert_interval, self.start) break def alert(self, uuid): message = '\nGap of more than %s seconds in data for %s: uuid=%s' % ( self.limit, self.restrict, uuid) print message self.server.sendmail(self.email, "%s@%s" % (self.recipient, self.carrier), message) self.process.stop()
class Alert(SmapDriver): def setup(self, opts): url = opts.get('url', 'http://new.openbms.org/backend') self.client = SmapClient(url) self.limit = float(opts.get('limit', 300)) # Seconds self.rate = float(opts.get('rate', 300)) self.alert_interval = float(opts.get('alert_interval', 86400)) # Minimum seconds between alerts smtp_server = opts.get('smtp_server') self.server = smtplib.SMTP(smtp_server, 587) self.server.starttls() self.email = opts.get('email_address') pw = opts.get('password') self.server.login(self.email, pw) self.restrict = opts.get('restrict') self.recipient = opts.get('recipient') self.carrier = opts.get('carrier') def start(self): self.process = periodicSequentialCall(self.read) self.process.start(self.rate) def read(self): data = self.client.latest(self.restrict, streamlimit=-1) for d in data: uuid = d["uuid"] latest = d["Readings"][0][0] / 1000 now = time.time() gap = now - latest if gap > self.limit: self.alert(uuid) self.process.stop() reactor.callLater(self.alert_interval, self.start) break def alert(self, uuid): message = '\nGap of more than %s seconds in data for %s: uuid=%s' % (self.limit, self.restrict, uuid) print message self.server.sendmail(self.email, "%s@%s" % (self.recipient, self.carrier), message) self.process.stop()
class ZoneController(driver.SmapDriver): def setup(self, opts): self.rate = float(opts.get('rate',10)) # Current state of the points self.heatSP=int(opts.get('defaultHeatSetpoint',68)) self.coolSP=int(opts.get('defaultCoolSetpoint',76)) self.therm_temp = 70 self.trim = int(opts.get('trim',0)) # dummy zoneCtrl action # create timeseries for zone controller actions heatSetPoint = self.add_timeseries('/heatSetpoint', 'F', data_type='double') coolSetPoint = self.add_timeseries('/coolSetpoint', 'F', data_type='double') # add actuators to them heatSetPoint.add_actuator(setpointActuator(controller=self, range=(40,90))) coolSetPoint.add_actuator(setpointActuator(controller=self, range=(40,90))) # get master set point stream paths self.archiver_url = opts.get('archiver_url','http://localhost:8079') self.heatSPwhere = opts.get('heatSPwhere', '') self.coolSPwhere = opts.get('coolSPwhere', '') self.thermwhere = opts.get('thermwhere', '') self.tempwhere = opts.get('tempwhere', '') print "ZoneController: heat sp where = ", self.heatSPwhere print "ZoneController: cool sp where = ", self.coolSPwhere print "ZoneController: thermostat where = ", self.thermwhere print "ZoneController: temp sensor where = ", self.tempwhere self.client = SmapClient(self.archiver_url) self.heatSPclient = RepublishClient(self.archiver_url, self.heatSPcb, restrict=self.heatSPwhere) self.coolSPclient = RepublishClient(self.archiver_url, self.coolSPcb, restrict=self.coolSPwhere) #self.tempclient = RepublishClient(self.archiver_url, self.tempcb, restrict=self.tempwhere) self.thermclient = RepublishClient(self.archiver_url, self.thermcb, restrict=self.thermwhere) def start(self): print "zone controller start: ", self.rate self.heatSPclient.connect() # activate subscription scheduler setpoints self.coolSPclient.connect() #self.tempclient.connect() self.thermclient.connect() periodicSequentialCall(self.read).start(self.rate) def read(self): all_readings = self.client.latest(self.tempwhere) for p in all_readings: print '-'*20 md = self.client.tags('uuid = "'+p['uuid']+'"')[0] print 'Room:', md['Metadata/Room'] print 'Reading:', p['Readings'][0][1] ts = dtutil.ts2dt(p['Readings'][0][0]/1000) print 'Time:', dtutil.strftime_tz(ts, tzstr='America/Los_Angeles') avg_room_temp = sum([x['Readings'][0][1] for x in all_readings]) / float(len(all_readings)) # get difference between avg room temperature and thermostat temperature new_diff = self.therm_temp - avg_room_temp # periodically update output streams. Here a bogus adjustment self.add('/heatSetpoint', self.heatSP + new_diff) self.add('/coolSetpoint', self.coolSP + new_diff) print "zone controller publish: ", self.heatSP, self.coolSP # Event handler for publication to heatSP stream def heatSPcb(self, _, data): # list of arrays of [time, val] print "ZoneController heatSPcb: ", data mostrecent = data[-1][-1] self.heatSP = mostrecent[1] def coolSPcb(self, _, data): # list of arrays of [time, val] print "ZoneController coolSPcb: ", data mostrecent = data[-1][-1] self.coolSP = mostrecent[1] def tempcb(self, _, data): # list of arrays of [time, val] print "ZoneController tempcb: ", data def thermcb(self, _, data): # list of arrays of [time, val] print "ZoneController thermcb: ", data self.therm_temp = data[-1][-1][1]
# Prepare actual message message = """\From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: #server = smtplib.SMTP(SERVER) server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work! server.ehlo() server.starttls() server.login(gmail_user, gmail_pwd) server.sendmail(FROM, TO, message) #server.quit() server.close() print 'successfully sent the mail' except: print "failed to send mail" # make a client c = SmapClient("http://www.openbms.org/backend") while True: test = c.latest("Metadata/Extra/Type = 'oat'", 1, 10) if test[0]['Readings'][0][1] > 56: send_email("ENTER RECIPIENT EMAIL ADDRESS") time.sleep(10) # sleep for 10 sec # example code - converting int to datetime # dtutil.ts2dt(1357027200) # dtutil.now() # take date int & divide by 1000 since time is in miliseconds