#
# Todos:
# - load stored json file at startup, fetch from server if missing
# - build menu from json
# - Keychain integration
#
import rumps
import control
import config
import json

class Harmenubar(rumps.App):
    def __init__(self):
        super(Harmenubar,self).__init__('Harmenubar',icon='resources/icon.png')
        self.activity = 5337127
        self.auth()

        cfg = self.get_and_store_config()

        self.cfg = config.HarmonyConfig(cfg)
        self.activities = self.cfg.get_activities()
        self.devices = self.cfg.get_devices()
        activity_menu = self.build_activity_menu()
        device_menu = self.build_device_menu()

        self.menu = [
            rumps.MenuItem('Current Activity:'),
            None,
            rumps.MenuItem('Power Off', callback=lambda m: self.set_activity(m,-1)),
            {'Activity': activity_menu},
            {'Device': device_menu},
        ]
        self.update_current_activity(self.activity)

    def auth(self):
        print 'Logging in to Logitech'
        self.session_token = control.login_to_logitech('adreg@megalan.org', 'ankeborg', '192.168.0.158')
        print 'Connecting client'
        client = control.get_client('192.168.0.158', self.session_token)
        print 'Getting current activity'
        self.activity = client.get_current_activity() # 5337127
        client.disconnect(send_close=True)

    def get_and_store_config(self):
        client = control.get_client('192.168.0.158', self.session_token)
        print 'Getting configuration'
        cfg = client.get_config()
        client.disconnect(send_close=True)
        with self.open('config.json','w') as f:
            f.write(json.dumps(cfg))
        return cfg

    def build_activity_menu(self):
        menu = []
        for key,value in self.activities.iteritems():
            if key != -1:
                menu.append(rumps.MenuItem(value, 
                                           callback=lambda m,i=key: self.set_activity(m,i)))
        return menu

    def build_device_menu(self):
        menu = []
        for key,value in self.devices.iteritems():
            if key != -1:
                menu.append(rumps.MenuItem(value))
        return menu

    def set_activity(self, menu,id_):
        print 'Setting activity:', menu,id_
        client = control.get_client('192.168.0.158', self.session_token)
        print 'call start_activity'
        client.start_activity(id_)
        client.disconnect(send_close=True)
        self.update_current_activity(id_)

    def update_current_activity(self, activity):
        self.menu['Current Activity:'].title = 'Current Activity: ' + self.activities[activity]
        self.activity = activity

if __name__ == '__main__':
    Harmenubar().run()

@@ -12,120 +12,66 @@
 # - 
-load stored json file at startup, fetch from server if missing%0A# - build menu from json%0A# - Keychain integration
+Keychain integration%0A# - move out activities to main menu?
 %0A#%0Ai
@@ -306,24 +306,160 @@
 elf.auth()%0A%0A
+        try:%0A            with self.open('config.json','r') as f:%0A                cfg = json.loads(f.read())%0A        except IOError:%0A    
         cfg 
@@ -488,16 +488,16 @@
 onfig()%0A
-
 %0A       
@@ -635,106 +635,8 @@
 es()
-%0A        activity_menu = self.build_activity_menu()%0A        device_menu = self.build_device_menu()
 %0A%0A  
@@ -832,16 +832,27 @@
 ivity': 
+self.build_
 activity
@@ -852,24 +852,26 @@
 ctivity_menu
+()
 %7D,%0A         
@@ -884,16 +884,27 @@
 evice': 
+self.build_
 device_m
@@ -906,16 +906,18 @@
 ice_menu
+()
 %7D,%0A     
@@ -1031,16 +1031,18 @@
 Logitech
+..
 '%0A      
@@ -1172,16 +1172,18 @@
 g client
+..
 '%0A      
@@ -1267,20 +1267,16 @@
 int 'Get
-ting
  current
@@ -1276,32 +1276,34 @@
 current activity
+..
 '%0A        self.a
@@ -1345,18 +1345,8 @@
 ty()
- # 5337127
 %0A   
@@ -1513,20 +1513,16 @@
 int 'Get
-ting
  configu
@@ -1527,16 +1527,18 @@
 guration
+..
 '%0A      

Read configuration from file on startup, minor refactoring