updates
This commit is contained in:
@@ -33,7 +33,7 @@ def read_tag(addr, tag, plc_type="CLX"):
|
||||
log.error("Data Error during readTag({}, {}): {}".format(addr, tag, err))
|
||||
except CommError:
|
||||
# err = c.get_status()
|
||||
clx.close()
|
||||
#clx.close()
|
||||
log.error("Could not connect during readTag({}, {})".format(addr, tag))
|
||||
except AttributeError as err:
|
||||
clx.close()
|
||||
@@ -87,7 +87,7 @@ def write_tag(addr, tag, val, plc_type="CLX"):
|
||||
except CommError as err:
|
||||
clx_err = clx.get_status()
|
||||
log.error("--\nCommError during write_tag({}, {}, {}, plc_type={})\n{}\n--".format(addr, tag, val, plc_type, err))
|
||||
clx.close()
|
||||
#clx.close()
|
||||
return False
|
||||
|
||||
|
||||
@@ -114,6 +114,8 @@ class Channel(object):
|
||||
"""Check to see if the new_value needs to be stored."""
|
||||
send_needed = False
|
||||
send_reason = ""
|
||||
if new_value is None:
|
||||
new_value = self.value
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
@@ -121,17 +123,17 @@ class Channel(object):
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif self.value != new_value:
|
||||
if self.map_:
|
||||
if not self.value == self.map_[new_value]:
|
||||
if (not self.value == self.map_[new_value]) or force_send:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
else:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
"file4": "Tags.py"
|
||||
},
|
||||
"deviceName": "dualactuator",
|
||||
"releaseVersion": "6",
|
||||
"releaseVersion": "7",
|
||||
"driverFileName": "dualactuator.py",
|
||||
"driverId": "0100"
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class start(threading.Thread, deviceBase):
|
||||
mqtt=mqtt, Nodes=Nodes)
|
||||
|
||||
self.daemon = True
|
||||
self.version = "6"
|
||||
self.version = "7"
|
||||
self.finished = threading.Event()
|
||||
self.force_send = False
|
||||
self.public_ip_address = ""
|
||||
@@ -76,7 +76,7 @@ class start(threading.Thread, deviceBase):
|
||||
val = chan.read()
|
||||
if chan.check(val, self.force_send):
|
||||
if chan.mesh_name in ["v1_open_fbk","v2_open_fbk"]:
|
||||
self.sendtodbDev(1, chan.mesh_name, str(round(chan.value,2)) + '%', 0, 'dualactuator')
|
||||
self.sendtodbDev(1, chan.mesh_name, str(round(chan.value,2)), 0, 'dualactuator')
|
||||
else:
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'dualactuator')
|
||||
#time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
|
||||
|
||||
@@ -9,15 +9,6 @@ tags = [
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_run_status","Raw_Run_Status","BOOL", 1, 3600, plc_type="Micro800", map_={0: "Stopped", 1: "Running", None: "Error"}),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_local_start","Raw_Local_Start","BOOL", 1, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "lifetime_flow_meter_gal","Lifetime_Flow_Meter_Gal","REAL", 1000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_yesterday_gal","Totalizer_FM_Yesterday_Total_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_day_gal","Totalizer_FM_Current_Day_Total_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_last_month_gal","Totalizer_FM_Last_Month_Gal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_month_gal","Totalizer_FM_Current_Month_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_yesterday_bbls","Totalizer_FM_Yesterday_Total_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_day_bbls","Totalizer_FM_CUrrent_Day_Total_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_last_month_bbls","Totalizer_FM_Last_Month_BBLs","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_month_bbls","Totalizer_FM_Current_Month_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "lifetime_flow_meter_bbls","Lifetime_Flow_Meter_BBLS","REAL", 1000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "spt_flow_meter_unit","SPT_Flow_Meter_Unit","BOOL", 0, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_overload_status", "Raw_Overload_Status", "BOOL", 0, 3600, plc_type="Micro800", map_={0: "Good", 1: "Down on Overload Tripped", None: "Error"})
|
||||
]
|
||||
@@ -8,7 +8,7 @@
|
||||
"file4": "Tags.py"
|
||||
},
|
||||
"deviceName": "plcfreshwater",
|
||||
"releaseVersion": "10",
|
||||
"releaseVersion": "11",
|
||||
"driverFileName": "plcfreshwater.py",
|
||||
"driverId": "0100"
|
||||
}
|
||||
23
plcfreshwater/old/Tags.py
Normal file
23
plcfreshwater/old/Tags.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from Channel import PLCChannel, ModbusChannel
|
||||
from plcfreshwater import PLC_IP_ADDRESS
|
||||
|
||||
tags = [
|
||||
PLCChannel(PLC_IP_ADDRESS, "scaled_flow_meter","Scaled_Flow_Meter","REAL", 3, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "scaled_pressure_transducer","Scaled_Pressure_Transducer","REAL", 3, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_hand_input","Raw_Hand_Input","BOOL", 1, 7200, plc_type="Micro800", map_={0: "Off", 1: "On", None: "Error"}),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_auto_input","Raw_Auto_Input","BOOL", 1, 7200, plc_type="Micro800", map_={0: "Off", 1: "On", None: "Error"}),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_run_status","Raw_Run_Status","BOOL", 1, 3600, plc_type="Micro800", map_={0: "Stopped", 1: "Running", None: "Error"}),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_local_start","Raw_Local_Start","BOOL", 1, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "lifetime_flow_meter_gal","Lifetime_Flow_Meter_Gal","REAL", 1000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_yesterday_gal","Totalizer_FM_Yesterday_Total_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_day_gal","Totalizer_FM_Current_Day_Total_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_last_month_gal","Totalizer_FM_Last_Month_Gal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_month_gal","Totalizer_FM_Current_Month_Gal","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_yesterday_bbls","Totalizer_FM_Yesterday_Total_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_day_bbls","Totalizer_FM_CUrrent_Day_Total_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_last_month_bbls","Totalizer_FM_Last_Month_BBLs","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "total_fm_month_bbls","Totalizer_FM_Current_Month_BBLs","REAL", 50, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "lifetime_flow_meter_bbls","Lifetime_Flow_Meter_BBLS","REAL", 1000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "spt_flow_meter_unit","SPT_Flow_Meter_Unit","BOOL", 0, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "raw_overload_status", "Raw_Overload_Status", "BOOL", 0, 3600, plc_type="Micro800", map_={0: "Good", 1: "Down on Overload Tripped", None: "Error"})
|
||||
]
|
||||
191
plcfreshwater/old/plcfreshwater.py
Normal file
191
plcfreshwater/old/plcfreshwater.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""Driver for plcfreshwater"""
|
||||
import threading
|
||||
import json
|
||||
import time
|
||||
from random import randint
|
||||
import os
|
||||
from device_base import deviceBase
|
||||
import persistence
|
||||
from utilities import get_public_ip_address, get_private_ip_address
|
||||
|
||||
_ = None
|
||||
|
||||
# GLOBAL VARIABLES
|
||||
WAIT_FOR_CONNECTION_SECONDS = 10
|
||||
IP_CHECK_PERIOD = 60
|
||||
PLC_IP_ADDRESS = ""
|
||||
TOPIC_MAC = ""
|
||||
|
||||
|
||||
# PERSISTENCE FILE
|
||||
IP_TABLE = persistence.load('persist.json')
|
||||
if not IP_TABLE:
|
||||
IP_TABLE = {
|
||||
"000000000001":"192.168.1.201",
|
||||
"000000000002":"192.168.1.202",
|
||||
"000000000003":"192.168.1.203",
|
||||
"000000000004":"192.168.1.211",
|
||||
"000000000005":"192.168.1.210",
|
||||
"000000000006":"192.168.1.208",
|
||||
"000000000007":"192.168.1.209",
|
||||
"000000000008":"192.168.1.208",
|
||||
"000000000009":"192.168.1.209",
|
||||
"000000000010":"192.168.1.210",
|
||||
"000000000011":"192.168.1.211",
|
||||
"000000000012":"192.168.1.212",
|
||||
"000000000013":"192.168.1.213",
|
||||
"000000000014":"192.168.1.214",
|
||||
"000000000015":"192.168.1.215",
|
||||
"000000000016":"192.168.1.216"
|
||||
}
|
||||
persistence.store(IP_TABLE, 'persist.json')
|
||||
|
||||
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 = "10"
|
||||
self.finished = threading.Event()
|
||||
self.force_send = False
|
||||
self.public_ip_address = ""
|
||||
self.public_ip_address_last_checked = 0
|
||||
self.private_ip_address = ""
|
||||
self.plcip = ""
|
||||
self.ping_counter = 0
|
||||
self.plc_ping_status = 'Default'
|
||||
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("plcfreshwater driver will start in {} seconds".format(WAIT_FOR_CONNECTION_SECONDS - i))
|
||||
time.sleep(1)
|
||||
|
||||
global TOPIC_MAC, PLC_IP_ADDRESS, log, write_tag
|
||||
|
||||
TOPIC_MAC = self.mac
|
||||
|
||||
from file_logger import filelogger as log
|
||||
log.info("plcfreshwater startup")
|
||||
log.info("BOOM! Starting plcfreshwater driver...")
|
||||
|
||||
self._check_ip_address()
|
||||
|
||||
self.nodes["plcfreshwater_0199"] = self
|
||||
|
||||
send_loops = 0
|
||||
|
||||
PLC_IP_ADDRESS = IP_TABLE[self.mac]
|
||||
self.plcip = PLC_IP_ADDRESS
|
||||
log.info("PLC IP is {}".format(self.plcip))
|
||||
self.sendtodbDev(1, 'plc_ip_address', self.plcip, 0, 'plcfreshwater')
|
||||
from Channel import PLCChannel, ModbusChannel,read_tag, write_tag, TAG_DATAERROR_SLEEPTIME
|
||||
from Tags import tags
|
||||
CHANNELS = tags
|
||||
while True:
|
||||
now = time.time()
|
||||
if self.force_send:
|
||||
log.warning("FORCE SEND: TRUE")
|
||||
|
||||
for chan in CHANNELS:
|
||||
try:
|
||||
val = chan.read()
|
||||
if chan.check(val, self.force_send):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater')
|
||||
#time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
|
||||
except Exception as e:
|
||||
log.error("Something went wrong in read: {}".format(e))
|
||||
|
||||
# print("plcfreshwater driver still alive...")
|
||||
try:
|
||||
plc_ping = os.system("ping -c 1 " + IP_TABLE[self.mac] + " > /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 == "OK":
|
||||
self.sendtodbDev(1, "plc_ping","OK", 0, 'plcfreshwater')
|
||||
self.plc_ping_status = "OK"
|
||||
else:
|
||||
if not self.plc_ping_status == "Comms Error to PLC":
|
||||
self.sendtodbDev(1, 'plc_ping', "Comms Error to PLC",0, 'plcfreshwater')
|
||||
self.plc_ping_status = 'Comms Error to PLC'
|
||||
time.sleep(10)
|
||||
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()
|
||||
|
||||
|
||||
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()
|
||||
test_public_ip = test_public_ip
|
||||
test_private_ip = get_private_ip_address()
|
||||
if not test_public_ip == self.public_ip_address and not test_public_ip == "0.0.0.0":
|
||||
self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'plcfreshwater')
|
||||
self.public_ip_address = test_public_ip
|
||||
if not test_private_ip == self.private_ip_address:
|
||||
self.sendtodbDev(1, 'private_ip_address', test_private_ip, 0, 'plcfreshwater')
|
||||
self.private_ip_address = test_private_ip
|
||||
hostname = "8.8.8.8"
|
||||
response = 1
|
||||
try:
|
||||
response = os.system("ping -c 1 " + hostname + " > /dev/null 2>&1")
|
||||
except Exception as e:
|
||||
print("Something went wrong in ping: {}".format(e))
|
||||
|
||||
#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:
|
||||
print("Rebooting because no internet detected")
|
||||
os.system('reboot')
|
||||
|
||||
|
||||
def plcfreshwater_sync(self, name, value):
|
||||
"""Sync all data from the driver."""
|
||||
self.force_send = True
|
||||
# self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
|
||||
def plcfreshwater_writeplctag(self, name, value):
|
||||
"""Write a value to the PLC."""
|
||||
from Channel import write_tag
|
||||
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="Micro800")
|
||||
print("Result of plcfreshwater_writeplctag(self, {}, {}) = {}".format(name, value, write_res))
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
@@ -7,7 +7,7 @@ import os
|
||||
from device_base import deviceBase
|
||||
import persistence
|
||||
from utilities import get_public_ip_address, get_private_ip_address
|
||||
|
||||
from datetime import datetime as dt
|
||||
_ = None
|
||||
|
||||
# GLOBAL VARIABLES
|
||||
@@ -40,6 +40,7 @@ if not IP_TABLE:
|
||||
}
|
||||
persistence.store(IP_TABLE, 'persist.json')
|
||||
|
||||
|
||||
class start(threading.Thread, deviceBase):
|
||||
"""Start class required by Meshify."""
|
||||
|
||||
@@ -52,7 +53,7 @@ class start(threading.Thread, deviceBase):
|
||||
mqtt=mqtt, Nodes=Nodes)
|
||||
|
||||
self.daemon = True
|
||||
self.version = "10"
|
||||
self.version = "11"
|
||||
self.finished = threading.Event()
|
||||
self.force_send = False
|
||||
self.public_ip_address = ""
|
||||
@@ -77,7 +78,7 @@ class start(threading.Thread, deviceBase):
|
||||
print("plcfreshwater driver will start in {} seconds".format(WAIT_FOR_CONNECTION_SECONDS - i))
|
||||
time.sleep(1)
|
||||
|
||||
global TOPIC_MAC, PLC_IP_ADDRESS, log, write_tag
|
||||
global TOPIC_MAC, PLC_IP_ADDRESS, log, write_tag, PERSIST
|
||||
|
||||
TOPIC_MAC = self.mac
|
||||
|
||||
@@ -95,6 +96,21 @@ class start(threading.Thread, deviceBase):
|
||||
self.plcip = PLC_IP_ADDRESS
|
||||
log.info("PLC IP is {}".format(self.plcip))
|
||||
self.sendtodbDev(1, 'plc_ip_address', self.plcip, 0, 'plcfreshwater')
|
||||
PERSIST = persistence.load('totalizers_{}.json'.format(self.mac))
|
||||
if not PERSIST:
|
||||
PERSIST = {
|
||||
'Todays': 0,
|
||||
'Yesterdays': 0,
|
||||
'Current Months': 0,
|
||||
'Previous Months': 0,
|
||||
'Monthly Holding': 0,
|
||||
'Daily Holding': 0,
|
||||
'Lifetime': 0,
|
||||
'Day': 0,
|
||||
'Month': 0,
|
||||
'Last Report': 0
|
||||
}
|
||||
persistence.store(PERSIST, 'totalizers_{}.json'.format(self.mac))
|
||||
from Channel import PLCChannel, ModbusChannel,read_tag, write_tag, TAG_DATAERROR_SLEEPTIME
|
||||
from Tags import tags
|
||||
CHANNELS = tags
|
||||
@@ -106,8 +122,11 @@ class start(threading.Thread, deviceBase):
|
||||
for chan in CHANNELS:
|
||||
try:
|
||||
val = chan.read()
|
||||
if chan.check(val, self.force_send):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater')
|
||||
if chan.mesh_name == "lifetime_flow_meter_gal":
|
||||
self.totalize(val)
|
||||
else:
|
||||
if chan.check(val, self.force_send):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater')
|
||||
#time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
|
||||
except Exception as e:
|
||||
log.error("Something went wrong in read: {}".format(e))
|
||||
@@ -188,4 +207,87 @@ class start(threading.Thread, deviceBase):
|
||||
if write_res is None:
|
||||
write_res = "Error writing to PLC..."
|
||||
return write_res
|
||||
|
||||
def totalize(self, val):
|
||||
right_now = dt.today()
|
||||
month = right_now.month
|
||||
day = right_now.day
|
||||
#Totalize Today, Yesterday, Month, Last Month
|
||||
#if the stored day is 0 then it's a fresh run of this should initalize values now
|
||||
if PERSIST['Day'] == 0:
|
||||
PERSIST['Day'] = day
|
||||
PERSIST['Month'] = month
|
||||
PERSIST['Daily Holding'] = val
|
||||
PERSIST['Monthly Holding'] = val
|
||||
persistence.store(PERSIST, 'totalizers_{}.json'.format(self.mac))
|
||||
#Communication error during initialization check if lifetime has reported properly and update holdings
|
||||
if PERSIST['Daily Holding'] == None and not(val == None):
|
||||
PERSIST['Daily Holding'] = val
|
||||
PERSIST['Monthly Holding'] = val
|
||||
|
||||
try:
|
||||
if val - PERSIST['Daily Holding'] - PERSIST['Todays'] > 500 or time.time() - PERSIST['Last Report'] > 3600 or self.force_send:
|
||||
PERSIST['Todays'] = val - PERSIST['Daily Holding']
|
||||
PERSIST['Current Months'] = val - PERSIST['Monthly Holding']
|
||||
PERSIST['Lifetime'] = val
|
||||
self.sendtodbDev(1, 'total_fm_day_gal', PERSIST['Todays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_day_bbls', PERSIST['Todays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_month_gal', PERSIST['Current Months'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_month_bbls', PERSIST['Current Months']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_gal', PERSIST['Yesterdays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_bbls', PERSIST['Yesterdays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_gal', PERSIST['Lifetime'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_bbls', PERSIST['Lifetime']/42, 0, 'plcfreshwater')
|
||||
PERSIST['Last Report'] = time.time()
|
||||
except:
|
||||
if time.time() - PERSIST['Last Report'] > 3600 or self.force_send:
|
||||
self.sendtodbDev(1, 'total_fm_day_gal', PERSIST['Todays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_day_bbls', PERSIST['Todays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_month_gal', PERSIST['Current Months'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_month_bbls', PERSIST['Current Months']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_gal', PERSIST['Yesterdays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_bbls', PERSIST['Yesterdays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_gal', PERSIST['Lifetime'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_bbls', PERSIST['Lifetime']/42, 0, 'plcfreshwater')
|
||||
PERSIST['Last Report'] = time.time()
|
||||
|
||||
#If the current day doesn't equal the stored day roll the dailies over
|
||||
if not(day == PERSIST['Day']):
|
||||
#if a comms error use the stored values else use the latested values
|
||||
if val == None:
|
||||
PERSIST['Yesterdays'] = PERSIST['Todays']
|
||||
PERSIST['Todays'] = 0
|
||||
PERSIST['Daily Holding'] = PERSIST['Lifetime']
|
||||
else:
|
||||
PERSIST['Yesterdays'] = val - PERSIST['Daily Holding']
|
||||
PERSIST['Todays'] = 0
|
||||
PERSIST['Daily Holding'] = val
|
||||
PERSIST['Lifetime'] = val
|
||||
PERSIST['Day'] = day
|
||||
self.sendtodbDev(1, 'total_fm_day_gal', PERSIST['Todays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_day_bbls', PERSIST['Todays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_gal', PERSIST['Yesterdays'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_yesterday_bbls', PERSIST['Yesterdays']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_gal', PERSIST['Lifetime'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_bbls', PERSIST['Lifetime']/42, 0, 'plcfreshwater')
|
||||
PERSIST['Last Report'] = time.time()
|
||||
#the day has rolled over if the month also rolls over
|
||||
if not(month == PERSIST['Month']):
|
||||
#if a comms error use the stored values else use the latested values
|
||||
if val == None:
|
||||
PERSIST['Previous Months'] = PERSIST['Current Months']
|
||||
PERSIST['Current Month'] = 0
|
||||
PERSIST['Monthly Holding'] = PERSIST['Lifetime']
|
||||
else:
|
||||
PERSIST['Previous Months'] = val - PERSIST['Monthly Holding']
|
||||
PERSIST['Current Month'] = 0
|
||||
PERSIST['Monthly Holding'] = val
|
||||
PERSIST['Month'] = month
|
||||
self.sendtodbDev(1, 'total_fm_month_gal', PERSIST['Current Months'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_month_bbls', PERSIST['Current Months']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_last_month_gal', PERSIST['Previous Months'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'total_fm_last_month_bbls', PERSIST['Previous Months']/42, 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_gal', PERSIST['Lifetime'], 0, 'plcfreshwater')
|
||||
self.sendtodbDev(1, 'lifetime_flow_meter_bbls', PERSIST['Lifetime']/42, 0, 'plcfreshwater')
|
||||
PERSIST['Last Report'] = time.time()
|
||||
persistence.store(PERSIST, 'totalizers_{}.json'.format(self.mac))
|
||||
18
promagmbs/file_logger.py
Normal file
18
promagmbs/file_logger.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Logging setup for PiFlow"""
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import sys
|
||||
|
||||
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||
log_file = './PiFlow.log'
|
||||
my_handler = RotatingFileHandler(log_file, mode='a', maxBytes=500*1024,
|
||||
backupCount=2, encoding=None, delay=0)
|
||||
my_handler.setFormatter(log_formatter)
|
||||
my_handler.setLevel(logging.INFO)
|
||||
filelogger = logging.getLogger('PiFlow')
|
||||
filelogger.setLevel(logging.INFO)
|
||||
filelogger.addHandler(my_handler)
|
||||
|
||||
console_out = logging.StreamHandler(sys.stdout)
|
||||
console_out.setFormatter(log_formatter)
|
||||
filelogger.addHandler(console_out)
|
||||
282
promagmbs/old/Channel.py
Normal file
282
promagmbs/old/Channel.py
Normal file
@@ -0,0 +1,282 @@
|
||||
"""Define Meshify channel class."""
|
||||
from pycomm.ab_comm.clx import Driver as ClxDriver
|
||||
from pycomm.cip.cip_base import CommError, DataError
|
||||
import time
|
||||
|
||||
|
||||
def binarray(intval):
|
||||
"""Split an integer into its bits."""
|
||||
bin_string = '{0:08b}'.format(intval)
|
||||
bin_arr = [i for i in bin_string]
|
||||
bin_arr.reverse()
|
||||
return bin_arr
|
||||
|
||||
|
||||
def read_tag(addr, tag):
|
||||
"""Read a tag from the PLC."""
|
||||
c = ClxDriver()
|
||||
try:
|
||||
if c.open(addr):
|
||||
try:
|
||||
v = c.read_tag(tag)
|
||||
return v
|
||||
except DataError:
|
||||
c.close()
|
||||
print("Data Error during readTag({}, {})".format(addr, tag))
|
||||
except CommError:
|
||||
# err = c.get_status()
|
||||
c.close()
|
||||
print("Could not connect during readTag({}, {})".format(addr, tag))
|
||||
# print err
|
||||
except AttributeError as e:
|
||||
c.close()
|
||||
print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, e))
|
||||
c.close()
|
||||
return False
|
||||
|
||||
|
||||
def read_array(addr, tag, start, end):
|
||||
"""Read an array from the PLC."""
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
arr_vals = []
|
||||
try:
|
||||
for i in range(start, end):
|
||||
tag_w_index = tag + "[{}]".format(i)
|
||||
v = c.read_tag(tag_w_index)
|
||||
# print('{} - {}'.format(tag_w_index, v))
|
||||
arr_vals.append(round(v[0], 4))
|
||||
# print(v)
|
||||
if len(arr_vals) > 0:
|
||||
return arr_vals
|
||||
else:
|
||||
print("No length for {}".format(addr))
|
||||
return False
|
||||
except Exception:
|
||||
print("Error during readArray({}, {}, {}, {})".format(addr, tag, start, end))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
pass
|
||||
c.close()
|
||||
|
||||
|
||||
def write_tag(addr, tag, val):
|
||||
"""Write a tag value to the PLC."""
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
try:
|
||||
cv = c.read_tag(tag)
|
||||
wt = c.write_tag(tag, val, cv[1])
|
||||
return wt
|
||||
except Exception:
|
||||
print("Error during writeTag({}, {}, {})".format(addr, tag, val))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
c.close()
|
||||
|
||||
|
||||
class Channel(object):
|
||||
"""Holds the configuration for a Meshify channel."""
|
||||
|
||||
def __init__(self, mesh_name, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
self.mesh_name = mesh_name
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def __str__(self):
|
||||
"""Create a string for the channel."""
|
||||
return "{}\nvalue: {}, last_send_time: {}".format(self.mesh_name, self.value, self.last_send_time)
|
||||
|
||||
def check(self, new_value, force_send=False):
|
||||
"""Check to see if the new_value needs to be stored."""
|
||||
send_needed = False
|
||||
send_reason = ""
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif not (self.value == new_value):
|
||||
if self.map_:
|
||||
if not self.value == self.map_[new_value]:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
else:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
else:
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif abs(self.value - new_value) > self.chg_threshold:
|
||||
send_needed = True
|
||||
send_reason = "change threshold"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
if send_needed:
|
||||
self.last_value = self.value
|
||||
if self.map_:
|
||||
try:
|
||||
self.value = self.map_[new_value]
|
||||
except KeyError:
|
||||
print("Cannot find a map value for {} in {} for {}".format(new_value, self.map_, self.mesh_name))
|
||||
self.value = new_value
|
||||
else:
|
||||
self.value = new_value
|
||||
self.last_send_time = time.time()
|
||||
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
||||
return send_needed
|
||||
|
||||
def read(self):
|
||||
"""Read the value."""
|
||||
pass
|
||||
|
||||
|
||||
def identity(sent):
|
||||
"""Return exactly what was sent to it."""
|
||||
return sent
|
||||
|
||||
|
||||
class ModbusChannel(Channel):
|
||||
"""Modbus channel object."""
|
||||
|
||||
def __init__(self, mesh_name, register_number, data_type, chg_threshold, guarantee_sec, channel_size=1, map_=False, write_enabled=False, transformFn=identity):
|
||||
"""Initialize the channel."""
|
||||
super(ModbusChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled)
|
||||
self.mesh_name = mesh_name
|
||||
self.register_number = register_number
|
||||
self.channel_size = channel_size
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
self.transformFn = transformFn
|
||||
|
||||
def read(self, mbsvalue):
|
||||
"""Return the transformed read value."""
|
||||
return self.transformFn(mbsvalue)
|
||||
|
||||
|
||||
class PLCChannel(Channel):
|
||||
"""PLC Channel Object."""
|
||||
|
||||
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
super(PLCChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled)
|
||||
self.plc_ip = ip
|
||||
self.mesh_name = mesh_name
|
||||
self.plc_tag = plc_tag
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def read(self):
|
||||
"""Read the value."""
|
||||
plc_value = None
|
||||
if self.plc_tag and self.plc_ip:
|
||||
read_value = read_tag(self.plc_ip, self.plc_tag)
|
||||
if read_value:
|
||||
plc_value = read_value[0]
|
||||
|
||||
return plc_value
|
||||
|
||||
|
||||
class BoolArrayChannels(Channel):
|
||||
"""Hold the configuration for a set of boolean array channels."""
|
||||
|
||||
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
self.plc_ip = ip
|
||||
self.mesh_name = mesh_name
|
||||
self.plc_tag = plc_tag
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def compare_values(self, new_val_dict):
|
||||
"""Compare new values to old values to see if the values need storing."""
|
||||
send = False
|
||||
for idx in new_val_dict:
|
||||
try:
|
||||
if new_val_dict[idx] != self.last_value[idx]:
|
||||
send = True
|
||||
except KeyError:
|
||||
print("Key Error in self.compare_values for index {}".format(idx))
|
||||
send = True
|
||||
return send
|
||||
|
||||
def read(self, force_send=False):
|
||||
"""Read the value and check to see if needs to be stored."""
|
||||
send_needed = False
|
||||
send_reason = ""
|
||||
if self.plc_tag:
|
||||
v = read_tag(self.plc_ip, self.plc_tag)
|
||||
if v:
|
||||
bool_arr = binarray(v[0])
|
||||
new_val = {}
|
||||
for idx in self.map_:
|
||||
try:
|
||||
new_val[self.map_[idx]] = bool_arr[idx]
|
||||
except KeyError:
|
||||
print("Not able to get value for index {}".format(idx))
|
||||
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif self.compare_values(new_val):
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
|
||||
if send_needed:
|
||||
self.value = new_val
|
||||
self.last_value = self.value
|
||||
self.last_send_time = time.time()
|
||||
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
||||
return send_needed
|
||||
10
promagmbs/old/Maps.py
Normal file
10
promagmbs/old/Maps.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""Holds map values for promagmbs."""
|
||||
|
||||
promagmbs_map = {}
|
||||
|
||||
def reverse_map(value, map_):
|
||||
"""Perform the opposite of mapping to an object."""
|
||||
for x in map_:
|
||||
if map_[x] == value:
|
||||
return x
|
||||
return None
|
||||
11
promagmbs/old/config.txt
Normal file
11
promagmbs/old/config.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"files": {
|
||||
"file3": "modbusMap.p",
|
||||
"file2": "utilities.py",
|
||||
"file1": "promagmbs.py"
|
||||
},
|
||||
"deviceName": "promagmbs",
|
||||
"driverId": "0190",
|
||||
"releaseVersion": "4",
|
||||
"driverFileName": "promagmbs.py"
|
||||
}
|
||||
2585
promagmbs/old/modbusMap.p
Normal file
2585
promagmbs/old/modbusMap.p
Normal file
File diff suppressed because it is too large
Load Diff
21
promagmbs/old/persistence.py
Normal file
21
promagmbs/old/persistence.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Data persistance functions."""
|
||||
# if more advanced persistence is needed, use a sqlite database
|
||||
import json
|
||||
|
||||
|
||||
def load(filename="persist.json"):
|
||||
"""Load persisted settings from the specified file."""
|
||||
try:
|
||||
with open(filename, 'r') as persist_file:
|
||||
return json.load(persist_file)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def store(persist_obj, filename="persist.json"):
|
||||
"""Store the persisting settings into the specified file."""
|
||||
try:
|
||||
with open(filename, 'w') as persist_file:
|
||||
return json.dump(persist_obj, persist_file)
|
||||
except Exception:
|
||||
return False
|
||||
91
promagmbs/old/promagmbs.py
Normal file
91
promagmbs/old/promagmbs.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""Driver for promagmbs."""
|
||||
|
||||
import threading
|
||||
from device_base import deviceBase
|
||||
from utilities import get_public_ip_address
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||
logFile = './promagmbs.log'
|
||||
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=500*1024, backupCount=2, encoding=None, delay=0)
|
||||
my_handler.setFormatter(log_formatter)
|
||||
my_handler.setLevel(logging.INFO)
|
||||
logger = logging.getLogger('promagmbs')
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.addHandler(my_handler)
|
||||
|
||||
console_out = logging.StreamHandler(sys.stdout)
|
||||
console_out.setFormatter(log_formatter)
|
||||
logger.addHandler(console_out)
|
||||
|
||||
|
||||
_ = None
|
||||
|
||||
|
||||
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 = "4"
|
||||
self.finished = threading.Event()
|
||||
self.forceSend = False
|
||||
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):
|
||||
# pass
|
||||
"""Actually run the driver."""
|
||||
global persist
|
||||
wait_sec = 60
|
||||
for i in range(0, wait_sec):
|
||||
logger.info("promagmbs driver will start in {} seconds".format(wait_sec - i))
|
||||
time.sleep(1)
|
||||
logger.warning("BOOM! Starting promagmbs driver...")
|
||||
|
||||
public_ip_address = get_public_ip_address()
|
||||
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'promagmbs')
|
||||
|
||||
send_loops = 0
|
||||
watchdog_loops = 0
|
||||
watchdog_check_after = 5000
|
||||
while True:
|
||||
if self.forceSend:
|
||||
logger.warning("FORCE SEND: TRUE")
|
||||
|
||||
logger.info("promagmbs driver still alive...")
|
||||
if self.forceSend:
|
||||
if send_loops > 2:
|
||||
logger.warning("Turning off forceSend")
|
||||
self.forceSend = False
|
||||
send_loops = 0
|
||||
else:
|
||||
send_loops += 1
|
||||
|
||||
watchdog_loops += 1
|
||||
if (watchdog_loops >= watchdog_check_after):
|
||||
test_public_ip = get_public_ip_address()
|
||||
if not test_public_ip == public_ip_address:
|
||||
self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'promagmbs')
|
||||
public_ip_address = test_public_ip
|
||||
watchdog_loops = 0
|
||||
time.sleep(10)
|
||||
|
||||
def promagmbs_sync(self, name, value):
|
||||
"""Sync all data from the driver."""
|
||||
self.forceSend = True
|
||||
self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
41
promagmbs/old/utilities.py
Normal file
41
promagmbs/old/utilities.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""Utility functions for the driver."""
|
||||
import socket
|
||||
import struct
|
||||
|
||||
|
||||
def get_public_ip_address():
|
||||
"""Find the public IP Address of the host device."""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
ip = s.getsockname()[0]
|
||||
s.close()
|
||||
return ip
|
||||
|
||||
|
||||
def int_to_float16(int_to_convert):
|
||||
"""Convert integer into float16 representation."""
|
||||
bin_rep = ('0' * 16 + '{0:b}'.format(int_to_convert))[-16:]
|
||||
|
||||
sign = -1 ** int(bin_rep[0])
|
||||
exponent = int(bin_rep[1:6], 2)
|
||||
fraction = int(bin_rep[7:17], 2)
|
||||
|
||||
return sign * 2 ** (exponent - 15) * float("1.{}".format(fraction))
|
||||
|
||||
|
||||
def ints_to_float(intlist):
|
||||
"""Convert 2 registers into a floating point number."""
|
||||
mypack = struct.pack('>HH', intlist[0], intlist[1])
|
||||
f = struct.unpack('>f', mypack)
|
||||
print("{} >> {}".format(intlist, f[0]))
|
||||
return f[0]
|
||||
|
||||
|
||||
def degf_to_degc(temp_f):
|
||||
"""Convert deg F to deg C."""
|
||||
return (temp_f - 32.0) * (5.0/9.0)
|
||||
|
||||
|
||||
def degc_to_degf(temp_c):
|
||||
"""Convert deg C to deg F."""
|
||||
return temp_c * 1.8 + 32.0
|
||||
474
promagmbs/rework/Channel.py
Normal file
474
promagmbs/rework/Channel.py
Normal file
@@ -0,0 +1,474 @@
|
||||
"""Define Meshify channel class."""
|
||||
from pycomm.ab_comm.clx import Driver as ClxDriver
|
||||
from pycomm.cip.cip_base import CommError, DataError
|
||||
import time
|
||||
import minimalmodbusM1
|
||||
from file_logger import filelogger as log
|
||||
import struct
|
||||
|
||||
def binarray(intval):
|
||||
"""Split an integer into its bits."""
|
||||
bin_string = '{0:08b}'.format(intval)
|
||||
bin_arr = [i for i in bin_string]
|
||||
bin_arr.reverse()
|
||||
return bin_arr
|
||||
|
||||
|
||||
def read_tag(addr, tag):
|
||||
"""Read a tag from the PLC."""
|
||||
c = ClxDriver()
|
||||
try:
|
||||
if c.open(addr):
|
||||
try:
|
||||
v = c.read_tag(tag)
|
||||
return v
|
||||
except DataError:
|
||||
c.close()
|
||||
print("Data Error during readTag({}, {})".format(addr, tag))
|
||||
except CommError:
|
||||
# err = c.get_status()
|
||||
c.close()
|
||||
print("Could not connect during readTag({}, {})".format(addr, tag))
|
||||
# print err
|
||||
except AttributeError as e:
|
||||
c.close()
|
||||
print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, e))
|
||||
c.close()
|
||||
return False
|
||||
|
||||
|
||||
def read_array(addr, tag, start, end):
|
||||
"""Read an array from the PLC."""
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
arr_vals = []
|
||||
try:
|
||||
for i in range(start, end):
|
||||
tag_w_index = tag + "[{}]".format(i)
|
||||
v = c.read_tag(tag_w_index)
|
||||
# print('{} - {}'.format(tag_w_index, v))
|
||||
arr_vals.append(round(v[0], 4))
|
||||
# print(v)
|
||||
if len(arr_vals) > 0:
|
||||
return arr_vals
|
||||
else:
|
||||
print("No length for {}".format(addr))
|
||||
return False
|
||||
except Exception:
|
||||
print("Error during readArray({}, {}, {}, {})".format(addr, tag, start, end))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
pass
|
||||
c.close()
|
||||
|
||||
|
||||
def write_tag(addr, tag, val):
|
||||
"""Write a tag value to the PLC."""
|
||||
c = ClxDriver()
|
||||
if c.open(addr):
|
||||
try:
|
||||
cv = c.read_tag(tag)
|
||||
wt = c.write_tag(tag, val, cv[1])
|
||||
return wt
|
||||
except Exception:
|
||||
print("Error during writeTag({}, {}, {})".format(addr, tag, val))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print err
|
||||
c.close()
|
||||
|
||||
|
||||
class Channel(object):
|
||||
"""Holds the configuration for a Meshify channel."""
|
||||
|
||||
def __init__(self, mesh_name, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
self.mesh_name = mesh_name
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def __str__(self):
|
||||
"""Create a string for the channel."""
|
||||
return "{}\nvalue: {}, last_send_time: {}".format(self.mesh_name, self.value, self.last_send_time)
|
||||
|
||||
def check(self, new_value, force_send=False):
|
||||
"""Check to see if the new_value needs to be stored."""
|
||||
send_needed = False
|
||||
send_reason = ""
|
||||
if self.data_type == 'BOOL' or self.data_type == 'STRING':
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif not (self.value == new_value):
|
||||
if self.map_:
|
||||
if not self.value == self.map_[new_value]:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
else:
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
else:
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif abs(self.value - new_value) > self.chg_threshold:
|
||||
send_needed = True
|
||||
send_reason = "change threshold"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
if send_needed:
|
||||
self.last_value = self.value
|
||||
if self.map_:
|
||||
try:
|
||||
self.value = self.map_[new_value]
|
||||
except KeyError:
|
||||
print("Cannot find a map value for {} in {} for {}".format(new_value, self.map_, self.mesh_name))
|
||||
self.value = new_value
|
||||
else:
|
||||
self.value = new_value
|
||||
self.last_send_time = time.time()
|
||||
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
||||
return send_needed
|
||||
|
||||
def read(self):
|
||||
"""Read the value."""
|
||||
pass
|
||||
|
||||
|
||||
def identity(sent):
|
||||
"""Return exactly what was sent to it."""
|
||||
return sent
|
||||
|
||||
|
||||
def volume_units(vunit):
|
||||
units = {
|
||||
0: "cm cubed/s",
|
||||
1: "cm cubed/min",
|
||||
2: "cm cubed/h",
|
||||
3: "cm cubed/d",
|
||||
4: "dm cubed/s",
|
||||
5: "dm cubed/min",
|
||||
6: "dm cubed/h",
|
||||
7: "dm cubed/d",
|
||||
8: "m cubed/s",
|
||||
9: "m cubed/min",
|
||||
10: "m cubed/h",
|
||||
11: "m cubed/d",
|
||||
12: "ml/s",
|
||||
13: "ml/min",
|
||||
14: "ml/h",
|
||||
15: "ml/d",
|
||||
16: "l/s",
|
||||
17: "l/min",
|
||||
18: "l/h (+)",
|
||||
19: "l/d",
|
||||
20: "hl/s",
|
||||
21: "hl/min",
|
||||
22: "hl/h",
|
||||
23: "hl/d",
|
||||
24: "Ml/s",
|
||||
25: "Ml/min",
|
||||
26: "Ml/h",
|
||||
27: "Ml/d",
|
||||
32: "af/s",
|
||||
33: "af/min",
|
||||
34: "af/h",
|
||||
35: "af/d",
|
||||
36: "ft cubed/s",
|
||||
37: "ft cubed/min",
|
||||
38: "ft cubed/h",
|
||||
39: "ft cubed/d",
|
||||
40: "fl oz/s (us)",
|
||||
41: "fl oz/min (us)",
|
||||
42: "fl oz/h (us)",
|
||||
43: "fl oz/d (us)",
|
||||
44: "gal/s (us)",
|
||||
45: "gal/min (us)",
|
||||
46: "gal/h (us)",
|
||||
47: "gal/d (us)",
|
||||
48: "Mgal/s (us)",
|
||||
49: "Mgal/min (us)",
|
||||
50: "Mgal/h (us)",
|
||||
51: "Mgal/d (us)",
|
||||
52: "bbl/s (us;liq.)",
|
||||
53: "bbl/min (us;liq.)",
|
||||
54: "bbl/h (us;liq.)",
|
||||
55: "bbl/d (us;liq.)",
|
||||
56: "bbl/s (us;beer)",
|
||||
57: "bbl/min (us;beer)",
|
||||
58: "bbl/h (us;beer)",
|
||||
59: "bbl/d (us;beer)",
|
||||
60: "bbl/s (us;oil)",
|
||||
61: "bbl/min (us;oil)",
|
||||
62: "bbl/h (us;oil)",
|
||||
63: "bbl/d (us;oil)",
|
||||
64: "bbl/s (us;tank)",
|
||||
65: "bbl/min (us;tank)",
|
||||
66: "bbl/h (us;tank)",
|
||||
67: "bbl/d (us;tank)",
|
||||
68: "gal/s (imp)",
|
||||
69: "gal/min (imp)",
|
||||
70: "gal/h (imp)",
|
||||
71: "gal/d (imp)",
|
||||
72: "Mgal/s (imp)",
|
||||
73: "Mgal/min (imp)",
|
||||
74: "Mgal/h (imp)",
|
||||
75: "Mgal/d (imp)",
|
||||
76: "bbl/s (imp;beer)",
|
||||
77: "bbl/min (imp;beer)",
|
||||
78: "bbl/h (imp;beer)",
|
||||
79: "bbl/d (imp;beer)",
|
||||
80: "bbl/s (imp;oil)",
|
||||
81: "bbl/min (imp;oil)",
|
||||
82: "bbl/h (imp;oil)",
|
||||
83: "bbl/d (imp;oil)",
|
||||
88: "kgal/s (us)",
|
||||
89: "kgal/min (us)",
|
||||
90: "kgal/h (us)",
|
||||
91: "kgal/d (us)",
|
||||
92: "MMft cubed/s",
|
||||
93: "MMft cubed/min",
|
||||
94: "MMft cubed/h",
|
||||
96: "Mft cubed/d"
|
||||
}
|
||||
return units[vunit]
|
||||
|
||||
def totalizer_units(tunit):
|
||||
|
||||
units = {
|
||||
0: "cm cubed",
|
||||
1: "dm cubed",
|
||||
2: "m cubed",
|
||||
3: "ml",
|
||||
4: "l",
|
||||
5: "hl",
|
||||
6: "Ml Mega",
|
||||
8: "af",
|
||||
9: "ft cubed",
|
||||
10: "fl oz (us)",
|
||||
11: "gal (us)",
|
||||
12: "Mgal (us)",
|
||||
13: "bbl (us;liq.)",
|
||||
14: "bbl (us;beer)",
|
||||
15: "bbl (us;oil)",
|
||||
16: "bbl (us;tank)",
|
||||
17: "gal (imp)",
|
||||
18: "Mgal (imp)",
|
||||
19: "bbl (imp;beer)",
|
||||
20: "bbl (imp;oil)",
|
||||
22: "kgal (us)",
|
||||
23: "Mft cubed",
|
||||
50: "g",
|
||||
51: "kg",
|
||||
52: "t",
|
||||
53: "oz",
|
||||
54: "lb",
|
||||
55: "STon",
|
||||
100: "Nl",
|
||||
101: "Nm cubed",
|
||||
102: "Sm cubed",
|
||||
103: "Sft cubed",
|
||||
104: "Sl",
|
||||
105: "Sgal (us)",
|
||||
106: "Sbbl (us;liq.)",
|
||||
107: "Sgal (imp)",
|
||||
108: "Sbbl (us;oil)",
|
||||
109: "MMSft cubed",
|
||||
110: "Nhl",
|
||||
251: "None"
|
||||
}
|
||||
return units[tunit]
|
||||
|
||||
|
||||
|
||||
class ModbusChannel(Channel):
|
||||
"""Modbus channel object."""
|
||||
|
||||
def __init__(self, mesh_name, register_number, data_type, chg_threshold, guarantee_sec, channel_size=1, map_=False, write_enabled=False, transform_fn=identity, unit_number=1, scaling=0, mcu=None):
|
||||
"""Initialize the channel."""
|
||||
super(ModbusChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled)
|
||||
self.mesh_name = mesh_name
|
||||
self.register_number = register_number
|
||||
self.channel_size = channel_size
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
self.transform_fn = transform_fn
|
||||
self.unit_number = unit_number
|
||||
self.scaling= scaling
|
||||
self.mcu = mcu
|
||||
|
||||
baud = 9600
|
||||
connected = False
|
||||
while connected == False:
|
||||
try:
|
||||
#print("CONNECTING!!!!!!!!!!!!!!!!!!!!!!!!!")
|
||||
connected = self.mcu.set485Baud(baud)
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print("Error on connect: ", e)
|
||||
serial4 = self.mcu.rs485
|
||||
|
||||
self.instrument = minimalmodbusM1.Instrument(1, serial4)
|
||||
self.instrument.address = 1
|
||||
|
||||
def read(self):
|
||||
"""Return the transformed read value."""
|
||||
if self.data_type == "FLOAT":
|
||||
try:
|
||||
read_value = self.instrument.read_float(self.register_number,4,self.channel_size)
|
||||
except IOError as e:
|
||||
log.error(e)
|
||||
return None
|
||||
|
||||
elif self.data_type == "INTEGER" or self.data_type == "STRING":
|
||||
try:
|
||||
read_value = self.instrument.read_register(self.register_number, self.scaling, 4)
|
||||
except IOError as e:
|
||||
log.error(e)
|
||||
return None
|
||||
elif self.data_type == "FLOATBS":
|
||||
try:
|
||||
t = self.instrument.read_registers(self.register_number, 2, 3)
|
||||
read_value = round(self.byteSwap32(t), 2)
|
||||
except IOError as e:
|
||||
log.error(e)
|
||||
return None
|
||||
read_value = self.transform_fn(read_value)
|
||||
return read_value
|
||||
|
||||
def byteSwap32(self, array):
|
||||
newVal = ""
|
||||
for i in array:
|
||||
i = hex(i).replace('0x', '')
|
||||
while len(i) < 4:
|
||||
i = "0" + i
|
||||
print i
|
||||
newVal = i + newVal
|
||||
print newVal
|
||||
return struct.unpack('!f', newVal.decode('hex'))[0]
|
||||
|
||||
|
||||
class PLCChannel(Channel):
|
||||
"""PLC Channel Object."""
|
||||
|
||||
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
super(PLCChannel, self).__init__(mesh_name, data_type, chg_threshold, guarantee_sec, map_, write_enabled)
|
||||
self.plc_ip = ip
|
||||
self.mesh_name = mesh_name
|
||||
self.plc_tag = plc_tag
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def read(self):
|
||||
"""Read the value."""
|
||||
plc_value = None
|
||||
if self.plc_tag and self.plc_ip:
|
||||
read_value = read_tag(self.plc_ip, self.plc_tag)
|
||||
if read_value:
|
||||
plc_value = read_value[0]
|
||||
|
||||
return plc_value
|
||||
|
||||
|
||||
class BoolArrayChannels(Channel):
|
||||
"""Hold the configuration for a set of boolean array channels."""
|
||||
|
||||
def __init__(self, ip, mesh_name, plc_tag, data_type, chg_threshold, guarantee_sec, map_=False, write_enabled=False):
|
||||
"""Initialize the channel."""
|
||||
self.plc_ip = ip
|
||||
self.mesh_name = mesh_name
|
||||
self.plc_tag = plc_tag
|
||||
self.data_type = data_type
|
||||
self.last_value = None
|
||||
self.value = None
|
||||
self.last_send_time = 0
|
||||
self.chg_threshold = chg_threshold
|
||||
self.guarantee_sec = guarantee_sec
|
||||
self.map_ = map_
|
||||
self.write_enabled = write_enabled
|
||||
|
||||
def compare_values(self, new_val_dict):
|
||||
"""Compare new values to old values to see if the values need storing."""
|
||||
send = False
|
||||
for idx in new_val_dict:
|
||||
try:
|
||||
if new_val_dict[idx] != self.last_value[idx]:
|
||||
send = True
|
||||
except KeyError:
|
||||
print("Key Error in self.compare_values for index {}".format(idx))
|
||||
send = True
|
||||
return send
|
||||
|
||||
def read(self, force_send=False):
|
||||
"""Read the value and check to see if needs to be stored."""
|
||||
send_needed = False
|
||||
send_reason = ""
|
||||
if self.plc_tag:
|
||||
v = read_tag(self.plc_ip, self.plc_tag)
|
||||
if v:
|
||||
bool_arr = binarray(v[0])
|
||||
new_val = {}
|
||||
for idx in self.map_:
|
||||
try:
|
||||
new_val[self.map_[idx]] = bool_arr[idx]
|
||||
except KeyError:
|
||||
print("Not able to get value for index {}".format(idx))
|
||||
|
||||
if self.last_send_time == 0:
|
||||
send_needed = True
|
||||
send_reason = "no send time"
|
||||
elif self.value is None:
|
||||
send_needed = True
|
||||
send_reason = "no value"
|
||||
elif self.compare_values(new_val):
|
||||
send_needed = True
|
||||
send_reason = "value change"
|
||||
elif (time.time() - self.last_send_time) > self.guarantee_sec:
|
||||
send_needed = True
|
||||
send_reason = "guarantee sec"
|
||||
elif force_send:
|
||||
send_needed = True
|
||||
send_reason = "forced"
|
||||
|
||||
if send_needed:
|
||||
self.value = new_val
|
||||
self.last_value = self.value
|
||||
self.last_send_time = time.time()
|
||||
print("Sending {} for {} - {}".format(self.value, self.mesh_name, send_reason))
|
||||
return send_needed
|
||||
14
promagmbs/rework/config.txt
Normal file
14
promagmbs/rework/config.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"files": {
|
||||
"file6": "persistence.py",
|
||||
"file5": "file_logger.py",
|
||||
"file4": "Channel.py",
|
||||
"file3": "modbusMap.p",
|
||||
"file2": "utilities.py",
|
||||
"file1": "promagmbs.py"
|
||||
},
|
||||
"deviceName": "promagmbs",
|
||||
"driverId": "0190",
|
||||
"releaseVersion": "5",
|
||||
"driverFileName": "promagmbs.py"
|
||||
}
|
||||
18
promagmbs/rework/file_logger.py
Normal file
18
promagmbs/rework/file_logger.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Logging setup for PiFlow"""
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import sys
|
||||
|
||||
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||
log_file = './PiFlow.log'
|
||||
my_handler = RotatingFileHandler(log_file, mode='a', maxBytes=500*1024,
|
||||
backupCount=2, encoding=None, delay=0)
|
||||
my_handler.setFormatter(log_formatter)
|
||||
my_handler.setLevel(logging.INFO)
|
||||
filelogger = logging.getLogger('PiFlow')
|
||||
filelogger.setLevel(logging.INFO)
|
||||
filelogger.addHandler(my_handler)
|
||||
|
||||
console_out = logging.StreamHandler(sys.stdout)
|
||||
console_out.setFormatter(log_formatter)
|
||||
filelogger.addHandler(console_out)
|
||||
0
promagmbs/rework/modbusMap.p
Normal file
0
promagmbs/rework/modbusMap.p
Normal file
21
promagmbs/rework/persistence.py
Normal file
21
promagmbs/rework/persistence.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Data persistance functions."""
|
||||
# if more advanced persistence is needed, use a sqlite database
|
||||
import json
|
||||
|
||||
|
||||
def load(filename="persist.json"):
|
||||
"""Load persisted settings from the specified file."""
|
||||
try:
|
||||
with open(filename, 'r') as persist_file:
|
||||
return json.load(persist_file)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def store(persist_obj, filename="persist.json"):
|
||||
"""Store the persisting settings into the specified file."""
|
||||
try:
|
||||
with open(filename, 'w') as persist_file:
|
||||
return json.dump(persist_obj, persist_file, indent=4)
|
||||
except Exception:
|
||||
return False
|
||||
168
promagmbs/rework/promagmbs.py
Normal file
168
promagmbs/rework/promagmbs.py
Normal file
@@ -0,0 +1,168 @@
|
||||
"""Driver for promagmbs."""
|
||||
|
||||
import threading
|
||||
from device_base import deviceBase
|
||||
from utilities import get_public_ip_address
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from Channel import Channel, ModbusChannel, volume_units, totalizer_units
|
||||
from datetime import datetime as dt
|
||||
from drivers import minimalmodbusM1
|
||||
import persistence
|
||||
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||
logFile = './promagmbs.log'
|
||||
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=500*1024, backupCount=2, encoding=None, delay=0)
|
||||
my_handler.setFormatter(log_formatter)
|
||||
my_handler.setLevel(logging.INFO)
|
||||
logger = logging.getLogger('promagmbs')
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.addHandler(my_handler)
|
||||
|
||||
console_out = logging.StreamHandler(sys.stdout)
|
||||
console_out.setFormatter(log_formatter)
|
||||
logger.addHandler(console_out)
|
||||
|
||||
|
||||
# PERSISTENCE FILE
|
||||
PERSIST = persistence.load('persist.json')
|
||||
if not PERSIST:
|
||||
PERSIST = {
|
||||
'yesterday_totalizer_1': dt.today().day,
|
||||
'yesterday_totalizer_2': dt.today().day,
|
||||
'yesterday_totalizer_3': dt.today().day,
|
||||
'yesterday_total_totalizer_1': 0,
|
||||
'yesterday_total_midnight_totalizer_1': 0,
|
||||
'yesterday_total_totalizer_2': 0,
|
||||
'yesterday_total_midnight_totalizer_2': 0,
|
||||
'yesterday_total_totalizer_3': 0,
|
||||
'yesterday_total_midnight_totalizer_3': 0
|
||||
}
|
||||
persistence.store(PERSIST, 'persist.json')
|
||||
_ = None
|
||||
|
||||
|
||||
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.lock = threading.Lock()
|
||||
self.mcu = mcu
|
||||
self.daemon = True
|
||||
self.version = "5"
|
||||
self.finished = threading.Event()
|
||||
self.forceSend = False
|
||||
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):
|
||||
# pass
|
||||
"""Actually run the driver."""
|
||||
global persist
|
||||
flowmeter_unit_number = 1
|
||||
with self.lock:
|
||||
CHANNELS = [
|
||||
ModbusChannel('volume_flow', 2008, 'FLOATBS', 500, 3600,channel_size=2, unit_number=flowmeter_unit_number,mcu=self.mcu),
|
||||
ModbusChannel('conductivity', 2012, 'FLOATBS', 500, 3600,channel_size=2, unit_number=flowmeter_unit_number,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_1', 2609, 'FLOATBS', 100, 3600,channel_size=2, unit_number=flowmeter_unit_number,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_2', 2809, 'FLOATBS', 100, 3600,channel_size=2, unit_number=flowmeter_unit_number,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_3', 3009, 'FLOATBS', 100, 3600,channel_size=2, unit_number=flowmeter_unit_number,mcu=self.mcu),
|
||||
ModbusChannel('volume_flow_units', 2102, 'STRING', 1, 86400,channel_size=1, unit_number=flowmeter_unit_number, transform_fn=volume_units,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_1_units', 4603, 'STRING', 1, 86400,channel_size=1, unit_number=flowmeter_unit_number, transform_fn=totalizer_units,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_2_units', 4604, 'STRING', 1, 86400,channel_size=1, unit_number=flowmeter_unit_number, transform_fn=totalizer_units,mcu=self.mcu),
|
||||
ModbusChannel('totalizer_3_units', 4605, 'STRING', 1, 86400,channel_size=1, unit_number=flowmeter_unit_number, transform_fn=totalizer_units,mcu=self.mcu)
|
||||
]
|
||||
wait_sec = 20
|
||||
for i in range(0, wait_sec):
|
||||
logger.info("promagmbs driver will start in {} seconds".format(wait_sec - i))
|
||||
time.sleep(1)
|
||||
logger.warning("BOOM! Starting promagmbs driver...")
|
||||
|
||||
public_ip_address = get_public_ip_address()
|
||||
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'promagmbs')
|
||||
|
||||
send_loops = 0
|
||||
watchdog_loops = 0
|
||||
watchdog_check_after = 5000
|
||||
while True:
|
||||
if self.forceSend:
|
||||
logger.warning("FORCE SEND: TRUE")
|
||||
|
||||
for chan in CHANNELS:
|
||||
try:
|
||||
val = None
|
||||
with self.lock:
|
||||
for _ in range(3):
|
||||
temp = chan.read()
|
||||
if not temp == None:
|
||||
val = temp
|
||||
if val == None:
|
||||
logger.info("No modbus data sending previous value")
|
||||
val = chan.value
|
||||
if chan.mesh_name in ['totalizer_1','totalizer_2','totalizer_3']:
|
||||
right_now = dt.today()
|
||||
today_total, yesterday_total = self.totalize(val, PERSIST['yesterday_'+chan.mesh_name], right_now.day, right_now.hour, right_now.minute, PERSIST['yesterday_total_midnight_'+chan.mesh_name], PERSIST['yesterday_total_'+chan.mesh_name], chan.mesh_name)
|
||||
if chan.check(val, self.forceSend):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'promagmbs')
|
||||
self.sendtodbDev(1,chan.mesh_name + "_today", today_total,0,'promagmbs')
|
||||
self.sendtodbDev(1,chan.mesh_name + "_yesterday", yesterday_total,0,'promagmbs')
|
||||
else:
|
||||
if chan.check(val, self.forceSend):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'promagmbs')
|
||||
|
||||
except Exception as e:
|
||||
logger.warning("An error occured: {}".format(e))
|
||||
time.sleep(3)
|
||||
|
||||
logger.info("promagmbs driver still alive...")
|
||||
if self.forceSend:
|
||||
if send_loops > 2:
|
||||
logger.warning("Turning off forceSend")
|
||||
self.forceSend = False
|
||||
send_loops = 0
|
||||
else:
|
||||
send_loops += 1
|
||||
|
||||
watchdog_loops += 1
|
||||
if (watchdog_loops >= watchdog_check_after):
|
||||
test_public_ip = get_public_ip_address()
|
||||
if not test_public_ip == public_ip_address:
|
||||
self.sendtodbDev(1, 'public_ip_address', test_public_ip, 0, 'promagmbs')
|
||||
public_ip_address = test_public_ip
|
||||
watchdog_loops = 0
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
def promagmbs_sync(self, name, value):
|
||||
"""Sync all data from the driver."""
|
||||
self.forceSend = True
|
||||
self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
|
||||
def totalize(self,val, yesterday, day, hour, minute, yesterday_total_midnight, yesterday_total,channel):
|
||||
if (yesterday_total == 0 and yesterday_total_midnight == 0) or (yesterday_total == None or yesterday_total_midnight == None):
|
||||
yesterday_total_midnight = val
|
||||
PERSIST['yesterday_total_midnight_'+channel] = yesterday_total_midnight
|
||||
persistence.store(PERSIST, 'persist.json')
|
||||
today_total = val - yesterday_total_midnight
|
||||
if hour == 0 and minute == 0 and not(day == yesterday):
|
||||
yesterday_total = today_total
|
||||
yesterday_total_midnight = val
|
||||
today_total = val - yesterday_total_midnight
|
||||
yesterday = day
|
||||
PERSIST['yesterday_'+channel] = yesterday
|
||||
PERSIST['yesterday_total_'+channel] = yesterday_total
|
||||
PERSIST['yesterday_total_midnight_'+channel] = yesterday_total_midnight
|
||||
persistence.store(PERSIST,'persist.json')
|
||||
|
||||
return today_total,yesterday_total
|
||||
41
promagmbs/rework/utilities.py
Normal file
41
promagmbs/rework/utilities.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""Utility functions for the driver."""
|
||||
import socket
|
||||
import struct
|
||||
|
||||
|
||||
def get_public_ip_address():
|
||||
"""Find the public IP Address of the host device."""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
ip = s.getsockname()[0]
|
||||
s.close()
|
||||
return ip
|
||||
|
||||
|
||||
def int_to_float16(int_to_convert):
|
||||
"""Convert integer into float16 representation."""
|
||||
bin_rep = ('0' * 16 + '{0:b}'.format(int_to_convert))[-16:]
|
||||
|
||||
sign = -1 ** int(bin_rep[0])
|
||||
exponent = int(bin_rep[1:6], 2)
|
||||
fraction = int(bin_rep[7:17], 2)
|
||||
|
||||
return sign * 2 ** (exponent - 15) * float("1.{}".format(fraction))
|
||||
|
||||
|
||||
def ints_to_float(intlist):
|
||||
"""Convert 2 registers into a floating point number."""
|
||||
mypack = struct.pack('>HH', intlist[0], intlist[1])
|
||||
f = struct.unpack('>f', mypack)
|
||||
print("{} >> {}".format(intlist, f[0]))
|
||||
return f[0]
|
||||
|
||||
|
||||
def degf_to_degc(temp_f):
|
||||
"""Convert deg F to deg C."""
|
||||
return (temp_f - 32.0) * (5.0/9.0)
|
||||
|
||||
|
||||
def degc_to_degf(temp_c):
|
||||
"""Convert deg C to deg F."""
|
||||
return temp_c * 1.8 + 32.0
|
||||
@@ -61,5 +61,24 @@ tags = [
|
||||
PLCChannel(PLC_IP_ADDRESS, "fm7_todays","Val_Flowmeter7TodaysTotal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "fm8_todays","Val_Flowmeter8TodaysTotal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "fm9_todays","Val_Flowmeter9TodaysTotal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "fm10_todays","Val_Flowmeter10TodaysTotal","REAL", 100, 3600, plc_type="Micro800")
|
||||
PLCChannel(PLC_IP_ADDRESS, "fm10_todays","Val_Flowmeter10TodaysTotal","REAL", 100, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_raw_min","pond1scaling.rawmin","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_raw_max","pond1scaling.rawmax","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_eu_min","pond1scaling.eumin","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_eu_max","pond1scaling.eumax","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_offset","pond1offset","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_raw_min","pond2scaling.rawmin","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_raw_max","pond2scaling.rawmax","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_eu_min","pond2scaling.eumin","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_eu_max","pond2scaling.eumax","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_offset","pond2offset","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_height","pond1Height","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_height","pond2height","REAL", 5, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_volume","pond1volume","REAL", 100000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_volume","pond2volume","REAL", 100000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_sum_volume","pondVolumeTotal","REAL", 300000, 3600, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_high_spt","SPT_Pond_1_High_Level","REAL", 1, 86400, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_high_spt","SPT_Pond_2_High_Level","REAL", 1, 86400, plc_type="Micro800"),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_1_alarm","Pond1HiAlarm","BOOL", 0, 3600, plc_type="Micro800", map_={0: "Good", 1: "High", None: "Error"}),
|
||||
PLCChannel(PLC_IP_ADDRESS, "pond_2_alarm","Pond2HiAlarm","BOOL", 0, 3600, plc_type="Micro800", map_={0: "Good", 1: "High", None: "Error"})
|
||||
]
|
||||
@@ -27,6 +27,7 @@ CHANNELS = tags
|
||||
# PERSISTENCE FILE
|
||||
PERSIST = persistence.load()
|
||||
|
||||
CALIBRATION_TABLES = [[],[], [], ]
|
||||
|
||||
class start(threading.Thread, deviceBase):
|
||||
"""Start class required by Meshify."""
|
||||
@@ -78,6 +79,9 @@ class start(threading.Thread, deviceBase):
|
||||
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'tenflowmeterskid')
|
||||
#time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
|
||||
|
||||
for pond_index in range(1, 3):
|
||||
self.read_pond_calibration(pond_index)
|
||||
|
||||
# print("tenflowmeterskid driver still alive...")
|
||||
if self.force_send:
|
||||
if send_loops > 2:
|
||||
@@ -120,6 +124,168 @@ class start(threading.Thread, deviceBase):
|
||||
self.force_send = True
|
||||
# self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
|
||||
def read_pond_calibration(self, pond_number):
|
||||
"""Read all calibration values for a specific pond."""
|
||||
last_read_height = -1.0
|
||||
cal_values = []
|
||||
cal_index = 1
|
||||
while last_read_height != 0 and cal_index <=10:
|
||||
cal_tag_height = "pond{}CalibrationHeight[{}]".format(pond_number, cal_index)
|
||||
cal_tag_volume = "pond{}CalibrationVolume[{}]".format(pond_number, cal_index)
|
||||
read_height = read_tag(PLC_IP_ADDRESS, cal_tag_height, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
read_volume = read_tag(PLC_IP_ADDRESS, cal_tag_volume, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
if read_height and read_volume:
|
||||
if read_height[0] > 0.0:
|
||||
cal_values.append({"height": read_height[0], "volume": read_volume[0]})
|
||||
last_read_height = read_height[0]
|
||||
cal_index += 1
|
||||
|
||||
if cal_values != CALIBRATION_TABLES[pond_number] or self.force_send:
|
||||
calibration_channel = "pond_{}_calibration".format(pond_number)
|
||||
calibration_string = json.dumps(cal_values).replace('"', "'")
|
||||
self.sendtodbDev(1, calibration_channel, calibration_string, 0, 'tenflowmeterskid')
|
||||
CALIBRATION_TABLES[pond_number] = cal_values
|
||||
|
||||
def tenflowmeterskid_deletecalibrationpoint(self, name, value):
|
||||
"""Delete a calibration point from a calibration table"""
|
||||
# {"pond": int, "point": int}
|
||||
value = value.replace("'", '"')
|
||||
parsed = json.loads(value)
|
||||
try:
|
||||
pond_number = int(parsed['pond'])
|
||||
point_number = int(parsed['point'])
|
||||
write_pond = write_tag(PLC_IP_ADDRESS, "inpPondNumber", pond_number, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
write_point = write_tag(PLC_IP_ADDRESS, "inpDeletePointIndex", point_number, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
if write_pond and write_point:
|
||||
write_cmd = write_tag(PLC_IP_ADDRESS, "cmdDeleteCalibrationPoint", 1, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
if write_cmd:
|
||||
read_val = read_tag(PLC_IP_ADDRESS, "deleteSuccess", plc_type="Micro800")
|
||||
if read_val[0] == 1:
|
||||
self.read_pond_calibration(pond_number)
|
||||
return True
|
||||
return "Wrote everything successfully, but delete didn't succeed (Check pond and point values)."
|
||||
return "Didn't write delete command correctly."
|
||||
return "Didn't write pond or point correctly."
|
||||
except KeyError as e:
|
||||
return "Couldn't parse input value: {} -- {}".format(value, e)
|
||||
|
||||
def tenflowmeterskid_addcalibrationpoint(self, name, value):
|
||||
"""Add a calibration point to the calibration table"""
|
||||
# value = {"pond": int, "height": float, "volume": float}
|
||||
value = value.replace("'", '"')
|
||||
parsed = json.loads(value)
|
||||
try:
|
||||
# parse json values, throw an error if one is missing
|
||||
pond_number = int(parsed['pond'])
|
||||
height = float(parsed['height'])
|
||||
volume = float(parsed['volume'])
|
||||
# write values to the tags
|
||||
write_pond = write_tag(PLC_IP_ADDRESS, "inpPondNumber", pond_number, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
write_height = write_tag(PLC_IP_ADDRESS, "inpPondHeight", height, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
write_volume= write_tag(PLC_IP_ADDRESS, "inpPondVolume", volume, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
if write_pond and write_height and write_volume:
|
||||
write_cmd = write_tag(PLC_IP_ADDRESS, "cmdAddCalibrationPoint", 1, plc_type="Micro800")
|
||||
#time.sleep(2)
|
||||
if write_cmd:
|
||||
read_val = read_tag(PLC_IP_ADDRESS, "addSuccess", plc_type="Micro800")
|
||||
if read_val[0] == 1:
|
||||
self.read_pond_calibration(pond_number)
|
||||
return True
|
||||
return "Wrote everything successfully, but delete didn't succeed (Check pond and point values)."
|
||||
return "Didn't write delete command correctly."
|
||||
return "Didn't write pond or point correctly."
|
||||
except KeyError as e:
|
||||
return "Couldn't parse input value: {} -- {}".format(value, e)
|
||||
|
||||
def tenflowmeterskid_setrawmin(self, name, value):
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
pond_num = tag_n[9]
|
||||
tag_n = 'pond{}scaling.rawmin'.format(pond_num)
|
||||
val_n = new_val['val']
|
||||
print("Raw Min Value: {}".format(val_n))
|
||||
try:
|
||||
write_resp = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="Micro800")
|
||||
print("Result of tenflowmeterskid_writeplctag(self, {}, {}) = {}".format(name, value, write_resp))
|
||||
self.sendtodbDev(1, 'pond_{}_raw_min'.format(pond_num), val_n, 0, 'tenflowmeterskid')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
def tenflowmeterskid_setrawmax(self, name, value):
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
pond_num = tag_n[9]
|
||||
tag_n = 'pond{}scaling.rawmax'.format(pond_num)
|
||||
val_n = new_val['val']
|
||||
print("Raw Max Value: {}".format(val_n))
|
||||
try:
|
||||
write_resp = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="Micro800")
|
||||
print("Result of tenflowmeterskid_writeplctag(self, {}, {}) = {}".format(name, value, write_resp))
|
||||
self.sendtodbDev(1, 'pond_{}_raw_max'.format(pond_num), val_n, 0, 'tenflowmeterskid')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
def tenflowmeterskid_seteumin(self, name, value):
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
pond_num = tag_n[9]
|
||||
tag_n = 'pond{}scaling.eumin'.format(pond_num)
|
||||
val_n = new_val['val']
|
||||
print("EU Min Value: {}".format(val_n))
|
||||
try:
|
||||
write_resp = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="Micro800")
|
||||
print("Result of tenflowmeterskid_writeplctag(self, {}, {}) = {}".format(name, value, write_resp))
|
||||
self.sendtodbDev(1, 'pond_{}_eu_min'.format(pond_num), val_n, 0, 'tenflowmeterskid')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
def tenflowmeterskid_seteumax(self, name, value):
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
pond_num = tag_n[9]
|
||||
tag_n = 'pond{}scaling.eumax'.format(pond_num)
|
||||
val_n = new_val['val']
|
||||
print("EU Max Value: {}".format(val_n))
|
||||
try:
|
||||
write_resp = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="Micro800")
|
||||
print("Result of tenflowmeterskid_writeplctag(self, {}, {}) = {}".format(name, value, write_resp))
|
||||
self.sendtodbDev(1, 'pond_{}_eu_max'.format(pond_num), val_n, 0, 'tenflowmeterskid')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
def tenflowmeterskid_setoffset(self, name, value):
|
||||
new_val = json.loads(str(value).replace("'", '"'))
|
||||
tag_n = str(new_val['tag']) # "cmd_Start"
|
||||
pond_num = tag_n[9]
|
||||
val_n = new_val['val']
|
||||
print("Offset Value: {}".format(val_n))
|
||||
try:
|
||||
write_resp = write_tag(str(PLC_IP_ADDRESS), tag_n, val_n, plc_type="Micro800")
|
||||
print("Result of tenflowmeterskid_writeplctag(self, {}, {}) = {}".format(name, value, write_resp))
|
||||
self.sendtodbDev(1, 'pond_{}_offset'.format(pond_num), val_n, 0, 'tenflowmeterskid')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def tenflowmeterskid_writeplctag(self, name, value):
|
||||
"""Write a value to the PLC."""
|
||||
|
||||
Reference in New Issue
Block a user