Skip to content

Commit

Permalink
JSON config support, improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cceremuga committed Apr 18, 2017
1 parent 166c408 commit 84e2c72
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 35 deletions.
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
___ ___ _ _
| _ \_ _| _ \__ _ __| |_____| |_
| _/ || | _/ _` / _| / / -_) _|
|_| \_, |_| \__,_\__|_\_\___|\__|
|__/
![PyPacket](https://i.imgur.com/MZYHAFG.png "PyPacket")

A simple CLI logger to receive and decode APRS packets via rtl_fm ([RTL-SDR](http://osmocom.org/projects/sdr/wiki/rtl-sdr)) and [multimon-ng](https://github.com/EliasOenal/multimon-ng). This project serves as an open source expirimental tool for research into the RF spectrum and APRS.

Expand All @@ -20,35 +16,37 @@ Requires the following to be installed and configured on your system in order to
* [pip](https://pypi.python.org/pypi/pip)
* [pytest](https://docs.pytest.org/en/latest/) (if you want to run tests)

Note, there are significant pieces of this which are hard coded and have no configurability options. See the Future Plans section for more info.
## Configuration

The `config/configuration.json` file contains all of the current configuration options including frequency, gain, etc. More options will be added as needed.

## Running

From the directory you've cloned the repository to, simply execute `python main.py`. The application will start and immediately begin listening on 144.39Mhz. Logged packets will be output to your terminal.
From the directory you've cloned the repository to, simply execute `python main.py`. The application will start and immediately begin listening on the configured frequency.

Logged packets will be output to your terminal and written to a file in the `logs` directory.

## Patch Notes
## Recent Patch Notes

* 4/18/17
* Basic JSON-based configuration support.
* Improved logging.
* Resolved bug when logs directory did not exist.
* 4/17/17
* Logging runtime activities to file in the logs subdirectory.
* 4/2/17
* Start of unit tests.
* Travis CI integration.
* 4/1/17
* Console colors are now constants.
* 3/27/17
* Restructured files to use proper Python module organization.
* Basic framework for logging.
* Resolved an exception involving unparseable decoded data.

## Future Plans

* JSON configuration options for frequency, gain, in progress.
* APRS frame deserialization for human readability, on deck.
* Quality code coverage, it's happening.
* Better documentation, definitely.
* Performance optimization, probably.
* Simple TCP server (for use in Xastir etc.), likely.
* Custom IGate uploading, maybe.

## Current / Future Plans

* JSON configuration options for frequency, gain [in progress].
* APRS frame deserialization for human readability [future].
* Quality code coverage [future].
* Better documentation [future].
* Performance optimization [future].
* Simple TCP server (for use in Xastir etc.) [future].
* Custom IGate uploading [future].

## Contributing

Expand All @@ -59,3 +57,4 @@ You are welcome to contribute by submitting pull requests on GitHub as you see f
Thanks to the following projects / libraries for open source code / inspiration.

* [pimultimonaprs](https://github.com/asdil12/pymultimonaprs)
* [The Noun Project](https://thenounproject.com/search/?q=radio%20tower&i=749293)
6 changes: 6 additions & 0 deletions config/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"radio": {
"frequency": "144390000",
"gain": "49.6"
}
}
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python

import time
from time import localtime, strftime
import logging
from pypacket.util.logger import Logger
from pypacket.base.listener import Listener
from pypacket.base.configuration import Configuration
from pypacket.util.colors import Colors

print(Colors.GREEN + """
Expand All @@ -15,13 +15,13 @@
""" + Colors.RESET)

# Configure logging.
logFormat = '[%(asctime)-15s] [%(levelname)s] %(message)s'
logging.basicConfig(filename='logs/pypacket_' + \
strftime("%Y_%m_%d_%H_%M_%S", localtime()) + '.log', \
format=logFormat, level=logging.INFO)
log_handler = Logger()

# Initialize configuration.
runtime_configuration = Configuration()

# The main runner.
pypacket_runtime = Listener()
pypacket_runtime = Listener(log_handler, runtime_configuration)

# Handles Control+c interrupts, existing the main loop and threads.
try:
Expand Down
23 changes: 23 additions & 0 deletions pypacket/base/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json

class Configuration:
CONFIG_FILE_NAME = 'config/configuration.json'

def __init__(self):
self.data = {}
self.load()
self.validate()

def load(self):
with open(self.CONFIG_FILE_NAME) as json_data_file:
self.data = json.load(json_data_file)

def frequency(self):
return self.data['radio']['frequency']

def gain(self):
return self.data['radio']['gain']

def validate(self):
pass
# TODO: Validate configuration.
9 changes: 5 additions & 4 deletions pypacket/base/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from pypacket.util.logger import Logger

class Listener:
def __init__(self):
self.log_handler = Logger()
def __init__(self, log_handler, config):
self.config = config
self.log_handler = log_handler
self.sub_processes = {}
self.start()
self.is_running = True
Expand All @@ -19,8 +20,8 @@ def start(self):
self.log_handler.log_info('Starting rtl_fm subprocess.')

rtl_subprocess = subprocess.Popen(
['rtl_fm', '-f', '144390000', '-s', '22050', '-o', '4',
'-g', '49.6', '-'],
['rtl_fm', '-f', self.config.frequency(), '-s', '22050', '-o', '4',
'-g', self.config.gain(), '-'],
stdout=subprocess.PIPE, stderr=open('/dev/null')
)

Expand Down
16 changes: 16 additions & 0 deletions pypacket/util/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import logging
import os
from time import localtime, strftime
from pypacket.util.colors import Colors

class Logger:
SYS_PREFIX = '[SYS] '
ERR_PREFIX = '[ERR] '
WRN_PREFIX = '[WRN] '
REC_PREFIX = '[REC] '
LOG_DIRECTORY = 'logs'

def __init__(self):
self.setup()

def log_info(self, logMessage):
self.log_any(Colors.BLUE, self.SYS_PREFIX, logMessage)
Expand All @@ -25,3 +31,13 @@ def log_packet(self, logMessage):

def log_any(self, color, prefix, logMessage):
print(color + prefix + Colors.RESET + logMessage)

def setup(self):
if not os.path.exists(self.LOG_DIRECTORY):
os.makedirs(self.LOG_DIRECTORY)

log_format = '[%(asctime)-15s] [%(levelname)s] %(message)s'
log_file_name = self.LOG_DIRECTORY + '/pypacket_' + \
strftime("%Y_%m_%d_%H_%M_%S", localtime()) + '.log'
logging.basicConfig(filename=log_file_name, format=log_format, \
level=logging.INFO)

0 comments on commit 84e2c72

Please sign in to comment.