Example #1
0
  def connect(self, link, read_config):
    """Retry read connections. Intended to be called when thread started
    Only shared resource here is the self.link
    Parameters
    ----------
    link : SbpHandler
    read_config :  SkylarkConsoleConnectConfig object 
    
    Returns
    ----------
    ret : int
       0 if exited normally by thread stopping
      -1 if unable to connect as base station
      -2 if unable to connect as rover
      -3 if we lost our net connection to skylark (we restart in this case unless stopped)

    """
    assert isinstance(read_config, SkylarkConsoleConnectConfig)
    self._connect_time = time.time()
    if self.verbose:
      print(("SkylarkWatchdogThread connection attempted at time {0} with parameters {1}".format(
                 self.get_connect_time(), read_config))) 
    i = 0
    repeats = 5
    http = HTTPDriver(device_uid=read_config.base_uuid, url=read_config.skylark_url)
    if not http.connect_write(link, read_config.whitelist, pragma=read_config.base_pragma):
        msg = ("\nUnable to connect to Skylark!\n\n" + 
               "Please check that you have a network connection.")
        self._prompt_networking_error(msg)
        http.close()
        self.stop()
        return -1 # unable to connect as base
    time.sleep(1)

    # If we get here, we were able to connect as a base
    print("Attempting to read observation from Skylark...")
    while (not self.stopped() and http 
           and not http.connect_read(device_uid=read_config.rover_uuid, pragma=read_config.rover_pragma)):
      time.sleep(0.1)
      i += 1
      if i >= repeats:
        msg = ("\nUnable to receive observations from Skylark!\n\n"
               "Please check that:\n"
               " - you have a network connection\n"
               " - your Piksi has a single-point position\n"
               " - a Skylark-connected Piksi receiver \n   is nearby (within 5km)")
        self._prompt_networking_error(msg)
        http.close()
        self.stop()
        return -2 # Unable to connect as rover

    # If we get here, we were able to connect as rover
    print("Connected as a rover!")
    with Handler(Framer(http.read, http.write)) as net_link:
      fwd = Forwarder(net_link, swriter(link))
      if self.verbose:
        print("Starting forwarder")
      fwd.start()
      # now we sleep until we stop the thread or our http handler dies
      while not self.stopped() and net_link.is_alive():
          time.sleep(0.1)

    # when we leave this loop, we are no longer connected to skylark so the fwd should be stopped
    if self.verbose:
      print("Stopping forwarder")
    fwd.stop()
    if self.verbose:
      print("Stopping HTTPDriver")
    http.close() 
    # now manage the return code
    if self.stopped():
      return 0 # If we stop from the event, it it intended and we return 0
    else:
      return -3 # Lost connection
Example #2
0
    def connect(self, link, read_config):
        """Retry read connections. Intended to be called when thread started
        Only shared resource here is the self.link
        Parameters
        ----------
        link : SbpHandler
        read_config :  HttpConsoleConnectConfig object

        Returns
        ----------
        ret : int
           0 if exited normally by thread stopping
          -1 if unable to connect as base station
          -2 if unable to connect as rover
          -3 if we lost our network connection (we restart in this case unless stopped)

        """
        assert isinstance(read_config, HttpConsoleConnectConfig)
        self._connect_time = time.time()
        if self.verbose:
            print(
                "HttpWatchdogThread connection attempted at time {0} with parameters {1}".
                format(self.get_connect_time(), read_config))
        i = 0
        repeats = 5
        http = HTTPDriver(
            device_uid=read_config.base_uuid, url=read_config.url)
        if not http.connect_write(
                link, read_config.whitelist, pragma=read_config.base_pragma):
            msg = ("\nUnable to connect!\n\n" +
                   "Please check that you have a network connection.")
            self._prompt_networking_error(msg)
            http.close()
            self.stop()
            return -1  # unable to connect as base
        time.sleep(1)

        # If we get here, we were able to connect as a base
        print("Attempting to read observations ...")
        while (not self.stopped() and http and not http.connect_read(
                device_uid=read_config.rover_uuid,
                pragma=read_config.rover_pragma)):
            time.sleep(0.1)
            i += 1
            if i >= repeats:
                msg = ("\nUnable to receive observations!\n\n"
                       "Please check that:\n"
                       " - you have a network connection\n"
                       " - your Piksi has a single-point position\n"
                       " - your Piksi has sent its settings to the console")
                self._prompt_networking_error(msg)
                http.close()
                self.stop()
                return -2  # Unable to connect as rover

        # If we get here, we were able to connect as rover
        print("Connected as a rover!")
        with Handler(Framer(http.read, http.write)) as net_link:
            fwd = Forwarder(net_link, swriter(link))
            if self.verbose:
                print("Starting forwarder")
            fwd.start()
            # now we sleep until we stop the thread or our http handler dies
            while not self.stopped() and net_link.is_alive():
                time.sleep(0.1)

        # when we leave this loop, we are no longer connected so the fwd should be stopped
        if self.verbose:
            print("Stopping forwarder")
        fwd.stop()
        if self.verbose:
            print("Stopping HTTPDriver")
        http.close()
        # now manage the return code
        if self.stopped():
            return 0  # If we stop from the event, it it intended and we return 0
        else:
            return -3  # Lost connection