From 7b3e20d11c143c6d86831c8ce809dfd96caf319c Mon Sep 17 00:00:00 2001 From: Ash Date: Thu, 26 Dec 2019 16:28:46 -0500 Subject: [PATCH] chg: dev: Collect all serial writes for modem, for consistent encoding. !minor Closes #139 --- sitch/sitchlib/__init__.py | 2 +- sitch/sitchlib/alert_manager.py | 3 +-- sitch/sitchlib/arfcn_correlator.py | 5 ++--- sitch/sitchlib/cgi_correlator.py | 3 +-- sitch/sitchlib/config_helper.py | 2 -- sitch/sitchlib/decomposer.py | 2 +- sitch/sitchlib/device_detector.py | 5 ++--- sitch/sitchlib/feed_manager.py | 2 +- sitch/sitchlib/feed_schema_translator.py | 2 +- sitch/sitchlib/geo_correlator.py | 2 +- sitch/sitchlib/geo_ip.py | 2 +- sitch/sitchlib/geoip_decomposer.py | 2 +- sitch/sitchlib/gps_decomposer.py | 2 +- sitch/sitchlib/gps_device.py | 2 +- sitch/sitchlib/gsm_decomposer.py | 2 +- sitch/sitchlib/gsm_modem.py | 20 +++++++++++--------- sitch/sitchlib/kal_decomposer.py | 2 +- sitch/sitchlib/location_tool.py | 2 +- 18 files changed, 29 insertions(+), 33 deletions(-) diff --git a/sitch/sitchlib/__init__.py b/sitch/sitchlib/__init__.py index 8a289e4..b074227 100644 --- a/sitch/sitchlib/__init__.py +++ b/sitch/sitchlib/__init__.py @@ -15,4 +15,4 @@ from .geo_correlator import GeoCorrelator # NOQA __author__ = "Ash Wilson" -__version__ = "4.0" +__version__ = "4.1" diff --git a/sitch/sitchlib/alert_manager.py b/sitch/sitchlib/alert_manager.py index 7845b32..c3e6a9c 100644 --- a/sitch/sitchlib/alert_manager.py +++ b/sitch/sitchlib/alert_manager.py @@ -2,7 +2,7 @@ from .utility import Utility -class AlertManager(object): +class AlertManager: """AlertManager is used to ensure alerts are consistently formatted.""" def __init__(self, device_id): @@ -20,7 +20,6 @@ def __init__(self, device_id): 310: "GPS time delta over threshold.", 400: "Failed to locate a valid license for ARFCN in this area." } - return def get_alert_type(self, alert_id): """Return the alert description for alert_id.""" diff --git a/sitch/sitchlib/arfcn_correlator.py b/sitch/sitchlib/arfcn_correlator.py index 26abe00..496c6f2 100644 --- a/sitch/sitchlib/arfcn_correlator.py +++ b/sitch/sitchlib/arfcn_correlator.py @@ -1,12 +1,12 @@ """ARFCN Correlator.""" -from . import alert_manager import os import sqlite3 +from . import alert_manager from .utility import Utility -class ArfcnCorrelator(object): +class ArfcnCorrelator: """The ArfcnCorrelator compares ARFCN metadata against feeds and threshold. The feed data is put in place by the FeedManager class, prior to @@ -34,7 +34,6 @@ def __init__(self, feed_dir, whitelist, power_threshold, device_id): self.observed_arfcn = whitelist self.arfcn_threshold = [] self.arfcn_range = [] - return def correlate(self, scan_bolus): """Entrypoint for correlation, wraps individual checks. diff --git a/sitch/sitchlib/cgi_correlator.py b/sitch/sitchlib/cgi_correlator.py index 9f319f6..83be326 100644 --- a/sitch/sitchlib/cgi_correlator.py +++ b/sitch/sitchlib/cgi_correlator.py @@ -6,7 +6,7 @@ from .utility import Utility -class CgiCorrelator(object): +class CgiCorrelator: """The CgiCorrelator compares CGI addressing against the OpenCellID DB. The feed data is put in place by the FeedManager class, prior to @@ -33,7 +33,6 @@ def __init__(self, feed_dir, cgi_whitelist, mcc_list, device_id): self.cgi_db = os.path.join(feed_dir, "cgi.db") self.alarm_140_cache = "" print(CgiCorrelator.cgi_whitelist_message(self.cgi_whitelist)) - return def correlate(self, scan_bolus): """Entrypoint for the CGI correlation component. diff --git a/sitch/sitchlib/config_helper.py b/sitch/sitchlib/config_helper.py index f3d301c..d2ba489 100644 --- a/sitch/sitchlib/config_helper.py +++ b/sitch/sitchlib/config_helper.py @@ -71,7 +71,6 @@ def print_devices_as_detected(self): pp.pprint(self.detector.gsm_radios) print("Configurator: Detected GPS devices:") pp.pprint(self.detector.gps_devices) - return def get_gsm_modem_port(self): """Get GSM modem port from detector, override with env var.""" @@ -129,7 +128,6 @@ def write_filebeat_config(self): fb = self.set_filebeat_logfile_paths(self.log_prefix, fb) with open(self.filebeat_config_file_path, 'w') as out_file: yaml.safe_dump(fb, out_file) - return @classmethod def set_filebeat_logfile_paths(cls, log_prefix, filebeat_config): diff --git a/sitch/sitchlib/decomposer.py b/sitch/sitchlib/decomposer.py index e15e76d..cf44b43 100644 --- a/sitch/sitchlib/decomposer.py +++ b/sitch/sitchlib/decomposer.py @@ -6,7 +6,7 @@ from .geoip_decomposer import GeoipDecomposer -class Decomposer(object): +class Decomposer: """Decompose device messages into normalized log messages.""" decomp_ref = {"kalibrate": KalDecomposer(), diff --git a/sitch/sitchlib/device_detector.py b/sitch/sitchlib/device_detector.py index 45f0b9e..e0bd265 100644 --- a/sitch/sitchlib/device_detector.py +++ b/sitch/sitchlib/device_detector.py @@ -1,12 +1,11 @@ """Device Detector interrogates USB TTY devices.""" - +import time import pyudev import serial -import time from .utility import Utility -class DeviceDetector(object): +class DeviceDetector: """Interrogate all USB TTY ports. Attributes: diff --git a/sitch/sitchlib/feed_manager.py b/sitch/sitchlib/feed_manager.py index f027c85..2226cd9 100644 --- a/sitch/sitchlib/feed_manager.py +++ b/sitch/sitchlib/feed_manager.py @@ -13,7 +13,7 @@ from .utility import Utility -class FeedManager(object): +class FeedManager: """Manage downloading the feed DB, and merging it into the sqlite DB.""" def __init__(self, config): diff --git a/sitch/sitchlib/feed_schema_translator.py b/sitch/sitchlib/feed_schema_translator.py index 527d06b..ad17689 100644 --- a/sitch/sitchlib/feed_schema_translator.py +++ b/sitch/sitchlib/feed_schema_translator.py @@ -3,7 +3,7 @@ import geopy -class FeedSchemaTranslator(object): +class FeedSchemaTranslator: def __init__(self, schema): self.field_maps = schema self.translators = self.translators_from_schema(schema) diff --git a/sitch/sitchlib/geo_correlator.py b/sitch/sitchlib/geo_correlator.py index ed11ac6..befb508 100644 --- a/sitch/sitchlib/geo_correlator.py +++ b/sitch/sitchlib/geo_correlator.py @@ -4,7 +4,7 @@ from .utility import Utility -class GeoCorrelator(object): +class GeoCorrelator: """Geographic correlator.""" def __init__(self, device_id): diff --git a/sitch/sitchlib/geo_ip.py b/sitch/sitchlib/geo_ip.py index 57093b1..3209522 100644 --- a/sitch/sitchlib/geo_ip.py +++ b/sitch/sitchlib/geo_ip.py @@ -7,7 +7,7 @@ GEO_DB_LOCATION = "/var/mmdb//GeoLite2-City.mmdb" # NOQA -class GeoIp(object): +class GeoIp: """Generate GeoIP events.""" def __init__(self, delay=60): diff --git a/sitch/sitchlib/geoip_decomposer.py b/sitch/sitchlib/geoip_decomposer.py index d38797d..b25eea5 100644 --- a/sitch/sitchlib/geoip_decomposer.py +++ b/sitch/sitchlib/geoip_decomposer.py @@ -1,7 +1,7 @@ """Decompose GeoIP Events.""" -class GeoipDecomposer(object): +class GeoipDecomposer: """GeoIP Decomposer.""" @classmethod diff --git a/sitch/sitchlib/gps_decomposer.py b/sitch/sitchlib/gps_decomposer.py index e1295bf..0b291f5 100644 --- a/sitch/sitchlib/gps_decomposer.py +++ b/sitch/sitchlib/gps_decomposer.py @@ -1,7 +1,7 @@ """Decompose GPS Events.""" -class GpsDecomposer(object): +class GpsDecomposer: """GPS Decomposer.""" @classmethod diff --git a/sitch/sitchlib/gps_device.py b/sitch/sitchlib/gps_device.py index 5a3c860..6739398 100644 --- a/sitch/sitchlib/gps_device.py +++ b/sitch/sitchlib/gps_device.py @@ -6,7 +6,7 @@ import time -class GpsListener(object): +class GpsListener: """Wrap the GPS device with an iterator.""" def __init__(self, delay=60): diff --git a/sitch/sitchlib/gsm_decomposer.py b/sitch/sitchlib/gsm_decomposer.py index 6f71a58..334e93c 100644 --- a/sitch/sitchlib/gsm_decomposer.py +++ b/sitch/sitchlib/gsm_decomposer.py @@ -3,7 +3,7 @@ from .utility import Utility -class GsmDecomposer(object): +class GsmDecomposer: """Decomposes GSM scans.""" @classmethod diff --git a/sitch/sitchlib/gsm_modem.py b/sitch/sitchlib/gsm_modem.py index a3d3ebf..4b97e7c 100644 --- a/sitch/sitchlib/gsm_modem.py +++ b/sitch/sitchlib/gsm_modem.py @@ -6,7 +6,7 @@ import time -class GsmModem(object): +class GsmModem: """GSM Modem handler class. Interfaces with device over serial. Calling GsmModem.set_eng_mode() causes the module to go into @@ -40,7 +40,6 @@ def __init__(self, ser_port): if ser_open_iter > 5: print("GSM: Failed to open serial port %s!" % ser_port) sys.exit(2) - return def __iter__(self): """Yield scans from GSM modem.""" @@ -63,6 +62,10 @@ def __iter__(self): else: page.append(processed_line) + def serial_write(self, write_me): + """Convert string to bytes then write to serial.""" + self.serconn.write(write_me.encode("utf-8")) + def eng_mode(self, status): """Set or unset engineering mode on the modem. @@ -72,7 +75,7 @@ def eng_mode(self, status): self.serconn.flush() if status is False: print("GsmModem: Unsetting engineering mode, flushing") - self.serconn.write(self.unset_eng) + self.serial_write(self.unset_eng) while True: output = self.serconn.readline() if output == '': @@ -81,17 +84,16 @@ def eng_mode(self, status): print(output) else: print("GsmModem: Setting engineering mode") - self.serconn.write(self.eng_init) + self.serial_write(self.eng_init) self.serconn.flush() time.sleep(2) output = self.serconn.readline() print(output) self.serconn.flush() - return def get_reg_info(self): """Get registration information from the modem.""" - self.serconn.write(self.reg_info) + self.serial_write(self.reg_info) self.serconn.flush() time.sleep(2) output = self.serconn.readline() @@ -103,7 +105,7 @@ def get_reg_info(self): def dump_config(self): """Dump modem's configuration.""" - self.serconn.write(self.config_dump) + self.serial_write(self.config_dump) self.serconn.flush() time.sleep(2) retval = [] @@ -118,7 +120,7 @@ def dump_config(self): def get_imsi(self): """Get the IMSI of the SIM installed in the modem.""" rx = r'(?P\S+)' - self.serconn.write(self.imsi_info) + self.serial_write(self.imsi_info) self.serconn.flush() time.sleep(2) retval = [] @@ -158,7 +160,7 @@ def set_band(self, band): "EGSM_PCS_MODE", "ALL_BAND"]: term_command = "AT+CBAND=\"%s\" \r\n" % band print("GSM: Setting GSM band with: %s" % term_command) - self.serconn.write(term_command) + self.serial_write(term_command) self.serconn.flush() time.sleep(2) output = self.serconn.readline() diff --git a/sitch/sitchlib/kal_decomposer.py b/sitch/sitchlib/kal_decomposer.py index 6f4c3d5..4366695 100644 --- a/sitch/sitchlib/kal_decomposer.py +++ b/sitch/sitchlib/kal_decomposer.py @@ -3,7 +3,7 @@ from .utility import Utility -class KalDecomposer(object): +class KalDecomposer: """Decompose Kalibrate scans.""" @classmethod diff --git a/sitch/sitchlib/location_tool.py b/sitch/sitchlib/location_tool.py index 0f14416..ee79076 100644 --- a/sitch/sitchlib/location_tool.py +++ b/sitch/sitchlib/location_tool.py @@ -2,7 +2,7 @@ from geopy.distance import great_circle -class LocationTool(object): +class LocationTool: """Class with location-oriented functions.""" @classmethod def validate_geo(cls, latlon):