def get(self): api_response = ApiResponse() sites = Site.query.all() for site in sites: api_response += site # print api_response.json() return api_response.json()
def post(self): response = ApiResponse(request) sensor_id = RequestHelper.get_form_data(response, 'sensor_id', int) value = RequestHelper.get_form_data(response, 'value', float) timestamp_str = RequestHelper.get_form_data(response, 'timestamp', str) store_reading(response, sensor_id, value, timestamp_str) return response.json()
def get(self): api_response = ApiResponse() sensors = Sensor.query.all() for sensor in sensors: api_response += sensor # print api_response.json() return api_response.json()
def post(self): """ Use a HTTP POST request to /node to create a new node inside the network Example in Python: >>> import requests >>> r = requests.post('http://localhost:8080/node', data = {'alias':'mynode', 'site_id':'1', 'latitude':'13.24234234', 'longitude':23.222, 'populate':3, 'node_type':'ricefield'}) """ response = ApiResponse(request) # RequestHelper.filter_valid_parameters(Node, response, request) node_alias = RequestHelper.get_form_data(response, 'alias', str) node_type = RequestHelper.get_form_data(response, 'node_type', str, default = 'empty') site_id = RequestHelper.get_form_data(response, 'site_id', int) longitude = RequestHelper.get_form_data(response, 'longitude', float) latitude = RequestHelper.get_form_data(response, 'latitude', float) populate = RequestHelper.get_form_data(response, 'populate', int, default = 0) site = Site.query.filter_by(id = site_id).first() if site: node = NodeSeeder.seed_node(node_type, alias = node_alias, site_id = site_id, latitude = latitude, longitude = longitude, populate = populate) response += node else: response += exc.MissingSiteException(site_id) return response.json()
def get(self, site_id): response = ApiResponse(request) site = Site.query.filter_by(id = site_id).first() if site: response += site else: response += exc.MissingResourceException(type(self), site_id) return response.json()
def get(self, site_id): response = ApiResponse(request) site = Site.query.filter_by(id=site_id).first() if site: response += site else: response += exc.MissingResourceException(type(self), site_id) return response.json()
def get(self, sensor_id): response = ApiResponse(request) sensor = Sensor.query.filter_by(id=sensor_id).first() if sensor: response += sensor else: response += exc.MissingSensorException(sensor_id) return response.json()
def get(self, sensor_id): response = ApiResponse(request) sensor = Sensor.query.filter_by(id = sensor_id).first() if sensor: response += sensor else: response += exc.MissingSensorException(sensor_id) return response.json()
def delete(self, node_id): response = ApiResponse(request) node = Node.query.filter_by(id=node_id).first() if node: response += node succes = Node.delete(node_id) else: response += MissingNodeException(node_id) return response.json()
def get(self): api_response = ApiResponse() filters = {} site_id = RequestHelper.get_form_data(api_response, 'site_id', str) if site_id: filters.update({'site_id': site_id}) nodes = Node.query.filter_by(**filters).all() for node in nodes: api_response += node return api_response.json()
def delete(self, site_id): response = ApiResponse(request) site = Site.query.filter_by(id = site_id).first() if site: response += site succes = Site.delete(site_id) else: response += MissingSiteException(site_id) return response.json()
def delete(self, site_id): response = ApiResponse(request) site = Site.query.filter_by(id=site_id).first() if site: response += site succes = Site.delete(site_id) else: response += MissingSiteException(site_id) return response.json()
def delete(self, node_id): response = ApiResponse(request) node = Node.query.filter_by(id = node_id).first() if node: response += node succes = Node.delete(node_id) else: response += MissingNodeException(node_id) return response.json()
def get(self, node_id=None): """ Use a HTTP GET request to /node/<int> get one node data where <int> is the unique id of the node. """ response = ApiResponse(request) if node_id: node = Node.query.filter_by(id=node_id).first() if node: response += node else: response += exc.MissingNodeException(node_id) else: response += MissingParameterException('node_id') return response.json()
def get(self, node_id = None): """ Use a HTTP GET request to /node/<int> get one node data where <int> is the unique id of the node. """ response = ApiResponse(request) if node_id: node = Node.query.filter_by(id = node_id).first() if node: response += node else: response += exc.MissingNodeException(node_id) else: response += MissingParameterException('node_id') return response.json()
def post(self): response = ApiResponse(request) format = request.form.get('format', 'json') data = request.form.get('data', '') print format print data if format == 'compact': data = data.split(';') data.remove('') stored_readings = 0 for reading in data: try: node_id, sensor_id, value, timestamp_str = reading.split( ',') # print node_id, sensor_id, value, timestamp_str except ValueError: response += Exception( 'Please submit readings as sensor_id,value,timestamp;') try: store_reading(response, sensor_id, value, timestamp_str) stored_readings += 1 except Exception, e: response += Exception('Could not store reading') return 'Stored: ' + str( stored_readings ) # This short return is to avoid crashing sensor nodes by sending back long response.
def get(self, reading_id = None): response = ApiResponse(request) if request.args: # print request.args.keys() ### Temporary hack to posting data but sending urlencoded data in a GET request value = request.args['value'] sensor_id = request.args['sensor_id'] if Sensor.query.filter_by(id = sensor_id).all(): store_reading(response, sensor_id, value, datetime.now()) return 'OK' else: return 'ERROR' else: if reading_id: reading = Reading.query.filter_by(id = reading_id).first() if reading: response += reading else: response += exc.MissingReadingException(reading_id) else: response += exc.IncompleteURLException(correct_url_format = 'GET /reading/<int:reading_id>') return response.json()
def get(self, reading_id=None): response = ApiResponse(request) if request.args: # print request.args.keys() ### Temporary hack to posting data but sending urlencoded data in a GET request value = request.args['value'] sensor_id = request.args['sensor_id'] if Sensor.query.filter_by(id=sensor_id).all(): store_reading(response, sensor_id, value, datetime.now()) return 'OK' else: return 'ERROR' else: if reading_id: reading = Reading.query.filter_by(id=reading_id).first() if reading: response += reading else: response += exc.MissingReadingException(reading_id) else: response += exc.IncompleteURLException( correct_url_format='GET /reading/<int:reading_id>') return response.json()
def post(self): """ Use a HTTP POST request to /node to create a new node inside the network Example in Python: >>> import requests >>> r = requests.post('http://localhost:8080/node', data = {'alias':'mynode', 'site_id':'1', 'latitude':'13.24234234', 'longitude':23.222, 'populate':3, 'node_type':'ricefield'}) """ response = ApiResponse(request) # RequestHelper.filter_valid_parameters(Node, response, request) node_alias = RequestHelper.get_form_data(response, 'alias', str) node_type = RequestHelper.get_form_data(response, 'node_type', str, default='empty') site_id = RequestHelper.get_form_data(response, 'site_id', int) longitude = RequestHelper.get_form_data(response, 'longitude', float) latitude = RequestHelper.get_form_data(response, 'latitude', float) populate = RequestHelper.get_form_data(response, 'populate', int, default=0) site = Site.query.filter_by(id=site_id).first() if site: node = NodeSeeder.seed_node(node_type, alias=node_alias, site_id=site_id, latitude=latitude, longitude=longitude, populate=populate) response += node else: response += exc.MissingSiteException(site_id) return response.json()
def get(self): response = ApiResponse(request) sensor_id = RequestHelper.get_form_data(response, 'sensor_id', int, http_verb = 'GET') node_id = RequestHelper.get_form_data(response, 'node_id', int, http_verb = 'GET') sensor_alias = RequestHelper.get_form_data(response, 'sensor_alias', str, http_verb = 'GET') from_date = RequestHelper.get_form_data(response, 'from', str, http_verb = 'GET') until_date = RequestHelper.get_form_data(response, 'until', str, http_verb = 'GET') query = Reading.query readings = list() if sensor_id: query = Reading.query.filter_by(sensor_id = sensor_id) readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() elif node_id and sensor_alias: node = Node.query.filter_by(id = node_id).first() sensor = Sensor.query.filter_by(alias = sensor_alias, node = node).first() if sensor: query = Reading.query.filter_by(sensor_id = sensor.id) readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() elif node_id and not sensor_alias: response += exc.MissingParameterException('sensor_alias') return response.json() elif not node_id and sensor_alias: response += exc.MissingParameterException('node_id') return response.json() elif from_date or until_date: readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() else: response += exc.MissingParameterException('sensor_id OR node_id and sensor_alias') return response.json()
def post(self): response = ApiResponse() site_alias = RequestHelper.get_form_data(response, 'alias', str) response += SiteSeeder.seed_empty_site(site_alias=site_alias) return response.json()
def get(self): api_response = ApiResponse() api_response += nodetypes return api_response.json()
def get(self): response = ApiResponse(request) sensor_id = RequestHelper.get_form_data(response, 'sensor_id', int, http_verb='GET') node_id = RequestHelper.get_form_data(response, 'node_id', int, http_verb='GET') sensor_alias = RequestHelper.get_form_data(response, 'sensor_alias', str, http_verb='GET') from_date = RequestHelper.get_form_data(response, 'from', str, http_verb='GET') until_date = RequestHelper.get_form_data(response, 'until', str, http_verb='GET') query = Reading.query readings = list() if sensor_id: query = Reading.query.filter_by(sensor_id=sensor_id) readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() elif node_id and sensor_alias: node = Node.query.filter_by(id=node_id).first() sensor = Sensor.query.filter_by(alias=sensor_alias, node=node).first() if sensor: query = Reading.query.filter_by(sensor_id=sensor.id) readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() elif node_id and not sensor_alias: response += exc.MissingParameterException('sensor_alias') return response.json() elif not node_id and sensor_alias: response += exc.MissingParameterException('node_id') return response.json() elif from_date or until_date: readings = Reading.query_interval(query, from_date, until_date).all() for reading in readings: response += reading return response.json() else: response += exc.MissingParameterException( 'sensor_id OR node_id and sensor_alias') return response.json()
def post(self): response = ApiResponse() site_alias = RequestHelper.get_form_data(response, 'alias', str) response += SiteSeeder.seed_empty_site(site_alias = site_alias) return response.json()