def getlivedata(): livedata = dbselect( '''SELECT timestamp, light, temp, humidity FROM general WHERE name = "livedata"''', fetchall=False) last30 = dbselect( '''SELECT light, temp, humidity FROM data ORDER BY id DESC LIMIT 12''', fetchall=True) t = [] h = [] for each in last30: t.append(each[1]) h.append(each[2]) tavg = float_trunc_1dec(sum(t) / len(t)) havg = float_trunc_1dec(sum(h) / len(h)) ttrend = float_trunc_1dec(livedata[2] - tavg) htrend = float_trunc_1dec(livedata[3] - havg) if ttrend > 0: ttrend = f'+{ttrend}' if htrend > 0: htrend = f'+{htrend}' return { 'timestamp': livedata[0], 'light': livedata[1], 'temp': livedata[2], 'humidity': livedata[3], 'ttrend': ttrend, 'htrend': htrend }
def check(self): try: led.cyan() hum, tmp = Adafruit_DHT.read_retry(self.sensor, self.pin) except: log.error(f'Error polling temp/humidity sensor') led.red() return (0, 0) else: log.debug( f'Temp Values Recieved: {float_trunc_1dec(tmp)}C {float_trunc_1dec(c2f(tmp))}F {self.humidity}%' ) if hum is not None and tmp is not None: if self.units == 'C': self.temp = float_trunc_1dec(tmp) elif self.units == 'F': self.temp = float_trunc_1dec(c2f(tmp)) else: log.error( f'Invalid temp units in config file {self.units}') self.humidity = float_trunc_1dec(hum) # print(self.temp, self.humidity) log.debug( f'Tempurature={self.temp}*{self.units} Humidity={self.humidity}%' ) led.green() return (self.temp, self.humidity) else: log.warning(f'Failed getting temp/humidity sensor reading') led.red() return (0, 0)
def getdata(): db = sqlite3.connect(dbfile) cursor = db.cursor() cursor.execute( '''SELECT timestamp, light, temp, humidity FROM general WHERE name = "livedata"''' ) livedata = cursor.fetchone() cursor.execute( '''SELECT timestamp, value, type FROM alarms ORDER BY id DESC LIMIT 1''' ) alarmdata = cursor.fetchone() cursor.execute( '''SELECT light, temp, humidity FROM data ORDER BY id DESC LIMIT 12''') last30 = cursor.fetchall() cursor.execute('''SELECT temp FROM general WHERE name = "lighthours"''') lighthours = cursor.fetchone() db.close() lp = [] t = [] h = [] for each in last30: lp.append(each[0]) t.append(each[1]) h.append(each[2]) try: lavg = int(sum(lp) / len(lp)) tavg = float_trunc_1dec(sum(t) / len(t)) havg = float_trunc_1dec(sum(h) / len(h)) except: lavg = 1 tavg = 1 havg = 1 if livedata[1] is not None: b = 0 + (100 - 0) * ((livedata[1] - 100000) / (300 - 100000)) if int(b) < 0: light2 = 0 elif int(b) > 100: light2 = 100 else: light2 = int(b) else: light2 = 'N/A' if alarmdata is not None: hasalarms = True else: hasalarms = False ttrend = float_trunc_1dec(livedata[2] - tavg) htrend = float_trunc_1dec(livedata[3] - havg) if ttrend > 0: ttrend = f'+{ttrend}' if htrend > 0: htrend = f'+{htrend}' resp = { 'timestamp': livedata[0], 'darkness': f'{livedata[1]}', 'lightscale': light2, 'tempc': livedata[2], 'tempf': f2c(livedata[2]), 'humidity': livedata[3], 'lighthours': lighthours[0], 'lightavg': lavg, 'tempavg': tavg, 'humidityavg': havg, 'temptrend': ttrend, 'humiditytrend': htrend, 'hasalarms': hasalarms, 'alarms': alarmdata } return jsonify(resp)
def index(): db = sqlite3.connect(dbfile) cursor = db.cursor() cursor.execute( '''SELECT timestamp, light, temp, humidity FROM general WHERE name = "livedata"''' ) livedata = cursor.fetchone() cursor.execute( '''SELECT timestamp, value, type FROM alarms ORDER BY id DESC LIMIT 10''' ) alarmdata = cursor.fetchall() cursor.execute( '''SELECT timestamp, light, temp, humidity FROM general WHERE name = "laston" LIMIT 1''' ) laston = cursor.fetchone() cursor.execute( '''SELECT timestamp, light, temp, humidity FROM general WHERE name = "lastoff" LIMIT 1''' ) lastoff = cursor.fetchone() cursor.execute( '''SELECT light, temp, humidity FROM data ORDER BY id DESC LIMIT 12''') last30 = cursor.fetchall() cursor.execute('''SELECT temp FROM general WHERE name = "lighthours"''') lighthours = cursor.fetchone() db.close() astdata.update() lp = [] t = [] h = [] for each in last30: lp.append(each[0]) t.append(each[1]) h.append(each[2]) lavg = int(sum(lp) / len(lp)) tavg = float_trunc_1dec(sum(t) / len(t)) havg = float_trunc_1dec(sum(h) / len(h)) if livedata is not None: log.debug(f'Livedata: {livedata}') b = 0 + (100 - 0) * ((livedata[1] - 100000) / (300 - 100000)) if int(b) < 0: light2 = 0 elif int(b) > 100: light2 = 100 else: light2 = int(b) else: light2 = 'N/A' td = astdata.nextphase[1] - datetime.now().date() tr = astdata.moondata["Full Moon"] - datetime.now().date() if len(alarmdata) > 0: hasalarms = True else: hasalarms = False ttrend = float_trunc_1dec(livedata[2] - tavg) htrend = float_trunc_1dec(livedata[3] - havg) if ttrend > 0: ttrend = f'+{ttrend}' if htrend > 0: htrend = f'+{htrend}' if livedata[1] > DARK_THRESHOLD: lightstring = f'All Lights are OFF' elif livedata[1] > HIDLIGHT_THRESHOLD: lightstring = f'All Lights are ON' else: lightstring = f'Secondary Lights are ON' return render_template('index.html', timestamp=livedata[0], light=f'{livedata[1]:,d}', light2=light2, temp=livedata[2], temp2=f2c(livedata[2]), humidity=livedata[3], laston=laston, lastoff=lastoff, lighthours=lighthours[0], currentmoon=astdata.currentphase, nextmoon=astdata.nextphase, moondata=astdata.moondata, npd=td.days, fmd=tr.days, lavg=lavg, tavg=tavg, havg=havg, ttrend=ttrend, htrend=htrend, wifi_info=get_wifi_info(), hasalarms=hasalarms, alarms=alarmdata, lightstring=lightstring)
def cpu_temp(): tempu = popen("vcgencmd measure_temp").readline() tempu = tempu.replace("temp=", "") tempu = tempu.replace("'C", "") return float_trunc_1dec(float(tempu))