112 lines
2.9 KiB
Python
112 lines
2.9 KiB
Python
'''
|
|
Created on 19 mar 2020
|
|
|
|
@author: Emanuele Trabattoni
|
|
'''
|
|
|
|
import queue, time, socket, logging
|
|
from driver.tsc_commands import TSC_COMMANDS, TSC_STATUS, LINE_SEP
|
|
from driver.notifier import URL_ERR, URL_EVENT, URL_RESPONSE
|
|
|
|
SOCK_TIMEOUT = 2
|
|
|
|
|
|
def tsc_printer(q_in: queue.Queue, q_out: queue.Queue, q_cmd: queue.Queue, ip: str, port: int, logger: logging.getLogger):
|
|
prt = None
|
|
|
|
def read_until(term):
|
|
buf = ''
|
|
|
|
c=prt.recv(1)
|
|
while c is not term:
|
|
buf = buf+c
|
|
c = prt.recv(1)
|
|
return buf
|
|
|
|
def list_files():
|
|
nonlocal prt
|
|
prt.sendall(TSC_COMMANDS['LIST']+LINE_SEP)
|
|
time.sleep(0.1)
|
|
retval = read_until(0x1A).split(0x0D)
|
|
q_out.put({'url':URL_RESPONSE,
|
|
'msg': retval})
|
|
|
|
pass
|
|
|
|
def send_file(name=None, content=None, ftype=None):
|
|
nonlocal prt
|
|
if ftype == 'batch':
|
|
to_send = TSC_COMMANDS['DOWNLOAD'].format(location='F,', name=f'"{name}"', content=LINE_SEP+content.join(LINE_SEP)+LINE_SEP)
|
|
elif ftype == 'data':
|
|
data = content.join(LINE_SEP)+LINE_SEP
|
|
to_send = TSC_COMMANDS['DOWNLOAD'].format(name=f'"{name}"',size=','+len(data),content=data)
|
|
prt.sendall(to_send)
|
|
pass
|
|
|
|
def delete_file(name=None):
|
|
nonlocal prt
|
|
if ".BAS" in name:
|
|
to_send = TSC_COMMANDS['REMOVE'].format(location='F,', name=f'"{name}"')
|
|
else:
|
|
to_send = TSC_COMMANDS['REMOVE'].format(name=f'"{name}"')
|
|
prt.sendall(to_send+LINE_SEP)
|
|
pass
|
|
|
|
def move_file(name=None):
|
|
#per default muove i file dalla RAM alla FLASH
|
|
nonlocal prt
|
|
to_send = TSC_COMMANDS['MOVE'].format(name=f'"{name}"')
|
|
prt.sendall(to_send+LINE_SEP)
|
|
pass
|
|
|
|
def run_batch(name=None):
|
|
nonlocal prt
|
|
to_send = TSC_COMMANDS['RUN'].format(name=f'"{name}"')
|
|
prt.sendall(to_send+LINE_SEP)
|
|
pass
|
|
|
|
def get_status():
|
|
nonlocal prt
|
|
prt.sendall(0x1B+TSC_COMMANDS['STATUS'])
|
|
time.sleep(0.1)
|
|
return TSC_STATUS[prt.recv(1)]
|
|
|
|
|
|
while True: # connetti mantieni vivo il socket
|
|
try:
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as prt:
|
|
prt.create_connection(address=(ip, port), timeout=SOCK_TIMEOUT)
|
|
while True:
|
|
itm = q_in.get(block=True)
|
|
cmd = itm['name']
|
|
par = itm['parameters']
|
|
if cmd == 'DOWNLOAD':
|
|
send_file(**par)
|
|
pass
|
|
elif cmd == 'REMOVE':
|
|
delete_file(**par)
|
|
pass
|
|
elif cmd == 'MOVE':
|
|
move_file(**par)
|
|
pass
|
|
elif cmd == 'LIST':
|
|
get_status()
|
|
pass
|
|
elif cmd == 'RUN':
|
|
pass
|
|
elif cmd == 'STATUS':
|
|
pass
|
|
else:
|
|
logger.error("Comando stampante non riconosciuto")
|
|
except socket.timeout as st:
|
|
logger.error(f"Stampante {__name__} irraggiungibile:{st}")
|
|
prt.close()
|
|
del prt
|
|
except socket.error as se:
|
|
logger.error(f"Errore generico stamoante {__name__}: {se}")
|
|
prt.close()
|
|
del prt
|
|
except Exception as e:
|
|
logger.error("Errore generale inizializzando la stampante: {}".format(e))
|
|
|
|
pass |