added public ip lookup
This commit is contained in:
@@ -6,6 +6,6 @@
|
|||||||
},
|
},
|
||||||
"deviceName": "flowmonitor",
|
"deviceName": "flowmonitor",
|
||||||
"driverId": "0140",
|
"driverId": "0140",
|
||||||
"releaseVersion": "20",
|
"releaseVersion": "22",
|
||||||
"driverFileName": "flow-monitor.py"
|
"driverFileName": "flow-monitor.py"
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
from device_base import deviceBase
|
from device_base import deviceBase
|
||||||
import persistence
|
import persistence
|
||||||
from utilities import get_public_ip_address
|
from utilities import get_public_ip_address, get_private_ip_address
|
||||||
|
|
||||||
# LOGGING SETUP
|
# LOGGING SETUP
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
@@ -184,7 +184,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
self.force_send = False
|
self.force_send = False
|
||||||
|
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.version = "20"
|
self.version = "22"
|
||||||
self.finished = threading.Event()
|
self.finished = threading.Event()
|
||||||
threading.Thread.start(self)
|
threading.Thread.start(self)
|
||||||
|
|
||||||
@@ -238,8 +238,19 @@ class start(threading.Thread, deviceBase):
|
|||||||
##############################################
|
##############################################
|
||||||
|
|
||||||
# Determine public IP address and send to Meshify
|
# Determine public IP address and send to Meshify
|
||||||
public_ip_address = get_public_ip_address()
|
public_ip_address = "0.0.0.0"
|
||||||
self.sendtodb('public_ip_address', public_ip_address, 0)
|
private_ip_address = "0.0.0.0"
|
||||||
|
try:
|
||||||
|
public_ip_address = get_public_ip_address()
|
||||||
|
self.sendtodb('public_ip_address', public_ip_address, 0)
|
||||||
|
except Exception as e:
|
||||||
|
self.sendtodb('error', 'could not get public ip', 0)
|
||||||
|
|
||||||
|
try:
|
||||||
|
private_ip_address = get_private_ip_address()
|
||||||
|
self.sendtodb('private_ip_address', private_ip_address, 0)
|
||||||
|
except Exception as e:
|
||||||
|
self.sendtodb('error', 'could not get private ip', 0)
|
||||||
ip_checked_time = time.time()
|
ip_checked_time = time.time()
|
||||||
|
|
||||||
# Attempt to retrieve data stored in the database
|
# Attempt to retrieve data stored in the database
|
||||||
|
|||||||
@@ -275,16 +275,19 @@ class start(threading.Thread, deviceBase):
|
|||||||
|
|
||||||
def _check_ip_address(self):
|
def _check_ip_address(self):
|
||||||
"""Check the public IP address and send to Meshify if changed."""
|
"""Check the public IP address and send to Meshify if changed."""
|
||||||
self.public_ip_address_last_checked = time.time()
|
try:
|
||||||
test_public_ip = get_public_ip_address()
|
self.public_ip_address_last_checked = time.time()
|
||||||
test_public_ip = test_public_ip
|
test_public_ip = get_public_ip_address()
|
||||||
test_private_ip = get_private_ip_address()
|
test_public_ip = test_public_ip
|
||||||
if not test_public_ip == self.public_ip_address and not test_public_ip == "0.0.0.0":
|
test_private_ip = get_private_ip_address()
|
||||||
self.sendtodb('public_ip_address', test_public_ip, 0)
|
if not test_public_ip == self.public_ip_address and not test_public_ip == "0.0.0.0":
|
||||||
self.public_ip_address = test_public_ip
|
self.sendtodb('public_ip_address', test_public_ip, 0)
|
||||||
if not test_private_ip == self.private_ip_address:
|
self.public_ip_address = test_public_ip
|
||||||
self.sendtodb('private_ip_address', test_private_ip, 0)
|
if not test_private_ip == self.private_ip_address:
|
||||||
self.private_ip_address = test_private_ip
|
self.sendtodb('private_ip_address', test_private_ip, 0)
|
||||||
|
self.private_ip_address = test_private_ip
|
||||||
|
except Exception as e:
|
||||||
|
self.sendtodb('error', e, 0)
|
||||||
|
|
||||||
|
|
||||||
def flowmonitor_sync(self, name, value):
|
def flowmonitor_sync(self, name, value):
|
||||||
|
|||||||
@@ -1,11 +1,26 @@
|
|||||||
"""Utility functions for the driver."""
|
"""Utility functions for the driver."""
|
||||||
import socket
|
import socket
|
||||||
|
try:
|
||||||
|
import urllib
|
||||||
|
import contextlib
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_private_ip_address():
|
||||||
def get_public_ip_address():
|
"""Find the private IP Address of the host device."""
|
||||||
"""Find the public IP Address of the host device."""
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
sock.connect(("8.8.8.8", 80))
|
sock.connect(("8.8.8.8", 80))
|
||||||
public_ip = sock.getsockname()[0]
|
public_ip = sock.getsockname()[0]
|
||||||
sock.close()
|
sock.close()
|
||||||
return public_ip
|
return public_ip
|
||||||
|
|
||||||
|
def get_public_ip_address():
|
||||||
|
"""Find the public IP Address of the host device."""
|
||||||
|
ip_address = "0.0.0.0"
|
||||||
|
try:
|
||||||
|
with contextlib.closing(urllib.urlopen("http://checkip.amazonaws.com")) as url:
|
||||||
|
ip_address = url.read()
|
||||||
|
except Exception as e:
|
||||||
|
print("could not resolve check IP: {}".format(e))
|
||||||
|
return ip_address
|
||||||
|
return ip_address[:-1]
|
||||||
@@ -6,6 +6,6 @@
|
|||||||
},
|
},
|
||||||
"deviceName": "promagmbs",
|
"deviceName": "promagmbs",
|
||||||
"driverId": "0199",
|
"driverId": "0199",
|
||||||
"releaseVersion": "4",
|
"releaseVersion": "5",
|
||||||
"driverFileName": "promagmbs.py"
|
"driverFileName": "promagmbs.py"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import threading
|
import threading
|
||||||
from device_base import deviceBase
|
from device_base import deviceBase
|
||||||
from utilities import get_public_ip_address
|
from utilities import get_public_ip_address, get_private_ip_address
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
@@ -34,7 +34,7 @@ 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)
|
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.daemon = True
|
||||||
self.version = "4"
|
self.version = "5"
|
||||||
self.finished = threading.Event()
|
self.finished = threading.Event()
|
||||||
self.forceSend = False
|
self.forceSend = False
|
||||||
threading.Thread.start(self)
|
threading.Thread.start(self)
|
||||||
@@ -55,9 +55,19 @@ class start(threading.Thread, deviceBase):
|
|||||||
logger.info("promagmbs driver will start in {} seconds".format(wait_sec - i))
|
logger.info("promagmbs driver will start in {} seconds".format(wait_sec - i))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
logger.warning("BOOM! Starting promagmbs driver...")
|
logger.warning("BOOM! Starting promagmbs driver...")
|
||||||
|
public_ip_address = "0.0.0.0"
|
||||||
public_ip_address = get_public_ip_address()
|
private_ip_address = "0.0.0.0"
|
||||||
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'promagmbs')
|
try:
|
||||||
|
public_ip_address = get_public_ip_address()
|
||||||
|
self.sendtodbDev(1, 'public_ip_address', public_ip_address, 0, 'promagmbs')
|
||||||
|
except:
|
||||||
|
self.sendtodbDev(1, 'error', "error in public ip", 0, 'promagmbs')
|
||||||
|
|
||||||
|
try:
|
||||||
|
private_ip_address = get_private_ip_address()
|
||||||
|
self.sendtodbDev(1, 'private_ip_address', private_ip_address, 0, 'promagmbs')
|
||||||
|
except:
|
||||||
|
self.sendtodbDev(1, 'error', "error in private ip", 0, 'promagmbs')
|
||||||
|
|
||||||
send_loops = 0
|
send_loops = 0
|
||||||
watchdog_loops = 0
|
watchdog_loops = 0
|
||||||
|
|||||||
@@ -1,15 +1,30 @@
|
|||||||
"""Utility functions for the driver."""
|
"""Utility functions for the driver."""
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
|
try:
|
||||||
|
import urllib
|
||||||
|
import contextlib
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_private_ip_address():
|
||||||
|
"""Find the private IP Address of the host device."""
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
sock.connect(("8.8.8.8", 80))
|
||||||
|
public_ip = sock.getsockname()[0]
|
||||||
|
sock.close()
|
||||||
|
return public_ip
|
||||||
|
|
||||||
def get_public_ip_address():
|
def get_public_ip_address():
|
||||||
"""Find the public IP Address of the host device."""
|
"""Find the public IP Address of the host device."""
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
ip_address = "0.0.0.0"
|
||||||
s.connect(("8.8.8.8", 80))
|
try:
|
||||||
ip = s.getsockname()[0]
|
with contextlib.closing(urllib.urlopen("http://checkip.amazonaws.com")) as url:
|
||||||
s.close()
|
ip_address = url.read()
|
||||||
return ip
|
except Exception as e:
|
||||||
|
print("could not resolve check IP: {}".format(e))
|
||||||
|
return ip_address
|
||||||
|
return ip_address[:-1]
|
||||||
|
|
||||||
|
|
||||||
def int_to_float16(int_to_convert):
|
def int_to_float16(int_to_convert):
|
||||||
|
|||||||
Reference in New Issue
Block a user