Tag Server website works without data

This commit is contained in:
Patrick McDonagh
2016-04-27 19:23:14 -05:00
parent 80d965dfcc
commit c64083c20b
32 changed files with 1615 additions and 168 deletions

View File

@@ -6,13 +6,13 @@ 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
import requests
import json
class Sample(Tag):
@@ -48,86 +48,93 @@ class Sample(Tag):
self.sendToDB()
return self.value
def sendToDB(self):
data = {}
data['val'] = self.value
data['tagID'] = self.db_id
r = requests.post('http://localhost:1337/tag_val', data=data)
resp = json.loads(r.text)
print("Stored {} for {} at {}".format(resp['val'], self.name, resp['createdAt']))
with open(os.path.realpath('.') + '/mysql_cfg.pickle', 'rb') as pickleconfig:
mysql_cfg = pickle.load(pickleconfig)
if mysql_cfg:
db = mysqlcon.connect(**mysql_cfg)
# 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 = {}
# 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()
# Get tags stored in database
get_tag_request_data = {'where':'{"tag_class":5}'}
get_tag_request = requests.get('http://localhost:1337/tag', params=get_tag_request_data)
tags = json.loads(get_tag_request.text)
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'
# 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'])
for t in tags:
# name, tag, db_id, data_type, change_threshold, guarantee_sec, mapFn=None, device_type='CLX', ip_address='192.168.1.10'):
tag_store[t['name']] = Sample(t['name'], t['tag'],t['id'], t['data_type'], t['change_threshold'], t['guarantee_sec'], mapFn=t['map_function'], ip_address=t['deviceID']['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'])
while True:
for tag in tag_store:
try:
tag_store[tag].read('test')
except:
print("ERROR EVALUATING {}".format(tag))
traceback.print_exc()
time.sleep(10)
if __name__ == '__main__':
main()
main()