1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import
6
7
8class EmulatorBattery(object):
9
10    def __init__(self, emulator):
11        self.emulator = emulator
12
13    def get_state(self):
14        status = {}
15        state = {}
16
17        response = self.emulator._run_telnet('power display')
18        for line in response:
19            if ':' in line:
20                field, value = line.split(':')
21                value = value.strip()
22                if value == 'true':
23                    value = True
24                elif value == 'false':
25                    value = False
26                elif field == 'capacity':
27                    value = float(value)
28                status[field] = value
29
30        state['level'] = status.get('capacity', 0.0) / 100
31        if status.get('AC') == 'online':
32            state['charging'] = True
33        else:
34            state['charging'] = False
35
36        return state
37
38    def get_charging(self):
39        return self.get_state()['charging']
40
41    def get_level(self):
42        return self.get_state()['level']
43
44    def set_level(self, level):
45        self.emulator._run_telnet('power capacity %d' % (level * 100))
46
47    def set_charging(self, charging):
48        if charging:
49            cmd = 'power ac on'
50        else:
51            cmd = 'power ac off'
52        self.emulator._run_telnet(cmd)
53
54    charging = property(get_charging, set_charging)
55    level = property(get_level, set_level)
56