Created
November 15, 2018 06:16
-
-
Save 0x9900/9c5e6b51b3c56ce903e8db09076a5d87 to your computer and use it in GitHub Desktop.
YSF2DMR Gateway management
This file contains hidden or 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
#!/usr/bin/env python | |
import argparse | |
import logging | |
import sys | |
import os | |
import subprocess | |
from ConfigParser import SafeConfigParser | |
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', | |
datefmt='%H:%M:%S', | |
level=logging.DEBUG) | |
CONFIGFILE = "/etc/YSF2DMR.ini" | |
SECTION = "DMR Network" | |
OPTION_ID = "StartupDstId" | |
class YSFConfig(SafeConfigParser): | |
def write(self, fp): | |
"""Write an .ini-format representation of the configuration state.""" | |
if self._defaults: | |
fp.write("[%s]\n" % "DEFAULT") | |
for (key, value) in self._defaults.items(): | |
fp.write("%s=%s\n" % (key, str(value).replace('\n', '\n\t'))) | |
fp.write("\n") | |
for section in self._sections: | |
fp.write("[%s]\n" % section) | |
for (key, value) in self._sections[section].items(): | |
if key == "__name__": | |
continue | |
if (value is not None) or (self._optcre == self.OPTCRE): | |
key = "=".join((key, str(value).replace('\n', '\n\t'))) | |
fp.write("%s\n" % (key)) | |
fp.write("\n") | |
def update_tg(configfile, new_tg): | |
if isinstance(new_tg, (int, float)): | |
new_tg = str(new_tg) | |
parser = YSFConfig() | |
parser.optionxform = str | |
parser.read(configfile) | |
current_tg = parser.get(SECTION, OPTION_ID) | |
if current_tg == new_tg: | |
logging.error('The talkgroup %s is already active', current_tg) | |
sys.exit(os.EX_CONFIG) | |
logging.info("Existing talkgroup is: %s", current_tg) | |
logging.info("Desired talkgroup is: %s", new_tg) | |
parser.set(SECTION, OPTION_ID, new_tg) | |
logging.info("Saving new configuration file") | |
with open(configfile, 'wb') as cfd: | |
parser.write(cfd) | |
def change_tg(configfile, talkgroup): | |
update_tg(configfile, talkgroup) | |
restart_bridge() | |
def start_bridge(): | |
logging.info("Starting YSF2DMR") | |
result = subprocess.call('systemctl start ysf2dmr'.split()) | |
if result != 0: | |
logging.error("start ysf2dmr exited with status %s", result) | |
def stop_bridge(): | |
logging.info("Stopping YSF2DMR") | |
result = subprocess.call('systemctl stop ysf2dmr'.split()) | |
if result != 0: | |
logging.error("stop ysf2dmr exited with status %s", result) | |
def restart_bridge(): | |
logging.info("Restarting YSF2DMR") | |
result = subprocess.call('systemctl restart ysf2dmr'.split()) | |
if result != 0: | |
logging.error("restart ysf2dmr exited with status %s", result) | |
def daemon_status(): | |
logging.info("YSF2DMR daemon status") | |
result = subprocess.call('systemctl status ysf2dmr'.split()) | |
print result | |
def main(): | |
aparser = argparse.ArgumentParser(description='Manage the YSF2DMR gateway') | |
subparsers = aparser.add_subparsers(help='Command list') | |
# commands | |
subp_tg = subparsers.add_parser('change', help='New Talkgroup') | |
subp_tg.set_defaults(func=change_tg) | |
subp_tg.add_argument('-C', '--configfile', default=CONFIGFILE, | |
help='Full path to YSF2DMR.ini [default: %(default)s]') | |
subp_tg.add_argument('-T', '--talkgroup', required=True, help='New DMR talkgroup id') | |
# stop | |
subp_stop = subparsers.add_parser('stop', help='Stop YSF2DMR') | |
subp_stop.set_defaults(func=stop_bridge) | |
# start | |
subp_start = subparsers.add_parser('start', help='Start YSF2DMR') | |
subp_start.set_defaults(func=start_bridge) | |
# status | |
subp_status = subparsers.add_parser('status', help='Daemon status YSF2DMR') | |
subp_status.set_defaults(func=daemon_status) | |
pargs = aparser.parse_args() | |
if os.geteuid() != 0: | |
logging.error('You need to be root to run this program') | |
sys.exit(os.EX_NOPERM) | |
if pargs.func == change_tg: | |
change_tg(pargs.configfile, pargs.talkgroup) | |
else: | |
pargs.func() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment