68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
#!/usr/bin/env python
|
|
|
|
'''
|
|
Created on Dec 8, 2015
|
|
|
|
@author: Patrick McDonagh
|
|
'''
|
|
|
|
|
|
from datetime import datetime
|
|
import sys
|
|
from random import randint
|
|
import time
|
|
import MySQLdb
|
|
import tuxeip
|
|
|
|
|
|
#TUXEIP Connection to PLC
|
|
from tuxeip import TuxEIP, LGX, LGX_REAL
|
|
|
|
|
|
def main():
|
|
|
|
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="SolarData")
|
|
cur = db.cursor()
|
|
query = "SELECT * FROM SolarData.tags WHERE deleted = 0;"
|
|
cur.execute(query)
|
|
tags = cur.fetchall()
|
|
# ((1L, 'DC_Bus_Voltage', datetime.datetime(2015, 12, 8, 16, 2, 32), 'V', 0L), (2L, 'Output_Frequency', datetime.datetime(2015, 12, 8, 16, 31, 12), 'Hz', 0L))
|
|
db.commit()
|
|
db.close()
|
|
|
|
PLC_IP_ADDRESS = "192.168.1.13" # MAKE THIS A db VALUE
|
|
|
|
tagList = [];
|
|
if len(tags) > 0:
|
|
for t in tags:
|
|
tagList.append({"id":int(t[0]), "name":t[1], "val":None, "lastVal":None});
|
|
|
|
try:
|
|
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')
|
|
|
|
while True:
|
|
for r in tagList:
|
|
r["val"] = tux.ReadLGXDataAsFloat(sess, conn, r['name'], 1)[0]
|
|
print("{0} - {1}".format(r["name"], r["val"]))
|
|
if not r["val"] == r["lastVal"]:
|
|
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="SolarData")
|
|
cur = db.cursor()
|
|
aQuery = """INSERT INTO SolarData.values (tagID, val) VALUES ('%d', '%f');"""%(r["id"], float(r["val"]))
|
|
print(aQuery)
|
|
storeVal = cur.execute(aQuery)
|
|
db.commit()
|
|
db.close()
|
|
r["lastVal"] = r["val"]
|
|
|
|
time.sleep(10)
|
|
except Exception as err:
|
|
print err
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|