-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate_historian_data.py
128 lines (98 loc) · 3.3 KB
/
populate_historian_data.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Last Updated on 25th Feb, 2023
Authored by: Shashi Shekhar
Reviewed by: Reetesh varshney
"""
import mysql.connector
import logging
import random
import uuid
import time
import calendar
import datetime
logger = logging.getLogger()
# Initialize database related parameters
db_host_name = "<Enter Host name or IP addres of the Historian DB>"
db_user = "<User ID of the Historian DB>"
db_password = "<Password of the Historian DB>"
db_name = "<Database name>"
# Create the connection object
myconn = mysql.connector.connect(
host=db_host_name, user=db_user, passwd=db_password, database=db_name)
# creating the cursor object
cur = myconn.cursor()
# Example: "/ER/297/Generator/Temperature"
property_alias = "<sitewise measurement property alias>"
temperature = 0.0000000
quality = 'UNCERTAIN'
# This method creates simulated historian record to be read by custom component
def insertIntoHistorian(uid):
# historian data are intialized as global variables
global temperature
global quality
global property_alias
global myconn
global cur
ct = datetime.datetime.now()
ts = ct.timestamp()
# create record into ER_297_GENERATOR historian table
insert_stmt = (
"INSERT INTO ER_297_GENERATOR (ASSET_VALUE, PROPERTY_ALIAS, DATA_QUALITY, ID, DATE_TIME) VALUES (%s, %s, %s, %s, %s);")
data = (temperature, property_alias, quality, uid, ct)
try:
cur.execute(insert_stmt, data)
myconn.commit()
print("Data insertion committed")
except Exception as error:
print(error)
print(type(error))
print("Data insertion rolledback")
myconn.rollback()
# This methiod creates random measurement value to be inserted into Historian table
def getValues():
global temperature
global quality
try:
temperature = random.uniform(20, 25) # nosec
quality = 'GOOD'
seed = random.uniform(1, 10) # nosec
logger.info("seed: {}".format(seed))
logger.info("temperature: {}".format(temperature))
if seed == 3 or seed == 6:
temperature = random.uniform(35, 45) # nosec
quality = 'BAD'
elif seed == 7:
temperature = random.uniform(25, 35) # nosec
quality = 'UNCERTAIN'
temperature = float(temperature)
logger.info("temperature: {}".format(temperature))
print(temperature)
print(quality)
except Exception as error:
logger.error("error during random temperature generation: %s", error)
# To maintain a balance between number of records inserted and streamed to sitewise, data older than 8 hours will be deleted.
def deleteOlderRecord():
global myconn
global cur
delete_stmt = (
"DELETE FROM ER_297_GENERATOR WHERE DATE_TIME < now() - interval 480 MINUTE;")
try:
cur.execute(delete_stmt)
myconn.commit()
print("Data delete committed")
except Exception as error:
print(error)
print(type(error))
print("Data delete rolledback")
myconn.rollback()
def main():
global myconn
while True:
deleteOlderRecord()
uid = str(uuid.uuid4())
getValues()
insertIntoHistorian(uid)
time.sleep(2)
myconn.close()
main()