def __init__(self): self.devices = {} self.sequencer = Sequencer() self.sequencer.start() self.logHandler = ControllerLogHandler() logging.getLogger().addHandler(self.logHandler) self.clients = []
class Controller(ScanConverterController, UnisonController, UpDownRelayController, VideoSwitcherController, VISCAController): ''' A Controller is essentially a bucket of devices, each identified with a string deviceID. ''' pyroName = "aldates.alix.controller" version = 0.9 def __init__(self): self.devices = {} self.sequencer = Sequencer() self.sequencer.start() self.logHandler = ControllerLogHandler() logging.getLogger().addHandler(self.logHandler) self.clients = [] def loadConfig(self, configFile): try: config = json.load(open(configFile)) for d in config["devices"]: device = Device.create(d, self) self.addDevice(device) except ValueError: logging.exception("Cannot parse config.json:") def registerClient(self, clientURI): self.clients.append(clientURI) logging.info("Registered client at " + str(clientURI)) logging.info(str(len(self.clients)) + " client(s) now connected") def unregisterClient(self, clientURI): self.clients.remove(clientURI) logging.info("Unregistered client at " + str(clientURI)) logging.info(str(len(self.clients)) + " client(s) still connected") def callAllClients(self, function): ''' function should take a client and do things to it''' for uri in self.clients: try: logging.debug("Calling function " + function.__name__ + " with client at " + str(uri)) client = Pyro4.Proxy(uri) result = function(client) logging.debug("Client call returned " + str(result)) except: logging.exception("Failed to call function on registered client " + str(uri) + ", removing.") self.clients.pop(uri) def getVersion(self): return self.version def addDevice(self, device): self.devices[device.deviceID] = device if hasattr(device, "registerDispatcher") and callable(getattr(device, "registerDispatcher")): device.registerDispatcher(self) def getDevice(self, deviceID): return self.devices[deviceID] def hasDevice(self, deviceID): return deviceID in self.devices def initialise(self): for device in self.devices.itervalues(): device.initialise() def systemPowerOn(self): if self.hasDevice("Power"): power = self.devices["Power"] logging.info("Turning ON system power") self.sequencer.sequence( Event(lambda: self.callAllClients(lambda c: c.showPowerOnDialog())), Event(power.on, 2), self.sequencer.wait(3), Event(power.on, 5), self.sequencer.wait(3), Event(power.on, 6), self.sequencer.wait(3), Event(power.on, 1), Event(self.initialise), # By this time all things we care about to initialise will have been switched on Event(lambda: self.callAllClients(lambda c: c.hidePowerDialog())), ) def systemPowerOff(self): if self.hasDevice("Power"): power = self.devices["Power"] logging.info("Turning OFF system power") self.sequencer.sequence( Event(lambda: self.callAllClients(lambda c: c.showPowerOffDialog())), Event(power.off, 1), self.sequencer.wait(3), Event(power.off, 6), self.sequencer.wait(3), Event(power.off, 5), self.sequencer.wait(3), Event(power.off, 2), Event(lambda: self.callAllClients(lambda c: c.hidePowerDialog())), ) def getLog(self): return self.logHandler.entries def updateOutputMappings(self, mapping): self.callAllClients(lambda c: c.updateOutputMappings(mapping))