10 from .nmea_parser
import NMEAParser
11 from .rtcm_parser
import RTCMParser
14 _SOURCETABLE_RESPONSES = [
17 _SUCCESS_RESPONSES = [
22 _UNAUTHORIZED_RESPONSES = [
29 DEFAULT_RECONNECT_ATTEMPT_MAX = 10
30 DEFAULT_RECONNECT_ATEMPT_WAIT_SECONDS = 5
31 DEFAULT_RTCM_TIMEOUT_SECONDS = 4
33 def __init__(self, host, port, mountpoint, ntrip_version, username, password, logerr=logging.error, logwarn=logging.warning, loginfo=logging.info, logdebug=logging.debug):
45 if username
is not None and password
is not None:
47 username, password).encode(
'utf-8')).decode(
'utf-8')
95 self.
_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
101 except Exception
as e:
103 'Unable to connect socket to server at http://{}:{}'.format(self.
_host, self.
_port))
104 self.
_logerr(
'Exception: {}'.format(str(e)))
123 except Exception
as e:
125 'Unable to send request to server at http://{}:{}'.format(self.
_host, self.
_port))
126 self.
_logerr(
'Exception: {}'.format(str(e)))
133 except Exception
as e:
135 'Unable to read response from server at http://{}:{}'.format(self.
_host, self.
_port))
136 self.
_logerr(
'Exception: {}'.format(str(e)))
140 if any(success
in response
for success
in _SUCCESS_RESPONSES):
145 if any(sourcetable
in response
for sourcetable
in _SOURCETABLE_RESPONSES):
146 self.
_logwarn(
'Received sourcetable response from the server. This probably means the mountpoint specified is not valid')
148 elif any(unauthorized
in response
for unauthorized
in _UNAUTHORIZED_RESPONSES):
149 self.
_logwarn(
'Received unauthorized response from the server. Check your username, password, and mountpoint to make sure they are correct.')
152 self.
_logwarn(
'Received unknown error from the server. Note that the NTRIP version was not specified in the launch file. This is not necesarilly the cause of this error, but it may be worth checking your NTRIP casters documentation to see if the NTRIP version needs to be specified.')
158 self.
_logerr(
'Invalid response received from http://{}:{}/{}'.format(
160 self.
_logerr(
'Response: {}'.format(response))
176 except Exception
as e:
177 self.
_logdebug(
'Encountered exception when shutting down the socket. This can likely be ignored')
178 self.
_logdebug(
'Exception: {}'.format(e))
184 except Exception
as e:
185 self.
_logdebug(
'Encountered exception when closing the socket. This can likely be ignored')
186 self.
_logdebug(
'Exception: {}'.format(e))
193 connect_success = self.
connect()
201 elif connect_success:
205 self.
_logdebug(
'Reconnect called while still connected, ignoring')
209 self.
_logwarn(
'NMEA sent before client was connected, discarding NMEA')
213 if sentence[-4:] ==
'\\r\\n':
214 sentence = sentence[:-4] +
'\r\n' 215 elif sentence[-2:] !=
'\r\n':
216 sentence = sentence +
'\r\n' 219 if not self.
nmea_parser.is_valid_sentence(sentence):
220 self.
_logwarn(
"Invalid NMEA sentence, not sending to server")
226 except Exception
as e:
227 self.
_logwarn(
'Unable to send NMEA sentence to server.')
228 self.
_logwarn(
'Exception: {}'.format(str(e)))
240 'RTCM requested before client was connected, returning empty list')
250 read_sockets, _, _ = select.select([self.
_server_socket], [], [], 0)
261 if len(chunk) < _CHUNK_SIZE:
263 except Exception
as e:
264 self.
_logerr(
'Error while reading {} bytes from socket'.format(_CHUNK_SIZE))
266 self.
_logerr(
'Socket appears to be closed. Reconnecting')
270 self.
_logdebug(
'Read {} bytes'.format(len(data)))
277 self.
_logwarn(
'Reconnecting because we received 0 bytes from the socket even though it said there was data available {} times'.format(self.
_read_zero_bytes_count))
287 return self.
rtcm_parser.parse(data)
if data
else []
296 request_str =
'GET /{} HTTP/1.0\r\nNtrip-Version: {}\r\nUser-Agent: NTRIP ntrip_client_ros\r\n'.format(
299 request_str =
'GET /{} HTTP/1.0\r\nUser-Agent: NTRIP ntrip_client_ros\r\n'.format(
302 request_str +=
'Authorization: Basic {}\r\n'.format(
304 request_str +=
'\r\n' 305 return request_str.encode(
'utf-8')
310 data = self.
_server_socket.recv(_CHUNK_SIZE, socket.MSG_DONTWAIT | socket.MSG_PEEK)
313 except BlockingIOError:
315 except ConnectionResetError:
316 self.
_logwarn(
'Connection reset by peer')
318 except socket.timeout:
320 except Exception
as e:
321 self.
_logwarn(
'Socket appears to be closed')
322 self.
_logwarn(
'Exception: {}'.format(e))
int DEFAULT_RECONNECT_ATEMPT_WAIT_SECONDS
def __init__(self, host, port, mountpoint, ntrip_version, username, password, logerr=logging.error, logwarn=logging.warning, loginfo=logging.info, logdebug=logging.debug)
reconnect_attempt_wait_seconds
int DEFAULT_RTCM_TIMEOUT_SECONDS
_recv_rtcm_last_packet_timestamp
int DEFAULT_RECONNECT_ATTEMPT_MAX
def send_nmea(self, sentence)
def _socket_is_open(self)