Example #1
0
 def control(self):
     rpc = RPC(port=settings.CLIENT_RPC)
     mes = self.request.env.get('wsgi.input').read()
     mes = json.loads(mes)
     mes['method'] = 'set_output_state'
     res = rpc.send(mes, settings.KEY)
     return Response(json.dumps(res, cls=ComplexEncoder))
Example #2
0
 def call(self, method, message=None):
     r = RPC(address=self.address, port=self.port)
     message = message if message else {}
     message['name'] = self.name
     message['method']=  method
     resp = r.send(message, self.key)
     self.logger.info("Response: %s" % resp)
     return resp
Example #3
0
class Node(Controller):

    def display(self):
        res = {}
        self.rpc = RPC(port=settings.CLIENT_RPC, address='127.0.0.1')
        res = self.rpc.send(dict(method='get_nodes'), settings.KEY)
        home = self.request.env.get('HTTP_HOST')
        db = sqlite3.connect("automaton.db", detect_types=sqlite3.PARSE_DECLTYPES)
        cur = db.cursor()
        q_t = datetime.datetime.utcnow()-datetime.timedelta(hours=settings.HISTORICAL_DISPLAY)
        for n in res:
            n['historical'] = cur.execute('SELECT * FROM logs WHERE node=? AND timestamp > ? LIMIT 20', (n.get('name'),q_t)).fetchall()
        
        self.logger.debug("Got Nodes: %s" % res)
        db.close()
        return Response(self.render("base.html", values=json.dumps(res, cls=ComplexEncoder), url=home))

    def historical(self, name):
        db = sqlite3.connect("automaton.db", detect_types=sqlite3.PARSE_DECLTYPES)
        cur = db.cursor()
        q_t = datetime.datetime.utcnow()-datetime.timedelta(hours=settings.HISTORICAL_DISPLAY)
        res = cur.execute('SELECT * FROM logs WHERE node=? AND timestamp > ?', (name,q_t)).fetchall()
        self.logger.debug(res)
        return Response(json.dumps(res, cls=ComplexEncoder))

    def index(self):
        ws = self.request.env['wsgi.websocket']
        def write_out(ob):
            try:
                st = "%s\n" % json.dumps(ob, cls=ComplexEncoder)
                self.logger.debug("Message: %s" % st)
                ws.send(st)
                return True
            except Exception as e:
                self.logger.info("Connection Closed: %s" % ws)
                return False
        sub = Subscriber(callback=write_out, port=settings.CLIENT_SUB, broadcast=False, spawn=False, parse_message=self.parse_message)
        return Response('')
    
    def parse_message(self, message):
        return aes.decrypt(message, settings.KEY)

    def control(self):
        rpc = RPC(port=settings.CLIENT_RPC)
        mes = self.request.env.get('wsgi.input').read()
        mes = json.loads(mes)
        mes['method'] = 'set_output_state'
        res = rpc.send(mes, settings.KEY)
        return Response(json.dumps(res, cls=ComplexEncoder))