From 1a293381e783f46d815de5cfff4633dc5d94b0b7 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 3 Apr 2015 15:55:10 +0200 Subject: [PATCH] Initial Commit --- boswatch.py | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++ config.ini | 22 +++++ raspoc.sql | 72 +++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 boswatch.py create mode 100644 config.ini create mode 100644 raspoc.sql diff --git a/boswatch.py b/boswatch.py new file mode 100644 index 0000000..1cf65e8 --- /dev/null +++ b/boswatch.py @@ -0,0 +1,229 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +##### Info ##### +# BOSWatch +# Python Script to Recive and Decode BOS Information with rtl_fm ans multimon-NG +##### Info ##### + +import time +#import sys +import subprocess +#import os +import mysql +import mysql.connector + +import argparse #for parse the args +import ConfigParser #for parse the config file +import re #Regex + + +def curtime(format="%Y-%m-%d %H:%M:%S"): + return time.strftime(format) + +def stop_script(err): + print "ERR: "+err + try: + if args.verbose: print "disconnect MySQL" + connection.close() + rtl_fm.terminate() + if args.verbose: print "rtl_fm terminated" + multimon_ng.terminate() + if args.verbose: print "multimon-ng terminated" + if args.verbose: print "exiting BOSWatch" + except: + pass + + +#With -h or --help you get the Args help +#ArgsParser +parser = argparse.ArgumentParser(prog="boswatch.py", description="BOSWatch is a Python Script to Recive and Decode BOS Information with rtl_fm ans multimon-NG", epilog="More Options you can find in the extern config.ini File in this Folder") +#parser.add_argument("-c", "--channel", help="BOS Channel you want to listen") +parser.add_argument("-f", "--freq", help="Frequency you want to listen", required=True) +parser.add_argument("-d", "--device", help="Device you want to use (Check with rtl_test)", type=int, default=0) +parser.add_argument("-e", "--error", help="Frequency-Error of your Device in PPM", type=int, default=0) +parser.add_argument("-a", "--demod", help="Demodulation Functions", choices=['FMS', 'ZVEI', 'POC512', 'POC1200', 'POC2400'], required=True, nargs="+") +parser.add_argument("-s", "--squelch", help="Level of Squelch", type=int, default=0) +parser.add_argument("-v", "--verbose", help="Shows more Information", action="store_true") +args = parser.parse_args() + + +#Read Data from Args, Put it into working Variables and Display them +print("###########################") +print("# #") +print("# BOSWatch beta #") +print("# #") +print("###########################") +print("") + +freq = args.freq +print "Frequency: "+freq + +#channel = args.channel +#print("Frequency: ",channel) + +device = args.device +print "Device-ID: "+str(device) + +error = args.error +print "Error in PPM: "+str(error) + +demodulation = "" +print "Active Demods: "+str(len(args.demod)) +if "FMS" in args.demod: + demodulation += "-a FMSFSK " + print "- FMS" +if "ZVEI" in args.demod: + demodulation += "-a ZVEI2 " + print "- ZVEI" +if "POC512" in args.demod: + demodulation += "-a POCSAG512 " + print "- POC512" +if "POC1200" in args.demod: + demodulation += "-a POCSAG1200 " + print "- POC1200" +if "POC2400" in args.demod: + demodulation += "-a POCSAG2400 " + print "- POC2400" + +squelch = args.squelch +print "Squelch: "+str(squelch) + +if args.verbose: + print("Verbose Mode!") + +print "" + +try: + #ConfigParser + if args.verbose: print "reading config file" + try: + config = ConfigParser.ConfigParser() + config.read("./config.ini") + fms_double_ignore_time = int(config.get("FMS", "double_ignore_time")) + zvei_double_ignore_time = int(config.get("ZVEI", "double_ignore_time")) + except: + stop_script("config reading error") + exit(0) + + + dbserver = config.get("MySQL", "dbserver") + dbuser = config.get("MySQL", "dbuser") + dbpassword = config.get("MySQL", "dbpassword") + database = config.get("MySQL", "database") + + tableFMS = config.get("MySQL", "tableFMS") + tableZVEI = config.get("MySQL", "tableZVEI") + tablePOC = config.get("MySQL", "tablePOC") + + if args.verbose: print "connect to MySQL database" + try: + connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) + except: + print "MySQL connect error" + exit(0) + + if args.verbose: print "pre-load variables" + fms_id = 0 + fms_id_old = 0 + fms_time_old = 0 + + zvei_id = 0 + zvei_id_old = 0 + zvei_time_old = 0 + + + if args.verbose: print "starting rtl_fm" + try: + rtl_fm = subprocess.Popen("rtl_fm -d "+str(device)+" -f "+str(freq)+" -M fm -s 22050 -p "+str(error)+" -E DC -F 0 -l "+str(squelch)+" -g 100", + #stdin=rtl_fm.stdout, + stdout=subprocess.PIPE, + stderr=open('log.txt','a'), + shell=True) + except: + stop_script("cannot start rtl_fm") + exit(0) + + #multimon_ng = subprocess.Popen("aplay -r 22050 -f S16_LE -t raw", + if args.verbose: print "starting multimon-ng" + try: + multimon_ng = subprocess.Popen("multimon-ng "+str(demodulation)+" -f alpha -t raw /dev/stdin - ", + stdin=rtl_fm.stdout, + stdout=subprocess.PIPE, + stderr=open('log.txt','a'), + shell=True) + except: + stop_script("cannot start multimon-ng") + exit(0) + + + if args.verbose: print "start decoding" + while True: + #RAW Data from Multimon-NG + #ZVEI2: 25832 + #FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST2=III(mit NA,ohneSIGNAL)) CRC correct\n' + decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout + + if True: #if input data avalable + + timestamp = int(time.time())#Get Timestamp + #if args.verbose: print "RAW: "+decoded #for verbose mode, print Raw input data + + #FMS Decoder Section + if "FMS:" in decoded: + if args.verbose: print "recived FMS" + + fms_service = decoded[19] #Organisation + fms_country = decoded[36] #Bundesland + fms_location = decoded[65:67] #Ort + fms_vehicle = decoded[72:76] #Fahrzeug + fms_status = decoded[84] #Status + fms_direction = decoded[101] #Richtung + fms_tsi = decoded[114:117] #Taktische Kruzinformation + + if "CRC correct" in decoded: #check CRC is correct + fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id + if fms_id == fms_id_old and timestamp < fms_time_old + fms_double_ignore_time: #check for double alarm + if args.verbose: print "FMS double alarm: "+fms_id_old + fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new + else: + print curtime("%H:%M:%S")+" BOS:"+fms_service+" Bundesland:"+fms_country+" Ort:"+fms_location+" Fahrzeug:"+fms_vehicle+" Status:"+fms_status+" Richtung:"+fms_direction+" TKI:"+fms_tsi + fms_id_old = fms_id #save last id + fms_time_old = timestamp #save last time + + cursor = connection.cursor() + cursor.execute("INSERT INTO "+tableFMS+" (time,service,country,location,vehicle,status,direction,tsi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",(curtime(),fms_service,fms_country,fms_location,fms_vehicle,fms_status,fms_direction,fms_tsi)) + cursor.close() + connection.commit() + + elif args.verbose: #crc error only in verbose mode + print "CRC error" + + + #ZVEI Decoder Section + if "ZVEI2:" in decoded: + if args.verbose: print "recived ZVEI" + + #ZVEI RegEX Pattern: http://www.regexr.com/3ao2u + zvei = re.search("[0-9F]{5}", decoded) + if zvei: #if ZVEI is valid + zvei_id = zvei.group() #save ZVEI in working var + if zvei_id == zvei_id_old and timestamp < zvei_time_old + zvei_double_ignore_time: #check for double alarm + if args.verbose: print "ZVEI double alarm: "+zvei_id_old + zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new + else: + print curtime("%H:%M:%S")+" 5-Ton: "+zvei_id + zvei_id_old = zvei_id #save last id + zvei_time_old = timestamp #save last time + + cursor = connection.cursor() + cursor.execute("INSERT INTO "+tableZVEI+" (time,zvei) VALUES (%s,%s)",(curtime(),zvei_id)) + cursor.close() + connection.commit() + + elif args.verbose: #Invalid error only in verbose mode + print "No valid ZVEI: "+decoded + + +except KeyboardInterrupt: + stop_script("Keyboard Interrupt") \ No newline at end of file diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..ee2f144 --- /dev/null +++ b/config.ini @@ -0,0 +1,22 @@ +######################## +# BOSWatch Config File # +######################## + +#Data for MySQL connection +[MySQL] +dbserver = localhost +dbuser = root +dbpassword = root +database = raspoc + +tableFMS = ras_fms_hist +tableZVEI = ras_zvei_hist +tablePOC = ras_pocsag_hist + +[FMS] +#time to ignore same alarm in a row in seconds +double_ignore_time = 5 + +[ZVEI] +#time to ignore same alarm in a row in seconds +double_ignore_time = 10 \ No newline at end of file diff --git a/raspoc.sql b/raspoc.sql new file mode 100644 index 0000000..a2d848d --- /dev/null +++ b/raspoc.sql @@ -0,0 +1,72 @@ +-- phpMyAdmin SQL Dump +-- version 3.4.11.1deb2+deb7u1 +-- http://www.phpmyadmin.net +-- +-- Host: localhost +-- Erstellungszeit: 03. Apr 2015 um 11:16 +-- Server Version: 5.5.41 +-- PHP-Version: 5.4.39-0+deb7u1 + +SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; + +-- +-- Datenbank: `raspoc` +-- + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `ras_fms_hist` +-- + +CREATE TABLE IF NOT EXISTS `ras_fms_hist` ( + `ID` int(11) NOT NULL AUTO_INCREMENT, + `time` datetime NOT NULL, + `service` varchar(1) NOT NULL, + `country` varchar(2) NOT NULL, + `location` varchar(2) NOT NULL, + `vehicle` varchar(4) NOT NULL, + `status` varchar(1) NOT NULL, + `direction` varchar(1) NOT NULL, + `tsi` varchar(3) NOT NULL, + PRIMARY KEY (`ID`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `ras_pocsag_hist` +-- + +CREATE TABLE IF NOT EXISTS `ras_pocsag_hist` ( + `ID` int(11) NOT NULL AUTO_INCREMENT, + `time` datetime NOT NULL, + `ric` varchar(7) NOT NULL DEFAULT '0', + `funktion` int(1) NOT NULL, + `text` text NOT NULL, + KEY `ID` (`ID`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `ras_zvei_hist` +-- + +CREATE TABLE IF NOT EXISTS `ras_zvei_hist` ( + `ID` int(11) NOT NULL AUTO_INCREMENT, + `time` datetime NOT NULL, + `zvei` varchar(5) NOT NULL DEFAULT '0', + PRIMARY KEY (`ID`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;