2015-05-22 20:17:01 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: cp1252 -*-
|
|
|
|
|
|
2015-05-27 07:48:24 +02:00
|
|
|
"""
|
|
|
|
|
POCSAG Decoder
|
|
|
|
|
|
|
|
|
|
@author: Bastian Schroll
|
2015-05-31 12:40:43 +02:00
|
|
|
@author: Jens Herrmann
|
2015-05-27 07:48:24 +02:00
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
|
|
|
|
|
2015-05-22 20:17:01 +02:00
|
|
|
import logging
|
|
|
|
|
import time #timestamp for doublealarm
|
|
|
|
|
import re #Regex for validation
|
|
|
|
|
|
|
|
|
|
from includes import globals # Global variables
|
|
|
|
|
|
2015-05-31 12:40:43 +02:00
|
|
|
##
|
|
|
|
|
#
|
2015-05-24 21:24:26 +02:00
|
|
|
# Simple Filter
|
2015-05-31 12:40:43 +02:00
|
|
|
#
|
2015-05-24 21:24:26 +02:00
|
|
|
def isAllowed(poc_id):
|
2015-05-27 07:48:24 +02:00
|
|
|
"""
|
2015-05-27 11:55:04 +02:00
|
|
|
Simple Filter Functions (Allowed, Denied and Range)
|
2015-05-27 07:48:24 +02:00
|
|
|
|
|
|
|
|
@type poc_id: string
|
|
|
|
|
@param poc_id: POCSAG Ric
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
|
2015-05-27 11:55:04 +02:00
|
|
|
@return: True if the Ric is allowed, other False
|
2015-05-27 07:48:24 +02:00
|
|
|
@exception: none
|
|
|
|
|
"""
|
2015-05-24 21:24:26 +02:00
|
|
|
# 1.) If allowed RICs is set, only they will path,
|
|
|
|
|
# If RIC is the right one return True, else False
|
|
|
|
|
if globals.config.get("POC", "allow_ric"):
|
|
|
|
|
if poc_id in globals.config.get("POC", "allow_ric"):
|
|
|
|
|
logging.debug("RIC %s is allowed", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("RIC %s is not in the allowed list", poc_id)
|
|
|
|
|
return False
|
|
|
|
|
# 2.) If denied RIC, return False
|
|
|
|
|
elif poc_id in globals.config.get("POC", "deny_ric"):
|
|
|
|
|
logging.debug("RIC %s is denied by config.ini", poc_id)
|
|
|
|
|
return False
|
|
|
|
|
# 3.) Check Range, return False if outside def. range
|
|
|
|
|
elif int(poc_id) < globals.config.getint("POC", "filter_range_start"):
|
|
|
|
|
logging.debug("RIC %s out of filter range (start)", poc_id)
|
|
|
|
|
return False
|
|
|
|
|
elif int(poc_id) > globals.config.getint("POC", "filter_range_end"):
|
|
|
|
|
logging.debug("RIC %s out of filter range (end)", poc_id)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2015-05-31 12:40:43 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# POCSAG Decoder Function
|
|
|
|
|
# validate -> check double alarm -> log
|
|
|
|
|
#
|
2015-05-22 20:17:01 +02:00
|
|
|
def decode(freq, decoded):
|
2015-05-27 07:48:24 +02:00
|
|
|
"""
|
|
|
|
|
Export POCSAG Information from Multimon-NG RAW String and call alarmHandler.processAlarm()
|
|
|
|
|
|
|
|
|
|
@type freq: string
|
|
|
|
|
@param freq: frequency of the SDR Stick
|
|
|
|
|
@type decoded: string
|
|
|
|
|
@param decoded: RAW Information from Multimon-NG
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
|
|
|
|
|
@return: nothing
|
|
|
|
|
@exception: Exception if POCSAG decode failed
|
|
|
|
|
"""
|
2015-05-22 20:17:01 +02:00
|
|
|
bitrate = 0
|
|
|
|
|
timestamp = int(time.time())#Get Timestamp
|
|
|
|
|
|
|
|
|
|
if "POCSAG512:" in decoded:
|
|
|
|
|
bitrate = 512
|
2015-06-04 19:07:46 +02:00
|
|
|
poc_id = decoded[20:27].replace(" ", "").zfill(7)
|
2015-05-22 20:17:01 +02:00
|
|
|
poc_sub = decoded[39].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1")
|
|
|
|
|
|
|
|
|
|
elif "POCSAG1200:" in decoded:
|
|
|
|
|
bitrate = 1200
|
2015-06-04 19:07:46 +02:00
|
|
|
poc_id = decoded[21:28].replace(" ", "").zfill(7)
|
2015-05-22 20:17:01 +02:00
|
|
|
poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1")
|
|
|
|
|
|
|
|
|
|
elif "POCSAG2400:" in decoded:
|
|
|
|
|
bitrate = 2400
|
2015-06-04 19:07:46 +02:00
|
|
|
poc_id = decoded[21:28].replace(" ", "").zfill(7)
|
2015-05-22 20:17:01 +02:00
|
|
|
poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1")
|
|
|
|
|
|
|
|
|
|
if bitrate is 0:
|
|
|
|
|
logging.warning("POCSAG Bitrate not found")
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("POCSAG Bitrate: %s", bitrate)
|
|
|
|
|
|
|
|
|
|
if "Alpha:" in decoded: #check if there is a text message
|
|
|
|
|
poc_text = decoded.split('Alpha: ')[1].strip().rstrip('<EOT>').strip()
|
|
|
|
|
else:
|
|
|
|
|
poc_text = ""
|
|
|
|
|
|
|
|
|
|
if re.search("[0-9]{7}", poc_id): #if POC is valid
|
2015-05-24 21:24:26 +02:00
|
|
|
if isAllowed(poc_id):
|
|
|
|
|
#check for double alarm
|
|
|
|
|
if poc_id == globals.poc_id_old and timestamp < globals.poc_time_old + globals.config.getint("POC", "double_ignore_time"):
|
|
|
|
|
logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, globals.poc_id_old, timestamp-globals.poc_time_old)
|
|
|
|
|
#in case of double alarm, poc_double_ignore_time set new
|
|
|
|
|
globals.poc_time_old = timestamp
|
2015-05-22 20:17:01 +02:00
|
|
|
else:
|
2015-05-24 21:24:26 +02:00
|
|
|
logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text)
|
2015-06-05 21:49:51 +02:00
|
|
|
data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate, "description":poc_id}
|
2015-06-05 11:26:01 +02:00
|
|
|
# Add function as character a-d to dataset
|
|
|
|
|
data["functionChar"] = data["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d")
|
2015-06-05 21:49:51 +02:00
|
|
|
# If enabled, look up description
|
|
|
|
|
if globals.config.getint("POC", "idDescribed"):
|
|
|
|
|
from includes import descriptionList
|
|
|
|
|
data["description"] = descriptionList.getDescription("POC", poc_id)
|
|
|
|
|
# processing the alarm
|
2015-05-24 21:24:26 +02:00
|
|
|
from includes import alarmHandler
|
|
|
|
|
alarmHandler.processAlarm("POC",freq,data)
|
|
|
|
|
|
|
|
|
|
globals.poc_id_old = poc_id #save last id
|
|
|
|
|
globals.poc_time_old = timestamp #save last time
|
2015-05-22 20:17:01 +02:00
|
|
|
else:
|
2015-05-24 21:24:26 +02:00
|
|
|
logging.info("POCSAG%s: %s is not allowed", bitrate, poc_id)
|
2015-05-22 20:17:01 +02:00
|
|
|
else:
|
|
|
|
|
logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id)
|