def monitor_loop(self): f = open(self.logdir + '/power.log', 'at') data = [0, 0, 0, 0, 0, 0, 0, 0] while True: try: data = self.bus.read_i2c_block_data(0x04, 0) except: if __debug__: print("Failed to read i2c data") sleep(1) self.extracted[0] = time.time() for i in range(0, 8): bytes = data[4 * i:4 * i + 4] self.extracted[i + 1] = struct.unpack('f', "".join(map(chr, bytes)))[0] if __debug__: print("Writing csv row") writer = csv.writer(f) writer.writerow(self.extracted) f.flush() sleep(self.interval) # If telegram messaging is active, do a few checks and notify if self.telegram: if __debug__: print("Telegram enabled") if (self.extracted[5] != 0) and (self.extracted[5] < 21) and not self.lowbat: if __debug__: print("Battery low") telegram.send("Battery below 21V") self.lowbat = True f.close()
def sendstatus(): """GET to send system status via telegram""" message = "" if request.method == 'GET': if "telegram" in modules: telegram.send(system_status()) message = "Sent" else: message = "Telegram module not configured" return message
def shutdown(): """GET to shutdown Raspberry Pi""" logging.info("****** Shutting down ******") if "telegram" in modules: telegram.send("Night night...") if request.method == 'GET': os.system('shutdown now -h') s = open("controllers/.shutdown", "w+") s.write(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')) s.flush() s.close() return "Shutting down"
def joystick_change(stick): """GET to change joystick to <stick> """ logging.info("Changing joystick to " + stick) if request.method == 'GET': message = "Invalid stick" for valid in list_joysticks(): logging.info("Checking controller type is valid: " + valid) if valid == stick: message = "Valid stick. Changed to " + stick with open("controllers/.current", "w") as current_joy: current_joy.write(stick) if "telegram" in modules: telegram.send("Setting joystick to " + stick) return message
def shutdown(): """GET to shutdown Raspberry Pi""" logging.info("****** Shutting down ******") if "telegram" in modules: telegram.send("Night night...") if request.method == 'GET': os.system('shutdown now -h') s = open("controllers/.shutdown", "w+") s.write( datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S')) s.flush() s.close() return "Shutting down"
def __init__(self, address, interval): # Check for telegram config self.telegram = False self.address = address self.interval = interval self.logdir = mainconfig.mainconfig['logdir'] self.lowbat = False self.extracted = [0, 0, 0, 0, 0, 0, 0, 0, 0] try: self.bus = smbus.SMBus(int(mainconfig.mainconfig['busid'])) except: print("Failed to connect to device on bus") if __debug__: print("Monitoring....") if self.telegram: telegram.send("Monitoring started") loop = Thread(target=self.monitor_loop) loop.daemon = True loop.start()