def findAlarm(self, name, useDefault=False): definedAlarms = self.param.get('Alarm') rt = None if definedAlarms is not None: for cmd in definedAlarms: if cmd.get('name') is not None and cmd.get('name') == name: param = cmd.get('parameter') if param == "": param = None if param is not None: param = AVNUtil.replaceParam( param, AVNConfig.filterBaseParam(self.getParam())) rt = { 'command': cmd.get('command'), 'autoclean': self.getBoolean(cmd, 'autoclean'), 'repeat': self.getInt(cmd, 'repeat'), 'parameter': param } break if rt is None and useDefault: rt = { 'command': self.getStringParam('defaultCommand'), 'parameter': self.getStringParam('defaultParameter'), 'autoclean': True, 'repeat': 1 } return rt
def findAlarm(self,name,useDefault=False): definedAlarms=self.param.get('Alarm') rt=None if definedAlarms is not None: for cmd in definedAlarms: if cmd.get('name') is not None and cmd.get('name') == name: param=cmd.get('parameter') if param=="": param=None if param is not None: param=AVNUtil.replaceParam(param,AVNConfig.filterBaseParam(self.getParam())) rt= { 'command':cmd.get('command'), 'autoclean':self.getBoolean(cmd,'autoclean'), 'repeat':self.getInt(cmd,'repeat'), 'parameter':param } break if rt is None and useDefault: rt={ 'command':self.getStringParam('defaultCommand'), 'parameter':self.getStringParam('defaultParameter'), 'autoclean':True, 'repeat':1} return rt
def findCommand(self,name): ''' find a command by its name :param name: :return: ''' for cmd in self.getConfiguredCommands(): if cmd.get('name') is not None and cmd.get('name') == name: return {'command':AVNUtil.replaceParam(cmd.get('command'),AVNConfig.filterBaseParam(self.getParam())),'repeat':cmd.get('repeat'),'name':cmd.get('name')}
def allowDenyWatcher(self): """ checks for the current active LAN to have id_str="wlan-internal" and open the firewall in this case using the allowDenyCommand @return: """ statusName="FwHandler" cmd=self.getStringParam(self.P_FWCOMMAND) if cmd is None or cmd == "": self.setInfo(statusName, "no command", AVNWorker.Status.INACTIVE) return cmdparam=cmd.split(" ") command=[] for par in cmdparam: command.append(AVNUtil.replaceParam(par, AVNConfig.filterBaseParam(self.getParam()))) self.setInfo(statusName,"running",AVNWorker.Status.NMEA) lastNet=None lastMode=None lastResult=-1 lastSuccess=AVNUtil.utcnow() while True: try: status=self.getStatus() if status.get('wpa_state') is not None and status.get('wpa_state') == 'COMPLETED': ssid=status.get('ssid') mode="deny" if status.get('id_str') is not None and status.get('id_str') == self.PRIVATE_NAME: mode="allow" waittime=0 if lastMode == mode and lastNet == ssid: if lastResult != 0: waittime=self.COMMAND_REPEAT else: waittime=self.COMMAND_REPEAT_OK if (AVNUtil.utcnow() - lastSuccess) >= waittime: lastNet=ssid lastMode=mode AVNLog.info("running command %s %s",command,mode) lastResult=AVNUtil.runCommand(command+[mode],statusName+"-command") if lastResult != 0: if lastResult is None: lastResult=-1 AVNLog.error("%s: unable to run firewall command on %s for mode %s, return %d"%(statusName,ssid,mode,lastResult)) self.setInfo(statusName,"unable to run firewall command on %s for %s, return %d"%(ssid,mode,lastResult),AVNWorker.Status.ERROR) else: self.setInfo(statusName, "firewall command on %s for %s ok" % (ssid,mode), AVNWorker.Status.NMEA) self.lastFwInfo=FwInfo(ssid,mode,lastResult) except: AVNLog.error("%s: exception %s"%(statusName,traceback.format_exc())) time.sleep(5)
def __init__(self,param): AVNWorker.__init__(self, param) self.track=[] #param checks throw=True self.getIntParam('cleanup', throw) self.getFloatParam('mindistance', throw) self.getFloatParam('interval', throw) self.tracklock=threading.Lock() trackdir=self.getStringParam("trackdir") if trackdir == "": trackdir=unicode(os.path.join(self.getStringParam(AVNConfig.BASEPARAM.DATADIR),'tracks')) else: trackdir=AVNUtil.replaceParam(os.path.expanduser(trackdir),AVNConfig.filterBaseParam(param)) self.trackdir=trackdir self.fname=None
def __init__(self,param): AVNWorker.__init__(self, param) self.track=[] #param checks throw=True self.getIntParam('cleanup', throw) self.getFloatParam('mindistance', throw) self.getFloatParam('interval', throw) self.tracklock=threading.Lock() trackdir=self.getStringParam("trackdir") if trackdir == "": trackdir=unicode(os.path.join(self.getStringParam(AVNConfig.BASEPARAM.DATADIR),'tracks')) else: trackdir=AVNUtil.replaceParam(os.path.expanduser(trackdir),AVNConfig.filterBaseParam(param)) self.trackdir=trackdir self.fname=None
def __init__(self,param): AVNWorker.__init__(self, param) self.importDir=None self.lastTimeStamps={} #a dictionary of timestamps - key is the directory/filename, value the last read timestamp self.candidateTimes={} #dictionary with candidates for conversion - same layout as lastTimeStamps self.runningConversions={} #dictionary of current running conversions (key is the filename/dirname) self.waittime=self.getIntParam('waittime',True) self.chartbase=None self.extensions=self.getStringParam('knownExtensions').split(',') self.importDir=AVNUtil.prependBase(AVNUtil.replaceParam(self.getStringParam('importDir'),AVNConfig.filterBaseParam(self.getParam())),self.getStringParam(AVNConfig.BASEPARAM.DATADIR)) self.workDir=AVNUtil.prependBase(AVNUtil.replaceParam(self.getStringParam('workDir'),AVNConfig.filterBaseParam(self.getParam())),self.getStringParam(AVNConfig.BASEPARAM.DATADIR)) self.converterDir=self.getStringParam('converterDir') # the location of the coneverter python if self.converterDir is None or self.converterDir=='': self.converterDir=os.path.join(os.path.dirname(os.path.realpath(__file__)),"../..","chartconvert")
def allowDenyWatcher(self): """ checks for the current active LAN to have id_str="wlan-internal" and open the firewall in this case using the allowDenyCommand @return: """ statusName = "FwHandler" cmd = self.getStringParam(self.P_FWCOMMAND) if cmd is None or cmd == "": self.setInfo(statusName, "no command", AVNWorker.Status.INACTIVE) return cmdparam = cmd.split(" ") command = [] for par in cmdparam: command.append( AVNUtil.replaceParam( par, AVNConfig.filterBaseParam(self.getParam()))) self.setInfo(statusName, "running", AVNWorker.Status.NMEA) lastNet = None lastMode = None lastResult = -1 lastSuccess = AVNUtil.utcnow() while True: try: status = self.getStatus() if status.get('wpa_state') is not None and status.get( 'wpa_state') == 'COMPLETED': ssid = status.get('ssid') mode = "deny" if status.get('id_str') is not None and status.get( 'id_str') == self.PRIVATE_NAME: mode = "allow" waittime = 0 if lastMode == mode and lastNet == ssid: if lastResult != 0: waittime = self.COMMAND_REPEAT else: waittime = self.COMMAND_REPEAT_OK if (AVNUtil.utcnow() - lastSuccess) >= waittime: lastNet = ssid lastMode = mode AVNLog.info("running command %s %s", command, mode) lastResult = AVNUtil.runCommand( command + [mode], statusName + "-command") if lastResult != 0: if lastResult is None: lastResult = -1 AVNLog.error( "%s: unable to run firewall command on %s for mode %s, return %d" % (statusName, ssid, mode, lastResult)) self.setInfo( statusName, "unable to run firewall command on %s for %s, return %d" % (ssid, mode, lastResult), AVNWorker.Status.ERROR) else: self.setInfo( statusName, "firewall command on %s for %s ok" % (ssid, mode), AVNWorker.Status.NMEA) lastSuccess = AVNUtil.utcnow() self.lastFwInfo = FwInfo(ssid, mode, lastResult) except: AVNLog.error("%s: exception %s" % (statusName, traceback.format_exc())) time.sleep(5)
def run(self): self.setName("[%s]%s" % (AVNLog.getThreadId(), self.getName())) interval = self.getIntParam('interval') routesdir = AVNUtil.replaceParam( os.path.expanduser(self.getStringParam("routesdir")), AVNConfig.filterBaseParam(self.getParam())) if routesdir == "": routesdir = os.path.join( self.getStringParam(AVNConfig.BASEPARAM.DATADIR), u'routes') self.routesdir = routesdir if not os.path.isdir(self.routesdir): AVNLog.info("creating routes directory %s" % (self.routesdir)) os.makedirs(self.routesdir, 0755) self.fillRouteInfos() self.currentLegFileName = os.path.join(self.routesdir, self.currentLegName) if os.path.exists(self.currentLegFileName): f = None try: f = open(self.currentLegFileName, "r") strleg = f.read(self.MAXROUTESIZE + 1000) self.currentLeg = self.parseLeg(strleg) if self.currentLeg.toWP is not None: distance = geo.length( [self.currentLeg.fromWP, self.currentLeg.toWP]) AVNLog.info( "read current leg, route=%s, from=%s, to=%s, length=%fNM" % (self.currentLeg.name, unicode(self.currentLeg.fromWP), unicode(self.currentLeg.toWP), distance / AVNUtil.NM)) else: AVNLog.info("read current leg, route=%s, from=%s, to=%s" % ( self.currentLeg.name, unicode(self.currentLeg.fromWP), "NONE", )) if self.currentLeg.name is not None: self.activeRouteName = self.currentLeg.name if self.currentLeg.currentRoute is not None: #this will also set the active route self.saveRoute(self.currentLeg.currentRoute) if self.currentLeg.name != self.currentLeg.currentRoute.name: AVNLog.error( "leg inconsistent, name in route %s different from name in leg %s, correcting to route name", self.currentLeg.name, self.currentLeg.currentRoute.name) self.currentLeg.name = self.currentLeg.currentRoute.name self.activeRouteName = self.currentLeg.name #write back the corrected leg self.setCurrentLeg(self.currentLeg) else: if self.currentLeg.name is not None: self.activeRoute = self.loadRoute(self.currentLeg.name) if self.activeRoute is None: self.activeRoute = gpx.GPXRoute( self.currentLeg.name) self.currentLeg.currentRoute = self.activeRoute except: AVNLog.error("error parsing current leg %s: %s" % (self.currentLegFileName, traceback.format_exc())) if f is not None: f.close() #TODO: open route else: AVNLog.info("no current leg %s found" % (self.currentLegFileName, )) AVNLog.info("router main loop started") while True: hasLeg = False hasRMB = False time.sleep(interval) if self.currentLeg and self.currentLeg.active: hasLeg = True if self.currentLeg.anchorDistance is not None: routerInfo = "Anchor watch, from %s, (anchor radius %dm)" % ( unicode(self.currentLeg.fromWP), int(self.currentLeg.anchorDistance)) else: routerInfo = "from %s, to %s, route=%s, activeWp=%d, approach=%s (approach radius %dm)" % ( unicode(self.currentLeg.fromWP) if self.currentLeg.fromWP else "NONE", unicode(self.currentLeg.toWP) if self.currentLeg.toWP else "NONE", self.currentLeg.name if self.currentLeg.name is not None else "NONE", self.currentLeg.currentTarget, "TRUE" if self.currentLeg.approach else "FALSE", int(self.currentLeg.approachDistance)) AVNLog.debug(routerInfo) self.setInfo("leg", routerInfo, AVNWorker.Status.RUNNING) try: if self.currentLeg is not None and self.currentLeg.anchorDistance is not None: self.computeAnchor() else: self.startStopAlarm(False, 'anchor') self.startStopAlarm(False, 'gps') computeRMB = self.getBoolParam("computeRMB") computeAPB = self.getBoolParam("computeAPB") if computeRMB or computeAPB: hasRMB = self.computeRMB(computeRMB, computeAPB) except Exception as e: AVNLog.warn("exception in computeRMB %s, retrying", traceback.format_exc()) try: self.computeApproach() except: AVNLog.warn("exception in computeApproach %s, retrying", traceback.format_exc()) if (not hasLeg): self.setInfo("leg", "no leg", AVNWorker.Status.INACTIVE) if (not hasRMB): self.setInfo("autopilot", "no autopilot data", AVNWorker.Status.INACTIVE) try: curTPV = self.navdata.getMergedEntries("TPV", []) lat = curTPV.data.get('lat') lon = curTPV.data.get('lon') if lat is not None and lon is not None: self.startStopAlarm(False, 'gps') except: pass AVNLog.debug("router main loop")
def run(self): self.setName("[%s]%s"%(AVNLog.getThreadId(),self.getName())) interval=self.getIntParam('interval') routesdir=AVNUtil.replaceParam(os.path.expanduser(self.getStringParam("routesdir")),AVNConfig.filterBaseParam(self.getParam())) if routesdir == "": routesdir=os.path.join(self.getStringParam(AVNConfig.BASEPARAM.DATADIR),u'routes') self.routesdir=routesdir if not os.path.isdir(self.routesdir): AVNLog.info("creating routes directory %s"%(self.routesdir)) os.makedirs(self.routesdir,0755) self.fillRouteInfos() self.currentLegFileName=os.path.join(self.routesdir,self.currentLegName) if os.path.exists(self.currentLegFileName): f=None try: f=open(self.currentLegFileName,"r") strleg=f.read(self.MAXROUTESIZE+1000) self.currentLeg=self.parseLeg(strleg) if self.currentLeg.toWP is not None: distance=geo.length([self.currentLeg.fromWP,self.currentLeg.toWP]) AVNLog.info("read current leg, route=%s, from=%s, to=%s, length=%fNM"%(self.currentLeg.name, unicode(self.currentLeg.fromWP),unicode(self.currentLeg.toWP),distance/AVNUtil.NM)) else: AVNLog.info("read current leg, route=%s, from=%s, to=%s"% (self.currentLeg.name, unicode(self.currentLeg.fromWP), "NONE", )) if self.currentLeg.name is not None: self.activeRouteName=self.currentLeg.name if self.currentLeg.currentRoute is not None: #this will also set the active route self.saveRoute(self.currentLeg.currentRoute) if self.currentLeg.name != self.currentLeg.currentRoute.name: AVNLog.error("leg inconsistent, name in route %s different from name in leg %s, correcting to route name",self.currentLeg.name, self.currentLeg.currentRoute.name) self.currentLeg.name=self.currentLeg.currentRoute.name self.activeRouteName=self.currentLeg.name #write back the corrected leg self.setCurrentLeg(self.currentLeg) else: if self.currentLeg.name is not None: self.activeRoute=self.loadRoute(self.currentLeg.name) if self.activeRoute is None: self.activeRoute=gpx.GPXRoute(self.currentLeg.name) self.currentLeg.currentRoute=self.activeRoute except: AVNLog.error("error parsing current leg %s: %s"%(self.currentLegFileName,traceback.format_exc())) if f is not None: f.close() #TODO: open route else: AVNLog.info("no current leg %s found"%(self.currentLegFileName,)) AVNLog.info("router main loop started") while True: hasLeg=False hasRMB=False time.sleep(interval) if self.currentLeg and self.currentLeg.active: hasLeg=True if self.currentLeg.anchorDistance is not None: routerInfo = "Anchor watch, from %s, (anchor radius %dm)" % ( unicode(self.currentLeg.fromWP), int(self.currentLeg.anchorDistance)) else: routerInfo="from %s, to %s, route=%s, activeWp=%d, approach=%s (approach radius %dm)"%(unicode(self.currentLeg.fromWP) if self.currentLeg.fromWP else "NONE",unicode(self.currentLeg.toWP) if self.currentLeg.toWP else "NONE", self.currentLeg.name if self.currentLeg.name is not None else "NONE", self.currentLeg.currentTarget, "TRUE" if self.currentLeg.approach else "FALSE",int(self.currentLeg.approachDistance)) AVNLog.debug(routerInfo) self.setInfo("leg",routerInfo ,AVNWorker.Status.RUNNING) try: if self.currentLeg is not None and self.currentLeg.anchorDistance is not None: self.computeAnchor() else: self.startStopAlarm(False,'anchor') self.startStopAlarm(False, 'gps') computeRMB=self.getBoolParam("computeRMB") computeAPB=self.getBoolParam("computeAPB") if computeRMB or computeAPB : hasRMB=self.computeRMB(computeRMB,computeAPB) except Exception as e: AVNLog.warn("exception in computeRMB %s, retrying",traceback.format_exc()) try: self.computeApproach() except: AVNLog.warn("exception in computeApproach %s, retrying",traceback.format_exc()) if (not hasLeg): self.setInfo("leg","no leg",AVNWorker.Status.INACTIVE) if (not hasRMB): self.setInfo("autopilot","no autopilot data",AVNWorker.Status.INACTIVE) try: curTPV = self.navdata.getMergedEntries("TPV", []) lat = curTPV.data.get('lat') lon = curTPV.data.get('lon') if lat is not None and lon is not None: self.startStopAlarm(False,'gps') except: pass AVNLog.debug("router main loop")