def sensor_delete(message: str) -> None: delete_sensor = json.loads(message) sensor_config = get_sensor_config() delete_sensor_id = delete_sensor['sensor_id'] for sensor in sensor_config: if sensor['sensor_id'] == delete_sensor_id: # Sensor does not found. if sensor['sensor'] == {}: raise SensorNotFound('Sensor does not found.') deleted_sensor = sensor['sensor'] sensor['sensor'] = {} break set_sensor_config(sensor_config) slack_post_text = SLACK_DELETE_SENSOR_NOTIFICATION_FORMAT.format( now=formated_str_now_date(), sensor_id=delete_sensor_id, name=deleted_sensor['name'], description=deleted_sensor['description'], type_=deleted_sensor['type']) post_slack_by_type(text=slack_post_text, type_=SLACK_NOTIFICATION_TYPE['NOTIFICATION']) publish_sensor_config()
def sensor_exchange(message: dict) -> None: exchange_sensors = json.loads(message)['sensors'] sensor_config = get_sensor_config() result = exchanged(exchange_sensors, sensor_config) set_sensor_config(result) post_slack_by_type(text='Sensors are exchanged.', type_=SLACK_NOTIFICATION_TYPE['NOTIFICATION'])
def sensor_calibration_update(sensor_id: int, calibration: List[List[int]]) -> None: sensor_config = get_sensor_config() for sensor in sensor_config: if sensor['sensor_id'] == sensor_id: # Sensor does not found. if sensor['sensor'] == {}: raise SensorNotFound('Sensor does not found.') before_sensor = dict(sensor['sensor']) sensor['sensor']['calibration'] = calibration_format(calibration) break set_sensor_config(sensor_config) publish_sensor_config()
def sensor_update(message: str) -> None: update_sensor = json.loads(message) sensor_config = get_sensor_config() update_sensor_id = update_sensor['sensor_id'] for sensor in sensor_config: if sensor['sensor_id'] == update_sensor_id: # Sensor does not found. if sensor['sensor'] == {}: raise SensorNotFound('Sensor does not found.') # This sensor type does not exist. if update_sensor['type'] not in SENSOR_TYPE.values(): raise SensorTypeNotExist('This sensor type does not exist.') before_sensor = dict(sensor['sensor']) sensor['sensor']['name'] = update_sensor['name'] sensor['sensor']['description'] = update_sensor['description'] sensor['sensor']['type'] = update_sensor['type'] sensor['sensor']['updated_at'] = int( datetime.datetime.now().strftime('%s')) break set_sensor_config(sensor_config) slack_post_text = SLACK_UPDATE_SENSOR_NOTIFICATION_FORMAT.format( now=formated_str_now_date(), sensor_id=update_sensor_id, before_name=before_sensor['name'], before_description=before_sensor['description'], before_type=before_sensor['type'], after_name=update_sensor['name'], after_description=update_sensor['description'], after_type=update_sensor['type'], ) post_slack_by_type(text=slack_post_text, type_=SLACK_NOTIFICATION_TYPE['NOTIFICATION']) publish_sensor_config()
def sensor_create(message: str) -> None: new_sensor = json.loads(message) sensor_config = get_sensor_config() new_sensor_id = new_sensor['sensor_id'] for sensor in sensor_config: if sensor['sensor_id'] == new_sensor_id: # Sensor already exists. if sensor['sensor']: raise SensorAlreadyExist('Sensor already exists.') # This sensor type does not exist. if new_sensor['type'] not in SENSOR_TYPE.values(): raise SensorTypeNotExist('This sensor type does not exist.') sensor['sensor'] = { 'name': new_sensor['name'], 'description': new_sensor['description'], 'type': new_sensor['type'], 'calibration': [], 'created_at': int( datetime.datetime.now().strftime('%s') ), 'updated_at': int( datetime.datetime.now().strftime('%s') ), } break set_sensor_config(sensor_config) slack_post_text = SLACK_CREATE_SENSOR_NOTIFICATION_FORMAT.format( now = formated_str_now_date(), sensor_id = new_sensor_id, name = new_sensor['name'], description = new_sensor['description'], type_ = new_sensor['type'] ) post_slack_by_type( text = slack_post_text, type_ = SLACK_NOTIFICATION_TYPE['NOTIFICATION'] ) publish_sensor_config()
def get_sensor_backup_file() -> str: return get_sensor_config()
def publish_sensor_data() -> None: sensing_config = get_config_item('SENSING') sensor_data_publish_period = sensing_config['SENSOR_DATA_PUBLISH_PERIOD'] sensing_number = sensing_config['SENSING_NUMBER'] sensor_config = get_sensor_config() total_value = {} for sensor in sensor_config: # if a sensor exists if sensor['sensor']: total_value[sensor['sensor_id']] = 0 for _ in range(sensing_number): for sensor_id in total_value.keys(): sensor_value = read_mcp(sensor_id) total_value[sensor_id] += sensor_value time.sleep(sensor_data_publish_period / sensing_number) publish_data = { "timestamp": int( datetime.datetime.now().strftime('%s') ), "sensors": [], } for sensor in sensor_config: # if a sensor does not exists if sensor['sensor'] == {}: continue # Least squares coefficients if len(sensor['sensor']['calibration']) > 1: a, b = least_squares( sensor['sensor']['calibration'] ) else: # use raw value a, b = 1, 0 publish_data['sensors'].append({ "sensor_id": sensor['sensor_id'], "name": sensor['sensor']['name'], "type": sensor['sensor']['type'], "value": round( a * total_value[sensor['sensor_id']] / sensing_number + b, 2), "raw_value": round(total_value[sensor['sensor_id']] / sensing_number, 1), }) publish_topic = publish_topics['SENSOR_DATA'] publish_data_json = json.dumps(publish_data) write_current_sensor_values(publish_data_json) publish( topic = publish_topic, message = publish_data_json, qos = 0, retain = True, ) print_color_log( title = LOG_TITLE['SENSOR'], title_color = Color.YELLOW, text = '{unixtime}: {topic}: {message}'.format( unixtime = datetime.datetime.now().strftime('%s'), topic = color_text(publish_topic, Color.GREEN), message = publish_data_json, ) )
def get_sensor_config_no_calibration() -> dict: sensor_config = get_sensor_config() for sensor in sensor_config: if sensor['sensor']: del sensor['sensor']['calibration'] return sensor_config
def get_sensors(): ams_logger(request, DEBUG) sensors = get_sensor_config() return jsonify(sensors)