Beispiel #1
0
    def run(self):
        # get the vehicle record
        try:
            vehicle = Vehicle.objects.get(veh_name=self.vehicle_name)
        except:
            logger.error("%s: Vehicle '%s' does not exist in database. Add it first.", MY_NAME, self.vehicle_name)
            sys.exit(2)

        # start GPS polling thread
        self.gps_poller.start()

        # catch signals for proper shutdown
        for sig in (SIGABRT, SIGTERM, SIGINT):
            signal(sig, self.cleanup)

        # main execution loop
        while True:
            try:
                time.sleep(self.interval)
                # If we are idle too long the database server may
                # close the connection on us, ping the server to check if
                # the connection is still up.
                if connection.connection is not None:
                    if connection.is_usable():
                        logger.debug("%s: Database connection is up.", MY_NAME)
                    else:
                        logger.error("%s: Database connection is down.", MY_NAME)
                        connection.close()
                else:
                    logger.error("%s: Database connection is closed.", MY_NAME)

                # process GPS data
                session = self.gps_poller.session
                if (session.fix.mode == MODE_NO_FIX) and not self.nofix:
                    logger.info("%s: Waiting for GPS to fix...", MY_NAME)
                    continue

                if not isnan(session.fix.time):
                    if (session.fix.speed < 0.1) and (self.last_speed < 0.1):
                        continue
                    self.last_speed = session.fix.speed
                    # if the time is valid the data record is valid
                    location = Location()
                    location.loc_vehicle = vehicle
                    location.loc_time = session.utc
                    location.loc_latitude = session.fix.latitude
                    location.loc_longitude = session.fix.longitude
                    if session.fix.mode == MODE_3D:
                        location.loc_altitude = session.fix.altitude
                    location.loc_speed = session.fix.speed
                    location.loc_climb = session.fix.climb
                    location.loc_track = session.fix.track
                    location.save()
                    logger.info("%s: Valid location: %s", MY_NAME, location)
                else:
                    logger.debug("%s: Invalid location: %s", MY_NAME)

            except KeyboardInterrupt:
                print ("\n")
                break
Beispiel #2
0
 def cleanup(self, *args):
     logger.info('%s: Caught signal: %d. Shutting down...', self.__class__.__name__, args[0])
     if self.gps_source:
         self.gps_source.shutdown()
     if self.file_source:
         self.file_source.shutdown()
     if self.munic_source:
         self.munic_source.shutdown()
     sys.exit(0)
Beispiel #3
0
    def run(self):
        # Execution starts here
        logger.info('%s: Starting...', self.__class__.__name__)

        # get configuration
        conf = get_settings()
        
        # setup gps source
        if conf['TRACKING_GPS_ENABLE'] == True:
            logger.info('%s: GPS Source enabled.', self.__class__.__name__)
            self.gps_source = GPSSource(conf, logger, self.queue)
            self.gps_source.start()
            
        # setup file source
        if conf['TRACKING_FILE_ENABLE'] == True:
            logger.info('%s: File Source enabled.', self.__class__.__name__)
            self.file_source = FileSources(conf, logger, self.queue)
            self.file_source.start()

        # setup municbox source
        if conf['TRACKING_MUNICBOX_ENABLE'] == True:
            logger.info('%s: Munic.Box Source enabled.', self.__class__.__name__)
            self.munic_source = MunicSource(conf, logger, self.queue)
            self.munic_source.start()

        # setup database sink
        if conf['TRACKING_DB_PUBLISH'] == True:
            logger.info('%s: Publishing to database enabled.', self.__class__.__name__)
            db_sink = DBSink(conf, logger)
            
        # setup message queue sink
        if conf['TRACKING_MQ_PUBLISH'] == True:
            logger.info('%s: Publishing to message queue enabled.', self.__class__.__name__)
            mq_sink = MQSink(conf, logger)
            
        # setup RVI sink
        if conf['TRACKING_RVI_PUBLISH'] == True:
            logger.info('%s: Publishing to RVI enabled.', self.__class__.__name__)
            rvi_sink = RVISink(conf, logger)
            
        # catch signals for proper shutdown
        for sig in (SIGABRT, SIGTERM, SIGINT):
            signal(sig, self.cleanup)

        # main execution loop
        while True:
            try:
                
                # get data from queue
                try:
                    data = self.queue.get(True, 60)
                except Exception as e:
                    if isinstance(e, KeyboardInterrupt):
                        break
                    else:
                        logger.info("%s: Queue timeout", self.__class__.__name__)
                        continue
                    
                
                # vin is required but not all data sources may provide it
                if (not 'vin' in data):
                    data[u'vin'] = conf['VIN_DEFAULT']


                logger.info("%s: Got data: %s", self.__class__.__name__, data)

                if conf['TRACKING_DB_PUBLISH'] == True:
                    db_sink.log(data)

                if conf['TRACKING_MQ_PUBLISH'] == True:
                    mq_sink.log(data)

                if conf['TRACKING_RVI_PUBLISH'] == True:
                    rvi_sink.log(data)


            except KeyboardInterrupt:
                print ('\n')
                break
    def run(self):
        # get the vehicle record
        try:
            vehicle = Vehicle.objects.get(veh_name=self.vehicle_name)
        except:
            logger.error(
                "%s: Vehicle '%s' does not exist in database. Add it first.",
                MY_NAME, self.vehicle_name)
            sys.exit(2)

        # start GPS polling thread
        self.gps_poller.start()

        # catch signals for proper shutdown
        for sig in (SIGABRT, SIGTERM, SIGINT):
            signal(sig, self.cleanup)

        # main execution loop
        while True:
            try:
                time.sleep(self.interval)
                # If we are idle too long the database server may
                # close the connection on us, ping the server to check if
                # the connection is still up.
                if (connection.connection is not None):
                    if (connection.is_usable()):
                        logger.debug('%s: Database connection is up.', MY_NAME)
                    else:
                        logger.error('%s: Database connection is down.',
                                     MY_NAME)
                        connection.close()
                else:
                    logger.error('%s: Database connection is closed.', MY_NAME)

                # process GPS data
                session = self.gps_poller.session
                if (session.fix.mode == MODE_NO_FIX) and not self.nofix:
                    logger.info("%s: Waiting for GPS to fix...", MY_NAME)
                    continue

                if not isnan(session.fix.time):
                    if (session.fix.speed < 0.1) and (self.last_speed < 0.1):
                        continue
                    self.last_speed = session.fix.speed
                    # if the time is valid the data record is valid
                    location = Location()
                    location.loc_vehicle = vehicle
                    location.loc_time = session.utc
                    location.loc_latitude = session.fix.latitude
                    location.loc_longitude = session.fix.longitude
                    if (session.fix.mode == MODE_3D):
                        location.loc_altitude = session.fix.altitude
                    location.loc_speed = session.fix.speed
                    location.loc_climb = session.fix.climb
                    location.loc_track = session.fix.track
                    location.save()
                    logger.info("%s: Valid location: %s", MY_NAME, location)
                else:
                    logger.debug("%s: Invalid location: %s", MY_NAME)

            except KeyboardInterrupt:
                print('\n')
                break
 def cleanup(self, *args):
     logger.info('%s: Caught signal: %d. Shutting down...', MY_NAME,
                 args[0])
     if self.gps_poller:
         self.gps_poller.shutdown()
     sys.exit(0)
            sys.exit(1)
        elif opt == '-f':
            nofix = True
        elif opt in ("-v", "-vehicle"):
            vehicle_name = arg
        elif opt in ("-i", "-interval"):
            interval = int(arg)
        elif opt in ("-p", "-pidfile"):
            pid_file = arg

    if vehicle_name is None:
        usage()
        print "Must provide vehicle"
        sys.exit(2)

    logger.info("%s: Logging GPS data for vehicle: %s", MY_NAME, vehicle_name)

    gps_collector = GPSCollector(vehicle_name,
                                 interval,
                                 pid_file,
                                 nofix,
                                 stdin='/dev/null',
                                 stdout='/dev/null',
                                 stderr='/dev/null')

    if len(sys.argv) >= 2:
        if sys.argv[1] in ('foreground', 'fg'):
            gps_collector.run()
        elif sys.argv[1] in ('start', 'st'):
            gps_collector.start()
        elif sys.argv[1] in ('stop', 'sp'):
Beispiel #7
0
 def cleanup(self, *args):
     logger.info("%s: Caught signal: %d. Shutting down...", MY_NAME, args[0])
     if self.gps_poller:
         self.gps_poller.shutdown()
     sys.exit(0)
Beispiel #8
0
            sys.exit(1)
        elif opt == "-f":
            nofix = True
        elif opt in ("-v", "-vehicle"):
            vehicle_name = arg
        elif opt in ("-i", "-interval"):
            interval = int(arg)
        elif opt in ("-p", "-pidfile"):
            pid_file = arg

    if vehicle_name is None:
        usage()
        print "Must provide vehicle"
        sys.exit(2)

    logger.info("%s: Logging GPS data for vehicle: %s", MY_NAME, vehicle_name)

    gps_collector = GPSCollector(
        vehicle_name, interval, pid_file, nofix, stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"
    )

    if len(sys.argv) >= 2:
        if sys.argv[1] in ("foreground", "fg"):
            gps_collector.run()
        elif sys.argv[1] in ("start", "st"):
            gps_collector.start()
        elif sys.argv[1] in ("stop", "sp"):
            gps_collector.stop()
        elif sys.argv[1] in ("restart", "re"):
            gps_collector.restart()
        else: