211 lines
7.2 KiB
Python
211 lines
7.2 KiB
Python
# Enter your python code.
|
|
import json
|
|
from common.Logger import logger
|
|
from quickfaas.remotebus import publish
|
|
import re, uuid
|
|
from paho.mqtt import client
|
|
|
|
lwtData = {
|
|
"init":False,
|
|
"client": client.Client(client_id=str(uuid.uuid4()), clean_session=True, userdata=None, protocol=client.MQTTv311, transport="tcp")
|
|
}
|
|
def lwt(mac):
|
|
try:
|
|
#if not lwtData["connected"]:
|
|
if not lwtData["init"]:
|
|
logger.info("INITIALIZING LWT CLIENT")
|
|
lwtData["client"].username_pw_set(username="admin", password="columbus")
|
|
lwtData["client"].will_set("meshify/db/194/_/mainHP/" + mac + ":00:00/connected",json.dumps({"value":False}))
|
|
lwtData["init"] = True
|
|
logger.info("Connecting to MQTT Broker for LWT purposes!!!!!!!")
|
|
lwtData["client"].connect("mq194.imistaway.net",1883, 600)
|
|
lwtData["client"].publish("meshify/db/194/_/mainHP/" + mac + ":00:00/connected", json.dumps({"value":True}))
|
|
except Exception as e:
|
|
logger.error("LWT DID NOT DO THE THING")
|
|
logger.error(e)
|
|
|
|
def sendData(message):
|
|
#logger.debug(message)
|
|
mac = __topic__.split("/")[-1] #':'.join(re.findall('..', '%012x' % uuid.getnode()))
|
|
lwt(mac)
|
|
for measure in message["measures"]:
|
|
try:
|
|
logger.debug(measure)
|
|
if measure["name"] in ["wellstatus","pidcontrolmode","downholesensorstatus","alarmflowrate","alarmintakepressure","alarmintaketemperature","alarmtubingpressure","alarmvfd","alarmlockout","alarmfluidlevel","runpermissive","startpermissive","last_vfd_fault_code","vfd_fault"]:
|
|
logger.debug("Converting DINT/BOOL to STRING")
|
|
value = convert_int(measure["name"], measure["value"])
|
|
logger.debug("Converted {} to {}".format(measure["value"], value))
|
|
publish(__topic__ + ":01:99/" + measure["name"], json.dumps({"value": value}), __qos__)
|
|
else:
|
|
publish(__topic__ + ":01:99/" + measure["name"], json.dumps({"value": measure["value"]}), __qos__)
|
|
except Exception as e:
|
|
logger.error(e)
|
|
|
|
#publish(__topic__, json.dumps({measure["name"]: measure["value"]}), __qos__)
|
|
|
|
def convert_int(plc_tag, value):
|
|
well_status_codes = {
|
|
0: "Running",
|
|
1: "Pumped Off",
|
|
2: "Alarmed",
|
|
3: "Locked Out",
|
|
4: "Stopped"
|
|
}
|
|
|
|
pid_control_codes = {
|
|
0: "Flow",
|
|
1: "Fluid Level",
|
|
2: "Tubing Pressure",
|
|
3: "Manual"
|
|
}
|
|
|
|
downhole_codes = {
|
|
0: "OK",
|
|
1: "Connecting",
|
|
2: "Open Circuit",
|
|
3: "Shorted",
|
|
4: "Cannot Decode"
|
|
}
|
|
|
|
permissive_codes = {
|
|
0: "OK",
|
|
1: "Flow",
|
|
2: "Intake Pressure",
|
|
3: "Intake Temperature",
|
|
4: "Tubing Pressure",
|
|
5: "VFD",
|
|
6: "Fluid Level",
|
|
7: "Min. Downtime"
|
|
}
|
|
|
|
alarm_codes = {
|
|
0: "OK",
|
|
1: "Alarm"
|
|
}
|
|
|
|
alarm_vfd_codes = {
|
|
0: "OK",
|
|
1: "Locked Out"
|
|
}
|
|
|
|
vfd_fault_codes = {
|
|
0: "No Fault",
|
|
2: "Auxiliary Input",
|
|
3: "Power Loss",
|
|
4: "UnderVoltage",
|
|
5: "OverVoltage",
|
|
7: "Motor Overload",
|
|
8: "Heatsink OverTemp",
|
|
9: "Thermister OverTemp",
|
|
10: "Dynamic Brake OverTemp",
|
|
12: "Hardware OverCurrent",
|
|
13: "Ground Fault",
|
|
14: "Ground Warning",
|
|
15: "Load Loss",
|
|
17: "Input Phase Loss",
|
|
18: "Motor PTC Trip",
|
|
19: "Task Overrun",
|
|
20: "Torque Prove Speed Band",
|
|
21: "Output Phase Loss",
|
|
24: "Decel Inhibit",
|
|
25: "OverSpeed Limit",
|
|
26: "Brake Slipped",
|
|
27: "Torque Prove Conflict",
|
|
28: "TP Encls Confict",
|
|
29: "Analog In Loss",
|
|
33: "Auto Restarts Exhausted",
|
|
35: "IPM OverCurrent",
|
|
36: "SW OverCurrent",
|
|
38: "Phase U to Ground",
|
|
39: "Phase V to Ground",
|
|
40: "Phase W to Ground",
|
|
41: "Phase UV Short",
|
|
42: "Phase VW Short",
|
|
43: "Phase WU Short",
|
|
44: "Phase UNeg to Ground",
|
|
45: "Phase VNeg to Ground",
|
|
46: "Phase WNeg to Ground",
|
|
48: "System Defaulted",
|
|
49: "Drive Powerup",
|
|
51: "Clear Fault Queue",
|
|
55: "Control Board Overtemp",
|
|
59: "Invalid Code",
|
|
61: "Shear Pin 1",
|
|
62: "Shear Pin 2",
|
|
64: "Drive Overload",
|
|
66: "OW Torque Level",
|
|
67: "Pump Off",
|
|
71: "Port 1 Adapter",
|
|
72: "Port 2 Adapter",
|
|
73: "Port 3 Adapter",
|
|
74: "Port 4 Adapter",
|
|
75: "Port 5 Adapter",
|
|
76: "Port 6 Adapter",
|
|
77: "IR Volts Range",
|
|
78: "FluxAmps Ref Range",
|
|
79: "Excessive Load",
|
|
80: "AutoTune Aborted",
|
|
81: "Port 1 DPI Loss",
|
|
82: "Port 2 DPI Loss",
|
|
83: "Port 3 DPI Loss",
|
|
84: "Port 4 DPI Loss",
|
|
85: "Port 5 DPI Loss",
|
|
86: "Port 6 DPI Loss",
|
|
87: "IXo Voltage Range",
|
|
91: "Primary Velocity Feedback Loss",
|
|
93: "Hardware Enable Check",
|
|
94: "Alternate Velocity Feedback Loss",
|
|
95: "Auxiliary Velocity Feedback Loss",
|
|
96: "Position Feedback Loss",
|
|
97: "Auto Tach Switch",
|
|
100: "Parameter Checksum",
|
|
101: "Power Down NVS Blank",
|
|
102: "NVS Not Blank",
|
|
103: "Power Down NVS Incompatible",
|
|
104: "Power Board Checksum",
|
|
106: "Incompat MCB-PB",
|
|
107: "Replaced MCB-PB",
|
|
108: "Analog Calibration Checksum",
|
|
110: "Invalid Power Board Data",
|
|
111: "Power Board Invalid ID",
|
|
112: "Power Board App Min Version",
|
|
113: "Tracking DataError",
|
|
115: "Power Down Table Full",
|
|
116: "Power Down Entry Too Large",
|
|
117: "Power Down Data Checksum",
|
|
118: "Power Board Power Down Checksum",
|
|
124: "App ID Changed",
|
|
125: "Using Backup App",
|
|
134: "Start on Power Up",
|
|
137: "External Precharge Error",
|
|
138: "Precharge Open",
|
|
141: "Autotune Enc Angle",
|
|
142: "Autotune Speed Restricted",
|
|
143: "Autotune Current Regulator",
|
|
144: "Autotune Inertia",
|
|
145: "Autotune Travel",
|
|
13035: "Net IO Timeout",
|
|
13037: "Net IO Timeout"
|
|
|
|
}
|
|
|
|
plc_tags = {
|
|
"wellstatus": well_status_codes.get(value, "Invalid Code"),
|
|
"pidcontrolmode": pid_control_codes.get(value, "Invalid Code"),
|
|
"downholesensorstatus": downhole_codes.get(value, "Invalid Code"),
|
|
"alarmflowrate": alarm_codes.get(value, "Invalid Code"),
|
|
"alarmintakepressure": alarm_codes.get(value, "Invalid Code"),
|
|
"alarmintaketemperature": alarm_codes.get(value, "Invalid Code"),
|
|
"alarmtubingpressure": alarm_codes.get(value, "Invalid Code"),
|
|
"alarmvfd": alarm_codes.get(value, "Invalid Code"),
|
|
"alarmlockout": alarm_vfd_codes.get(value, "Invalid Code"),
|
|
"alarmfluidlevel": alarm_codes.get(value, "Invalid Code"),
|
|
"runpermissive": permissive_codes.get(value, "Invalid Code"),
|
|
"startpermissive": permissive_codes.get(value, "Invalid Code"),
|
|
"last_vfd_fault_code": vfd_fault_codes.get(value, "Invalid Code"),
|
|
"vfd_fault": vfd_fault_codes.get(value, "Invalid Code")
|
|
}
|
|
|
|
return plc_tags.get(plc_tag, "Invalid Tag")
|
|
|
|
|