mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-04-05 06:15:31 +00:00
some refactorings
This commit is contained in:
parent
45062bd888
commit
4f389723c0
9 changed files with 67 additions and 85 deletions
54
boswatch/utils/misc.py
Normal file
54
boswatch/utils/misc.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: misc.py
|
||||
@date: 11.03.2019
|
||||
@author: Bastian Schroll
|
||||
@description: Some misc functions
|
||||
"""
|
||||
import logging
|
||||
from boswatch import version
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
def addClientDataToPacket(bwPacket, config):
|
||||
"""!Add the client information to the decoded data
|
||||
|
||||
This function adds the following data to the bwPacket:
|
||||
- clientName
|
||||
- clientVersion
|
||||
- clientBuildDate
|
||||
- clientBranch
|
||||
- inputSource
|
||||
- frequency"""
|
||||
logging.debug("add client data to bwPacket")
|
||||
bwPacket.set("clientName", config.get("client", "name"))
|
||||
bwPacket.set("clientVersion", version.client)
|
||||
bwPacket.set("clientBuildDate", version.date)
|
||||
bwPacket.set("clientBranch", version.branch)
|
||||
bwPacket.set("inputSource", config.get("client", "inoutSource"))
|
||||
bwPacket.set("frequency", config.get("inputSource", "sdr", "frequency"))
|
||||
|
||||
|
||||
def addServerDataToPacket(bwPacket, config):
|
||||
"""!Add the server information to the decoded data
|
||||
|
||||
This function adds the following data to the bwPacket:
|
||||
- serverName
|
||||
- serverVersion
|
||||
- serverBuildDate
|
||||
- serverBranch"""
|
||||
logging.debug("add server data to bwPacket")
|
||||
bwPacket.set("serverName", config.get("server", "name"))
|
||||
bwPacket.set("serverVersion", version.server)
|
||||
bwPacket.set("serverBuildDate", version.date)
|
||||
bwPacket.set("serverBranch", version.branch)
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: timer.py
|
||||
@date: 21.09.2018
|
||||
@author: Bastian Schroll
|
||||
@description: Timer class for interval timed events
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
from threading import Thread, Event
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
|
||||
class RepeatedTimer:
|
||||
|
||||
def __init__(self, interval, targetFunction, *args, **kwargs):
|
||||
"""!Create a new instance of the RepeatedTimer
|
||||
|
||||
@param interval: interval in sec. to recall target function
|
||||
@param targetFunction: function to call on timer event
|
||||
@param *args: arguments for the called function
|
||||
@param *kwargs: keyword arguments for the called function
|
||||
"""
|
||||
self._interval = interval
|
||||
self._function = targetFunction
|
||||
self._args = args
|
||||
self._kwargs = kwargs
|
||||
self._start = 0
|
||||
self._overdueCount = 0
|
||||
self._lostEvents = 0
|
||||
self._isRunning = False
|
||||
self._event = Event()
|
||||
self._thread = None
|
||||
|
||||
def start(self):
|
||||
"""!Start a new timer worker thread
|
||||
|
||||
@return True or False"""
|
||||
if self._thread is None:
|
||||
self._event.clear()
|
||||
self._thread = Thread(target=self._target)
|
||||
self._thread.name = "RepTim(" + str(self._interval) + ")"
|
||||
self._thread.daemon = True # start as daemon (thread dies if main program ends)
|
||||
self._thread.start()
|
||||
logging.debug("start repeatedTimer: %s", self._thread.name)
|
||||
return True
|
||||
logging.debug("repeatedTimer always started")
|
||||
return True
|
||||
|
||||
def stop(self):
|
||||
"""!Stop the timer worker thread
|
||||
|
||||
@return True or False"""
|
||||
self._event.set()
|
||||
if self._thread is not None:
|
||||
logging.debug("stop repeatedTimer: %s", self._thread.name)
|
||||
self._thread.join()
|
||||
return True
|
||||
logging.warning("repeatedTimer always stopped")
|
||||
return True
|
||||
|
||||
def _target(self):
|
||||
"""!Runs the target function with his arguments in own thread"""
|
||||
self._start = time.time()
|
||||
while not self._event.wait(self.restTime):
|
||||
logging.debug("work")
|
||||
startTime = time.time()
|
||||
|
||||
try:
|
||||
self._function(*self._args, **self._kwargs)
|
||||
except: # pragma: no cover
|
||||
logging.exception("target throws an exception")
|
||||
|
||||
runTime = time.time() - startTime
|
||||
if runTime < self._interval:
|
||||
logging.debug("ready after: %0.3f sec. - next call in: %0.3f sec.", runTime, self.restTime)
|
||||
else:
|
||||
lostEvents = int(runTime / self._interval)
|
||||
logging.warning("timer overdue! interval: %0.3f sec. - runtime: %0.3f sec. - "
|
||||
"%d events lost - next call in: %0.3f sec.", self._interval, runTime, lostEvents, self.restTime)
|
||||
self._lostEvents += lostEvents
|
||||
self._overdueCount += 1
|
||||
logging.debug("repeatedTimer thread stopped: %s", self._thread.name)
|
||||
self._thread = None # set to none after leave teh thread (running recognize)
|
||||
|
||||
@property
|
||||
def isRunning(self):
|
||||
"""!Property for repeatedTimer running state"""
|
||||
if self._thread:
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def restTime(self):
|
||||
"""!Property to get remaining time till next call"""
|
||||
return self._interval - ((time.time() - self._start) % self._interval)
|
||||
|
||||
@property
|
||||
def overdueCount(self):
|
||||
"""!Property to get a count over all overdues"""
|
||||
return self._overdueCount
|
||||
|
||||
@property
|
||||
def lostEvents(self):
|
||||
"""!Property to get a count over all lost events"""
|
||||
return self._lostEvents
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""!
|
||||
____ ____ ______ __ __ __ _____
|
||||
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
||||
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
||||
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
||||
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
||||
German BOS Information Script
|
||||
by Bastian Schroll
|
||||
|
||||
@file: wildcard.py
|
||||
@date: 15.01.2018
|
||||
@author: Bastian Schroll
|
||||
@description: Little Helper to replace wildcards in stings
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
# from boswatch.module import file
|
||||
|
||||
logging.debug("- %s loaded", __name__)
|
||||
|
||||
# todo check function and document + write an test
|
||||
|
||||
|
||||
def replaceWildcards(message, bwPacket):
|
||||
_wildcards = {
|
||||
# formatting wildcards
|
||||
"{BR}": "\r\n",
|
||||
"{LPAR}": "(",
|
||||
"{RPAR}": ")",
|
||||
"{TIME}": time.time(),
|
||||
|
||||
# info wildcards
|
||||
# server
|
||||
"{SNAME}": bwPacket.getField("serverName"),
|
||||
"{SVERS}": bwPacket.getField("serverVersion"),
|
||||
"{SDATE}": bwPacket.getField("serverBuildDate"),
|
||||
"{SBRCH}": bwPacket.getField("serverBranch"),
|
||||
# client
|
||||
"{CNAME}": bwPacket.getField("clientName"),
|
||||
"{CIP}": bwPacket.getField("clientIP"),
|
||||
"{CVERS}": bwPacket.getField("clientVersion"),
|
||||
"{CDATE}": bwPacket.getField("clientBuildDate"),
|
||||
"{CBRCH}": bwPacket.getField("clientBranch"),
|
||||
|
||||
# boswatch wildcards
|
||||
"{INSRC}": bwPacket.getField("mode"),
|
||||
"{TIMES}": bwPacket.getField("mode"),
|
||||
"{FREQ}": bwPacket.getField("frequency"),
|
||||
"{MODE}": bwPacket.getField("mode"),
|
||||
"{DESCS}": bwPacket.getField("descriptionShort"),
|
||||
"{DESCL}": bwPacket.getField("descriptionLong"),
|
||||
|
||||
# fms wildcards
|
||||
"{FMS}": bwPacket.getField("fms"),
|
||||
"{SERV}": bwPacket.getField("service"),
|
||||
"{COUNT}": bwPacket.getField("country"),
|
||||
"{LOC}": bwPacket.getField("location"),
|
||||
"{VEHC}": bwPacket.getField("vehicle"),
|
||||
"{STAT}": bwPacket.getField("status"),
|
||||
"{DIR}": bwPacket.getField("direction"),
|
||||
"{DIRT}": bwPacket.getField("dirextionText"),
|
||||
"{TACI}": bwPacket.getField("tacticalInfo"),
|
||||
|
||||
# pocsag wildcards
|
||||
"{BIT}": bwPacket.getField("bitrate"),
|
||||
"{RIC}": bwPacket.getField("ric"),
|
||||
"{SRIC}": bwPacket.getField("subric"),
|
||||
"{SRICT}": bwPacket.getField("subricText"),
|
||||
"{MSG}": bwPacket.getField("message"),
|
||||
|
||||
# zvei wildcards
|
||||
"{TONE}": bwPacket.getField("tone"),
|
||||
|
||||
# message for MSG packet is done in poc
|
||||
}
|
||||
for wildcard in _wildcards:
|
||||
message = message.replace(wildcard, _wildcards.get(wildcard))
|
||||
|
||||
return message
|
||||
Loading…
Add table
Add a link
Reference in a new issue