예제 #1
0
 def routeFromJson(self,dct):
   if dct.get('name') is None:
     raise Exception("missing name in route")
   route=gpx.GPXRoute(**self.convertToGpx(dict((x,dct.get(x)) for x in ['name','description'])))
   points=dct.get('points')
   if points is not None:
     for p in points:
       rp=gpx.GPXRoutePoint(**self.convertToGpx(dict((x,p.get(x)) for x in ['name','lon','lat'])))
       route.points.append(rp)
   AVNLog.debug("routeFromJson: %s",unicode(route))
   return route
예제 #2
0
 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")