示例#1
0
    def run_offline_rosmon(self, log_path):
        system = OfflineInterface(log_path)

        # get the trace file
        trace_file = system.connect()
        trace = Trace()

        # check the traces
        for json_line in trace_file:
            if json_line == '\n':
                continue

            varanus_logger.debug("json_line" + json_line)

            event_map = self.eventMapper.convert_to_internal(
                json.loads(json_line))

            varanus_logger.debug("event_map: " + str(event_map))
            #### THIS IS A BAD PLACE FOR THIS
            event = Event(event_map["channel"], event_map["params"])
            trace.add_event(event)
            varanus_logger.debug("event: " + event)

            if event_map["channel"] == "speed":
                speed_ok = Event("speed_ok")
                trace.add_event(speed_ok)

            if event_map["channel"] == "foot_pedal_pressed" and event_map[
                    "params"] == True:
                mode_change = Event("enter_hands_on_mode")
                trace.add_event(mode_change)
            elif event_map["channel"] == "foot_pedal_pressed" and event_map[
                    "params"] == False:
                mode_change = Event("enter_autonomous_mode")
                trace.add_event(mode_change)

            ####

            #trace = eventMapper.new_traces(event)

            varanus_logger.debug("trace: " + trace)
            ###############

            result = self.fdr.check_trace(trace)
            varanus_logger.debug("result: " + result)

            if not result:
                system.close()
                return result

        return result
示例#2
0
    def new_traces(self, update):
        """ Returns the new trace(s) of the system after the update.
        Returns a Trace object """

        # I think this is only useful if the system under monitoring is sending
        # update telgrams (of all the variables)

        new_traces = self._decode(update)
        assert (isinstance(new_traces, tuple) or isinstance(new_traces, Trace))
        # This will be a Trace if we're sure of the order of events
        # or a tuple of Traces if we're not

        if isinstance(new_traces, tuple):
            #first split
            original_trace = self.event_traces[0]
            varanus_logger.debug("Original Trace = " + original_trace)
            varanus_logger.debug("Type of original trace = " +
                                 str(type(original_trace)))

            new_event_traces = []

            for trace in new_traces:

                new_trace = Trace(original_trace)
                new_trace.append_trace(trace)

                new_event_traces.append(new_trace)

            self.event_traces = new_event_traces
        elif len(self.event_traces) > 1:
            #after first split

            if isinstance(new_traces, tuple):
                #split again
                #TODO
                pass
            else:
                #just update what we have
                for trace in self.event_traces:
                    trace.append_trace(new_traces)

        else:
            #no splits
            self.event_traces[0].append_trace(new_traces)

        return self.event_traces
示例#3
0
    def run_online_traces_accumulate(self, ip, port, timeRun=False):
        """Accepts events transferred across a socket, accumulates a trace,
        and for each new event checks the new trace in FDR. """

        varanus_logger.info("+++ Running Offline Traces Single +++")
        ##connect to the system
        system = TCPInterface_Client(ip, port)
        conn = system.connect()

        trace = Trace()

        if timeRun:
            time_list = []
            t0 = time.time()

        # How to terminate? What is the end program signal?
        while 1:

            #get the data from the system
            data = conn.recv(1024)
            # break if it's empty
            if not data: break

            varanus_logger.info("+++ Varanus received: " + data + " +++")
            conn.send(data)  # echo

            if data.find(".") == -1:
                channel, params = data, None
                new_event = Event(channel, params)
                trace.add_event(new_event)
            else:
                channel, params = data.split(".", 1)
                new_event = Event(channel, params)
                trace.add_event(new_event)

            #Send to FDR
            result = self.fdr.check_trace(trace)
            #result = True

            varanus_logger.debug("result: " + str(result))

            if timeRun:
                t1 = time.time()
                total = t1 - t0
                time_tuple = (trace.get_length(), total)
                time_list.append(time_tuple)

        conn.close()
        if timeRun:
            varanus_logger.info("Times:")
            for t in time_list:
                varanus_logger.info(str(t))
        system.close()
示例#4
0
    def _decode_velocity(self, curr_velocity):
        """Decodes the change in velocity. Returns a Trace object """

        # This is an assumption that the speed reading was ok, because the
        # system did it. Otherwise, FDR will reject the trace,
        # which is what we want.
        new_event = Event(str(self.event_map["velocity"]), curr_velocity)
        speed_ok_event = Event("speed_ok", None)

        self.last_values["velocity"] = curr_velocity

        new_trace = Trace([new_event, speed_ok_event])

        return new_trace
示例#5
0
    def _decode_footswitch(self, curr_footswitch):
        """ Decodes the change in footswitch. Returns a Trace object """

        new_trace = Trace()

        new_event = Event(self.event_map["footswitch"], curr_footswitch)
        new_trace.add_event(new_event)

        ## Again, assuming that the event we receive was performed correctly
        if curr_footswitch:
            hom_event = Event("enter_hands_on_mode")
            new_trace.add_event(hom_event)
        else:
            am_event = Event("enter_autonomous_mode")
            new_trace.add_event(am_event)

        self.last_values["footswitch"] = curr_footswitch

        return new_trace
示例#6
0
    def _run_offline_traces_single(self, trace_path):
        """ Runs Varanus Offline, taking a single trace and sending it to FDR"""

        varanus_logger.info("+++ Running Offline Traces Single +++")

        system = OfflineInterface(trace_path)
        trace_file = system.connect()
        trace = Trace()

        # Take a single line
        trace_line = trace_file.read()
        # parse to list
        event_list = json.loads(trace_line)
        varanus_logger.debug("event_list:" + str(event_list))

        # build trace from list
        for event in event_list:
            event = str(event)
            if event.find(".") == -1:
                channel, params = event, None
                new_event = Event(channel, params)
                trace.add_event(new_event)
            else:
                channel, params = event.split(".", 1)
                new_event = Event(channel, params)
                trace.add_event(new_event)

        varanus_logger.debug("trace: " + str(trace))

        # throw at FDR
        print("before check_trace")
        result = self.fdr.check_trace(trace)
        varanus_logger.debug("result: " + str(result))

        if not result:
            system.close()
            return result

        return result
示例#7
0
    def _run_offline_traces(self, log_path):
        system = OfflineInterface(log_path)

        trace_file = system.connect()
        trace = Trace()

        for json_line in trace_file:
            if json_line == '\n':
                continue
            varanus_logger.debug("json_line:" + json_line)
            # No convert_to_internal here becasue it's for a file of traces
            event_list = json.loads(json_line)
            varanus_logger.debug("event_list" + str(event_list))
            last_event = event_list[-1]
            varanus_logger.debug("last_event" + last_event)

            if last_event.find(".") == -1:
                channel, params = last_event, None
                event = Event(channel, params)
                trace.add_event(event)
            else:
                channel, params = last_event.split(".", 1)
                event = Event(channel, params)
                trace.add_event(event)

            varanus_logger.debug("trace: " + str(trace) + "and type: " +
                                 type(trace))

            result = self.fdr.check_trace(trace)
            varanus_logger.debug("result: " + result)

            if not result:
                system.close()
                return result

        return result
示例#8
0
 def __init__(self):
     # this tracks the event trace(s)
     # Initialised assuiming we're dealing with CSP
     self.event_traces = [Trace("system_init")]
示例#9
0
    def _decode(self, update):
        """ Decodes the updates in the telegram and returns one more Trace objects """

        ## Some of this needs to move to convert_to_internal for this implementation
        ## Decode should be working on the internal representation of CSP events
        curr_velocity = update["velocity"]
        curr_footswitch = update["footswitch"]

        new_trace = None

        # This is hard-coded, needs to be extracted.
        if curr_velocity != self.last_values[
                "velocity"] and curr_footswitch != self.last_values[
                    "footswitch"]:
            first_trace_fragment = Trace()
            first_trace_fragment.append_trace(
                self._decode_velocity(curr_velocity))
            first_trace_fragment.append_trace(
                self._decode_footswitch(curr_footswitch))

            new_trace_fragment = Trace()
            new_trace_fragment.append_trace(
                self._decode_footswitch(curr_footswitch))
            new_trace_fragment.append_trace(
                self._decode_velocity(curr_velocity))

            new_trace = (first_trace_fragment, new_trace_fragment)

        elif curr_velocity != self.last_values["velocity"]:
            # becomes an event
            new_trace = self._decode_velocity(curr_velocity)

        elif curr_footswitch != self.last_values["footswitch"]:
            # becomes an event
            new_trace = self._decode_footswitch(curr_footswitch)
        else:
            # Assume this is just a speed update
            new_trace = self._decode_velocity(curr_velocity)

        return new_trace