diff --git a/app/datalogger/getDailyTotals.py b/app/datalogger/getDailyTotals.py new file mode 100644 index 0000000..b3b4d16 --- /dev/null +++ b/app/datalogger/getDailyTotals.py @@ -0,0 +1,63 @@ +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() diff --git a/app/datalogger/views.py b/app/datalogger/views.py index 0a53d5a..149465b 100644 --- a/app/datalogger/views.py +++ b/app/datalogger/views.py @@ -8,6 +8,7 @@ from datetime import datetime from app import app, db from app.datalogger.models import * +from app.datalogger.getDailyTotals import getTotals ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'zip']) @@ -161,3 +162,7 @@ def get_tags_at_time(datetime): tag_data = res.fetchall() tag_data_list = list(map(tagsattime_to_obj, tag_data)) return jsonify(tag_data_list) + +@app.route('/api/today_totals') +def today_totals(): + return jsonify(getTotals())