from dynamicModule import DynamicModule
from serverHttpRequest import ServerHttpRequest
from player import Player

confPath = expanduser("~") + "/.hommyPi_conf"

config = SafeConfigParser()
config.read(confPath)

players = [
    {
        "moduleName": "spotify",
        "className": "SpotifyPlayer",
        "path": "/home/nolitsou/Documents/dev/homyPi/homyPi_raspberryApp/modules/spotify/spotify_player.py",
    }
]
modules = DynamicModule(players).load()

serverHttpRequest = ServerHttpRequest(
    config.get("Server", "url"), config.get("Server", "username"), config.get("Server", "password")
)
server = ServerRequester("serverRequest.player")
player = Player("Jonny", modules, config, serverHttpRequest, server)


resp = serverHttpRequest.get("api/modules/music/playlists/generate?generator=musicgraph&musicSource=spotify")
if "error" not in resp:
    player.playlist.set(resp["data"])
else:
    print "error"
Esempio n. 2
0
class AlarmManager:
    spotifyPlayer = None
    def __init__(self, config):
        LOGGER.info("starting alarm module")
        LOGGER.info("Getting a token")
        self.name = config.get("Server", "name")
        Alarm.name = self.name
        self.serverHttpRequest = ServerHttpRequest(config.get("Server", "url"),
                                                   config.get("Server", "username"),
                                                   config.get("Server", "password"))
        LOGGER.info("ServerHttpRequest ready")
        try:
            self.server = ServerRequester("serverRequest.player")
        except:
            LOGGER.error("module alarm crashed")
            LOGGER.error(traceback.format_exc())
        Alarm.serverRequester = self.serverHttpRequest
        self.rabbitConnectionAlarm = RabbitConsumer("module.alarm", "module.alarm")
        self.rabbitConnectionPlayer = RabbitEmitter("module.player", "module.player")
        self.notifier = RabbitEmitter("module.notifier", "module.notifier")
        Alarm.notifier = self.notifier;
        Alarm.rabbitConnectionPlayer = self.rabbitConnectionPlayer
        try:
            self.setHandlers()
            self.rabbitConnectionAlarm.onConnected(self.init)
            self.rabbitConnectionAlarm.start()
            self.rabbitConnectionPlayer.start()
            self.notifier.start();
            self.onSocketReconnect();
            while True:
                  time.sleep(0.2)
        except KeyboardInterrupt:
            print("stopping consuming")
            self.server.stop()
            self.rabbitConnectionAlarm.stop()
            self.rabbitConnectionPlayer.stop()
            self.notifier.stop();
            print("see ya later!")
            sys.exit(0)
    
    def init(self):
        LOGGER.info("Ready...")
     
    def onSocketReconnect(self):
        self.server.emit("raspberry:module:new", {
                "name": "alarm",
                "status": "PAUSED" 
            });
        LOGGER.info("Getting alarms api/modules/alarms/raspberries/" + self.name);
        res = None
        res = self.serverHttpRequest.get("api/modules/alarms/raspberries/" + self.name);
        if "status" in res and res["status"] == "success" and "data" in res and "items" in res["data"]:
            Alarm.setAlarmsFromJSON(res["data"]["items"]);
        else:
            LOGGER.info("invalid data");

    def setHandlers(self):
        LOGGER.info("set handlers")
        self.rabbitConnectionAlarm.addHandler("alarm:updated", Alarm.updateAlarmFromJSON)
        self.rabbitConnectionAlarm.addHandler("alarm:new", Alarm.addAlarmsFromJSON)
        self.rabbitConnectionAlarm.addHandler("alarm:removed", Alarm.removeByData)
        self.rabbitConnectionAlarm.addHandler("reconnected", self.onSocketReconnect)