Added multi-master, moved logging

Seemed like multiple masters could be a thing, so I set it up to create
an arbitrary number of them. Moved logging out of the main script file
so there’s less junk to scroll through.
This commit is contained in:
Cort Buffington 2016-07-20 19:14:42 -05:00
parent 51c9b39b24
commit 4297e6a374
3 changed files with 112 additions and 93 deletions

View File

@ -7,13 +7,22 @@ LOG_HANDLERS: console-timed
LOG_LEVEL: INFO LOG_LEVEL: INFO
LOG_NAME: HBlink LOG_NAME: HBlink
[MASTER] [MASTER-1]
MODE: MASTER
ENABLED: True ENABLED: True
IP: IP:
PORT: 54000 PORT: 54000
PASSPHRASE: PASSPHRASE: s3cr37w0rd
[MASTER-2]
MODE: MASTER
ENABLED: True
IP:
PORT: 55000
PASSPHRASE: 13370p3r470r
[REPEATER-1] [REPEATER-1]
MODE: CLIENT
ENABLED: True ENABLED: True
IP: IP:
PORT: 54001 PORT: 54001
@ -36,6 +45,7 @@ SOFTWARE_ID: HBlink v1.0
PACKAGE_ID: HBlink v1.0 PACKAGE_ID: HBlink v1.0
[REPEATER-2] [REPEATER-2]
MODE: CLIENT
ENABLED: True ENABLED: True
IP: IP:
PORT: 54002 PORT: 54002

131
hblink.py
View File

@ -12,9 +12,8 @@ import ConfigParser
import argparse import argparse
import sys import sys
import os import os
import logging import log
from logging.config import dictConfig
from binascii import b2a_hex as h from binascii import b2a_hex as h
from socket import gethostbyname from socket import gethostbyname
@ -42,6 +41,7 @@ cli_args = parser.parse_args()
#************************************************ #************************************************
CLIENTS = {} CLIENTS = {}
MASTERS = {}
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
if not cli_args.CFG_FILE: if not cli_args.CFG_FILE:
@ -66,40 +66,41 @@ try:
'LOG_LEVEL': config.get(section, 'LOG_LEVEL'), 'LOG_LEVEL': config.get(section, 'LOG_LEVEL'),
'LOG_NAME': config.get(section, 'LOG_NAME') 'LOG_NAME': config.get(section, 'LOG_NAME')
} }
elif section == 'MASTER':
# HomeBrew Master Configuration
MASTER = {
'ENABLED': config.getboolean(section, 'ENABLED'),
'IP': gethostbyname(config.get(section, 'IP')),
'PORT': config.getint(section, 'PORT'),
'PASSPHRASE': config.get(section, 'PASSPHRASE')
}
elif config.getboolean(section, 'ENABLED'): elif config.getboolean(section, 'ENABLED'):
# HomeBrew Client (Repeater) Configuration(s) # HomeBrew Client (Repeater) Configuration(s)
CLIENTS.update({section: { if config.get(section, 'MODE') == 'CLIENT':
'ENABLED': config.getboolean(section, 'ENABLED'), CLIENTS.update({section: {
'IP': gethostbyname(config.get(section, 'IP')), 'ENABLED': config.getboolean(section, 'ENABLED'),
'PORT': config.getint(section, 'PORT'), 'IP': gethostbyname(config.get(section, 'IP')),
'MASTER_IP': gethostbyname(config.get(section, 'MASTER_IP')), 'PORT': config.getint(section, 'PORT'),
'MASTER_PORT': config.getint(section, 'MASTER_PORT'), 'MASTER_IP': gethostbyname(config.get(section, 'MASTER_IP')),
'PASSPHRASE': config.get(section, 'PASSPHRASE'), 'MASTER_PORT': config.getint(section, 'MASTER_PORT'),
'CALLSIGN': config.get(section, 'CALLSIGN'), 'PASSPHRASE': config.get(section, 'PASSPHRASE'),
'RADIO_ID': hex(int(config.get(section, 'RADIO_ID')))[2:].rjust(8,'0').decode('hex'), 'CALLSIGN': config.get(section, 'CALLSIGN'),
'RX_FREQ': config.get(section, 'RX_FREQ'), 'RADIO_ID': hex(int(config.get(section, 'RADIO_ID')))[2:].rjust(8,'0').decode('hex'),
'TX_FREQ': config.get(section, 'TX_FREQ'), 'RX_FREQ': config.get(section, 'RX_FREQ'),
'TX_POWER': config.get(section, 'TX_POWER'), 'TX_FREQ': config.get(section, 'TX_FREQ'),
'COLORCODE': config.get(section, 'COLORCODE'), 'TX_POWER': config.get(section, 'TX_POWER'),
'LATITUDE': config.get(section, 'LATITUDE'), 'COLORCODE': config.get(section, 'COLORCODE'),
'LONGITUDE': config.get(section, 'LONGITUDE'), 'LATITUDE': config.get(section, 'LATITUDE'),
'HEIGHT': config.get(section, 'HEIGHT'), 'LONGITUDE': config.get(section, 'LONGITUDE'),
'LOCATION': config.get(section, 'LOCATION'), 'HEIGHT': config.get(section, 'HEIGHT'),
'DESCRIPTION': config.get(section, 'DESCRIPTION'), 'LOCATION': config.get(section, 'LOCATION'),
'URL': config.get(section, 'URL'), 'DESCRIPTION': config.get(section, 'DESCRIPTION'),
'SOFTWARE_ID': config.get(section, 'SOFTWARE_ID'), 'URL': config.get(section, 'URL'),
'PACKAGE_ID': config.get(section, 'PACKAGE_ID') 'SOFTWARE_ID': config.get(section, 'SOFTWARE_ID'),
}}) 'PACKAGE_ID': config.get(section, 'PACKAGE_ID')
}})
elif config.get(section, 'MODE') == 'MASTER':
# HomeBrew Master Configuration
MASTERS.update({section: {
'ENABLED': config.getboolean(section, 'ENABLED'),
'IP': gethostbyname(config.get(section, 'IP')),
'PORT': config.getint(section, 'PORT'),
'PASSPHRASE': config.get(section, 'PASSPHRASE')
}})
except: except:
sys.exit('Could not parse configuration file, exiting...') sys.exit('Could not parse configuration file, exiting...')
@ -109,61 +110,7 @@ except:
# CONFIGURE THE SYSTEM LOGGER # CONFIGURE THE SYSTEM LOGGER
#************************************************ #************************************************
dictConfig({ logger = log.config_logging(LOGGER)
'version': 1,
'disable_existing_loggers': False,
'filters': {
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'timed': {
'format': '%(levelname)s %(asctime)s %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'syslog': {
'format': '%(name)s (%(process)d): %(levelname)s %(message)s'
}
},
'handlers': {
'null': {
'class': 'logging.NullHandler'
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'console-timed': {
'class': 'logging.StreamHandler',
'formatter': 'timed'
},
'file': {
'class': 'logging.FileHandler',
'formatter': 'simple',
'filename': LOGGER['LOG_FILE'],
},
'file-timed': {
'class': 'logging.FileHandler',
'formatter': 'timed',
'filename': LOGGER['LOG_FILE'],
},
'syslog': {
'class': 'logging.handlers.SysLogHandler',
'formatter': 'syslog',
}
},
'loggers': {
LOGGER['LOG_NAME']: {
'handlers': LOGGER['LOG_HANDLERS'].split(','),
'level': LOGGER['LOG_LEVEL'],
'propagate': True,
}
}
})
logger = logging.getLogger(LOGGER['LOG_NAME'])
#************************************************ #************************************************
# HERE ARE THE IMPORTANT PARTS # HERE ARE THE IMPORTANT PARTS
@ -186,9 +133,11 @@ if __name__ == '__main__':
logger.info('HBlink \'HBlink.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') logger.info('HBlink \'HBlink.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...')
# HBlink Master # HBlink Master
if MASTER: masters = {}
hbmaster = HBMASTER() for master in MASTERS:
reactor.listenUDP(MASTER['PORT'], hbmaster, interface=MASTER['IP']) if MASTERS[master]['ENABLED']:
masters[master] = HBMASTER(master)
reactor.listenUDP(MASTERS[master]['PORT'], masters[master], interface=MASTERS[master]['IP'])
clients = {} clients = {}
for client in CLIENTS: for client in CLIENTS:

60
log.py Normal file
View File

@ -0,0 +1,60 @@
import logging
from logging.config import dictConfig
def config_logging(_logger):
dictConfig({
'version': 1,
'disable_existing_loggers': False,
'filters': {
},
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'timed': {
'format': '%(levelname)s %(asctime)s %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
'syslog': {
'format': '%(name)s (%(process)d): %(levelname)s %(message)s'
}
},
'handlers': {
'null': {
'class': 'logging.NullHandler'
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'console-timed': {
'class': 'logging.StreamHandler',
'formatter': 'timed'
},
'file': {
'class': 'logging.FileHandler',
'formatter': 'simple',
'filename': _logger['LOG_FILE'],
},
'file-timed': {
'class': 'logging.FileHandler',
'formatter': 'timed',
'filename': _logger['LOG_FILE'],
},
'syslog': {
'class': 'logging.handlers.SysLogHandler',
'formatter': 'syslog',
}
},
'loggers': {
_logger['LOG_NAME']: {
'handlers': _logger['LOG_HANDLERS'].split(','),
'level': _logger['LOG_LEVEL'],
'propagate': True,
}
}
})
return logging.getLogger(_logger['LOG_NAME'])