def handleException(request, exception): log = request.ctx.logger if isinstance(exception, SanicException): log.error(__name__ + '.error', status=exception.args[0]) return sanicjson(dict(status=exception.args[0]), status=exception.status_code) else: _, _, exc_info = sys.exc_info() log.error(__name__ + '.error', exc_info=exc_info) return sanicjson(dict(status='bug'), status=500)
async def httpd_routes_dht(request, name, timestamp, temperature, humidity): self.debug("[+] sensors: %s (%s): temperature: %s, humidity: %s" % (name, timestamp, temperature, humidity)) cursor = self.db.cursor() rows = (name, int(timestamp), float(temperature), float(humidity)) cursor.execute( "INSERT INTO dht (id, timestamp, temp, hum) VALUES (?, ?, ?, ?)", rows) self.db.commit() self.sensors_dht_last[name] = { 'id': name, 'timestamp': int(timestamp), 'temperature': float(temperature), 'humidity': float(humidity), } # update sensors stats await self.wsbroadcast("sensors-dht", self.sensors_dht_last) """ # pushing chart temp = {"id": name, "serie": self.sensors_backlog(name)} await self.wsbroadcast("sensors-backlog", temp) """ return sanicjson({})
async def httpd_routes_index(request, name, timestamp, value): self.debug("[+] sensors: %s (%s): value: %s" % (name, timestamp, value)) cursor = self.db.cursor() rows = (name, int(timestamp), float(value)) cursor.execute( "INSERT INTO sensors (id, timestamp, value) VALUES (?, ?, ?)", rows) self.db.commit() self.sensors_last[name] = { 'id': name, 'timestamp': int(timestamp), 'value': float(value), } # update sensors stats await self.wsbroadcast("sensors", self.sensors_last) # pushing chart temp = {"id": name, "serie": self.sensors_backlog(name)} await self.wsbroadcast("sensors-backlog", temp) return sanicjson({})
async def auth(request): if 'authorization' not in request.headers: return sanicjson( {"error": "you cannot be authenticated to access the service, no credentials provided"}, 401 ) loop = asyncio.get_event_loop() try: auth_struct = await parse_auth(request) anonymized_passwd = await anonymize_password(auth_struct['password']) except Exception as ex: logger.warning("Not parsable auth header received: {}".format(ex)) return sanicjson( {"error": "wrong authorization header received, you cannot be authenticated to access the service"}, 401 ) logger.debug("An attempt to auth: {}:{}".format(auth_struct['username'], anonymized_passwd)) conn = await loop.run_in_executor(None, get_ldap_connection, auth_struct['username'], auth_struct['password']) if conn is None: logger.warning("wrong credentials for: {}".format(auth_struct['username'])) return sanicjson({"error": "you cannot be authenticated to access the service"}, 401) try: user_dn = await loop.run_in_executor(None, get_user_dn, conn, auth_struct['username']) user_group_dn = settings.app['service']['ldap']['ugroup'] is_auth_as_user = await loop.run_in_executor(None, check_group, conn, user_group_dn, user_dn) if is_auth_as_user: request.headers['LDAP_AUTHORISED_LOGIN'] = auth_struct['username'] request.headers['LDAP_AUTHORISED_DN'] = user_dn request.headers['AUTHORISED_AS'] = 'user' return admin_group_dn = settings.app['service']['ldap']['agroup'] is_auth_as_admin = await loop.run_in_executor(None, check_group, conn, admin_group_dn, user_dn) if is_auth_as_admin: request.headers['LDAP_AUTHORISED_LOGIN'] = auth_struct['username'] request.headers['LDAP_AUTHORISED_DN'] = user_dn request.headers['AUTHORISED_AS'] = 'admin' return return sanicjson({"error": "you are not authorized to see the content"}, 403) finally: await loop.run_in_executor(None, terminate_ldap_connection, conn)
async def httpd_routes_power(request, timestamp, value): self.debug("[+] power: %s watt at %s" % (value, timestamp)) cursor = self.db.cursor() rows = (int(timestamp), float(value)) cursor.execute( "INSERT OR IGNORE INTO power (timestamp, value, phase) VALUES (?, ?, 1000)", rows) self.db.commit() self.power["1000"] = { 'timestamp': int(timestamp), 'value': float(value), } # update sensors stats await self.wsbroadcast("power", self.power) # pushing chart # temp = {"id": name, "serie": self.sensors_backlog(name)} # await self.wsbroadcast("sensors-backlog", temp) return sanicjson({})
async def httpd_routes_power(request, timestamp, phase, value): print("[+] power: phase %s: %s watt at %s" % (phase, value, timestamp)) cursor = self.db_power.cursor() rows = (int(timestamp), float(value), int(phase)) cursor.execute( "INSERT OR IGNORE INTO power (timestamp, value, phase) VALUES (?, ?, ?)", rows) self.db_power.commit() self.power[phase] = { 'timestamp': int(timestamp), 'value': float(value), } self.slave_power.set(self.power) self.slave_power.publish() # pushing chart # temp = {"id": name, "serie": self.sensors_backlog(name)} # await self.wsbroadcast("sensors-backlog", temp) return sanicjson({})
def jsonify(body, status=200, headers=None, content_type="application/json"): result = json.loads(json.dumps(body, default=_json_default)) return sanicjson(body=result, status=status, headers=headers, content_type=content_type)