diff --git a/addons/hw_escpos/controllers/main.py b/addons/hw_escpos/controllers/main.py index 083e5cd5557..0b0f58027ab 100644 --- a/addons/hw_escpos/controllers/main.py +++ b/addons/hw_escpos/controllers/main.py @@ -52,60 +52,37 @@ class EscposDriver(Thread): self.lock = Lock() self.status = {'status':'connecting', 'messages':[]} - def supported_devices(self): - if not os.path.isfile('escpos_devices.pickle'): - return supported_devices.device_list - else: - try: - f = open('escpos_devices.pickle','r') - return pickle.load(f) - f.close() - except Exception as e: - self.set_status('error',str(e)) - return supported_devices.device_list - - def add_supported_device(self,device_string): - r = re.compile('[0-9A-Fa-f]{4}:[0-9A-Fa-f]{4}'); - match = r.search(device_string) - if match: - match = match.group().split(':') - vendor = int(match[0],16) - product = int(match[1],16) - name = device_string.split('ID') - if len(name) >= 2: - name = name[1] - else: - name = name[0] - _logger.info('ESC/POS: adding support for device: '+match[0]+':'+match[1]+' '+name) - - device_list = supported_devices.device_list[:] - if os.path.isfile('escpos_devices.pickle'): - try: - f = open('escpos_devices.pickle','r') - device_list = pickle.load(f) - f.close() - except Exception as e: - self.set_status('error',str(e)) - device_list.append({ - 'vendor': vendor, - 'product': product, - 'name': name, - }) - - try: - f = open('escpos_devices.pickle','w+') - f.seek(0) - pickle.dump(device_list,f) - f.close() - except Exception as e: - self.set_status('error',str(e)) - def connected_usb_devices(self): connected = [] - - for device in self.supported_devices(): - if usb.core.find(idVendor=device['vendor'], idProduct=device['product']) != None: - connected.append(device) + + # printers can either define bDeviceClass=7, or they can define one of + # their interfaces with bInterfaceClass=7. This class checks for both. + class FindUsbClass(object): + def __init__(self, usb_class): + self._class = usb_class + def __call__(self, device): + # first, let's check the device + if device.bDeviceClass == self._class: + return True + # transverse all devices and look through their interfaces to + # find a matching class + for cfg in device: + intf = usb.util.find_descriptor(cfg, bInterfaceClass=self._class) + + if intf is not None: + return True + + return False + + printers = usb.core.find(find_all=True, custom_match=FindUsbClass(7)) + + for printer in printers: + connected.append({ + 'vendor': printer.idVendor, + 'product': printer.idProduct, + 'name': usb.util.get_string(printer, 256, printer.iManufacturer) + " " + usb.util.get_string(printer, 256, printer.iProduct) + }) + return connected def lockedstart(self): @@ -377,18 +354,3 @@ class EscposProxy(hw_proxy.Proxy): _logger.info('ESC/POS: PRINT XML RECEIPT') driver.push_task('xml_receipt',receipt) - @http.route('/hw_proxy/escpos/add_supported_device', type='http', auth='none', cors='*') - def add_supported_device(self, device_string): - _logger.info('ESC/POS: ADDED NEW DEVICE:'+device_string) - driver.add_supported_device(device_string) - return "The device:\n"+device_string+"\n has been added to the list of supported devices.
Ok" - - @http.route('/hw_proxy/escpos/reset_supported_devices', type='http', auth='none', cors='*') - def reset_supported_devices(self): - try: - os.remove('escpos_devices.pickle') - except Exception as e: - pass - return 'The list of supported devices has been reset to factory defaults.
Ok' - - diff --git a/addons/hw_escpos/escpos/__init__.py b/addons/hw_escpos/escpos/__init__.py index 3fdeddee28b..22a5af61029 100644 --- a/addons/hw_escpos/escpos/__init__.py +++ b/addons/hw_escpos/escpos/__init__.py @@ -1 +1 @@ -__all__ = ["constants","escpos","exceptions","printer","supported_devices"] +__all__ = ["constants","escpos","exceptions","printer"] diff --git a/addons/hw_escpos/escpos/supported_devices.py b/addons/hw_escpos/escpos/supported_devices.py deleted file mode 100644 index bd08433de0e..00000000000 --- a/addons/hw_escpos/escpos/supported_devices.py +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/python - -# This is a list of esc/pos compatible usb printers. The vendor and product ids can be found by -# typing lsusb in a linux terminal, this will give you the ids in the form ID VENDOR:PRODUCT - -device_list = [ - { 'vendor' : 0x04b8, 'product' : 0x0e03, 'name' : 'Epson TM-T20' }, - { 'vendor' : 0x04b8, 'product' : 0x0202, 'name' : 'Epson TM-T70' }, - { 'vendor' : 0x04b8, 'product' : 0x0e15, 'name' : 'Epson TM-T20II' }, -] -