def insDataRead(dataRead): """Inserimento dei dati a Database vengono anche insiriti sensori e dispositivi nel caso non esistessero""" sensorType = session.query(SensorType).filter_by( code=dataRead['sensorType']).first() if (sensorType is None): sensorType = SensorType(code=dataRead['sensorType']) session.add(sensorType) deviceType = session.query(DeviceType).filter_by( code=dataRead['deviceType']).first() if (deviceType is None): deviceType = DeviceType(code=dataRead['deviceType']) session.add(deviceType) sensor = session.query(Sensor).filter_by(code=dataRead['sensor']).first() if (sensor is None): sensor = Sensor(code=dataRead['sensor'], sensorType=sensorType) session.add(sensor) device = session.query(Device).filter_by(code=dataRead['device']).first() if (device is None): device = Device(code=dataRead['device'], deviceType=deviceType) device.sensors.append(sensor) else: if (not sensor in device.sensors): device.sensors.append(sensor) session.add(device) deviceData = DeviceData(device=device,\ sensor=sensor,\ value=dataRead['value'],\ datetimeRead=datetime.datetime.utcnow()\ ) session.add(deviceData) # Commit data session.commit()
def device_res(self, data_dict): try: db_session = DB_Session() device = db_session.query(Device).filter(Device.device_id == data_dict['device_id']).first() if device: logging.info('device is has') return 'device is has' else: data = time.strftime("%Y-%m-%d") company = db_session.query(Company).filter(Company.code == data_dict['company']).first() device_data = Device(device_id=data_dict['device_id'], device_id_s=data_dict['device_id_s'], device_name=data_dict['device_name'], ip=data_dict['ip'], port=data_dict['port'], data=data, model=data_dict['model'], board_model=data_dict['board_model'], screen_size=data_dict['screen_size'], screen_resolution=data_dict['screen_resolution'], screen_number='3', cpu_model=data_dict['cpu_model'], cpu_num=data_dict['cpu_num'], ram_model=data_dict['ram_model'], ram_size=data_dict['ram_size'], mac_address=data_dict['mac_address'], voltage=data_dict['voltage'], power_consumption=data_dict['power_consumption'], device_position=data_dict['device_position'], company_id=company.id ) db_session.add(device_data) db_session.commit() logging.info("设备注册成功:" + str(device_data)) return 'device_register_success' except: return 'device_register_fail' finally: db_session.close()
def post(self): args = device_parser.parse_args() # Get Device arguments if DeviceValidator.is_name_valid(args["name"]): name = args["name"] response = self.repository.get_device(name) if response: return Response.error(EXISTS_ID) else: return Response.error(FIELD_NOT_VALID) if DeviceValidator.is_category_valid(args["category"]): category = args["category"] response = self.repository.get_device_category(category) if response is None: return Response.error(NOT_DEVICE_CATEGORY) else: return Response.error(FIELD_NOT_VALID) if DeviceValidator.is_location_valid(args["location"]): location = args["location"] response = self.repository.get_location(location) if response is None: return Response.error(NOT_EXISTS_LOCATION) else: location = None device = Device(name=name, category=category, location=location) result = self.repository.add_device(device) if result: return Response.success({"name": result}) return Response.error(GENERIC)
def post(self): try: device = self.session.query(Device).filter( Device.device_id == self.get_argument('device_id')).first() if device: return 'device_already_exists' else: data = time.strftime("%Y-%m-%d") company = self.session.query(Company).filter( Company.code == self.get_argument('company')).first() device_data = Device( device_id=self.get_argument('device_id'), device_id_s=self.get_argument('device_id_s'), device_name=self.get_argument('device_name'), ip=self.get_argument('ip'), port=self.get_argument('port'), data=data, model=self.get_argument('model'), board_model=self.get_argument('board_model'), screen_size=self.get_argument('screen_size'), screen_resolution=self.get_argument('screen_resolution'), screen_number='3', cpu_model=self.get_argument('cpu_model'), cpu_num=self.get_argument('cpu_num'), ram_model=self.get_argument('ram_model'), ram_size=self.get_argument('ram_size'), mac_address=self.get_argument('mac_address'), voltage=self.get_argument('voltage'), power_consumption=self.get_argument('power_consumption'), device_position=self.get_argument('device_position'), company_id=company.id) self.session.add(device_data) self.session.commit() return 'device_register_ok' except: return 'database_error'
def testCRUD(self): # Insert device type dd1 = DeviceData(value=10.0, datetimeRead=datetime.datetime.now()) self.session.add(dd1) self.session.commit() # Check if inserted dd = self.session.query(DeviceData).filter_by(value=10.0).first() self.assertEquals(dd.value, dd1.value) # Check for non insertion dd = self.session.query(DeviceData).filter_by(value=99.0).first() self.assertTrue(dd is None) # Check Update dd = self.session.query(DeviceData).filter_by(value=10.0).first() dd.value = 20.0 self.session.commit() ddTst = self.session.query(DeviceData).filter_by(value=20.0).first() self.assertEquals(ddTst.value, 20.0) # Check printout (to see this you have to run nosetest --nocapture dd = self.session.query(DeviceData).filter_by(value=20.0).first() print('DeviceData = %s' % dd) # Insert a second record and check insertion dd2 = DeviceData(value=100.0) self.session.add(dd2) self.session.commit() dd = self.session.query(DeviceData).filter_by(value=100.0).first() self.assertEquals(dd.value, dd2.value) # Rollback test dd3 = DeviceData(value=1000.0) self.session.add(dd3) self.session.rollback() dd = self.session.query(DeviceData).filter_by(value=1000.0).first() self.assertTrue(dd is None) # Delete record dd = self.session.query(DeviceData).filter_by(value=100.0).first() self.session.delete(dd) self.session.commit() self.assertTrue(self.session.query(DeviceData).filter_by(value=100.0).count()==0) # Add to device data data read from a device sensorType1 = SensorType(code='sensorType1', description='SENSORTYPEDESCR1') sensorType2 = SensorType(code='sensorType2', description='SENSORTYPEDESCR2') deviceType1 = DeviceType(code='deviceType1', description='DEVICETYPEDESCR1') deviceType2 = DeviceType(code='deviceType2', description='DEVICETYPEDESCR2') sensor11 = Sensor(code='sensor11', description='SENSORDESCR11', sensorType=sensorType1) sensor12 = Sensor(code='sensor12', description='SENSORDESCR12', sensorType=sensorType2) sensor21 = Sensor(code='sensor21', description='SENSORDESCR21', sensorType=sensorType1) sensor22 = Sensor(code='sensor22', description='SENSORDESCR22', sensorType=sensorType2) device1 = Device(code='device1', description='DEVICEDESCR1',\ deviceType=deviceType1) device1.sensors.append(sensor11) device1.sensors.append(sensor12) device2 = Device(code='device2', description='DEVICEDESCR2',\ deviceType=deviceType2) device2.sensors.append(sensor21) device2.sensors.append(sensor22) deviceData11=DeviceData(datetimeRead=datetime.datetime.now(),\ value=11.0,\ device=device1) deviceData12=DeviceData(datetimeRead=datetime.datetime.now(),\ value=12.0,\ device=device1) deviceData21=DeviceData(datetimeRead=datetime.datetime.now(),\ value=21.0,\ device=device2,) deviceData22=DeviceData(datetimeRead=datetime.datetime.now(),\ value=22.0,\ device=device2) self.session.add(deviceData11) self.session.add(deviceData12) self.session.add(deviceData21) self.session.add(deviceData22) self.session.commit() # Print data inserted devicedatas = self.session.query(DeviceData) for dd in devicedatas: print('Device Data : %s'%dd) print('\tDevice : %s'%dd.device) if dd.device is not None and dd.device.sensors is not None: for sens in dd.device.sensors: print('\t\tSensor : %s'%sens)
def testCRUD(self): # Insert device type device1 = Device(code='device1', description='DESCRIPTION1') self.session.add(device1) self.session.commit() # Check if inserted device = self.session.query(Device).filter_by(code='device1').first() self.assertEquals(device.code, device1.code) # Check for non insertion device = self.session.query(Device).filter_by( code='deviceFake').first() self.assertTrue(device is None) # Check Update device = self.session.query(Device).filter_by(code='device1').first() device.description = 'DESCRIPTIONChg' self.session.commit() deviceTst = self.session.query(Device).filter_by( code='device1').first() self.assertEquals(deviceTst.description, 'DESCRIPTIONChg') # Check printout (to see this you have to run nosetest --nocapture device = self.session.query(Device).filter_by(code='device1').first() print('Device = %s' % device) # Insert a second record and check insertion device2 = Device(code='device2', description='DESCRIPTION2') self.session.add(device2) self.session.commit() device = self.session.query(Device).filter_by(code='device2').first() self.assertEquals(device.code, device2.code) # Rollback test device3 = Device(code='device3', description='DESCRIPTION3') self.session.add(device3) self.session.rollback() device = self.session.query(Device).filter_by(code='device3').first() self.assertTrue(device is None) # Delete record device = self.session.query(Device).filter_by(code='device2').first() self.session.delete(device) self.session.commit() self.assertTrue( self.session.query(Device).filter_by(code='device2').count() == 0) # Add a relation to a device type deviceType = DeviceType(code='devicetype1', description='DESCRIPTIONDT1') device = self.session.query(Device).filter_by(code='device1').first() device.deviceType = deviceType self.session.commit() print('Device = %s' % device) self.assertEquals(self.session.query(Device).filter_by(code='device1').first()\ .deviceType.code, 'devicetype1') # Add list of sensors sensor1 = Sensor(code='sensor1', description='DESCRIPTIONS1', \ sensorType=SensorType(code='sensortype1', description='DESCRIPTIONST1')) sensor2 = Sensor(code='sensor2', description='DESCRIPTIONS2', \ sensorType=SensorType(code='sensortype2', description='DESCRIPTIONST2')) device.sensors.append(sensor1) device.sensors.append(sensor2) self.session.commit() # List sensors of a device device = self.session.query(Device).filter_by(code='device1').first() print('Device = %s' % device) for sensor in device.sensors: print('\tSensor = %s' % sensor)
def post(self): device_id = self.get_argument('device_id') device_name = self.get_argument("device_name") ip = self.get_argument("ip") device_position = self.get_argument("device_position") port = self.get_argument("port") device_use = self.get_argument("device_use") device_audio = self.get_argument("device_audio") screen_number = self.get_argument("screen_number") ad_environment = self.get_argument("ad_environment") device_group_id = self.get_argument('deviceGroup_id') try: device = Device( device_id=device_id, device_name=device_name, ip=ip, device_position=device_position, port=port, device_audio=device_audio, screen_number=screen_number, device_use=device_use, ad_environment=ad_environment, deviceGroup_id=device_group_id, ) self.session.merge(device) self.session.commit() # 新建任务 my_task = MyTask( name='设备信息任务', type='device', time=time.strftime("%Y-%m-%d %H:%M:%S"), user_id=self.get_secure_cookie('user_id'), company_id=self.get_secure_cookie('company_id'), ) self.session.add(my_task) self.session.commit() # 新建任务内容 data_list = list() data_dict = dict() data_dict['cmd'] = 'device' data_list.append(data_dict) data_dict = dict() data_dict['device_audio'] = device_audio data_dict['screen_number'] = screen_number data_list.append(data_dict) my_task_content = MyTaskContent( device_id=device.device_id, from_user=self.get_secure_cookie('username'), new_time=time.strftime("%Y-%m-%d %H:%M:%S"), send_time=time.strftime("%Y-%m-%d %H:%M:%S"), send_data=str(data_list), my_task_id=my_task.id) self.session.add(my_task_content) self.session.commit() ApiDeviceSocketHandler.send_to_one_device(device_id, data_list) self.redirect('/dev/group?device_group_id=' + device_group_id) except: err = '修改失败' device = self.session.query(Device).filter( Device.device_id == device_id).first() self.render("dev_manage_detail.html", auth_user=self.current_user, device=device, err=err)