#!/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 ''' from tag.tag_mysql import Tag import traceback import time import random import requests import json 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 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) tag_store = {} # configProperties = {} def main(): # 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' 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('test') except: print("ERROR EVALUATING {}".format(tag)) traceback.print_exc() time.sleep(10) if __name__ == '__main__': main()