From afa654af0a2d2e6b34b47af52ed214977b7a3e12 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 13:56:44 +0200 Subject: Refactor. Move. --- parrot_zik/interface/base.py | 2 +- parrot_zik/interface/version1.py | 2 +- parrot_zik/interface/version2.py | 6 +- parrot_zik/model/__init__.py | 0 parrot_zik/model/base.py | 58 ++++++++++ parrot_zik/model/version1.py | 48 +++++++++ parrot_zik/model/version2.py | 118 +++++++++++++++++++++ parrot_zik/parrot_zik_model.py | 222 --------------------------------------- 8 files changed, 229 insertions(+), 227 deletions(-) create mode 100644 parrot_zik/model/__init__.py create mode 100644 parrot_zik/model/base.py create mode 100644 parrot_zik/model/version1.py create mode 100644 parrot_zik/model/version2.py delete mode 100644 parrot_zik/parrot_zik_model.py (limited to 'parrot_zik') diff --git a/parrot_zik/interface/base.py b/parrot_zik/interface/base.py index 9ddf47a..1611874 100644 --- a/parrot_zik/interface/base.py +++ b/parrot_zik/interface/base.py @@ -1,6 +1,6 @@ from .. import resource_manager from ..indicator import MenuItem -from ..parrot_zik_model import BatteryStates +from ..model.base import BatteryStates RECONNECT_FREQUENCY = 5000 diff --git a/parrot_zik/interface/version1.py b/parrot_zik/interface/version1.py index 5a00441..1af71ec 100644 --- a/parrot_zik/interface/version1.py +++ b/parrot_zik/interface/version1.py @@ -1,7 +1,7 @@ from .. import resource_manager from ..indicator import MenuItem from ..interface.base import ParrotZikBaseInterface -from ..parrot_zik_model import ParrotZikVersion1 +from ..model.version1 import ParrotZikVersion1 class ParrotZikVersion1Interface(ParrotZikBaseInterface): diff --git a/parrot_zik/interface/version2.py b/parrot_zik/interface/version2.py index fc4af21..ff21004 100644 --- a/parrot_zik/interface/version2.py +++ b/parrot_zik/interface/version2.py @@ -3,9 +3,9 @@ import functools from .. import resource_manager from ..indicator import MenuItem, Menu from .base import ParrotZikBaseInterface -from ..parrot_zik_model import ParrotZikVersion2 -from ..parrot_zik_model import NoiseControlTypes -from ..parrot_zik_model import Rooms +from ..model.version2 import ParrotZikVersion2 +from ..model.version2 import NoiseControlTypes +from ..model.version2 import Rooms class ParrotZikVersion2Interface(ParrotZikBaseInterface): diff --git a/parrot_zik/model/__init__.py b/parrot_zik/model/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/parrot_zik/model/base.py b/parrot_zik/model/base.py new file mode 100644 index 0000000..0cd4652 --- /dev/null +++ b/parrot_zik/model/base.py @@ -0,0 +1,58 @@ +class ParrotZikBase(object): + def __init__(self, resource_manager): + self.resource_manager = resource_manager + + @property + def version(self): + return self.resource_manager.api_version + + def refresh_battery(self): + self.resource_manager.fetch('/api/system/battery') + + @property + def battery_state(self): + answer = self.resource_manager.get("/api/system/battery") + return answer.system.battery["state"] + + def get_battery_level(self, field_name): + answer = self.resource_manager.get("/api/system/battery") + return int(answer.system.battery[field_name]) + + @property + def friendly_name(self): + answer = self.resource_manager.get("/api/bluetooth/friendlyname") + return answer.bluetooth["friendlyname"] + + @property + def auto_connect(self): + answer = self.resource_manager.get("/api/system/auto_connection/enabled") + return self._result_to_bool( + answer.system.auto_connection["enabled"]) + + @auto_connect.setter + def auto_connect(self, arg): + self.resource_manager.set("/api/system/auto_connection/enabled", arg) + + @property + def anc_phone_mode(self): + answer = self.resource_manager.get("/api/system/anc_phone_mode/enabled") + return self._result_to_bool( + answer.system.anc_phone_mode["enabled"]) + + def _result_to_bool(self, result): + if result == "true": + return True + elif result == "false": + return False + else: + raise AssertionError(result) + +class BatteryStates: + CHARGED = 'charged' + IN_USE = 'in_use' + CHARGING = 'charging' + representation = { + CHARGED: 'Charged', + IN_USE: 'In Use', + CHARGING: 'Charging', + } diff --git a/parrot_zik/model/version1.py b/parrot_zik/model/version1.py new file mode 100644 index 0000000..b39684f --- /dev/null +++ b/parrot_zik/model/version1.py @@ -0,0 +1,48 @@ +from .base import ParrotZikBase +from ..resource_manager import Version1ResourceManager + + +class ParrotZikVersion1(ParrotZikBase): + def __init__(self, resource_manager): + super(ParrotZikVersion1, self).__init__( + resource_manager.get_resource_manager( + Version1ResourceManager)) + + @property + def version(self): + answer = self.resource_manager.get('/api/software/version') + return answer.software['version'] + + @property + def battery_level(self): + return int(self.get_battery_level('level')) + + @property + def lou_reed_mode(self): + answer = self.resource_manager.get("/api/audio/specific_mode/enabled") + return self._result_to_bool( + answer.audio.specific_mode["enabled"]) + + @lou_reed_mode.setter + def lou_reed_mode(self, arg): + self.resource_manager.get("/api/audio/specific_mode/enabled", arg) + + @property + def concert_hall(self): + answer = self.resource_manager.get("/api/audio/sound_effect/enabled") + return self._result_to_bool( + answer.audio.sound_effect["enabled"]) + + @concert_hall.setter + def concert_hall(self, arg): + self.resource_manager.get("/api/audio/sound_effect/enabled", arg) + + @property + def cancel_noise(self): + answer = self.resource_manager.get("/api/audio/noise_cancellation/enabled") + return self._result_to_bool( + answer.audio.noise_cancellation["enabled"]) + + @cancel_noise.setter + def cancel_noise(self, arg): + self.resource_manager.set("/api/audio/noise_cancellation/enabled", arg) \ No newline at end of file diff --git a/parrot_zik/model/version2.py b/parrot_zik/model/version2.py new file mode 100644 index 0000000..9ef6267 --- /dev/null +++ b/parrot_zik/model/version2.py @@ -0,0 +1,118 @@ +from .base import ParrotZikBase +from ..resource_manager import Version2ResourceManager + + +class ParrotZikVersion2(ParrotZikBase): + def __init__(self, resource_manager): + super(ParrotZikVersion2, self).__init__( + resource_manager.get_resource_manager( + Version2ResourceManager)) + + @property + def version(self): + answer = self.resource_manager.get('/api/software/version') + return answer.software['sip6'] + + @property + def battery_level(self): + return self.get_battery_level('percent') + + @property + def flight_mode(self): + answer = self.resource_manager.get('/api/flight_mode') + return self._result_to_bool(answer.flight_mode['enabled']) + + @flight_mode.setter + def flight_mode(self, arg): + if arg: + self.resource_manager.toggle_on('/api/flight_mode') + else: + self.resource_manager.toggle_off('/api/flight_mode') + + @property + def sound_effect(self): + answer = self.resource_manager.get('/api/audio/sound_effect/enabled') + return self._result_to_bool(answer.audio.sound_effect['enabled']) + + @sound_effect.setter + def sound_effect(self, arg): + self.resource_manager.set('/api/audio/sound_effect/enabled', arg) + + @property + def room(self): + answer = self.resource_manager.get('/api/audio/sound_effect/room_size') + return answer.audio.sound_effect['room_size'] + + @room.setter + def room(self, arg): + self.resource_manager.set('/api/audio/sound_effect/room_size', arg) + + @property + def external_noise(self): + answer = self.resource_manager.get('/api/audio/noise') + return int(answer.audio.noise['external']) + + @property + def internal_noise(self): + answer = self.resource_manager.get('/api/audio/noise') + return int(answer.audio.noise['internal']) + + @property + def angle(self): + answer = self.resource_manager.get('/api/audio/sound_effect/angle') + return int(answer.audio.sound_effect['angle']) + + @angle.setter + def angle(self, arg): + self.resource_manager.set('/api/audio/sound_effect/angle', arg) + + @property + def noise_control(self): + answer = self.resource_manager.get('/api/audio/noise_control') + return NoiseControl.from_noise_control(answer.audio.noise_control) + + @noise_control.setter + def noise_control(self, arg): + pass + + @property + def noise_control_enabled(self): + answer = self.resource_manager.get('/api/audio/noise_control/enabled') + return self._result_to_bool(answer.audio.noise_control['enabled']) + + +class NoiseControl(object): + def __init__(self, type, value): + self.type = type + self.value = value + + @classmethod + def from_noise_control(cls, noise_control): + return cls(noise_control['type'], int(noise_control['value'])) + + def __eq__(self, other): + return self.type == other.type and self.value == other.value + + def __str__(self): + return '{}++{}'.format(self.type, self.value) + + +class NoiseControlTypes: + NOISE_CONTROL_MAX = NoiseControl('anc', 2) + NOISE_CONTROL_ON = NoiseControl('anc', 1) + NOISE_CONTROL_OFF = NoiseControl('off', 1) + STREET_MODE = NoiseControl('aoc', 1) + STREET_MODE_MAX = NoiseControl('aoc', 2) + + +class Rooms: + CONCERT_HALL = 'concert' + JAZZ_CLUB = 'jazz' + LIVING_ROOM = 'living' + SILENT_ROOM = 'silent' + representation = { + CONCERT_HALL: 'Concert Hall', + JAZZ_CLUB: 'Jazz Club', + LIVING_ROOM: 'Living Room', + SILENT_ROOM: 'Silent Room', + } \ No newline at end of file diff --git a/parrot_zik/parrot_zik_model.py b/parrot_zik/parrot_zik_model.py deleted file mode 100644 index e6a8bc7..0000000 --- a/parrot_zik/parrot_zik_model.py +++ /dev/null @@ -1,222 +0,0 @@ -from .resource_manager import Version1ResourceManager -from .resource_manager import Version2ResourceManager - - -class BatteryStates: - CHARGED = 'charged' - IN_USE = 'in_use' - CHARGING = 'charging' - representation = { - CHARGED: 'Charged', - IN_USE: 'In Use', - CHARGING: 'Charging', - } - -class Rooms: - CONCERT_HALL = 'concert' - JAZZ_CLUB = 'jazz' - LIVING_ROOM = 'living' - SILENT_ROOM = 'silent' - representation = { - CONCERT_HALL: 'Concert Hall', - JAZZ_CLUB: 'Jazz Club', - LIVING_ROOM: 'Living Room', - SILENT_ROOM: 'Silent Room', - } - -class NoiseControl(object): - def __init__(self, type, value): - self.type = type - self.value = value - - @classmethod - def from_noise_control(cls, noise_control): - return cls(noise_control['type'], int(noise_control['value'])) - - def __eq__(self, other): - return self.type == other.type and self.value == other.value - - def __str__(self): - return '{}++{}'.format(self.type, self.value) - -class NoiseControlTypes: - NOISE_CONTROL_MAX = NoiseControl('anc', 2) - NOISE_CONTROL_ON = NoiseControl('anc', 1) - NOISE_CONTROL_OFF = NoiseControl('off', 1) - STREET_MODE = NoiseControl('aoc', 1) - STREET_MODE_MAX = NoiseControl('aoc', 2) - - -class ParrotZikBase(object): - def __init__(self, resource_manager): - self.resource_manager = resource_manager - - @property - def version(self): - return self.resource_manager.api_version - - def refresh_battery(self): - self.resource_manager.fetch('/api/system/battery') - - @property - def battery_state(self): - answer = self.resource_manager.get("/api/system/battery") - return answer.system.battery["state"] - - def get_battery_level(self, field_name): - answer = self.resource_manager.get("/api/system/battery") - return int(answer.system.battery[field_name]) - - @property - def friendly_name(self): - answer = self.resource_manager.get("/api/bluetooth/friendlyname") - return answer.bluetooth["friendlyname"] - - @property - def auto_connect(self): - answer = self.resource_manager.get("/api/system/auto_connection/enabled") - return self._result_to_bool( - answer.system.auto_connection["enabled"]) - - @auto_connect.setter - def auto_connect(self, arg): - self.resource_manager.set("/api/system/auto_connection/enabled", arg) - - @property - def anc_phone_mode(self): - answer = self.resource_manager.get("/api/system/anc_phone_mode/enabled") - return self._result_to_bool( - answer.system.anc_phone_mode["enabled"]) - - def _result_to_bool(self, result): - if result == "true": - return True - elif result == "false": - return False - else: - raise AssertionError(result) - - -class ParrotZikVersion1(ParrotZikBase): - def __init__(self, resource_manager): - super(ParrotZikVersion1, self).__init__( - resource_manager.get_resource_manager( - Version1ResourceManager)) - - @property - def version(self): - answer = self.resource_manager.get('/api/software/version') - return answer.software['version'] - - @property - def battery_level(self): - return int(self.get_battery_level('level')) - - @property - def lou_reed_mode(self): - answer = self.resource_manager.get("/api/audio/specific_mode/enabled") - return self._result_to_bool( - answer.audio.specific_mode["enabled"]) - - @lou_reed_mode.setter - def lou_reed_mode(self, arg): - self.resource_manager.get("/api/audio/specific_mode/enabled", arg) - - @property - def concert_hall(self): - answer = self.resource_manager.get("/api/audio/sound_effect/enabled") - return self._result_to_bool( - answer.audio.sound_effect["enabled"]) - - @concert_hall.setter - def concert_hall(self, arg): - self.resource_manager.get("/api/audio/sound_effect/enabled", arg) - - @property - def cancel_noise(self): - answer = self.resource_manager.get("/api/audio/noise_cancellation/enabled") - return self._result_to_bool( - answer.audio.noise_cancellation["enabled"]) - - @cancel_noise.setter - def cancel_noise(self, arg): - self.resource_manager.set("/api/audio/noise_cancellation/enabled", arg) - - -class ParrotZikVersion2(ParrotZikBase): - def __init__(self, resource_manager): - super(ParrotZikVersion2, self).__init__( - resource_manager.get_resource_manager( - Version2ResourceManager)) - - @property - def version(self): - answer = self.resource_manager.get('/api/software/version') - return answer.software['sip6'] - - @property - def battery_level(self): - return self.get_battery_level('percent') - - @property - def flight_mode(self): - answer = self.resource_manager.get('/api/flight_mode') - return self._result_to_bool(answer.flight_mode['enabled']) - - @flight_mode.setter - def flight_mode(self, arg): - if arg: - self.resource_manager.toggle_on('/api/flight_mode') - else: - self.resource_manager.toggle_off('/api/flight_mode') - - @property - def sound_effect(self): - answer = self.resource_manager.get('/api/audio/sound_effect/enabled') - return self._result_to_bool(answer.audio.sound_effect['enabled']) - - @sound_effect.setter - def sound_effect(self, arg): - self.resource_manager.set('/api/audio/sound_effect/enabled', arg) - - @property - def room(self): - answer = self.resource_manager.get('/api/audio/sound_effect/room_size') - return answer.audio.sound_effect['room_size'] - - @room.setter - def room(self, arg): - self.resource_manager.set('/api/audio/sound_effect/room_size', arg) - - @property - def external_noise(self): - answer = self.resource_manager.get('/api/audio/noise') - return int(answer.audio.noise['external']) - - @property - def internal_noise(self): - answer = self.resource_manager.get('/api/audio/noise') - return int(answer.audio.noise['internal']) - - @property - def angle(self): - answer = self.resource_manager.get('/api/audio/sound_effect/angle') - return int(answer.audio.sound_effect['angle']) - - @angle.setter - def angle(self, arg): - self.resource_manager.set('/api/audio/sound_effect/angle', arg) - - @property - def noise_control(self): - answer = self.resource_manager.get('/api/audio/noise_control') - return NoiseControl.from_noise_control(answer.audio.noise_control) - - @noise_control.setter - def noise_control(self, arg): - pass - - @property - def noise_control_enabled(self): - answer = self.resource_manager.get('/api/audio/noise_control/enabled') - return self._result_to_bool(answer.audio.noise_control['enabled']) -- cgit v1.2.1