Fixes and updates
This commit is contained in:
@@ -1,2 +1,12 @@
|
||||
"""Dummy class for device Base."""
|
||||
|
||||
class deviceBase(object):
|
||||
pass
|
||||
"""Dummy class for device Base."""
|
||||
|
||||
def sendtodbDev(self, idnum, name, value, timestamp, drivername):
|
||||
"""Dummy sendtodbDev function."""
|
||||
pass
|
||||
|
||||
def sendtodb(self, name, value, timestamp):
|
||||
"""Dummy sendtodb function."""
|
||||
pass
|
||||
|
||||
@@ -5,11 +5,11 @@ 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
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.connect(("8.8.8.8", 80))
|
||||
ip_address = sock.getsockname()[0]
|
||||
sock.close()
|
||||
return ip_address
|
||||
|
||||
|
||||
def int_to_float16(int_to_convert):
|
||||
@@ -26,19 +26,17 @@ def int_to_float16(int_to_convert):
|
||||
elif exponent == float(0b11111):
|
||||
if fraction == 0:
|
||||
return sign * float("inf")
|
||||
else:
|
||||
return float("NaN")
|
||||
else:
|
||||
frac_part = 1.0 + fraction / (2.0 ** 10.0)
|
||||
return sign * (2 ** (exponent - 15)) * frac_part
|
||||
return float("NaN")
|
||||
frac_part = 1.0 + fraction / (2.0 ** 10.0)
|
||||
return sign * (2 ** (exponent - 15)) * frac_part
|
||||
|
||||
|
||||
def ints_to_float(int1, int2):
|
||||
"""Convert 2 registers into a floating point number."""
|
||||
mypack = struct.pack('>HH', int1, int2)
|
||||
f = struct.unpack('>f', mypack)
|
||||
print("[{}, {}] >> {}".format(int1, int2, f[0]))
|
||||
return f[0]
|
||||
f_unpacked = struct.unpack('>f', mypack)
|
||||
print("[{}, {}] >> {}".format(int1, int2, f_unpacked[0]))
|
||||
return f_unpacked[0]
|
||||
|
||||
|
||||
def degf_to_degc(temp_f):
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
import threading
|
||||
import sys
|
||||
from device_base import deviceBase
|
||||
from Channel import Channel, read_tag, write_tag
|
||||
import persistence
|
||||
from random import randint
|
||||
from utilities import get_public_ip_address
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
from random import randint
|
||||
|
||||
from device_base import deviceBase
|
||||
from Channel import Channel, read_tag, write_tag
|
||||
import persistence
|
||||
from utilities import get_public_ip_address
|
||||
|
||||
_ = None
|
||||
|
||||
@@ -18,7 +19,8 @@ from logging.handlers import RotatingFileHandler
|
||||
|
||||
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
|
||||
logFile = './{{cookiecutter.driver_name}}.log'
|
||||
my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=500*1024, backupCount=2, encoding=None, delay=0)
|
||||
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('{{cookiecutter.driver_name}}')
|
||||
@@ -43,15 +45,18 @@ persist = persistence.load()
|
||||
class start(threading.Thread, deviceBase):
|
||||
"""Start class required by Meshify."""
|
||||
|
||||
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None):
|
||||
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)
|
||||
deviceBase.__init__(self, name=name, number=number, mac=mac, Q=Q,
|
||||
mcu=mcu, companyId=companyId, offset=offset,
|
||||
mqtt=mqtt, Nodes=Nodes)
|
||||
|
||||
self.daemon = True
|
||||
self.version = "1"
|
||||
self.finished = threading.Event()
|
||||
self.forceSend = False
|
||||
self.force_send = False
|
||||
threading.Thread.start(self)
|
||||
|
||||
# this is a required function for all drivers, its goal is to upload some piece of data
|
||||
@@ -80,20 +85,20 @@ class start(threading.Thread, deviceBase):
|
||||
watchdog_loops = 0
|
||||
watchdog_check_after = 5000
|
||||
while True:
|
||||
if self.forceSend:
|
||||
if self.force_send:
|
||||
logger.warning("FORCE SEND: TRUE")
|
||||
|
||||
for c in CHANNELS:
|
||||
v = c.read()
|
||||
if c.check(self.forceSend):
|
||||
if c.check(self.force_send):
|
||||
self.sendtodbDev(1, c.mesh_name, c.value, 0, '{{cookiecutter.driver_name}}')
|
||||
|
||||
|
||||
# print("{{cookiecutter.driver_name}} driver still alive...")
|
||||
if self.forceSend:
|
||||
if self.force_send:
|
||||
if send_loops > 2:
|
||||
logger.warning("Turning off forceSend")
|
||||
self.forceSend = False
|
||||
logger.warning("Turning off force_send")
|
||||
self.force_send = False
|
||||
send_loops = 0
|
||||
else:
|
||||
send_loops += 1
|
||||
@@ -124,7 +129,7 @@ class start(threading.Thread, deviceBase):
|
||||
|
||||
def {{cookiecutter.driver_name}}_sync(self, name, value):
|
||||
"""Sync all data from the driver."""
|
||||
self.forceSend = True
|
||||
self.force_send = True
|
||||
# self.sendtodb("log", "synced", 0)
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user