def temperature_init(row): temperature = Temperature() num = Temperature.query.filter_by(name=row['Nom']).count() if num > 0: Temperature.query.delete() temperature.id = row['Id'] temperature.name = row['Nom'] return temperature
def run_water(zone, alias, minutes): with SessionLocal() as db: start_time = datetime.now() b = Board( ) # init a new board since tasks dont seem to have GPIO setup... b.register_flow() temperature = b.get_temp() db.add(Temperature(temperature)) db.commit() # moisture0 = board.read_analog_sensor(analog0) # moisture1 = board.read_analog_sensor(analog1) # moisture2 = board.read_analog_sensor(analog2) # moisture3 = board.read_analog_sensor(analog3) # sql_helper.insert_moistures(moisture0, moisture1, moisture2, moisture3) b.set_high(pins[zone]) time.sleep(minutes * 60) # sleep for our duration with the solenoid open b.set_low(pins[zone]) water_used = b.read_water_flow() db.add(Water(zone, alias, start_time, water_used)) db.commit() b.deregister_flow()
def saveTemperature(self, employeeId, temperature): temperatureObj = Temperature(employeeId=employeeId, temperature=temperature, createdAt=datetime.datetime.now(), updatedAt=datetime.datetime.now()) db.session.add(temperatureObj) db.session.commit() return "employeeId " + employeeId + " info upload success!"
def get_sensortemperature(): #row = Temperature.query.filter_by(thermosensor_id="012033add00d").order_by(Temperature.date.desc()).first() #row = Temperature.query.filter().order_by(Temperature.date.desc()).limit(10).all() subquery = Temperature.query.filter().order_by( Temperature.date.desc()).first() row = Temperature.query.filter(Temperature.date == subquery.date) return jsonify(Temperature.to_collection_dict(row))
async def _save_db(self, data: List[dict]): date = datetime.utcnow() recorded_at = date.replace(minute=(date.minute // 15) * 15, second=0, microsecond=0) for item in data: if not item.get('temperature'): return instance: Optional[Temperature] = self.db.query(Temperature).filter_by(sensor_id=item['sensor_id'], recorded_at=recorded_at).one_or_none() if instance is None: instance = Temperature(sensor_id=item['sensor_id'], temperature=item['temperature'], recorded_at=recorded_at) self.db.add(instance) self.db.commit()
def temperature(): if request.method == 'POST': if not request.json or not 'data' in request.json: abort(400) else: if not request.json.get('data').get('temperature'): abort(404) t = Temperature(temperature=request.json.get('data')['temperature']) db.session.add(t) db.session.commit() return jsonify({'status': 'ok'}) temperatures = Temperature.query.all() return jsonify(temperature_data=[i.serialize for i in temperatures])
def temperature(): if request.method == "POST": if request.headers.get("X-API-KEY") != app.config["API_KEY"]: abort(401) if not request.json or not "data" in request.json: abort(400) else: if not request.json.get("data").get("temperature"): abort(404) t = Temperature(temperature=request.json.get("data")["temperature"]) db.session.add(t) db.session.commit() return jsonify({"status": "ok"}) temperatures = Temperature.query.all() return jsonify(temperature_data=[i.serialize for i in temperatures])
def saveOrUpdate(self, employeeId, temperature): temperatureObj = Temperature(employeeId=employeeId, temperature=temperature, createdAt=datetime.datetime.now(), updatedAt=datetime.datetime.now()) temperatureOlds = self.queryByEmployeeIdAndDate( employeeId, datetime.datetime.now().date()) if (temperatureOlds is not None and len(temperatureOlds) != 0): temperatureOlds[0].temperature = temperature db.session.commit() else: db.session.add(temperatureObj) db.session.commit() return employeeId
def update(sensor_type): if sensor_type not in ("humidity", "temperature"): abort(404) if request.headers.get("X-API-KEY") != app.config["API_KEY"]: abort(401) if not request.json or not "data" in request.json: abort(400) if sensor_type == "humidity": if not request.json.get("data").get("humidity"): abort(404) h = Humidity(humidity=request.json.get("data")["humidity"]) db.session.add(h) db.session.commit() else: if not request.json.get("data").get("temperature"): abort(404) t = Temperature(temperature=request.json.get("data")["temperature"]) db.session.add(t) db.session.commit() return jsonify({"status": "ok"})
def generate_daily_backup(): """Generate the daily backup.""" # Create daily resume Humidity.backup() Temperature.backup() return "BACKUP CREATED"
def get(self): log = logging.getLogger(__name__) time_now = datetime.datetime.now() try: grain_storehouses = list() grain_storehouses.append( GrainStorehouse(storehouse_no='1', storehouse_name='福州直属库')) db.session.add(grain_storehouses[0]) db.session.commit() except Exception as e: log.error("Creating grain_storehouse: %s", e) db.session.rollback() try: lora_gateways = list() lora_gateways.append( LoraGateway(gateway_addr='1', grain_storehouse=grain_storehouses[0])) db.session.add(lora_gateways[0]) db.session.commit() except Exception as e: log.error("Creating lora_gateway: %s", e) db.session.rollback() try: grain_barns = list() grain_barns.append( GrainBarn(barn_no='1', barn_name='37号仓', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], high_limit=30, low_limit=20)) grain_barns.append( GrainBarn(barn_no='2', barn_name='34号仓', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], high_limit=30, low_limit=20)) grain_barns.append( GrainBarn(barn_no='3', barn_name='41号仓', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], high_limit=30, low_limit=20)) grain_barns.append( GrainBarn(barn_no='4', barn_name='11号仓', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], high_limit=30, low_limit=20)) for i in range(len(grain_barns)): db.session.add(grain_barns[i]) db.session.commit() except Exception as e: log.error("Creating barns: %s", e) db.session.rollback() try: power_ios = list() power_ios.append( PowerIo(addr='1', name='34号仓配电箱1#', grain_barn=grain_barns[1])) power_ios.append( PowerIo(addr='2', name='34号仓配电箱2#', grain_barn=grain_barns[1])) power_ios.append( PowerIo(addr='3', name='37号仓配电箱1#', grain_barn=grain_barns[0])) power_ios.append( PowerIo(addr='4', name='11号仓配电箱1#', grain_barn=grain_barns[3])) power_ios.append( PowerIo(addr='5', name='41号仓配电箱1#', grain_barn=grain_barns[2])) power_ios.append( PowerIo(addr='6', name='37号仓配电箱2#', grain_barn=grain_barns[0])) power_ios.append( PowerIo(addr='7', name='41号仓配电箱2#', grain_barn=grain_barns[2])) power_ios.append( PowerIo(addr='8', name='11号仓配电箱2#', grain_barn=grain_barns[3])) db.session.add(power_ios[0]) db.session.add(power_ios[1]) db.session.add(power_ios[2]) db.session.add(power_ios[3]) db.session.add(power_ios[4]) db.session.add(power_ios[5]) db.session.add(power_ios[6]) db.session.add(power_ios[7]) db.session.commit() except Exception as e: log.error("Creating power_io: %s", e) db.session.rollback() try: tianshuo_485s = list() tianshuo_485s.append( TianshuoRs485(addr='1', name='11号仓1#空调', grain_barn=grain_barns[3])) tianshuo_485s.append( TianshuoRs485(addr='2', name='11号仓2#空调', grain_barn=grain_barns[3])) db.session.add(tianshuo_485s[0]) db.session.add(tianshuo_485s[1]) db.session.commit() except Exception as e: log.error("Creating tianshuo_485: %s", e) db.session.rollback() try: lora_nodes = list() lora_nodes.append( LoraNode(node_addr='29', node_name='37-1', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[0], power_io=power_ios[2], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='21', node_name='37-2', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[0], power_io=power_ios[2], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='17', node_name='37-3', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[0], power_io=power_ios[5], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='25', node_name='37-4', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[0], power_io=power_ios[5], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='18', node_name='34-1', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[1], power_io=power_ios[0], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='24', node_name='34-2', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[1], power_io=power_ios[0], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='10', node_name='34-3', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[1], power_io=power_ios[1], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='15', node_name='34-4', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[1], power_io=power_ios[1], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='16', node_name='41-1', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[2], power_io=power_ios[4], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='20', node_name='41-2', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[2], power_io=power_ios[4], current=1.0, current_no=2, current_limit=10, auto_manual='auto', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='19', node_name='41-3', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[2], power_io=power_ios[6], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='26', node_name='41-4', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[2], power_io=power_ios[6], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='27', node_name='11-1', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[3], power_io=power_ios[3], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='28', node_name='11-2', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[3], power_io=power_ios[3], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='22', node_name='11-3', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[3], power_io=power_ios[7], current=1.0, current_no=1, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) lora_nodes.append( LoraNode(node_addr='30', node_name='11-4', grain_storehouse=grain_storehouses[0], lora_gateway=lora_gateways[0], grain_barn=grain_barns[3], power_io=power_ios[7], current=1.0, current_no=2, current_limit=10, auto_manual='manual', manual_start_time=time_now, manual_end_time=time_now + datetime.timedelta(seconds=600), auto_start_time=time_now, auto_end_time=time_now)) db.session.add(lora_nodes[0]) db.session.add(lora_nodes[1]) db.session.add(lora_nodes[2]) db.session.add(lora_nodes[3]) db.session.add(lora_nodes[4]) db.session.add(lora_nodes[5]) db.session.add(lora_nodes[6]) db.session.add(lora_nodes[7]) db.session.add(lora_nodes[8]) db.session.add(lora_nodes[9]) db.session.add(lora_nodes[10]) db.session.add(lora_nodes[11]) db.session.add(lora_nodes[12]) db.session.add(lora_nodes[13]) db.session.add(lora_nodes[14]) db.session.add(lora_nodes[15]) db.session.commit() except Exception as e: log.error("Creating lora_node: %s", e) db.session.rollback() for i in range(1, 100): gt = GrainTemp() gt.grain_storehouse = grain_storehouses[0] gt.lora_gateway = lora_gateways[0] gt.grain_barn = grain_barns[0] gt.lora_node = lora_nodes[random.randint(0, 3)] gt.switch = False gt.temp1 = random.randrange(20, 30) gt.temp2 = random.randrange(20, 30) gt.temp3 = random.randrange(20, 30) gt.battery_vol = random.randint(1, 3) gt.datetime = datetime.datetime.now() db.session.add(gt) try: db.session.commit() print("inserted", gt) except Exception as e: log.error("Creating GrainTemp: %s", e) db.session.rollback() try: relay_current_rs485_funcs = list() relay_current_rs485_funcs.append( RelayCurrentRs485Func(function_name='suck_func_code', function_code='1000000001020100')) relay_current_rs485_funcs.append( RelayCurrentRs485Func(function_name='release_func_code', function_code='1000000001020000')) relay_current_rs485_funcs.append( RelayCurrentRs485Func(function_name='current_A1_A2_func_code', function_code='0300040004')) db.session.add(relay_current_rs485_funcs[0]) db.session.add(relay_current_rs485_funcs[1]) db.session.add(relay_current_rs485_funcs[2]) db.session.commit() except Exception as e: log.error("Creating relay_current_rs485_funcs: %s", e) db.session.rollback() try: power_io_rs485_funcs = list() power_io_rs485_funcs.append( PowerIoRs485Func(function_name='open_channel_1', function_code=5, start_at_reg_high=0, start_at_reg_low=16, num_of_reg_high=255, num_of_reg_low=0)) power_io_rs485_funcs.append( PowerIoRs485Func(function_name='close_channel_1', function_code=5, start_at_reg_high=0, start_at_reg_low=16, num_of_reg_high=0, num_of_reg_low=0)) # power_1_close = '010500100000CC0F' # power_1_open = '01050010FF008DFF' db.session.add(power_io_rs485_funcs[0]) db.session.add(power_io_rs485_funcs[1]) db.session.commit() except Exception as e: log.error("Creating power_io_rs485_funcs: %s", e) db.session.rollback() try: tianshuo_rs485_funcs = list() tianshuo_rs485_funcs.append( TianshuoRs485Func(function_name='off_and_cold', function_code=6, start_at_reg_high=0, start_at_reg_low=0, num_of_reg_high=0, num_of_reg_low=1)) tianshuo_rs485_funcs.append( TianshuoRs485Func(function_name='on_and_cold', function_code=6, start_at_reg_high=0, start_at_reg_low=0, num_of_reg_high=0, num_of_reg_low=9)) # off_cold = '010600000001' # on_cold = '010600000009' db.session.add(tianshuo_rs485_funcs[0]) db.session.add(tianshuo_rs485_funcs[1]) db.session.commit() except Exception as e: log.error("Creating tianshuo_rs485_funcs: %s", e) db.session.rollback() for i in range(1, 50): mq_func = NodeMqttTransFunc() mq_func.gateway_addr = '001' mq_func.node_addr = bitstring.pack('uint:13', i).bin mq_func.trans_direct = '1' mq_func.func_code = '0010001' mq_func.wind_direct = '00' mq_func.wind_speed = '11' mq_func.model = '0000011011' mq_func.on_off = '00' mq_func.work_mode = '001' mq_func.temp = '10000' db.session.add(mq_func) try: db.session.commit() print("inserted", mq_func) except Exception as e: log.error("Creating NodeMqttTransFunc: %s", e) db.session.rollback() try: alarm_level_setting = list() alarm_level_setting.append(AlarmLevelSetting(warning=57, error=68)) db.session.add(alarm_level_setting[0]) db.session.commit() except Exception as e: log.error("Creating alarm_level_setting: %s", e) db.session.rollback() try: alarm_statuses = list() alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[0], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[1], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[2], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[3], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[4], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[5], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[6], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[7], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[8], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[9], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[10], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[11], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[12], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[13], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[14], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) alarm_statuses.append( AlarmStatus(lora_node=lora_nodes[15], alarm_status=False, datetime=time_now, send_alarm_datetime=time_now)) db.session.add(alarm_statuses[0]) db.session.add(alarm_statuses[1]) db.session.add(alarm_statuses[2]) db.session.add(alarm_statuses[3]) db.session.add(alarm_statuses[4]) db.session.add(alarm_statuses[5]) db.session.add(alarm_statuses[6]) db.session.add(alarm_statuses[7]) db.session.add(alarm_statuses[8]) db.session.add(alarm_statuses[9]) db.session.add(alarm_statuses[10]) db.session.add(alarm_statuses[11]) db.session.add(alarm_statuses[12]) db.session.add(alarm_statuses[13]) db.session.add(alarm_statuses[14]) db.session.add(alarm_statuses[15]) db.session.commit() except Exception as e: log.error("Creating alarm_status: %s", e) db.session.rollback() try: alarm_types = list() alarm_types.append( AlarmTypes( alarm_type='Now temperature higher than the warning level!' )) alarm_types.append( AlarmTypes( alarm_type='Now temperature higher than the error level!')) alarm_types.append( AlarmTypes( alarm_type='Now current higher than the current limit!')) db.session.add(alarm_types[0]) db.session.add(alarm_types[1]) db.session.add(alarm_types[2]) db.session.commit() except Exception as e: log.error("Creating alarm_types: %s", e) db.session.rollback() try: alarm_records = list() alarm_records.append( AlarmRecords(lora_node=lora_nodes[0], alarm_type=alarm_types[0], datetime=time_now)) alarm_records.append( AlarmRecords(lora_node=lora_nodes[1], alarm_type=alarm_types[1], datetime=time_now)) alarm_records.append( AlarmRecords(lora_node=lora_nodes[2], alarm_type=alarm_types[2], datetime=time_now)) alarm_records.append( AlarmRecords(lora_node=lora_nodes[3], alarm_type=alarm_types[0], datetime=time_now)) alarm_records.append( AlarmRecords(lora_node=lora_nodes[4], alarm_type=alarm_types[1], datetime=time_now)) alarm_records.append( AlarmRecords(lora_node=lora_nodes[5], alarm_type=alarm_types[2], datetime=time_now)) db.session.add(alarm_records[0]) db.session.add(alarm_records[1]) db.session.add(alarm_records[2]) db.session.add(alarm_records[3]) db.session.add(alarm_records[4]) db.session.add(alarm_records[5]) db.session.commit() except Exception as e: log.error("Creating alarm_records: %s", e) db.session.rollback() try: daq_projects = list() daq_projects.append(Project(name='project1')) daq_projects.append(Project(name='project2')) daq_projects.append(Project(name='project3')) db.session.add(daq_projects[0]) db.session.add(daq_projects[1]) db.session.add(daq_projects[2]) db.session.commit() except Exception as e: log.error("Creating daq_projects: %s", e) db.session.rollback() try: daq_workers = list() daq_workers.append(Worker(name='worker1', project=daq_projects[0])) daq_workers.append(Worker(name='worker2', project=daq_projects[0])) daq_workers.append(Worker(name='worker1', project=daq_projects[1])) daq_workers.append(Worker(name='worker2', project=daq_projects[1])) daq_workers.append(Worker(name='worker1', project=daq_projects[2])) daq_workers.append(Worker(name='worker2', project=daq_projects[2])) db.session.add(daq_workers[0]) db.session.add(daq_workers[1]) db.session.add(daq_workers[2]) db.session.add(daq_workers[3]) db.session.add(daq_workers[4]) db.session.add(daq_workers[5]) db.session.commit() except Exception as e: log.error("Creating daq_workers: %s", e) db.session.rollback() for i in range(1, 100): gt = Temperature() gt.project = daq_projects[0] gt.worker = daq_workers[0] gt.datetime = datetime.datetime.now() gt.value = json.dumps([[x, round(random.uniform(250, 300), 2)] for x in range(20)]) db.session.add(gt) try: db.session.commit() print("inserted", gt) except Exception as e: log.error("Creating Temperature: %s", e) db.session.rollback() for i in range(1, 10): power = Power() power.project = daq_projects[0] power.worker = daq_workers[0] power.datetime = datetime.datetime.now() powerValue1 = [ round(random.uniform(0, 30), 2) for x in range(0, 8) ] powerValue2 = [ round(random.uniform(0, 30), 2) for x in range(0, 8) ] powerMoudle1 = [1, powerValue1] powerMoudle2 = [2, powerValue2] power.value = json.dumps([powerMoudle1, powerMoudle2]) db.session.add(power) try: db.session.commit() print("inserted", power) except Exception as e: log.error("Creating Power: %s", e) db.session.rollback() return jsonify({'success': 'auto insert init datas!'})
def get(self): log = logging.getLogger(__name__) time_now = datetime.datetime.now() try: daq_projects = list() daq_projects.append(Project(name='project1')) daq_projects.append(Project(name='project2')) daq_projects.append(Project(name='project3')) db.session.add(daq_projects[0]) db.session.add(daq_projects[1]) db.session.add(daq_projects[2]) db.session.commit() except Exception as e: log.error("Creating daq_projects: %s", e) db.session.rollback() try: daq_workers = list() daq_workers.append(Worker(name='worker1', project=daq_projects[0])) daq_workers.append(Worker(name='worker2', project=daq_projects[0])) daq_workers.append(Worker(name='worker1', project=daq_projects[1])) daq_workers.append(Worker(name='worker2', project=daq_projects[1])) daq_workers.append(Worker(name='worker1', project=daq_projects[2])) daq_workers.append(Worker(name='worker2', project=daq_projects[2])) db.session.add(daq_workers[0]) db.session.add(daq_workers[1]) db.session.add(daq_workers[2]) db.session.add(daq_workers[3]) db.session.add(daq_workers[4]) db.session.add(daq_workers[5]) db.session.commit() except Exception as e: log.error("Creating daq_workers: %s", e) db.session.rollback() for i in range(1, 100): gt = Temperature() gt.project = daq_projects[0] gt.worker = daq_workers[0] gt.datetime = datetime.datetime.now() gt.value = json.dumps([[x,round(random.uniform(250,300),2)] for x in range(20)]) db.session.add(gt) try: db.session.commit() print("inserted", gt) except Exception as e: log.error("Creating Temperature: %s", e) db.session.rollback() for i in range(1, 10): power = Power() power.project = daq_projects[0] power.worker = daq_workers[0] power.datetime = datetime.datetime.now() powerValue1 = [round(random.uniform(0, 30),2) for x in range(0,8)] powerValue2 = [round(random.uniform(0, 30),2) for x in range(0,8)] powerMoudle1 = [1, powerValue1] powerMoudle2 = [2, powerValue2] power.value = json.dumps([powerMoudle1,powerMoudle2]) db.session.add(power) try: db.session.commit() print("inserted", power) except Exception as e: log.error("Creating Power: %s", e) db.session.rollback() return jsonify({'success': 'auto insert init datas!'})
def parse_data(data): latestDate = str(data['time']) if latestDate[:10] != currentData['date'][:10]: d = Day(year=currentData['date'][:4], month=currentData['date'][5:7], day=currentData['date'][8:10], maxtemp=temprange['maxtemp'], mintemp=temprange['mintemp'], avetemp=temprange['avetemp'], rain=currentData['rain']) db.session.add(d) db.session.commit() del temps[:] del temptime[:] del rain[:] print("New Day!") currentData['date'] = str(data['time']) try: try: checkDate = Temperature.query.all() if checkDate[0].date[:10] != latestDate[:10]: Temperature.query.delete() Rain.query.delete() db.session.commit() except Exception: pass currentData['temp'] = round( (float(data['temperature_F']) - 32) * (0.55), 2) t = Temperature(date=currentData['date'], temp=currentData['temp']) db.session.add(t) db.session.commit() myTemps = Temperature.query.all() for x in myTemps: temps.append(x.temp) temptime.append(x.date) temprange['maxtemp'] = max(temps) temprange['maxtime'] = temptime[temps.index(max(temps))] temprange['mintemp'] = min(temps) temprange['mintime'] = temptime[temps.index(min(temps))] temprange['avetemp'] = round(sum(temps) / float(len(temps)), 2) del temps[:] del temptime[:] except Exception: pass try: currentData['windspeed'] = round( float(data['wind_speed_mph']) * 1.60934, 2) #print(currentWindSpeed) except Exception: pass try: currentData['winddir'] = str(data['wind_dir']) #print(currentWindDir) except Exception: pass try: currentData['humidity'] = str(data['humidity']) #print(currentHumidity) except Exception: pass try: r = Rain(rain=float(data['raincounter_raw'])) db.session.add(r) db.session.commit() myRain = Rain.query.all() for x in myRain: rain.append(x.rain) currentData['rain'] = (max(rain) - min(rain)) * 0.25 del rain[:] #print(currentRain) except Exception: pass
gender='男', name='小华爸爸', phone_number='11111111110') visitor3 = Visitors(id_number='111111111111111101', id_type='身份证', address='鹏翔小区B栋103', gender='男', name='小华妈妈', phone_number='11111111101') visitor4 = Visitors(id_number='111111111111111100', id_type='身份证', address='鹏翔小区B栋203', gender='男', name='甜甜', phone_number='11111111100') visitor5 = Visitors(id_number='111111111111111001', id_type='身份证', address='鹏翔小区B栋303', gender='男', name='妞妞', phone_number='11111111001') face6 = Face(id_type='身份证', id_number='111111111111111111') face7 = Face(id_type='身份证', id_number='111111111111111110') face8 = Face(id_type='身份证', id_number='111111111111111101') face9 = Face(id_type='身份证', id_number='111111111111111100') face10 = Face(id_type='身份证', id_number='111111111111111001') #temperatures = [Temperature(resident=resident1, isresident=True, temperature=26.5, gate_number=1), # Temperature(visitor=visitor1, isresident=False, temperature=26.5, gate_number=2)] temperatures1 = Temperature(resident=resident1, isresident=True, temperature=36.5, gate_number=1, record_timestamp=datetime.now()) temperatures2 = Temperature(resident=resident2, isresident=True, temperature=36.5, gate_number=2, record_timestamp=datetime.now()+timedelta(hours=1)) temperatures3 = Temperature(visitor=visitor1, isresident=False, temperature=36.5, gate_number=3, record_timestamp=datetime.now()+timedelta(hours=3)) temperatures4 = Temperature(visitor=visitor2, isresident=False, temperature=36.5, gate_number=2, record_timestamp=datetime.now()+timedelta(hours=2)) temperatures5 = Temperature(resident=resident3, isresident=True, temperature=37.5, gate_number=3, record_timestamp=datetime.now()+timedelta(hours=4)) db.session.add_all([admin, user1, user2, user3, user4, book1, book2, book3, book4, book5, book6] + logs) db.session.add_all([resident1, resident2, resident3, resident4, resident5]) db.session.add_all([visitor1, visitor2, visitor3, visitor4, visitor5]) db.session.add_all([temperatures1, temperatures2, temperatures3, temperatures4, temperatures5]) db.session.add_all([face1, face2, face3, face4, face5, face6, face7, face8, face9, face10]) # db.session.add_all([people1, people2])