134 lines
4.2 KiB
Python
134 lines
4.2 KiB
Python
#!/usr/bin/env python
|
|
|
|
'''
|
|
MySQL Tag Server
|
|
Created on April 7, 2016
|
|
@author: Patrick McDonagh
|
|
@description: Continuously loops through a list of tags to store values from a PLC into a MySQL database
|
|
'''
|
|
import mysql.connector as mysqlcon
|
|
import pickle
|
|
from tag.tag_mysql import Tag
|
|
import traceback
|
|
import time
|
|
import os
|
|
import random
|
|
|
|
|
|
class Sample(Tag):
|
|
def read(self, forceSend):
|
|
writeToDB = False
|
|
if self.tag:
|
|
v = 0.0
|
|
if not (self.value is None):
|
|
v = [self.value + (10.0 * (random.random() - 0.5))]
|
|
else:
|
|
v = [random.random() * 100.0]
|
|
if v:
|
|
val = v[0]
|
|
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
|
if self.mapFn:
|
|
val = self.mapFn[val]
|
|
if (self.last_send_time == 0) or (self.value is None) or not (self.value == val) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend is True):
|
|
self.last_value = self.value
|
|
self.value = val
|
|
writeToDB = True
|
|
else:
|
|
writeToDB = False
|
|
else:
|
|
if (self.last_send_time == 0) or (self.value is None) or (abs(self.value - v[0]) > self.chg_threshold) or ((time.time() - self.last_send_time) > self.guarantee_sec) or (forceSend is True):
|
|
self.last_value = self.value
|
|
self.value = v[0]
|
|
writeToDB = True
|
|
else:
|
|
writeToDB = False
|
|
if forceSend is False:
|
|
writeToDB = False
|
|
if writeToDB:
|
|
self.sendToDB()
|
|
return self.value
|
|
|
|
|
|
with open(os.path.realpath('.') + '/mysql_cfg.pickle', 'rb') as pickleconfig:
|
|
mysql_cfg = pickle.load(pickleconfig)
|
|
|
|
if mysql_cfg:
|
|
db = mysqlcon.connect(**mysql_cfg)
|
|
|
|
tag_store = {}
|
|
configProperties = {}
|
|
|
|
def main():
|
|
db.connect()
|
|
cur = db.cursor()
|
|
query = "SELECT * FROM tags WHERE class = 5 AND deleted = 0"
|
|
cur.execute(query)
|
|
tags = cur.fetchall()
|
|
print tags
|
|
# [(1, u'Century Counter Up', 5, u'Century_Counter_Up', u'REAL', 10.0, 3600, None, 0)]
|
|
db.disconnect()
|
|
|
|
|
|
configObj = {}
|
|
db.connect()
|
|
cur = db.cursor()
|
|
query = "SELECT parameter, val FROM config GROUP BY parameter;"
|
|
cur.execute(query)
|
|
config = cur.fetchall()
|
|
db.disconnect()
|
|
for x in config:
|
|
configObj[x[0]] = x[1]
|
|
|
|
try:
|
|
configProperties['PLC_IP_ADDRESS'] = str(configObj['ip_address'])
|
|
print("FYI, using PLC IP Address from the database {0}".format(configProperties['PLC_IP_ADDRESS']))
|
|
except KeyError:
|
|
print("FYI, there is no PLC IP Address stored in the database, defaulting to 192.168.1.10")
|
|
configProperties['PLC_IP_ADDRESS'] = "192.168.1.10"
|
|
|
|
try:
|
|
configProperties['plc_type'] = str(configObj['plc_type'])
|
|
print("FYI, using PLC Type from the database {0}".format(configProperties['plc_type']))
|
|
except KeyError:
|
|
print("FYI, there is no PLC Type stored in the database, defaulting to CLX")
|
|
configProperties['plc_type'] = "CLX"
|
|
|
|
try:
|
|
configProperties['scan_rate'] = int(configObj['scan_rate'])
|
|
print("FYI, using Scan Rate from the database {0}".format(configProperties['scan_rate']))
|
|
except KeyError:
|
|
print("FYI, there is no Scan Rate stored in the database, defaulting to 10 seconds")
|
|
configProperties['scan_rate'] = 10
|
|
|
|
try:
|
|
sa_test = str(configObj['save_all'])
|
|
if sa_test.lower() == "true":
|
|
configProperties['save_all'] = True
|
|
elif sa_test.lower() == "false":
|
|
configProperties['save_all'] = False
|
|
else:
|
|
configProperties['save_all'] = "test"
|
|
print("FYI, value for save_all is {0}".format(configProperties['save_all']))
|
|
except KeyError:
|
|
print("FYI, there is no save_all value stored in the database, using 'test'")
|
|
configProperties['save_all'] = 'test'
|
|
|
|
|
|
|
|
|
|
for t in tags:
|
|
tag_store[t[1]] = Sample(t[1], t[3], t[0], t[5], t[6], t[7], mapFn=t[8], device_type=configProperties['plc_type'], ip_address=configProperties['PLC_IP_ADDRESS'])
|
|
|
|
|
|
while True:
|
|
for tag in tag_store:
|
|
try:
|
|
tag_store[tag].read(configProperties['save_all'])
|
|
except:
|
|
print("ERROR EVALUATING {}".format(tag))
|
|
traceback.print_exc()
|
|
time.sleep(configProperties['scan_rate'])
|
|
|
|
if __name__ == '__main__':
|
|
main()
|