def post(self): args = parser.parse(device_args, request) device = Devices.query.filter_by(device_name=args['device_name']).first() if device is not None: return conflict("Device with name {n} already exists".format(n=args['device_name'])), 409 try: device_id = str(uuid.uuid4()) location = None temperature_sensor = False humidity_sensor = False uptime = 1 if 'location' in args: location = args['location'] if 'temperature_sensor' in args: temperature_sensor = True if 'humidity_sensor' in args: humidity_sensor = True if 'uptime' in args: uptime = long(args['uptime']) else: uptime = long(0) device = Devices(device_id=device_id, device_name=args['device_name'], location=location, temperature_sensor=temperature_sensor, humidity_sensor=humidity_sensor, uptime=uptime) db.session.add(device) db.session.commit() return "Location: " + device.url, 200 except Exception as e: return internal_error("Server Error " + str(e)), 500
def post(self): try: args = parser.parse(reading_args) log_id = post_reading(args, HUMIDITY) return "Location: " + url_for("HumidityReadingsView:get", log_id=log_id, _external=True), 200 except InvalidRequestException as ire: return handle_bad_request(ire.message), 422 except NotFoundError as nfe: return not_found(nfe.message), 404 except Exception as e: print traceback.format_exc() return internal_error(), 500
def delete(self): args = parser.parse(device_name_args, request) device_name = args['device_name'] if device_name is not None and len(device_name) > 0: try: device = Devices.query.filter_by(device_name=device_name).first() if device is None: return not_found("Device Not Found " + device_name), 404 db.session.delete(device) db.session.commit() return "Device Deleted.", 200 except Exception as e: return internal_error("Server Error " + str(e)), 500
def post(self): try: args = parser.parse(reading_args) log_id = post_reading(args, HUMIDITY) return "Location: " + url_for( "HumidityReadingsView:get", log_id=log_id, _external=True), 200 except InvalidRequestException as ire: return handle_bad_request(ire.message), 422 except NotFoundError as nfe: return not_found(nfe.message), 404 except Exception as e: print traceback.format_exc() return internal_error(), 500
def get(self, log_id): if log_id is not None and len(log_id) > 0: reading = None items = [] links = [] try: items, links = generate_get_response(log_id, HUMIDITY, humidity_reading_schema) except KeyError as ke: return not_found(), 404 except Exception as e: return internal_error(), 500 collection_href = url_for("HumidityReadingsView:index", _external=True) results = create_collection('1.0', collection_href, items, links) return results, 200
def get(self, log_id): if log_id is not None and len(log_id) > 0: reading = None items = [] links = [] try: items, links = generate_get_response(log_id, TEMPERATURE, temperature_reading_schema) except KeyError as ke: print traceback.format_exc() return not_found(), 404 except Exception as e: print traceback.format_exc() return internal_error(), 500 collection_href = url_for("TemperatureReadingsView:index", _external=True) results = create_collection('1.0', collection_href, items, links) return results, 200
def get(self, log_id): if log_id is not None and len(log_id) > 0: reading = None items = [] links = [] try: items, links = generate_get_response( log_id, TEMPERATURE, temperature_reading_schema) except KeyError as ke: print traceback.format_exc() return not_found(), 404 except Exception as e: print traceback.format_exc() return internal_error(), 500 collection_href = url_for("TemperatureReadingsView:index", _external=True) results = create_collection('1.0', collection_href, items, links) return results, 200
def by_datetime(self, device_id, datetime): try: if datetime is None: raise InvalidRequestException('Missing required datetime value') if device_id is None: raise InvalidRequestException('Missing required device_id value') results = get_reading_by_device_and_time(device_id=device_id, timestamp=datetime) items, links = generate_reading_response(device_id, results) collection_href = url_for("DevicesView:index", _external=True) results = create_collection('1.0', collection_href, items, links) return results, 200 except NotFoundError as nfe: return not_found('No readings found for device {d} at {t}' .format(d=device_id, t=datetime)), 404 except InvalidRequestException as ire: return handle_bad_request(ire.message), 422 except Exception as e: print traceback.format_exc() return internal_error(e.message), 500