Files
datalogger-POC/getTodaysTotals.py
2016-04-23 16:40:50 -05:00

78 lines
2.5 KiB
Python

from pycomm.ab_comm.clx import Driver as ClxDriver
import traceback
import math
from readConfig import readConfig
import sys, os
today_tags = [
{'name':"Average_SPM",'tag':"TODAY_Average_SPM"},
{'name':"Downhole_Net_Stroke",'tag':"TODAY_Downhole_NetStroke"},
{'name':"Electricity_Cost",'tag':"TODAY_Electricity_Cost"},
{'name':"Fluid_Level",'tag':"TODAY_Fluid_Above_Pump"},
{'name':"Full_Card_Production",'tag':"TODAY_Full_Card_Production"},
{'name':"Inflow_Rate",'tag':"TODAY_Inflow_Rate"},
{'name':"kWh",'tag':"TODAY_kWh"},
{'name':"kWh_Regen",'tag':"TODAY_kWh_Regen"},
{'name':"Lifting_Cost",'tag':"TODAY_Lifting_Cost"},
{'name':"Peak_Load",'tag':"TODAY_Max_Load"},
{'name':"Min_Load",'tag':"TODAY_Min_Load"},
{'name':"Percent_Run",'tag':"TODAY_Percent_Run"},
{'name':"Polished_Rod_HP",'tag':"TODAY_Polished_Rod_HP"},
{'name':"Calculated_Production",'tag':"TODAY_Production_Calculated"},
{'name':"Projected_Production",'tag':"TODAY_Production_Projected"},
{'name':"Pump_HP",'tag':"TODAY_Pump_HP"},
{'name':"Pump_Intake_Presure",'tag':"TODAY_Pump_Intake_Pressure"},
{'name':"Surface_Stroke_Length",'tag':"TODAY_Surface_StrokeLength"},
{'name':"Tubing_Movement",'tag':"TODAY_Tubing_Movement"}
]
retry_attempts = 0
retries_allowed = 10
def readTag(addr, tag):
# logging.basicConfig(
# filename="clx.log",
# format="%(levelname)-10s %(asctime)s %(message)s",
# level=logging.DEBUG
# )
c = ClxDriver()
if c.open(addr):
try:
v = c.read_tag(tag)
# print(v)
return v
except Exception:
err = c.get_status()
c.close()
# print err
# pass
c.close()
def main():
global today_tags, retry_attempts, retries_allowed
try:
configProperties = readConfig()
outList = []
for tag in today_tags:
val = readTag(configProperties['PLC_IP_ADDRESS'], tag['tag'])[0]
if not math.isnan(val):
outList.append({'name':tag['name'], 'value':val})
print outList
return outList
except Exception, e:
# print("FATAL ERROR: Communication Error connecting to the PLC... ", e)
# traceback.print_exc()
retry_attempts = retry_attempts + 1
if retry_attempts < retries_allowed:
main()
else:
print {'status':'error', 'message':'{}'.format(e)}
return {'status':'error', 'message':'{}'.format(e)}
if __name__ == '__main__':
main()