Updates driver to send public ip address correctly

This commit is contained in:
Patrick McDonagh
2018-07-18 14:17:43 -05:00
parent b423bc41f9
commit b216a435f2
5 changed files with 3083 additions and 2927 deletions

View File

@@ -0,0 +1,13 @@
{
"files": {
"file3": "channel.py",
"file2": "utilities.py",
"file1": "tanktransfer.py",
"file5": "modbusMap.p",
"file4": "file_logger.py"
},
"deviceName": "tanktransfer",
"driverId": "0250",
"releaseVersion": "2",
"driverFileName": "tanktransfer.py"
}

View File

@@ -1,12 +1,13 @@
{
"name": "tanktransfer",
"driverFilename": "tanktransfer.py",
"driverId": "0000",
"name": "tanktransfer",
"driverFilename": "tanktransfer.py",
"driverId": "0250",
"additionalDriverFiles": [
"utilities.py",
"persistence.py",
"Channel.py"
],
"version": 1,
"utilities.py",
"channel.py",
"file_logger.py",
"modbusMap.p"
],
"version": 2,
"s3BucketName": "tanktransfer"
}
}

View File

@@ -0,0 +1,18 @@
"""Logging setup for tanktransfer"""
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 = './tanktransfer.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('tanktransfer')
filelogger.setLevel(logging.INFO)
filelogger.addHandler(my_handler)
console_out = logging.StreamHandler(sys.stdout)
console_out.setFormatter(log_formatter)
filelogger.addHandler(console_out)

File diff suppressed because it is too large Load Diff

View File

@@ -2,45 +2,29 @@
import threading
import sys
from device_base import deviceBase
from Channel import Channel, read_tag, write_tag, BoolArrayChannels
from Maps import tanktransfer_map as maps
from Maps import reverse_map
import persistence
from random import randint
from utilities import get_public_ip_address
import json
import time
import logging
from device_base import deviceBase
from random import randint
from utilities import get_public_ip_address
from file_logger import filelogger as log
from channel import Channel, read_tag, write_tag
_ = None
# LOGGING SETUP
from logging.handlers import RotatingFileHandler
log.info("tanktransfer startup")
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
logFile = './tanktransfer.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('tanktransfer')
logger.setLevel(logging.INFO)
logger.addHandler(my_handler)
console_out = logging.StreamHandler(sys.stdout)
console_out.setFormatter(log_formatter)
logger.addHandler(console_out)
logger.info("tanktransfer startup")
# GLOBAL VARIABLES
WAIT_FOR_CONNECTION_SECONDS = 60
IP_CHECK_PERIOD = 60
WATCHDOG_ENABLE = True
WATCHDOG_CHECK_PERIOD = 60
WATCHDOG_SEND_PERIOD = 3600 # Seconds, the longest amount of time before sending the watchdog status
PLC_IP_ADDRESS = "192.168.1.10"
CHANNELS = []
# PERSISTENCE FILE
persist = persistence.load()
class start(threading.Thread, deviceBase):
"""Start class required by Meshify."""
@@ -51,9 +35,14 @@ class start(threading.Thread, deviceBase):
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.version = "2"
self.finished = threading.Event()
self.forceSend = False
self.public_ip_address = ""
self.public_ip_address_last_checked = 0
self.watchdog = False
self.watchdog_last_checked = 0
self.watchdog_last_sent = 0
threading.Thread.start(self)
# this is a required function for all drivers, its goal is to upload some piece of data
@@ -65,52 +54,60 @@ class start(threading.Thread, deviceBase):
def run(self):
"""Actually run the driver."""
global persist
wait_sec = 60
for i in range(0, wait_sec):
print("tanktransfer driver will start in {} seconds".format(wait_sec - i))
for i in range(0, WAIT_FOR_CONNECTION_SECONDS):
print("tanktransfer driver will start in {} seconds".format(WAIT_FOR_CONNECTION_SECONDS - i))
time.sleep(1)
logger.info("BOOM! Starting tanktransfer driver...")
log.info("BOOM! Starting tanktransfer driver...")
public_ip_address = get_public_ip_address()
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'tanktransfer')
watchdog = self.tanktransfer_watchdog()
self.sendtodbDev(1, 'watchdog', watchdog, 0, 'tanktransfer')
watchdog_send_timestamp = time.time()
self._check_watchdog()
self._check_ip_address()
self.nodes["tanktransfer_0199"] = self
send_loops = 0
watchdog_loops = 0
watchdog_check_after = 5000
while True:
now = time.time()
if self.forceSend:
logger.warning("FORCE SEND: TRUE")
log.warning("FORCE SEND: TRUE")
for c in CHANNELS:
if c.read(self.forceSend):
self.sendtodbDev(1, c.mesh_name, c.value, 0, 'tanktransfer')
# print("tanktransfer driver still alive...")
if self.forceSend:
if send_loops > 2:
logger.warning("Turning off forceSend")
log.warning("Turning off forceSend")
self.forceSend = False
send_loops = 0
else:
send_loops += 1
watchdog_loops += 1
if (watchdog_loops >= watchdog_check_after):
test_watchdog = self.tanktransfer_watchdog()
if not test_watchdog == watchdog or (time.time() - watchdog_send_timestamp) > WATCHDOG_SEND_PERIOD:
self.sendtodbDev(1, 'watchdog', test_watchdog, 0, 'tanktransfer')
watchdog = test_watchdog
if WATCHDOG_ENABLE:
if (now - self.watchdog_last_checked) > WATCHDOG_CHECK_PERIOD:
self._check_watchdog()
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, 'tanktransfer')
public_ip_address = test_public_ip
watchdog_loops = 0
if (now - self.public_ip_address_last_checked) > IP_CHECK_PERIOD:
self._check_ip_address()
def _check_watchdog(self):
"""Check the watchdog and send to Meshify if changed or stale."""
test_watchdog = self.tanktransfer_watchdog()
now = time.time()
self.watchdog_last_checked = now
if test_watchdog != self.watchdog or (now - self.watchdog_last_sent) > WATCHDOG_SEND_PERIOD:
self.sendtodbDev(1, 'watchdog', test_watchdog, 0, 'tanktransfer')
self.watchdog = test_watchdog
self.watchdog_last_sent = now
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, 'tanktransfer')
self.public_ip_address = test_public_ip
def tanktransfer_watchdog(self):
"""Write a random integer to the PLC and then 1 seconds later check that it has been decremented by 1."""