Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for last packet received, null for none.
:return: 0 if successfully added, 1 if an entry for network and station
already exists.
:raise SeedLinkException: on error.
"""
# Sanity, check for a uni-station mode entry
# print("DEBUG: selectors_str:", selectors_str)
if len(self.streams) > 0:
stream = self.streams[0]
if stream.net == SeedLinkConnection.UNINETWORK and \
stream.station == SeedLinkConnection.UNISTATION:
msg = "add_stream called, but uni-station mode configured!"
logger.critical(msg)
raise SeedLinkException(msg)
if not selectors_str:
selectors = []
else:
selectors = selectors_str.split()
# Search the stream chain if net/station/selector already present
for stream in self.streams:
if stream.net == net and stream.station == station:
return stream.append_selectors(selectors_str)
# Add new stream
newstream = SLNetStation(net, station, selectors, seqnum, timestamp)
self.streams.append(newstream)
self.multistation = True
return 0
logger.debug("saving connection state to state file")
stacount = 0
try:
# Loop through the stream chain
for curstream in self.streams:
# print("DEBUG: curstream:", curstream.net, curstream.station,
# curstream.btime)
if curstream.btime is not None:
statefile_file.write(
curstream.net + " " +
curstream.station + " " + str(curstream.seqnum) +
" " + curstream.btime.format_seedlink() + "\n")
except IOError as e:
msg = "%s: writing state file: %s" % (e, self.statefile)
logger.critical(msg)
raise SeedLinkException(msg)
finally:
try:
statefile_file.close()
except Exception as e:
pass
return stacount
def __init__(self, data=None, offset=None):
if data is None or offset is None:
return
if len(data) - offset < self.SLHEADSIZE + self.SLRECSIZE:
msg = "not enough bytes in sub array to construct a new SLPacket"
raise SeedLinkException(msg)
self.slhead = data[offset: offset + self.SLHEADSIZE]
self.msrecord = data[offset + self.SLHEADSIZE:
offset + self.SLHEADSIZE + self.SLRECSIZE]
self.trace = None
"file: %s"
logger.error(msg % (linecount, sle.value))
except Exception as e:
msg = "parsing timestamp in line %s of state " + \
"file: %s"
logger.error(msg % (linecount, str(e)))
if (stacount == 0):
msg = "no matching streams found in %s"
logger.error(msg % (self.statefile))
else:
msg = "recovered state for %s streams in %s"
logger.debug(msg % (stacount, self.statefile))
except IOError as e:
msg = "%s: reading state file: %s" % (e, self.statefile)
logger.critical(msg)
raise SeedLinkException(msg)
finally:
try:
statefile_file.close()
except Exception as e:
pass
return stacount
raise SeedLinkException(msg % (read_str))
# negotiate the station connection
try:
self.negotiate_station(curstream)
except SeedLinkException as sle:
logger.error(sle.value)
continue
except Exception as e:
logger.error(str(e))
continue
acceptsta += 1
# Fail if no stations were accepted
if acceptsta < 1:
raise SeedLinkException("no stations accepted")
logger.info("%s station(s) accepted" % (acceptsta))
# Issue END action command
send_str = b"END"
logger.debug("sending: %s" % (send_str.decode()))
bytes_ = send_str + b"\r"
self.send_data(bytes_, self.sladdr, 0)
# Send action command and receive response
logger.debug("sending: %s" % (sendStr))
bytes_ = sendStr + b"\r"
bytesread = None
bytesread = self.sendData(bytes_, self.sladdr,
SeedLinkConnection.DFT_READBUF_SIZE)
# Check response to DATA/FETCH/TIME
readStr = bytesread.decode()
if readStr == "OK\r\n":
logger.debug("response: DATA/FETCH/TIME command is OK")
acceptsel += 1
elif readStr == "ERROR\r\n":
msg = "response: DATA/FETCH/TIME command is not accepted"
raise SeedLinkException(msg)
else:
msg = "response: invalid response to DATA/FETCH/TIME command: %s"
raise SeedLinkException(msg % (readStr))
bytes_ = sendStr + b"\r"
bytesread = None
bytesread = self.sendData(bytes_, self.sladdr,
SeedLinkConnection.DFT_READBUF_SIZE)
# Check response to DATA/FETCH/TIME
readStr = bytesread.decode()
if readStr == "OK\r\n":
logger.debug("response: DATA/FETCH/TIME command is OK")
acceptsel += 1
elif readStr == "ERROR\r\n":
msg = "response: DATA/FETCH/TIME command is not accepted"
raise SeedLinkException(msg)
else:
msg = "response: invalid response to DATA/FETCH/TIME command: %s"
raise SeedLinkException(msg % (readStr))
"""
Negotiate a SeedLink connection using multi-station mode and
issue the END action command. This is compatible with SeedLink
Protocol version 3, multi-station mode.
If selectors are defined, then the string is parsed on space and each
selector is sent.
If 'seqnum' != -1 and the SLCD 'resume' flag is true then data is
requested starting at seqnum.
:raise SeedLinkException: on error.
:raise IOException: if an I/O error occurs.
"""
acceptsta = 0
if len(self.streams) < 1:
msg = "cannot negotiate multi-station, stream list is empty"
raise SeedLinkException(msg)
# Loop through the stream chain
for curstream in self.streams:
# A ring identifier
# slring = curstream.net + curstream.station
# Build STATION command, send it and receive response
sendStr = ("STATION " + curstream.station + " " +
curstream.net).encode('ascii', 'strict')
logger.debug("sending: %s" % sendStr.decode())
bytes_ = sendStr + b"\r"
bytesread = None
bytesread = self.sendData(bytes_, self.sladdr,
SeedLinkConnection.DFT_READBUF_SIZE)
readStr = bytesread
tmpstr = servstr[vndx + 2:]
endndx = tmpstr.find(" ")
# print("DEBUG: tmpstr:", tmpstr)
# print("DEBUG: tmpstr[0:endndx]:", tmpstr[0:endndx])
self.server_version = float(tmpstr[0:endndx])
except:
msg = "bad server ID/version string: '%s'"
raise SeedLinkException(msg % (servstr))
# Check the response to HELLO
if self.server_id.lower() == "seedlink":
msg = "connected to: '" + servstr[0:servstr.find('\r')] + "'"
logger.info(msg)
else:
msg = "ncorrect response to HELLO: '%s'" % (servstr)
raise SeedLinkException(msg)