62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
import time
|
|
import random
|
|
import pymysql
|
|
import datetime
|
|
|
|
DB_CONFIG = {
|
|
'host': 'localhost',
|
|
'user': 'root',
|
|
'password': 'password',
|
|
'database': 'mmcl_db',
|
|
'autocommit': True
|
|
}
|
|
|
|
def get_db_connection():
|
|
try:
|
|
return pymysql.connect(**DB_CONFIG)
|
|
except Exception as e:
|
|
print(f"DB Connection Error: {e}")
|
|
return None
|
|
|
|
def generate_mock_data():
|
|
conn = get_db_connection()
|
|
if not conn:
|
|
return
|
|
|
|
try:
|
|
with conn.cursor() as cursor:
|
|
# Type 1 (NILM) 센서 가져오기
|
|
cursor.execute("SELECT sensor_no FROM sensor_info WHERE sensor_typeid = 1")
|
|
devices = cursor.fetchall()
|
|
|
|
while True:
|
|
for device in devices:
|
|
sensor_no = device[0]
|
|
pwr = random.uniform(300.0, 800.0)
|
|
current = pwr / 220.0
|
|
volt = random.uniform(218.0, 222.0)
|
|
now = datetime.datetime.now()
|
|
|
|
# 1. sensor_info (현재 상태) 업데이트
|
|
update_sql = """UPDATE sensor_info
|
|
SET update_time = %s, value_ch1_pwr = %s, value_ch1_current = %s, value_ch1_volt = %s
|
|
WHERE sensor_no = %s"""
|
|
cursor.execute(update_sql, (now, pwr, current, volt, sensor_no))
|
|
|
|
# 2. sensor_history_log (이력) 삽입
|
|
insert_sql = """INSERT INTO sensor_history_log (sensor_no, log_time, value_ch1_pwr, value_ch1_current, value_ch1_volt)
|
|
VALUES (%s, %s, %s, %s, %s)"""
|
|
cursor.execute(insert_sql, (sensor_no, now, pwr, current, volt))
|
|
|
|
print(f"[NILM Agent] Data updated & logged for Sensor {sensor_no}: {pwr:.2f}W")
|
|
|
|
time.sleep(2)
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
finally:
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
print("Starting NILM Mock Agent...")
|
|
generate_mock_data()
|