64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
from pycomm.ab_comm.clx import Driver as ClxDriver
|
|
import traceback
|
|
import math
|
|
|
|
from app.datalogger.models import Device
|
|
from app import db
|
|
|
|
def getMainPLC():
|
|
dev = Device.query.first()
|
|
dev_obj = dev.serialize
|
|
return dev_obj
|
|
|
|
def readTag(addr, tag):
|
|
c = ClxDriver()
|
|
if c.open(addr):
|
|
try:
|
|
v = c.read_tag(tag)
|
|
return v
|
|
except Exception:
|
|
print("ERROR RETRIEVING TAG: {}".format(tag))
|
|
c.close()
|
|
traceback.print_exc()
|
|
c.close()
|
|
|
|
def getTotals():
|
|
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':"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"}
|
|
]
|
|
|
|
main_plc = getMainPLC()
|
|
print("PLC IP ADDRESS = {}".format(main_plc['address']))
|
|
outList = []
|
|
for tag in today_tags:
|
|
try:
|
|
val = readTag(main_plc['address'], tag['tag'])[0]
|
|
print("READING {}: {}".format(tag['tag'], val))
|
|
if not math.isnan(val):
|
|
outList.append({'name':tag['name'], 'value':val})
|
|
except Exception as e:
|
|
print("Error while reading total: {}".format(e))
|
|
|
|
return outList
|
|
|
|
|
|
if __name__ == '__main__':
|
|
getTotals()
|