From eb6fb90329b1bb0355e00bc588d3740c57ca87bc Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Sat, 13 Jun 2015 00:26:23 +0200 Subject: Refactor battery state. --- ParrotZik.py | 31 ++++++++++++------------------- ParrotZikTray | 32 ++++++++++++++------------------ 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/ParrotZik.py b/ParrotZik.py index 6c1cd69..c47ce26 100644 --- a/ParrotZik.py +++ b/ParrotZik.py @@ -91,12 +91,20 @@ class ParrotZikApi(object): def close(self): self.sock.close() +class BatteryStates: + CHARGED = 'charged' + IN_USE = 'in_use' + CHARGING = 'charging' + representation = { + CHARGED: 'Charged', + IN_USE: 'In Use', + CHARGING: 'Charging', + } class ParrotZikBase(object): + def __init__(self, api): self.api = api - self.BatteryLevel = 100 - self.BatteryCharging = False @property def version(self): @@ -109,22 +117,7 @@ class ParrotZikBase(object): def get_battery_level(self, field_name): data = self.api.get("/api/system/battery/get") - try: - if data.answer.system.battery[field_name] != '': - self.BatteryLevel = data.answer.system.battery[field_name] - if data.answer.system.battery["state"] == 'charging': - self.BatteryCharging = True - else: - self.BatteryCharging = False - except Exception: - pass - - try: - print "notification received" + data.notify["path"] - except Exception: - pass - - return self.BatteryLevel + return data.answer.system.battery[field_name] @property def friendly_name(self): @@ -185,7 +178,7 @@ class ParrotZikBase(object): class ParrotZikVersion1(ParrotZikBase): @property def battery_level(self): - return self.get_battery_level('level') + return int(self.get_battery_level('level')) @property def lou_reed_mode(self): diff --git a/ParrotZikTray b/ParrotZikTray index 45aaaf0..a69abe4 100755 --- a/ParrotZikTray +++ b/ParrotZikTray @@ -1,8 +1,9 @@ #!/usr/bin/env python import gtk -import ParrotZik import BluetoothPairedDevices +from ParrotZik import BatteryStates +from ParrotZik import connect from SysIndicator import MenuItem from SysIndicator import Menu from SysIndicator import SysIndicator @@ -62,7 +63,7 @@ class ParrotZikIndicator(SysIndicator): else: mac = BluetoothPairedDevices.ParrotZikMac() if mac: - parrot = ParrotZik.connect(mac) + parrot = connect(mac) if parrot.api.sock: if parrot.version.startswith('1'): self.version_1_interface.activate(parrot) @@ -145,35 +146,29 @@ class ParrotZikBaseInterface(object): def refresh(self): if self.connected: print "Updating battery" - self.batteryLevel = int(self.parrot.battery_level) + battery_level = self.parrot.battery_level + battery_state = self.parrot.battery_state - if self.parrot.BatteryCharging: - self.batteryLevel = "Charging" + if battery_state == BatteryStates.CHARGING: self.indicator.setIcon("zik-battery-charging") - self.batteryLevel = "Unknown" - self.batteryState = "Charging" - elif self.batteryLevel > 80: + elif battery_level > 80: self.indicator.setIcon("zik-battery-100") - self.batteryState = "In Use" - elif self.batteryLevel > 60: + elif battery_level > 60: self.indicator.setIcon("zik-battery-080") - self.batteryState = "In Use" - elif self.batteryLevel > 40: + elif battery_level > 40: self.indicator.setIcon("zik-battery-060") - self.batteryState = "In Use" - elif self.batteryLevel > 20: + elif battery_level > 20: self.indicator.setIcon("zik-battery-040") - self.batteryState = "In Use" else: self.indicator.setIcon("zik-battery-low") - self.batteryState = "In Use" self.indicator.info_item.set_label("Connected to: " + self.name) self.firmware_version.set_label( "Firmware version: " + self.version) - self.battery_state.set_label("State: " + self.batteryState) + self.battery_state.set_label( + "State: " + BatteryStates.representation[battery_state]) self.battery_level.set_label( - "Battery Level: " + str(self.batteryLevel)) + "Battery Level: " + str(battery_level)) else: self.deactivate() @@ -294,6 +289,7 @@ class ParrotZikVersion2Interface(ParrotZikBaseInterface): def toggledummy(self, widget): print(widget.get_name()) + if __name__ == "__main__": indicator = ParrotZikIndicator() indicator.main() -- cgit v1.2.1