aboutsummaryrefslogtreecommitdiff
path: root/ParrotZikTray
blob: 934062c0703bfff2b0b99e03660ae5af449992a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python

import ParrotZik
import BluetoothPairedDevices
from SysIndicator import *

UPDATE_FREQUENCY = 1000
RECONNECT_FREQUENCY = 5000

class repeat(object):
    def __init__(self, f):
        self.f = f
        self.id = None

    def start(self, cls, frequency):
        if not self.id:
            def run():
                self.f(cls)
                return True

            self.id = gtk.timeout_add(frequency, run)

    def stop(self):
        if self.id:
            gtk.timeout_remove(self.id)
            self.id = None


class Stop(Exception):
    pass


class ParrotZikIndicator(SysIndicator):
    def __init__(self):
 
        self.menu = UniversalMenu()

        self.info_item = MenuItem("Parrot Zik Not connected..",
                                  None, sensitive=False)
        self.menu.append(self.info_item)

        self.version_1_interface = ParrotZikVersion1Interface(self)
        self.quit = MenuItem("Quit", gtk.main_quit, checkitem=True)
        self.menu.append(self.quit)

        SysIndicator.__init__(self, icon="zik-audio-headset", menu=self.menu)

        self.active_interface = None

    @repeat
    def EstablishConnection(self):
        if self.active_interface:
            if not self.active_interface.parrot.sock:
                print "Lost connection"
                self.active_interface.deactivate()
            else:
                self.EstablishConnection.stop()
        else:
            mac = BluetoothPairedDevices.ParrotZikMac()
            if mac:
                parrot = ParrotZik.ParrotZik(mac)
                if parrot.sock:
                    self.version_1_interface.activate(parrot)
                    self.CheckBattery.start(self, UPDATE_FREQUENCY)
                    self.EstablishConnection.stop()
                    print "Connected"
                else:
                    print "Failed to connect to Parrot Zik %s" % mac

    @repeat
    def CheckBattery(self):
        if self.active_interface:
            self.active_interface.CheckBattery()
        else:
            self.EstablishConnection.start(self, RECONNECT_FREQUENCY)
            self.CheckBattery.stop()

    def main(self):
        self.EstablishConnection.start(self, RECONNECT_FREQUENCY)
        SysIndicator.main(self)

class ParrotZikVersion1Interface:
    def __init__(self, indicator):
        self.indicator = indicator
        self.battery_level = MenuItem("Battery Level:", None, sensitive=False,
                                      visible=False)
        self.battery_state = MenuItem("Battery State:", None, sensitive=False,
                                      visible=False)
        self.firmware_version = MenuItem("Firmware Version:", None,
                                         sensitive=False, visible=False)
        self.check = MenuItem("Noise Cancellation", self.toggleANC,
                              checkitem=True, visible=False)
        self.check2 = MenuItem("Auto Connection", self.toggleAuto,
                               checkitem=True, visible=False)
        self.check3 = MenuItem("Lou Reed Mode", self.toggleLouReedMode,
                               checkitem=True, visible=False)
        self.check4 = MenuItem(
            "Concert Hall Mode", self.toggleParrotConcertHall,
            checkitem=True, visible=False)
        self.indicator.menu.append(self.battery_level)
        self.indicator.menu.append(self.battery_state)
        self.indicator.menu.append(self.firmware_version)
        self.indicator.menu.append(self.check)
        self.indicator.menu.append(self.check2)
        self.indicator.menu.append(self.check3)
        self.indicator.menu.append(self.check4)

    @property
    def connected(self):
        if self.parrot:
            return self.parrot.sock
        else:
            return False

    def activate(self, parrot):
        self.parrot = parrot
        self.battery_level.show()
        self.battery_state.show()
        self.firmware_version.show()
        self.check.show()
        self.check2.show()
        self.check3.show()
        self.check4.show()

        self.name = self.parrot.friendly_name
        self.version = self.parrot.version

        if self.parrot.noise_cancel == "true":
            self.check.set_active(True)
        else:
            self.check.set_active(False)

        if self.parrot.auto_connect == "true":
            self.check2.set_active(True)
        else:
            self.check2.set_active(False)

        if self.parrot.lou_reed_mode == "true":
            self.check3.set_active(True)
        else:
            self.check3.set_active(False)

        if self.parrot.concert_hall == "true":
            self.check4.set_active(True)
        else:
            self.check4.set_active(False)

        self.indicator.menu.reposition()
        self.indicator.active_interface = self

    def deactivate(self):
        self.parrot = None
        self.battery_level.hide()
        self.battery_state.hide()
        self.firmware_version.hide()
        self.check.hide()
        self.check2.hide()
        self.check3.hide()
        self.check4.hide()
        self.indicator.setIcon("zik-audio-headset")
        self.indicator.info_item.set_label("Parrot Zik Not connected..")
        self.indicator.menu.reposition()
        self.indicator.active_interface = None

    def toggleANC(self, widget):
        if self.connected:
            if self.check.get_active():
                self.parrot.noise_cancel = "true"
            else:
                self.parrot.noise_cancel = "false"

    def toggleAuto(self, widget):
        if self.connected:
            if self.check2.get_active():
                self.parrot.auto_connection = "true"
            else:
                self.parrot.auto_connection = "false"

    def toggleLouReedMode(self, widget):
        if self.connected:
            if self.check3.get_active():
                self.parrot.lou_reed_mode = "true"
                self.check4.set_sensitive(False)
            else:
                self.parrot.lou_reed_mode = "false"
                self.check4.set_sensitive(True)

    def toggleParrotConcertHall(self, widget):
        if self.connected:
            if self.check4.get_active():
                self.parrot.concert_hall = "true"
            else:
                self.parrot.concert_hall = "false"

    def CheckBattery(self):
        if self.connected:
            print "Updating battery"
            self.batteryLevel = int(self.parrot.battery_level)

            if self.parrot.BatteryCharging:
                self.batteryLevel = "Charging"
                self.indicator.setIcon("zik-battery-charging")
                self.batteryLevel = "Unknown"
                self.batteryState = "Charging"
            elif self.batteryLevel > 80:
                self.indicator.setIcon("zik-battery-100")
                self.batteryState = "In Use"
            elif self.batteryLevel > 60:
                self.indicator.setIcon("zik-battery-080")
                self.batteryState = "In Use"
            elif self.batteryLevel > 40:
                self.indicator.setIcon("zik-battery-060")
                self.batteryState = "In Use"
            elif self.batteryLevel > 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_level.set_label(
                "Battery Level: " + str(self.batteryLevel))
        else:
            self.deactivate()

if __name__ == "__main__":
    indicator = ParrotZikIndicator()
    indicator.main()