File size: 3,329 Bytes
3b66598
66977cd
 
3b66598
e8d4213
dbd7ac0
9627b36
3b66598
 
 
 
767e14d
 
 
 
 
66977cd
 
3b66598
66977cd
 
 
e0c688d
 
 
 
66977cd
 
 
767e14d
 
66977cd
 
 
 
 
3b66598
e0c688d
 
 
 
3b66598
4a389dc
3b66598
4a389dc
dbd7ac0
e8d4213
816790f
 
 
 
 
 
 
e8d4213
 
 
 
3b66598
816790f
 
 
 
 
 
 
767e14d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a389dc
3b66598
 
 
 
816790f
3b66598
 
 
 
4a389dc
 
 
3b66598
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json
from rtu.RTUAnomalizer1 import RTUAnomalizer1
from rtu.RTUAnomalizer2 import RTUAnomalizer2
from rtu.RTUPipeline import RTUPipeline
from vav.VAVPipeline import VAVPipeline
from vav.VAVAnomalizer import VAVAnomalizer
from energy_prediction.EnergyPredictionNorth import EnergyPredictionNorth
import paho.mqtt.client as mqtt


def main():
    rtu_data_pipeline = RTUPipeline(
        scaler1_path="src/rtu/models/scaler_rtu_1_2.pkl",
        scaler2_path="src/rtu/models/scaler_rtu_3_4.pkl",
    )
    # RTU - 1, 2
    rtu_anomalizer1 = RTUAnomalizer1(
        prediction_model_path="src/rtu/models/lstm_2rtu_smooth_04.keras",
        clustering_model_paths=[
            "src/rtu/models/kmeans_rtu_1.pkl",
            "src/rtu/models/kmeans_rtu_2.pkl",
        ],
        pca_model_paths=[
            "src/rtu/models/pca_rtu_1.pkl",
            "src/rtu/models/pca_rtu_2.pkl",
        ],
        num_inputs=rtu_data_pipeline.num_inputs,
        num_outputs=rtu_data_pipeline.num_outputs,
    )
    print(rtu_anomalizer1.kmeans_models)
    # RTU - 3,4
    rtu_anomalizer2 = RTUAnomalizer2(
        prediction_model_path="src/rtu/models/lstm_2rtu_smooth_03.keras",
        clustering_model_paths=[
            "src/rtu/models/kmeans_rtu_3.pkl",
            "src/rtu/models/kmeans_rtu_4.pkl",
        ],
        pca_model_paths=[
            "src/rtu/models/pca_rtu_3.pkl",
            "src/rtu/models/pca_rtu_4.pkl",
        ],
        num_inputs=rtu_data_pipeline.num_inputs,
        num_outputs=rtu_data_pipeline.num_outputs,
    )

    vav_pipeline = VAVPipeline(rtu_id=1, scaler_path="src/vav/models/scaler_vav_1.pkl")

    vav_anomalizer = VAVAnomalizer(
        rtu_id=1,
        prediction_model_path="src/vav/models/lstm_vav_01.keras",
        clustering_model_path="src/vav/models/kmeans_vav_1.pkl",
        num_inputs=vav_pipeline.num_inputs,
        num_outputs=vav_pipeline.num_outputs,
    )
    # print(vav_pipeline.input_col_names)

    # print(len(vav_pipeline.output_col_names))

    def on_message(client, userdata, message):
        df_new_vav, df_trans_vav = vav_pipeline.fit(message)
        vav_anomalizer.num_inputs = vav_pipeline.num_inputs
        vav_anomalizer.num_outputs = vav_pipeline.num_outputs
        if not df_new_vav is None and not df_trans_vav is None:
            out_vav = vav_anomalizer.pipeline(
                df_new_vav, df_trans_vav, vav_pipeline.scaler
            )

        df_new1, df_trans1, df_new2, df_trans2 = rtu_data_pipeline.fit(message)
        if (
            not df_new1 is None
            and not df_trans1 is None
            and not df_new2 is None
            and not df_trans2 is None
        ):
            out1, out2, out3, out4 = rtu_anomalizer1.pipeline(
                df_new1, df_trans1, rtu_data_pipeline.scaler1
            )
            out5, out6, out7, out8 = rtu_anomalizer2.pipeline(
                df_new2, df_trans2, rtu_data_pipeline.scaler2
            )
            # print(out2)

    broker_address = "localhost"
    broker_port = 1883
    topic = "sensor_data"
    client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
    print("Connecting to broker")
    client.on_message = on_message
    client.connect(broker_address, broker_port)
    client.subscribe(topic)
    client.loop_forever()


if __name__ == "__main__":
    main()