class GassService(object): def __init__(self): self.logger = api.server.app.logger self.gateway_service = GatewaysService() def send_device_update(self, gateway_uuid, device_uuid, body): gateway = self.gateway_service.find(uuid=gateway_uuid) if not gateway: self.logger.critical( "Failed to retrieve the gateway {} from DB. The performed actions will not be sent" .format(gateway_uuid)) return False url = "{}/api/devices/{}".format(gateway["ip"], device_uuid) self.logger.info("Gateway URL: {}".format(url)) try: response = requests.patch(url, json=body) if response.status_code == HTTPStatusCodes.NO_CONTENT: return True self.logger.warning( "Unexpected status code received: {} . Response: {}".format( response.status_code, response.text)) return False except Exception as err: self.logger.error( "Failed to send to the gateway the performed actions") return False
def __init__(self): api.server.app.logger = api.server.app.logger self.devices_service = DevicesService() self.gateways_service = GatewaysService() self.rules_service = RulesService() self.gass_service = GassService()
class GatewayRulesView(MethodView): def __init__(self): self.gateway_service = GatewaysService() self.gateway_rules_service = RulesService() def _send_rule_to_gateway(self, gateway_url, rule): url = "{}/api/rules".format(gateway_url) response = requests.post(url, json=rule) if not response: raise Exception("Failed to communicate with the gateway") return response.json()['_id'] def post(self, gateway_uuid): rule = request.get_json() gateway = self.gateway_service.find(uuid=str(gateway_uuid)) if not gateway: api.server.app.logger.error( "Gateway {} don't exist".format(gateway_uuid)) response = {"message": "Gateway not found"} return jsonify(response), HTTPStatusCodes.NOT_FOUND rule_id = self._send_rule_to_gateway(gateway["ip"], rule) rule["_id"] = rule_id self.gateway_rules_service.create(rule) return jsonify({'rule_id': rule_id}), HTTPStatusCodes.CREATED def get(self, gateway_uuid): gateway_uuid = str(gateway_uuid) rules = self.gateway_rules_service.find_from_gateway(gateway_uuid) for rule in rules: rule["_id"] = str(rule["_id"]) return jsonify(rules), HTTPStatusCodes.OK def put(self, gateway_uuid, rule_id): body = request.get_json() body.pop("_id", None) print(gateway_uuid, rule_id, body) return jsonify({}), HTTPStatusCodes.NO_CONTENT
def __init__(self): self.logger = api.server.app.logger self.gateway_service = GatewaysService()
def __init__(self): self.gateway_service = GatewaysService() self.gateway_rules_service = RulesService()
class GatewaysView(MethodView): POST_BODY_REQUIRED_FIELDS = {"uuid", "name", "description", "ip", "port"} def __init__(self): self.gateways_service = GatewaysService() def _validate_post_body(self, body): if not isinstance(body, dict): return "Body must be an object" missing_keys = self.POST_BODY_REQUIRED_FIELDS - body.keys() if missing_keys: return "Missing keys: {}".format(missing_keys) def get(self): return jsonify({}), HTTPStatusCodes.OK def post(self): """ Function used to register a gateway with the initial information from the vendor :return: """ body = request.json validation_error_message = self._validate_post_body(body) if validation_error_message: api.server.app.logger.error( "Some error occurred during the validation of the body. Reason: {}" .format(validation_error_message)) response = {"message": validation_error_message} return jsonify(response), HTTPStatusCodes.BAD_REQUEST updated_rows = self.gateways_service.update(body["uuid"], body["name"], body["description"], body["ip"], body["port"]) if updated_rows: api.server.app.logger.debug( "Gateway with uuid {} has already been registered".format( body["uuid"])) api.server.app.logger.debug("Updated Gateway info") response = { "message": "Gateways has been already registered. Update gateway info" } return jsonify(response), HTTPStatusCodes.CREATED gateway_data = { "uuid": body["uuid"], "name": body["name"], "description": body["description"], "ip": body["ip"], "port": body["port"], } _id = self.gateways_service.create(gateway_data) if not _id: response = { "message": "Failed to register the gateway. Regenerate the UUID" } # TODO: This may happen on a race condition return jsonify(response), HTTPStatusCodes.CONFLICT api.server.app.logger.info("Gateway {} has been registeredu") response = {"message": "Gateway has been register"} return jsonify(response), HTTPStatusCodes.CREATED
def __init__(self): self.gateways_service = GatewaysService()