Beispiel #1
0
    def __init__(self, channel_config, sleep_time=0.005):
        self.config = channel_config
        self.r = redis.Redis(host=settings.XGDS_CORE_REDIS_HOST,
                             port=settings.XGDS_CORE_REDIS_PORT)
        self.tq = TelemetryQueue(self.config['input_channel'], sleep_time)

        # Spawn listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()
Beispiel #2
0
 def run(self):
     print '%s listener started' % self.channel_name
     tq = TelemetryQueue(self.channel_name)
     for msg in tq.listen():
         print '%s: %s' % (self.channel_name, msg)
         publishRedisSSE(
             self.channel_name + "_sse", 
             self.channel_name, 
             json.dumps({
                 "timestamp": int(time.time()),
                 "message": msg,
             },
             ))
    def __init__(self, config, prefix=""):
        self.verbose = config['verbose']
        self.prefix = prefix

        self.delimiter = '\t'
        self.tq = TelemetryQueue(config['channel_name'])
        self.author = User.objects.get(username='******')
        self.argus = VEHICLE_MODEL.get().objects.get(name='Argus')
        self.hercules = VEHICLE_MODEL.get().objects.get(name='Hercules')

        # Start listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()
Beispiel #4
0
class RedisDemux:
    def __init__(self, channel_config, sleep_time=0.005):
        self.config = channel_config
        self.r = redis.Redis(host=settings.XGDS_CORE_REDIS_HOST,
                             port=settings.XGDS_CORE_REDIS_PORT)
        self.tq = TelemetryQueue(self.config['input_channel'], sleep_time)

        # Spawn listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()

    def run(self):
        print '%s listener started' % self.config['input_channel']
        # polling loop:
        for msg in self.tq.listen():
            try:
                payload = msg['data'].split('\t')[3]
                for payload_identifier, output_channel in self.config[
                        'outputs'].iteritems():
                    strlen = len(payload_identifier)
                    if payload[0:strlen] == payload_identifier:
                        if verbose:
                            print '%s %s -> %s' % (
                                self.config['input_channel'],
                                payload_identifier, output_channel)
                        self.r.publish(output_channel, msg['data'])
            except Exception as e:
                persist_error(e)
Beispiel #5
0
    def __init__(self, config, prefix=""):
        self.verbose = config['verbose']
        self.prefix = prefix

        # Is there a current active group flight?
        self.active_dive = None
        active_flight = getActiveFlight()
        if active_flight is not None:
            # there's an active flight underway
            self.active_dive = active_flight.group
            print 'found active dive %s ' % self.active_dive.name

        if not self.active_dive:
            print 'NO ACTIVE DIVE'

        self.delimiter = '\t'
        self.tq = TelemetryQueue(config['channel_name'])

        # Start listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()
class FrameGrabber(object):
    def __init__(self, config, prefix=""):
        self.verbose = config['verbose']
        self.prefix = prefix

        self.delimiter = '\t'
        self.tq = TelemetryQueue(config['channel_name'])
        self.author = User.objects.get(username='******')
        self.argus = VEHICLE_MODEL.get().objects.get(name='Argus')
        self.hercules = VEHICLE_MODEL.get().objects.get(name='Hercules')

        # Start listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()

    def parse_data(self, data):
        """
        Gets the time and channel from the data
        :param data: the incoming data
        :return: time and channel
        """
        values = data.split(self.delimiter)

        # get the time
        time_value = values[1]
        the_time = dateparser(time_value)
        the_time.replace(tzinfo=pytz.UTC)

        # get the channel
        chan_value = values[2]
        chan = int(chan_value.split('=')[1])
        return the_time, chan

    def run(self):
        for msg in self.tq.listen():
            try:
                data = msg['data']
                the_time, channel = self.parse_data(data)
                vehicle = self.hercules
                if channel == 2:
                    vehicle = self.argus

                t = Timer(settings.XGDS_VIDEO_RECORDING_LAG_SECONDS,
                          grab_frame,
                          [the_time, channel, vehicle, self.author])
                t.start()
            except Exception as e:
                persist_error(e)
Beispiel #7
0
class DiveCreator(object):
    def __init__(self, config, prefix=""):
        self.verbose = config['verbose']
        self.prefix = prefix

        # Is there a current active group flight?
        self.active_dive = None
        active_flight = getActiveFlight()
        if active_flight is not None:
            # there's an active flight underway
            self.active_dive = active_flight.group
            print 'found active dive %s ' % self.active_dive.name

        if not self.active_dive:
            print 'NO ACTIVE DIVE'

        self.delimiter = '\t'
        self.tq = TelemetryQueue(config['channel_name'])

        # Start listener thread
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()

    def start_dive(self, group_flight_name, start_time):
        # call create_group_flight
        print('starting dive %s' % group_flight_name)
        extras = {
            "cruise": settings.CRUISE_ID,
            "dive": group_flight_name,
            "inwatertime": start_time.isoformat()
        }
        print extras
        self.active_dive = create_group_flight(group_flight_name,
                                               notes=None,
                                               active=True,
                                               start_time=start_time,
                                               extras=extras)

        for flight in self.active_dive.flights:
            # create a track for each flight
            track = get_or_create_track(flight.name, flight.vehicle, flight)
            # record video if enabled
            if settings.XGDS_VIDEO_ON:
                startFlightRecording(flight.name)

        if settings.XGDS_SSE:
            # publishing on sse with type group_flight
            result = {
                'status': 'started',
                'name': self.active_dive.name,
                'time': start_time
            }
            publishRedisSSE('sse', settings.XGDS_GROUP_FLIGHT_SSE_TYPE.lower(),
                            json.dumps(result, cls=DatetimeJsonEncoder))
        print('started dive %s' % group_flight_name)

    def end_dive(self, end_time=None):
        """
        End a dive, stopping active flights and clearing self.active_dive
        :param end_time: the end time to set on all the flights
        """
        group_flight_name = self.active_dive.name
        print('ending dive %s %s' % (group_flight_name, end_time))

        # stop video recording once ROVs are on deck
        if settings.XGDS_VIDEO_ON:
            totalFlights = self.active_dive.flights.count()
            flightCount = 0
            endEpisode = False  # Flag to stopRecording to end video episode
            for flight in self.active_dive.flights:
                flightCount += 1
                if flightCount == totalFlights:
                    endEpisode = True  # End video episode when we stop last flight in list
                stopFlightRecording(flight.name, endEpisode)

        # delay so other things have time to finish
        t = Timer(30, end_group_flight, [group_flight_name, end_time])
        t.start()

        self.active_dive = None

    def end_other_dive(self, group_flight_name, end_time):
        """
        End a dive that is not our active dive
        :param group_flight_name: the group flight name to look for
        :param end_time: the end time
        """
        print('ending dive %s %s' % (group_flight_name, end_time))
        end_group_flight(group_flight_name, end_time)

    def parse_data(self, data):
        """
        Gets the group flight name and the dive number and the time from the data
        :param data:
        :return: dictionary with group flight name, dive number and time
        """
        dive_number = None
        group_flight_name = None
        values = data.split(self.delimiter)

        if 'DIVENUMBER' in data:
            # Try to get the group flight name from the event message
            for v in values:
                if v.startswith('DIVENUMBER'):
                    dive_number = v.split(':')[1]
                    group_flight_name = self.prefix + dive_number
                    break
            if not group_flight_name:
                # should never happen but just in case
                last_flight_number = self.get_last_flight_number()
                flight_number = last_flight_number + 1
                group_flight_name = '%sH%d' % (self.prefix, flight_number)

        # get the time
        value = values[1]
        the_time = dateparser(value)
        the_time.replace(tzinfo=pytz.UTC)

        result = {
            'group_flight_name': group_flight_name,
            'dive_number': dive_number,
            'time': the_time
        }
        print result
        return result

    def run(self):
        for msg in self.tq.listen():
            try:
                data = msg['data']
                # data = data.lower()

                if 'divestatusevent:inwater' in data.lower():
                    print data
                    reconnect_db()
                    parsed_data = self.parse_data(data)

                    # see if we got an inwater with an invalid / completed dive
                    try:
                        GROUP_FLIGHT_MODEL.get().objects.get(
                            name=parsed_data['group_flight_name'])
                        # this group flight already exists so we do not want this script to create one.
                        return
                    except ObjectDoesNotExist:
                        pass

                    # make the dive and start it
                    self.start_dive(parsed_data['group_flight_name'],
                                    parsed_data['time'])

                elif 'divestatusevent:ondeck' in data.lower():
                    print data
                    reconnect_db()
                    parsed_data = self.parse_data(data)
                    end_time = None

                    if self.active_dive:
                        if parsed_data['dive_number']:
                            if parsed_data[
                                    'dive_number'] in self.active_dive.name:
                                end_time = parsed_data['time']
                        self.end_dive(end_time)
                    else:
                        # no active dive, but we got an end so let's end that dive
                        self.end_other_dive(parsed_data['dive_number'],
                                            parsed_data['time'])
            except Exception as e:
                persist_error(e, traceback.format_exc())
Beispiel #8
0
 def run(self):
     print '%s listener started' % self.channel_name
     tq = TelemetryQueue(self.channel_name)
     for msg in tq.listen():
         print '%s: %s' % (self.channel_name, msg)