diff --git a/python/tagserver.py b/python/tagserver_MySQL.py similarity index 100% rename from python/tagserver.py rename to python/tagserver_MySQL.py diff --git a/python/tagserver_SQLite.py b/python/tagserver_SQLite.py new file mode 100644 index 0000000..0ed7d70 --- /dev/null +++ b/python/tagserver_SQLite.py @@ -0,0 +1,63 @@ +#!/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 sqlite3 as lite +import tuxeip + + +# TUXEIP Connection to PLC +from tuxeip import TuxEIP, LGX, LGX_REAL +con = lite.connect("/usr/db/data.db") + + +def main(): + with con: + cur = con.cursor() + query = "SELECT * FROM tags WHERE deleted = 0;" + cur.execute(query) + tags = cur.fetchall() + + PLC_IP_ADDRESS = "10.10.10.3" # MAKE THIS A db VALUE + scan_rate = 10 + + 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"]: + with con: + cur = con.cursor() + aQuery = """INSERT INTO vals (tagID, val) VALUES ('%d', '%f');""" % (r["id"], float(r["val"])) + print(aQuery) + cur.execute(aQuery) + con.commit() + r["lastVal"] = r["val"] + + time.sleep(scan_rate) + except Exception as err: + print err + pass + +if __name__ == '__main__': + main()