Files
datalogger-POC/backupPLC.py
Patrick McDonagh 340ac5ab60 Initial Commit
2015-12-08 22:19:39 -06:00

139 lines
4.2 KiB
Python

#!/usr/bin/env python
'''
Created on Oct 1, 2014
@author: PJMcdona
'''
#from prettytable import PrettyTable
import csv
from datetime import datetime
import os
import sys
from random import randint
from time import sleep
import MySQLdb
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData")
cur = db.cursor()
query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;"
cur.execute(query)
setup = cur.fetchall()
db.commit()
db.close()
PLC_IP_ADDRESS = setup[0][2]
PLC_TYPE = setup[0][1]
sys.path.append("../")
path_to_CSV = "/mnt/usb/"
#TUXEIP Connection to PLC
from tuxeip import TuxEIP, LGX, LGX_REAL
tux = TuxEIP(libpath="/usr/lib/libtuxeip.so")
sess = tux.OpenSession(PLC_IP_ADDRESS)
reg = tux.RegisterSession(sess)
conn = tux.ConnectPLCOverCNET(sess, LGX, 1, 100, 123, randint(0,9999), 123, 321, 100, 5000, 1, '01')
now = datetime.now()
nowDT = datetime.strftime(now,"%Y%m%d_%H%M%S")
filename = path_to_CSV + "backup_"+nowDT+".csv"
if os.path.exists(filename):
os.remove(filename)
taper_setup_params = [
["Length","REAL"],
["Diameter","REAL"],
["Material","DINT"],
["c","REAL"],
["UseRodCount","BOOL"],
["RodCount","DINT"]
]
data = {}
tags_to_read = [['Casing_ID', 'REAL'],
['UnitConfig.MotorNameplate.Volts', 'REAL'],
['UnitConfig.MotorNameplate.Amps', 'REAL'],
['UnitConfig.MotorNameplate.Hertz', 'REAL'],
['UnitConfig.MotorNameplate.Poles', 'INT'],
['UnitConfig.MotorNameplate.RPM', 'REAL'],
['UnitConfig.MotorNameplate.ServiceFactor', 'REAL'],
['UnitConfig.MotorNameplate.Horsepower', 'REAL'],
['UnitConfig.Pump_Diameter', 'REAL'],
['UnitConfig.Pump_Constant', 'REAL'],
['UnitConfig.Anchor_Depth', 'REAL'],
['UnitConfig.Total_Stroke_Length', 'REAL'],
['UnitConfig.Motor_Sheave_Size', 'REAL'],
['UnitConfig.Gearbox_Limit', 'REAL'],
['UnitConfig.Gearbox_Ratio', 'REAL'],
['UnitConfig.Gearbox_Sheave', 'REAL'],
['UnitConfig.RPM_Maximum', 'REAL'],
['UnitConfig.RPM_Minimum', 'REAL'],
['UnitConfig.MotorCntrlMode', 'DINT'],
['UnitConfig.MaxFreq', 'REAL'],
['UnitConfig.Speed_Torque_Mode', 'DINT'],
['UnitConfig.Rating_Gearbox', 'REAL'],
['UnitConfig.Rating_Structural', 'REAL'],
['UnitConfig.Well_Type', 'SINT'],
['UnitConfig.Total_Vertical_Depth', 'REAL'],
['UnitConfig.Total_Vertical_Depth_Input', 'REAL'],
['UnitConfig.Tubing_Size_ID', 'REAL'],
['UnitConfig.Tubing_Size_OD', 'REAL'],
['UnitConfig.API_Oil', 'REAL'],
['UnitConfig.SG_Water', 'REAL'],
['UnitConfig.Percent_Water', 'REAL'],
['Youngs_Modulus_Fiberglass', 'REAL'],
['Youngs_Modulus_Steel', 'REAL'],
]
def ReadFromPLC(tag, tag_type):
try:
if (tag_type == "REAL"):
a = tux.ReadLGXDataAsFloat(sess, conn, tag, 1)
result = round(float(a[0]),3)
#elif (tag_type == "INT" or tag_type == "DINT" or tag_type =="SINT"):
elif tag_type[-3:] == "INT":
a = tux.ReadLGXDataAsInteger(sess, conn, tag, 1)
result = int(a[0])
elif (tag_type == "BOOL"):
a = tux.ReadLGXDataAsInteger(sess, conn, tag, 1)
intermediate = int(a[0])
if intermediate == 0:
result = 0
else:
result = 1
elif (tag_type =="STRING"):
a = tux.ReadLGXDataAsInteger(sess, conn, tag, 82)
word = ""
for ch in range(len(a)):
word = word + unichr(int(a[ch]))
result = word
else:
result = "?"
return result
except:
print "Unable to read " + tag
return 911
myfile = open(filename, 'wb')
wr = csv.writer(myfile)
for t in tags_to_read:
tagVal = ReadFromPLC(t[0],t[1])
if tagVal != 911:
wr.writerow([t[0], tagVal, t[1]])
wr.writerow(["TAPER", "CONFIG"])
for i in range(0,11):
for p in taper_setup_params:
wr.writerow(["Taper.Taper[" + str(i) + "].Setup." + p[0], ReadFromPLC("Taper.Taper[" + str(i) + "].Setup." + p[0], p[1]), p[1]])
myfile.close()
print "Backed up PLC settings to", filename