-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
195 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import asyncio | ||
import importlib.util | ||
import logging.config | ||
import os | ||
import sys | ||
from signal import SIGINT, SIGTERM | ||
|
||
from okdmr.hhb.hytera_homebrew_bridge import HyteraHomebrewBridge | ||
|
||
|
||
def main(): | ||
logger_configured: bool = False | ||
if len(sys.argv) > 2: | ||
if os.path.isfile(path=sys.argv[2]): | ||
logging.config.fileConfig(fname=sys.argv[2]) | ||
logger_configured = True | ||
else: | ||
logging.getLogger().error(f"logging ini file not valid {sys.argv[2]}") | ||
exit() | ||
if not logger_configured: | ||
logging.basicConfig( | ||
level=logging.DEBUG, | ||
format="%(levelname)s - %(asctime)s - %(name)s - %(message)s", | ||
) | ||
logging.getLogger(name="puresnmp.transport").setLevel(logging.WARN) | ||
|
||
mainlog = logging.getLogger(name="hytera-homebrew-bridge") | ||
|
||
mainlog.info("Hytera Homebrew Bridge") | ||
|
||
if len(sys.argv) < 2: | ||
mainlog.error( | ||
"use as hytera-homebrew-bridge <path to settings.ini> <optionally path to logger.ini>" | ||
) | ||
mainlog.error( | ||
"If you do not have the settings.ini file, you can obtain one here: " | ||
"https://github.com/OK-DMR/Hytera_Homebrew_Bridge/blob/master/settings.ini.default" | ||
) | ||
exit(1) | ||
|
||
uvloop_spec = importlib.util.find_spec(name="uvloop") | ||
if uvloop_spec: | ||
# noinspection PyUnresolvedReferences | ||
import uvloop | ||
|
||
uvloop.install() | ||
|
||
# suppress puresnmp_plugins experimental warning | ||
if not sys.warnoptions: | ||
import warnings | ||
|
||
warnings.filterwarnings( | ||
message="Experimental SNMPv1 support", category=UserWarning, action="ignore" | ||
) | ||
|
||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop=loop) | ||
# order is IMPORTANT, various asyncio object are created at bridge init | ||
# and those must be created after the main loop is created | ||
bridge: HyteraHomebrewBridge = HyteraHomebrewBridge(settings_ini_path=sys.argv[1]) | ||
if os.name != "nt": | ||
for signal in [SIGINT, SIGTERM]: | ||
loop.add_signal_handler(signal, bridge.stop_running) | ||
|
||
try: | ||
loop.run_until_complete(future=bridge.go()) | ||
loop.run_forever() | ||
except BaseException as e: | ||
mainlog.exception(msg="", exc_info=e) | ||
finally: | ||
mainlog.info("Hytera Homebrew Bridge Ended") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
class CallbackInterface: | ||
async def homebrew_connect(self, ip: str) -> None: | ||
""" | ||
This interface doesn't have other purpose than code de-duplication | ||
""" | ||
|
||
async def homebrew_connect(self, ip: str, port: int) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
from asyncio import protocols | ||
|
||
from okdmr.dmrlib.utils.logging_trait import LoggingTrait | ||
|
||
from okdmr.hhb.settings import BridgeSettings | ||
from okdmr.hhb.snmp import SNMP | ||
|
||
|
||
class CustomBridgeDatagramProtocol(protocols.DatagramProtocol, LoggingTrait): | ||
""" | ||
Code de-duplication | ||
""" | ||
|
||
def __init__(self, settings: BridgeSettings) -> None: | ||
""" | ||
:param settings: | ||
""" | ||
super().__init__() | ||
self.settings = settings | ||
|
||
async def hytera_repeater_obtain_snmp( | ||
self, address: tuple, force: bool = False | ||
) -> None: | ||
""" | ||
:param address: | ||
:param force: | ||
:return: | ||
""" | ||
self.settings.hytera_repeater_ip = address[0] | ||
if self.settings.snmp_enabled: | ||
if force or not self.settings.hytera_snmp_data.get(address[0]): | ||
await SNMP().walk_ip(address, self.settings) | ||
if self.settings.snmp_enabled and ( | ||
force or not self.settings.hytera_snmp_data.get(address[0]) | ||
): | ||
await SNMP().walk_ip(address, self.settings) | ||
else: | ||
self.log_warning("SNMP is disabled") | ||
self.log_warning( | ||
f"SNMP is disabled or not available " | ||
f"snmp_enabled:{self.settings.snmp_enabled} " | ||
f"force:{force} " | ||
f"hytera_snmp_data:{address[0] in self.settings.hytera_snmp_data}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.