Files
HenryPump-Drivers/plcfreshwater/plcfreshwater.py
2020-09-11 14:08:38 -05:00

299 lines
14 KiB
Python

"""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
from datetime import datetime as dt
_ = 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 = "13"
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, PERSIST
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')
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
while True:
now = time.time()
if self.force_send:
log.warning("FORCE SEND: TRUE")
for chan in CHANNELS:
try:
val = chan.read()
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))
# 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
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')
if self.force_send:
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')
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')
if self.force_send:
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')
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 Months'] = 0
PERSIST['Monthly Holding'] = PERSIST['Lifetime']
else:
PERSIST['Previous Months'] = val - PERSIST['Monthly Holding']
PERSIST['Current Months'] = 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))