def get(self, device_id): logging.info('Deleting device') device = Device.query_device_id(device_key, device_id) if device is None: logging.info('the device {0} is unknow. Nothing to delete'.format(device_id)) return self.redirect_to('home') logging.info('technical id {0}'.format(device.key)) device.key.delete() return self.redirect_to('home')
def post(self, device_id): current_user = users.get_current_user() device = Device.query_device_id(device_key, device_id) if device is None: return self.device_unknow(device_id) device.owner = current_user device.put() self.display_message('Device Associated', 'INFO')
def post(self, device_id): """ Update the device information """ logging.info('Post Edit device {0}'.format(device_id)) device = Device.query_device_id(device_key, device_id) remove_association = (self.request.get('remove_association') == 'on') # Collection form data device.device_id = self.request.get('device_id') device.name = self.request.get('device_name') device.active = (self.request.get('device_active') == 'on') if remove_association: logging.info("Removing association between the user and the device") device.owner = None device.put() self.redirect_to('home')
def get(self, device_id): message = '' current_user = users.get_current_user().nickname() device = Device.query_device_id(device_key, device_id) if device is None: return self.device_unknow(device_id) # you cannot associate a device with a owner if device.owner is not None: message = 'This device already have an owner.' return self.display_message(message, 'ERROR') viewbag = {'message': message, 'current_user': current_user, 'device_id': device_id } template = self.load_template('associatedevice.html') self.response.write(template.render(viewbag))
def post(self, device_id): """ Persist in database the temperature records performed by the device """ request_id = str(uuid.uuid1()) logging.info("SetRecord for device {0} - request {1}".format(device_id, request_id)) self.response.headers['Content-Type'] = "application/json" device_ok = Device.is_active_registered_device(device_key, device_id) if not device_ok : msg_error = {"status": "KO", "message": "The device is not registered and active", "requestId": request_id } logging.info(msg_error) return self.response.out.write(json.dumps(msg_error)) try: temperature_data = json.loads(self.request.body) except ValueError: msg_error = {"status": "KO", "message": "unable to convert JSON data", "requestId": request_id } logging.info(msg_error) return self.response.out.write(json.dumps(msg_error)); except: msg_error = {"status": "KO", "message": "unexpected error", "requestId": request_id } logging.info(msg_error) return self.response.out.write(json.dumps(msg_error)); device = Device.query_device_id(device_key, device_id) nb_records = 0 status = "OK" message = "" for temperature in temperature_data: try: tempData = TemperatureRecord() tempData.sensor_id = temperature['sensorId'] tempData.temperature = temperature['temperature'] device.add_temperature(tempData) nb_records += 1 except Exception, e: message = "An unexpected error occurred while persisting data : {0}. {1}".format(temperature, e) status = "WARN"
def get(self, device_id, date_start, date_end): message_response = { 'status' : '', 'message': '', 'requestid' : '', 'temperatures' : [] } """ Handle Http request for temperature retrieving """ device = Device.query_device_id(device_key, device_id) if device is None : return self.write_response(message_response, False, "the device is unknow device"); date_start = datetime.datetime.strptime(date_start,'%Y%m%d') date_end = datetime.datetime.strptime(date_end,'%Y%m%d') temperatures = device.get_temperature(date_start, date_end) for temperature in temperatures: message_response['temperatures'].append( { 'sensorid' : temperature.sensor_id, 'temperature' : temperature.temperature, 'timestamp' : temperature.timestamp.strftime('%Y-%d-%m %H:%M:%S %z')}) return self.write_response(message_response, True, '');
def get(self, device_id=None): logging.info('Get Edit device {0}'.format(device_id)) device = Device.query_device_id(device_key, device_id) if device is None: device = Device() device.device_id = '' device.name = '' owner_name = '' if device.owner is not None: owner_name = device.owner.nickname() if device.active: device_active = "checked=""checked""" else: device_active = "" logging.info('device loaded {0}'.format(device.device_id)) viewbag = {'device_name': device.name, 'device_id': device.device_id, 'device_active': device_active, 'device_owner': owner_name} template = self.load_template('editdevice.html') return self.response.out.write(template.render(viewbag))