Пример #1
0
class PlayerManager:
    spotifyPlayer = None
    def __init__(self, config, playersConfig):
        LOGGER.info("__init__ player")
        PlayerManager.config = config
        self.rabbitConnection = None
        self.name = config.get("Server", "name")
        self.serverHttpRequest = ServerHttpRequest(config.get("Server", "url"),
                                                   config.get("Server", "username"),
                                                   config.get("Server", "password"))
        Playlist.serverHttpRequest = self.serverHttpRequest;
        LOGGER.info("=====================+++>" + str(Playlist.serverHttpRequest))
        self.modules = DynamicModule(playersConfig).load()
        #self.players = self.moduleLoader.load()
        #name, modules, config, serverHttpRequest, serverMq):
        self.job = None;
        signal.signal(signal.SIGINT, self.stopApp)
        LOGGER.info("starting player module")
        self.server = ServerRequester("serverRequest.player")
        
        self.player = Player(self.name, self.modules, config, self.serverHttpRequest, self.server)
        
        self.rabbitConnection = RabbitConsumer("module.player", "module.player")
        Playlist.rabbitConnection = self.server;
        try:
            #for p in players:
            #    self.spotifyPlayer = p["class"](config, self.next)
            print("starting spotify player")
            self.setHandlers()
            self.rabbitConnection.onConnected(self.init)
            self.rabbitConnection.start()
            self.onSocketReconnect();
        
            while True:
                  time.sleep(0.2)
        except KeyboardInterrupt, SystemExit:
            self.stopApp()
        except:
Пример #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)