creating SQLite Python driver

This commit is contained in:
Patrick McDonagh
2016-01-22 18:33:20 -06:00
parent 089b9de297
commit 6035044737
2 changed files with 63 additions and 0 deletions

View File

@@ -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()