Fixing issues with SSTV and FAX parsers.

This commit is contained in:
Marat Fayzullin 2023-05-01 20:55:43 -04:00
parent 9fbd21952a
commit 92c67cb6d7
2 changed files with 53 additions and 31 deletions

View File

@ -74,15 +74,21 @@ class FaxParser(ThreadModule):
def setDialFrequency(self, frequency: int) -> None: def setDialFrequency(self, frequency: int) -> None:
self.frequency = frequency self.frequency = frequency
def myName(self):
return "%s%s" % (
"Service" if self.service else "Client",
" at %dkHz" % (self.frequency // 1000) if self.frequency>0 else ""
)
def run(self): def run(self):
logger.debug("%s starting..." % ("Service" if self.service else "Client")) logger.debug("%s starting..." % self.myName())
# Run while there is input data # Run while there is input data
while self.doRun: while self.doRun:
# Read input data # Read input data
inp = self.reader.read() inp = self.reader.read()
# Terminate if no input data # Terminate if no input data
if inp is None: if inp is None:
logger.debug("%s exiting..." % ("Service" if self.service else "Client")) logger.debug("%s exiting..." % self.myName())
self.doRun = False self.doRun = False
break break
# Add read data to the buffer # Add read data to the buffer
@ -91,7 +97,8 @@ class FaxParser(ThreadModule):
out = self.process() out = self.process()
# Keep processing while there is input to parse # Keep processing while there is input to parse
while out is not None: while out is not None:
self.writer.write(pickle.dumps(out)) if len(out)>0:
self.writer.write(pickle.dumps(out))
out = self.process() out = self.process()
def process(self): def process(self):
@ -104,7 +111,9 @@ class FaxParser(ThreadModule):
b = self.depth / 8 if self.depth>8 else 1 b = self.depth / 8 if self.depth>8 else 1
w = self.width w = self.width
if len(self.data)>=w*b: if len(self.data)>=w*b:
#logger.debug("LINE %d: Got %d/%d pixels" % (self.line, w, len(self.data)/b)) #logger.debug("%s got line %d of %d/%d pixels" % (
# self.myName(), self.line, w, len(self.data)/b
#))
# Advance scanline # Advance scanline
self.line = self.line + 1 self.line = self.line + 1
# If running as a service... # If running as a service...
@ -114,6 +123,8 @@ class FaxParser(ThreadModule):
# Close once the last scanline reached # Close once the last scanline reached
if self.line>=self.height: if self.line>=self.height:
self.closeFile() self.closeFile()
# Empty result
out = {}
else: else:
# Compose result # Compose result
out = { out = {
@ -136,8 +147,7 @@ class FaxParser(ThreadModule):
# Remove parsed data # Remove parsed data
del self.data[0:w*b] del self.data[0:w*b]
# Parse bitmap (BMP) file header starting with 'BM' or debug msgs else:
elif len(self.data)>=54+4*256:
# Search for the leading 'BM' or ' [' # Search for the leading 'BM' or ' ['
w = self.data.find(b'BM') w = self.data.find(b'BM')
d = self.data.find(b' [') d = self.data.find(b' [')
@ -156,13 +166,13 @@ class FaxParser(ThreadModule):
# Remove parsed data # Remove parsed data
del self.data[0:w+1] del self.data[0:w+1]
# Log message # Log message
logger.debug("%s%s says [%s]" % ( logger.debug("%s says [%s]" % (self.myName(), msg))
("Service" if self.service else "Client"), # If running as a service...
((" at %d" % (self.frequency // 1000)) if self.frequency>0 else ""), if self.service:
msg # Empty result
)) out = {}
# If not running as a service, compose result else:
if not self.service: # Compose result
out = { out = {
"mode": "Fax", "mode": "Fax",
"message": msg, "message": msg,
@ -194,14 +204,16 @@ class FaxParser(ThreadModule):
timeStamp = datetime.utcnow().strftime("%H:%M:%S") timeStamp = datetime.utcnow().strftime("%H:%M:%S")
fileName = Storage().makeFileName("FAX-{0}", self.frequency) fileName = Storage().makeFileName("FAX-{0}", self.frequency)
logger.debug("%s receiving %dx%d %s frame as '%s'." % ( logger.debug("%s receiving %dx%d %s frame as '%s'." % (
("Service" if self.service else "Client"), self.myName(), self.width, self.height,
self.width, self.height, modeName, fileName modeName, fileName
)) ))
# If running as a service... # If running as a service...
if self.service: if self.service:
# Create a new image file and write BMP header # Create a new image file and write BMP header
self.newFile(fileName) self.newFile(fileName)
self.writeFile(self.data[0:headerSize]) self.writeFile(self.data[0:headerSize])
# Empty result
out = {}
else: else:
# Compose result # Compose result
out = { out = {
@ -218,7 +230,7 @@ class FaxParser(ThreadModule):
del self.data[0:headerSize] del self.data[0:headerSize]
except Exception as exptn: except Exception as exptn:
logger.debug("Exception parsing: %s" % str(exptn)) logger.debug("%s: Exception parsing: %s" % (self.myName(), str(exptn)))
# Return parsed result or None if no result yet # Return parsed result or None if no result yet
return out return out

View File

@ -117,15 +117,21 @@ class SstvParser(ThreadModule):
def setDialFrequency(self, frequency: int) -> None: def setDialFrequency(self, frequency: int) -> None:
self.frequency = frequency self.frequency = frequency
def myName(self):
return "%s%s" % (
"Service" if self.service else "Client",
" at %dkHz" % (self.frequency // 1000) if self.frequency>0 else ""
)
def run(self): def run(self):
logger.debug("%s starting..." % ("Service" if self.service else "Client")) logger.debug("%s starting..." % self.myName())
# Run while there is input data # Run while there is input data
while self.doRun: while self.doRun:
# Read input data # Read input data
inp = self.reader.read() inp = self.reader.read()
# Terminate if no input data # Terminate if no input data
if inp is None: if inp is None:
logger.debug("%s exiting..." % ("Service" if self.service else "Client")) logger.debug("%s exiting..." % self.myName())
self.doRun = False self.doRun = False
break break
# Add read data to the buffer # Add read data to the buffer
@ -134,7 +140,8 @@ class SstvParser(ThreadModule):
out = self.process() out = self.process()
# Keep processing while there is input to parse # Keep processing while there is input to parse
while out is not None: while out is not None:
self.writer.write(pickle.dumps(out)) if len(out)>0:
self.writer.write(pickle.dumps(out))
out = self.process() out = self.process()
def process(self): def process(self):
@ -155,6 +162,8 @@ class SstvParser(ThreadModule):
# Close once the last scanline reached # Close once the last scanline reached
if self.line>=self.height: if self.line>=self.height:
self.closeFile() self.closeFile()
# Empty result
out = {}
else: else:
# Compose result # Compose result
out = { out = {
@ -173,8 +182,7 @@ class SstvParser(ThreadModule):
# Remove parsed data # Remove parsed data
del self.data[0:w] del self.data[0:w]
# Parse bitmap (BMP) file header starting with 'BM' or debug msgs else:
elif len(self.data)>=54:
# Search for the leading 'BM' or ' [' # Search for the leading 'BM' or ' ['
w = self.data.find(b'BM') w = self.data.find(b'BM')
d = self.data.find(b' [') d = self.data.find(b' [')
@ -193,13 +201,13 @@ class SstvParser(ThreadModule):
# Remove parsed data # Remove parsed data
del self.data[0:w+1] del self.data[0:w+1]
# Log message # Log message
logger.debug("%s%s says [%s]" % ( logger.debug("%s says [%s]" % (self.myName(), msg))
("Service" if self.service else "Client"), # If running as a service...
((" at %d" % (self.frequency // 1000)) if self.frequency>0 else ""), if self.service:
msg # Empty result
)) out = {}
# If not running as service, compose result else:
if not service: # Compose result
out = { out = {
"mode": "SSTV", "mode": "SSTV",
"message": msg, "message": msg,
@ -222,14 +230,16 @@ class SstvParser(ThreadModule):
timeStamp = datetime.utcnow().strftime("%H:%M:%S") timeStamp = datetime.utcnow().strftime("%H:%M:%S")
fileName = Storage().makeFileName("SSTV-{0}", self.frequency) fileName = Storage().makeFileName("SSTV-{0}", self.frequency)
logger.debug("%s receiving %dx%d %s frame as '%s'." % ( logger.debug("%s receiving %dx%d %s frame as '%s'." % (
("Service" if self.service else "Client"), self.myName(), self.width, self.height,
self.width, self.height, modeName, fileName modeName, fileName
)) ))
# If running as a service... # If running as a service...
if self.service: if self.service:
# Create a new image file and write BMP header # Create a new image file and write BMP header
self.newFile(fileName) self.newFile(fileName)
self.writeFile(self.data[0:54]) self.writeFile(self.data[0:54])
# Empty result
out = {}
else: else:
# Compose result # Compose result
out = { out = {
@ -245,7 +255,7 @@ class SstvParser(ThreadModule):
del self.data[0:54] del self.data[0:54]
except Exception as exptn: except Exception as exptn:
logger.debug("Exception parsing: %s" % str(exptn)) logger.debug("%s: Exception parsing: %s" % (self.myName(), str(exptn)))
# Return parsed result or None if no result yet # Return parsed result or None if no result yet
return out return out