diff --git a/myems-api/core/datasource.py b/myems-api/core/datasource.py index c2da0c3811b3b744eeba77f8b8fc9aec30ea2b39..d05c7e093b6872410adc5ded67953b2ee0c09442 100644 --- a/myems-api/core/datasource.py +++ b/myems-api/core/datasource.py @@ -462,6 +462,23 @@ class DataSourcePointCollection: cursor.execute(query_point, (id_,)) rows_point = cursor.fetchall() + cnx_history = mysql.connector.connect(**config.myems_historical_db) + history = cnx_history.cursor() + history.execute(" SELECT point_id, utc_date_time, actual_value " + " FROM tbl_analog_value_latest " + " WHERE utc_date_time >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 10 MINUTE)") + analog = history.fetchall() + + history.execute(" SELECT point_id, utc_date_time, actual_value " + " FROM tbl_digital_value_latest " + " WHERE utc_date_time >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 10 MINUTE)") + digital = history.fetchall() + + history.execute(" SELECT point_id, utc_date_time, actual_value " + " FROM tbl_energy_value_latest " + " WHERE utc_date_time >= DATE_SUB(UTC_TIMESTAMP(), INTERVAL 10 MINUTE)") + energy = history.fetchall() + if rows_point is not None and len(rows_point) > 0: for row in rows_point: meta_result = {"id": row[0], @@ -476,11 +493,25 @@ class DataSourcePointCollection: "is_trend": bool(row[9]), "is_virtual": bool(row[10]), "address": row[11], - "description": row[12]} + "description": row[12], + "analog_value": None, + "digital_value": None, + "energy_value": None} + for point in analog: + if point[0] == meta_result['id'] and isinstance(point[1], datetime): + meta_result['analog_value'] = point[2] + for point in digital: + if point[0] == meta_result['id'] and isinstance(point[1], datetime): + meta_result['digital_value'] = point[2] + for point in energy: + if point[0] == meta_result['id'] and isinstance(point[1], datetime): + meta_result['energy_value'] = point[2] result.append(meta_result) cursor.close() cnx.close() + history.close() + cnx_history.close() resp.text = json.dumps(result)