2018-01-07 14:09:40 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
|
|
|
|
@file: pocsag.py
|
|
|
|
|
@date: 06.01.2018
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
@description: Decoder class for pocsag
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
from boswatch.packet import packet
|
|
|
|
|
|
|
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
2018-09-09 16:17:49 +02:00
|
|
|
class PocsagDecoder:
|
2018-01-07 14:09:40 +01:00
|
|
|
"""!POCSAG decoder class
|
|
|
|
|
|
|
|
|
|
This class decodes POCSAG data.
|
2018-02-03 22:32:28 +01:00
|
|
|
First step is to validate the data and _check if the format is correct.
|
2018-01-07 14:09:40 +01:00
|
|
|
In the last step a valid BOSWatch packet is created and returned"""
|
|
|
|
|
|
2018-01-08 07:56:10 +01:00
|
|
|
@staticmethod
|
2018-01-08 07:58:33 +01:00
|
|
|
def decode(data):
|
2018-01-07 14:09:40 +01:00
|
|
|
"""!Decodes POCSAG
|
|
|
|
|
|
|
|
|
|
@param data: POCSAG for decoding
|
|
|
|
|
@return BOSWatch POCSAG packet or None"""
|
2018-09-09 16:17:49 +02:00
|
|
|
bitrate, ric, subric = PocsagDecoder._getBitrateRicSubric(data)
|
2018-01-07 14:09:40 +01:00
|
|
|
|
2018-01-08 08:09:23 +01:00
|
|
|
if re.search("[0-9]{7}", ric) and re.search("[1-4]", subric):
|
2018-01-07 14:09:40 +01:00
|
|
|
if "Alpha:" in data:
|
|
|
|
|
message = data.split('Alpha: ')[1].strip()
|
2018-01-08 08:09:23 +01:00
|
|
|
message = message.replace('<NUL>', '').replace('<NUL', '').replace('< NUL>', '').replace('<EOT>', '').strip()
|
2018-01-07 14:09:40 +01:00
|
|
|
else:
|
|
|
|
|
message = ""
|
|
|
|
|
subricText = subric.replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d")
|
|
|
|
|
|
|
|
|
|
logging.debug("found valid POCSAG")
|
|
|
|
|
|
|
|
|
|
bwPacket = packet.Packet()
|
2018-01-09 11:33:23 +01:00
|
|
|
bwPacket.set("mode", "pocsag")
|
|
|
|
|
bwPacket.set("bitrate", bitrate)
|
|
|
|
|
bwPacket.set("ric", ric)
|
|
|
|
|
bwPacket.set("subric", subric)
|
|
|
|
|
bwPacket.set("subricText", subricText)
|
|
|
|
|
bwPacket.set("message", message)
|
2018-01-07 14:09:40 +01:00
|
|
|
|
|
|
|
|
logging.debug(bwPacket)
|
|
|
|
|
return bwPacket
|
|
|
|
|
|
|
|
|
|
logging.warning("no valid data")
|
|
|
|
|
return None
|
2018-01-08 07:56:10 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _getBitrateRicSubric(data):
|
|
|
|
|
"""!Gets the Bitrate, Ric and Subric from data
|
|
|
|
|
|
|
|
|
|
@param data: POCSAG data string
|
|
|
|
|
@return bitrate
|
|
|
|
|
@return ric
|
|
|
|
|
@return subric"""
|
|
|
|
|
bitrate, ric, subric = 0, 0, 0
|
|
|
|
|
|
|
|
|
|
if "POCSAG512:" in data:
|
|
|
|
|
bitrate = 512
|
|
|
|
|
ric = data[20:27].replace(" ", "").zfill(7)
|
|
|
|
|
subric = str(int(data[39]) + 1)
|
|
|
|
|
|
|
|
|
|
elif "POCSAG1200:" in data:
|
|
|
|
|
bitrate = 1200
|
|
|
|
|
ric = data[21:28].replace(" ", "").zfill(7)
|
|
|
|
|
subric = str(int(data[40]) + 1)
|
|
|
|
|
|
|
|
|
|
elif "POCSAG2400:" in data:
|
|
|
|
|
bitrate = 2400
|
|
|
|
|
ric = data[21:28].replace(" ", "").zfill(7)
|
|
|
|
|
subric = str(int(data[40]) + 1)
|
|
|
|
|
|
|
|
|
|
return bitrate, ric, subric
|