Updated to work with Sqlite

This commit is contained in:
Patrick McDonagh
2016-03-30 15:58:10 -05:00
parent 2437380c6e
commit 00af9f0144
3 changed files with 49 additions and 25 deletions

View File

@@ -91,8 +91,8 @@ def main():
if retry_attempts < retries_allowed: if retry_attempts < retries_allowed:
main() main()
else: else:
print {'status':'error', 'message':e} print {'status':'error', 'message':'{}'.format(e)}
return {'status':'error', 'message':e} return {'status':'error', 'message':'{}'.format(e)}
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -2,18 +2,31 @@
import os import os
import sys import sys
import MySQLdb import sqlite3 as lite
con = lite.connect("/mnt/usb/data.db")
PLC_IP_ADDRESS = "192.168.1.10"
PLC_TYPE = "VFD"
def getPLCIP():
global PLC_IP_ADDRESS, PLC_TYPE
with con:
cur = con.cursor()
query = "SELECT * FROM config ORDER BY dateChanged DESC LIMIT 1;"
cur.execute(query)
setup = cur.fetchall()
try:
PLC_IP_ADDRESS = setup[0][2]
PLC_TYPE = setup[0][1]
except:
PLC_IP_ADDRESS = "192.168.1.10"
PLC_TYPE = "VFD"
return
def main(tagName): def main(tagName):
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData") global PLC_IP_ADDRESS
cur = db.cursor() getPLCIP()
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]
#PYCOMM Connection to PLC #PYCOMM Connection to PLC
from pycomm.ab_comm.clx import Driver as ClxDriver from pycomm.ab_comm.clx import Driver as ClxDriver

View File

@@ -1,24 +1,32 @@
from pycomm.ab_comm.clx import Driver as ClxDriver from pycomm.ab_comm.clx import Driver as ClxDriver
import sys import sys
import MySQLdb
from time import sleep from time import sleep
import sqlite3 as lite
con = lite.connect("/mnt/usb/data.db")
PLC_IP_ADDRESS = "192.168.1.10"
PLC_TYPE = "VFD"
def closeEnough(a,b): def closeEnough(a,b):
return abs(a - b) <= 0.1 return abs(a - b) <= 0.1
def getPLCIP(): def getPLCIP():
db = MySQLdb.connect(host="127.0.0.1",user="website",passwd="henrypump",db="WellData") global PLC_IP_ADDRESS, PLC_TYPE
cur = db.cursor() with con:
query = "SELECT * FROM WellData.config ORDER BY dateChanged DESC LIMIT 1;" cur = con.cursor()
cur.execute(query) query = "SELECT * FROM config ORDER BY dateChanged DESC LIMIT 1;"
setup = cur.fetchall() cur.execute(query)
db.commit() setup = cur.fetchall()
db.close() try:
PLC_IP_ADDRESS = setup[0][2]
return setup[0][2] PLC_TYPE = setup[0][1]
except:
PLC_IP_ADDRESS = "192.168.1.10"
PLC_TYPE = "VFD"
return
def readTag(tagName): def readTag(tagName):
PLC_IP_ADDRESS = getPLCIP() global PLC_IP_ADDRESS
c = ClxDriver(True, 'ClxDriver.log') c = ClxDriver(True, 'ClxDriver.log')
@@ -43,9 +51,10 @@ def readTag(tagName):
return out return out
def main(tag, value): def main(tag, value):
global PLC_IP_ADDRESS
getPLCIP()
r = 0 r = 0
PLC_IP_ADDRESS = getPLCIP()
readObj = readTag(tag) readObj = readTag(tag)
if readObj['status'] == "error": if readObj['status'] == "error":
@@ -70,8 +79,10 @@ def main(tag, value):
def writeTagAndVerify(tag,value, sleepValue=2): def writeTagAndVerify(tag,value, sleepValue=2):
"""Writes the specified value to tag and confirms that the value has been set""" """Writes the specified value to tag and confirms that the value has been set"""
global PLC_IP_ADDRESS
r = 0 r = 0
PLC_IP_ADDRESS = getPLCIP() getPLCIP()
readObj = readTag(tag) readObj = readTag(tag)
if readObj['status'] == "error": if readObj['status'] == "error":
return readObj return readObj