Пример #1
0
 def __init__(self, url, debug=False, debugCodePaths=False):
     WebSocketServerFactory.__init__(self,
                                     url,
                                     debug=debug,
                                     debugCodePaths=debugCodePaths)
     self.clients = []
     self.lighting = Buttons()
     self.lighting.button1Callback = self.broadcast
     self.lighting.button2Callback = self.broadcast
     self.lighting.start()
     self.plugs = PlugPoller('10.10.55.25', 8080)
     self.plugs.statusChangeCallback = self.broadcast
     self.lighting.button2LongPressCallback = self.plugs.toggleAll
Пример #2
0
class BroadcastServerFactory(WebSocketServerFactory):
    def __init__(self, url, debug=False, debugCodePaths=False):
        WebSocketServerFactory.__init__(self,
                                        url,
                                        debug=debug,
                                        debugCodePaths=debugCodePaths)
        self.clients = []
        self.lighting = Buttons()
        self.lighting.button1Callback = self.broadcast
        self.lighting.button2Callback = self.broadcast
        self.lighting.start()
        self.plugs = PlugPoller('10.10.55.25', 8080)
        self.plugs.statusChangeCallback = self.broadcast
        self.lighting.button2LongPressCallback = self.plugs.toggleAll

    def register(self, client):
        if client not in self.clients:
            client.sendMessage(
                json.dumps([{
                    "Outputs": self.lighting.output_status()
                }, {
                    "Inputs": self.lighting.input_status()
                }, {
                    "Lamps": self.plugs.getstatus()
                }]))
            print("registered client {}".format(client.peer))
            self.clients.append(client)

    def unregister(self, client):
        if client in self.clients:
            print("unregistered client {}".format(client.peer))
            self.clients.remove(client)

    def broadcast(self, msg):
        print("broadcasting message '{}' ..".format(msg))
        for c in self.clients:
            c.sendMessage(msg.encode('utf8'))
            print("message sent to {}".format(c.peer))