added swd controller and promagmbs rework
This commit is contained in:
435
swdcontroller/swdcontroller.py
Normal file
435
swdcontroller/swdcontroller.py
Normal file
@@ -0,0 +1,435 @@
|
||||
"""Driver for swdcontroller"""
|
||||
|
||||
import threading
|
||||
import json
|
||||
import time
|
||||
from random import randint
|
||||
import os
|
||||
from device_base import deviceBase
|
||||
from Channel import PLCChannel, ModbusChannel,read_tag, write_tag, TAG_DATAERROR_SLEEPTIME
|
||||
import persistence
|
||||
from utilities import get_public_ip_address
|
||||
from file_logger import filelogger as log
|
||||
# PERSISTENCE FILE
|
||||
PERSIST = persistence.load('persist.json')
|
||||
if not PERSIST:
|
||||
PERSIST = {"b1":"192.168.1.10", "b2":"192.168.1.20", "vs1": "192.168.1.14", "swd1":"192.168.1.15"}
|
||||
persistence.store(PERSIST, 'persist.json')
|
||||
PLC_IP_ADDRESS = PERSIST
|
||||
from Tags import tags
|
||||
|
||||
_ = None
|
||||
|
||||
log.info("swdcontroller startup")
|
||||
|
||||
# GLOBAL VARIABLES
|
||||
WAIT_FOR_CONNECTION_SECONDS = 20
|
||||
IP_CHECK_PERIOD = 60
|
||||
|
||||
|
||||
CHANNELS = tags
|
||||
|
||||
# PERSISTENCE FILE
|
||||
PERSIST = persistence.load()
|
||||
|
||||
|
||||
class start(threading.Thread, deviceBase):
|
||||
"""Start class required by Meshify."""
|
||||
|
||||
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None,
|
||||
companyId=None, offset=None, mqtt=None, Nodes=None):
|
||||
"""Initialize the driver."""
|
||||
threading.Thread.__init__(self)
|
||||
deviceBase.__init__(self, name=name, number=number, mac=mac, Q=Q,
|
||||
mcu=mcu, companyId=companyId, offset=offset,
|
||||
mqtt=mqtt, Nodes=Nodes)
|
||||
|
||||
self.daemon = True
|
||||
self.version = "1"
|
||||
self.finished = threading.Event()
|
||||
self.force_send = False
|
||||
self.public_ip_address = ""
|
||||
self.public_ip_address_last_checked = 0
|
||||
self.plc_ping_status = {"b1": "", "b2": "", "vs1":"", "swd1": ""}
|
||||
threading.Thread.start(self)
|
||||
|
||||
# this is a required function for all drivers, its goal is to upload some piece of data
|
||||
# about your device so it can be seen on the web
|
||||
def register(self):
|
||||
"""Register the driver."""
|
||||
# self.sendtodb("log", "BOOM! Booted.", 0)
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
"""Actually run the driver."""
|
||||
for i in range(0, WAIT_FOR_CONNECTION_SECONDS):
|
||||
print("swdcontroller driver will start in {} seconds".format(WAIT_FOR_CONNECTION_SECONDS - i))
|
||||
time.sleep(1)
|
||||
log.info("BOOM! Starting swdcontroller driver...")
|
||||
|
||||
self._check_ip_address()
|
||||
|
||||
self.nodes["swdcontroller_0199"] = self
|
||||
|
||||
send_loops = 0
|
||||
|
||||
while True:
|
||||
now = time.time()
|
||||
if self.force_send:
|
||||
log.warning("FORCE SEND: TRUE")
|
||||
|
||||
for chan in CHANNELS:
|
||||
val = chan.read()
|
||||
if chan.check(val, self.force_send):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
|
||||
|
||||
# print("swdcontroller driver still alive...")
|
||||
if self.force_send:
|
||||
if send_loops > 2:
|
||||
log.warning("Turning off force_send")
|
||||
self.force_send = False
|
||||
send_loops = 0
|
||||
else:
|
||||
send_loops += 1
|
||||
|
||||
|
||||
if (now - self.public_ip_address_last_checked) > IP_CHECK_PERIOD:
|
||||
self._check_ip_address()
|
||||
self._check_devices()
|
||||
|
||||
|
||||
def _check_ip_address(self):
|
||||
"""Check the public IP address and send to Meshify if changed."""
|
||||
self.public_ip_address_last_checked = time.time()
|
||||
test_public_ip = get_public_ip_address()
|
||||
if not test_public_ip == self.public_ip_address:
|
||||
self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'swdcontroller')
|
||||
self.public_ip_address = test_public_ip
|
||||
hostname = "google.com"
|
||||
response = os.system("ping -c 1 " + hostname)
|
||||
|
||||
#and then check the response...
|
||||
if response == 0:
|
||||
print(hostname, 'is up!')
|
||||
self.ping_counter = 0
|
||||
else:
|
||||
print(hostname, 'is down!')
|
||||
self.ping_counter += 1
|
||||
|
||||
if self.ping_counter >= 3:
|
||||
log.info("Rebooting because no internet detected")
|
||||
os.system('reboot')
|
||||
|
||||
def _check_devices(self):
|
||||
name = {
|
||||
"b1": "Booster #1",
|
||||
"b2": "Booster #2",
|
||||
"vs1": "Valve Station",
|
||||
"swd1": "SWD"
|
||||
}
|
||||
for device in PLC_IP_ADDRESS:
|
||||
try:
|
||||
plc_ping = os.system("ping -c 1 " + PLC_IP_ADDRESS[device] + " > /dev/null 2>&1")
|
||||
except Exception as e:
|
||||
log.error("something went wrong in ping: {}".format(e))
|
||||
if plc_ping == 0:
|
||||
if not self.plc_ping_status[device] == "OK":
|
||||
self.sendtodbDev(1, device + "_ping","OK", 0, 'swdcontroller')
|
||||
self.plc_ping_status[device] = "OK"
|
||||
else:
|
||||
if not self.plc_ping_status[device] == "Comms Error to PLC":
|
||||
self.sendtodbDev(1, device + '_ping', "Comms Error to " + name[device],0, 'swdcontroller')
|
||||
self.plc_ping_status[device] = 'Comms Error to PLC'
|
||||
|
||||
def swdcontroller_sync(self, name, value):
|
||||
"""Sync all data from the driver."""
|
||||
self.force_send = True
|
||||
# self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
|
||||
def swdcontroller_writeplctag(self, name, value):
|
||||
"""Write a value to the PLC."""
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
val_n = new_val['val']
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="CLX")
|
||||
print("Result of swdcontroller_writeplctag(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_cmd_start(self, name, value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cmd_Start", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_start(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_cmd_stop(self, name, value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cmd_Stop", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_stop(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_spt_pid_manual(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 100.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_ManualSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_spt_manual(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b1_spt_pid_manual":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_spt_pid_discharge(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 300.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_DischargePressureSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_spt_discharge(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b1_spt_pid_discharge":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_spt_pid_suction(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 100.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_SuctionPressureSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_spt_suction(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b1_spt_pid_suction":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1pidmanual(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_Manual", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_pid_manual(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1piddischarge(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_DischargePressure", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_pid_discharge(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1pidsuction(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b1"]), "cfg_PID_SuctionPressure", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b1_pid_suction(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b1_pid_control(self, name, value):
|
||||
resp = False
|
||||
if value == "manual":
|
||||
resp = self.swdcontroller_b1pidmanual(name, value)
|
||||
elif value == "discharge":
|
||||
resp = self.swdcontroller_b1piddischarge(name, value)
|
||||
elif value == "suction":
|
||||
resp = self.swdcontroller_b1pidsuction(name, value)
|
||||
else:
|
||||
return False
|
||||
demap = {"suction": 0, "discharge": 1, "manual": 2}
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b1_pid_control":
|
||||
val = demap[value]
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return resp
|
||||
|
||||
def swdcontroller_b2_cmd_start(self, name, value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cmd_Start", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_start(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2_cmd_stop(self, name, value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cmd_Stop", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_stop(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2_spt_pid_manual(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 100.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_ManualSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_spt_manual(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b2_spt_pid_manual":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2_spt_pid_discharge(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 300.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_DischargePressureSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_spt_discharge(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b2_spt_pid_discharge":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2_spt_pid_suction(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 100.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_SuctionPressureSP", value, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_spt_suction(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b2_spt_pid_suction":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2pidmanual(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_Manual", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_pid_manual(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2piddischarge(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_DischargePressure", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_pid_discharge(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2pidsuction(self,name,value):
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["b2"]), "cfg_PID_SuctionPressure", 1, plc_type="CLX")
|
||||
print("Result of swdcontroller_b2_pid_suction(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def swdcontroller_b2_pid_control(self, name, value):
|
||||
resp = False
|
||||
if value == "manual":
|
||||
resp = self.swdcontroller_b2pidmanual(name, value)
|
||||
elif value == "discharge":
|
||||
resp = self.swdcontroller_b2piddischarge(name, value)
|
||||
elif value == "suction":
|
||||
resp = self.swdcontroller_b2pidsuction(name, value)
|
||||
else:
|
||||
return False
|
||||
demap = {"suction": 0, "discharge": 1, "manual": 2}
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "b2_pid_control":
|
||||
val = demap[value]
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return resp
|
||||
|
||||
def swdcontroller_vs1_spt_hi_pressure(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 200.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["vs1"]), "PT_High_Alarm_SP", value, plc_type="Micro800")
|
||||
print("Result of swdcontroller_vs1_hi_pressure(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "vs1_spt_hi_pressure":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_vs1_spt_lo_pressure(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 200.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["vs1"]), "PT_Low_Alarm_SP", value, plc_type="Micro800")
|
||||
print("Result of swdcontroller_vs1_lo_pressure(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "vs1_spt_lo_pressure":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_vs1_spt_hi_flow(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 1500.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["vs1"]), "FM_High_Alarm_SP", value, plc_type="Micro800")
|
||||
print("Result of swdcontroller_vs1_hi_flow(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "vs1_spt_hi_flow":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
def swdcontroller_vs1_spt_lo_flow(self,name,value):
|
||||
value = float(value)
|
||||
if 0.0 < value < 1500.0:
|
||||
write_res = write_tag(str(PLC_IP_ADDRESS["vs1"]), "FM_Low_Alarm_SP", value, plc_type="Micro800")
|
||||
print("Result of swdcontroller_vs1_lo_flow(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
for chan in CHANNELS:
|
||||
if chan.mesh_name == "vs1_spt_lo_flow":
|
||||
val = value
|
||||
if chan.check(val, True):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'swdcontroller')
|
||||
return write_res
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user