diff --git a/addons/hw_escpos/controllers/main.py b/addons/hw_escpos/controllers/main.py index 938fa064d52..57fba3a4191 100644 --- a/addons/hw_escpos/controllers/main.py +++ b/addons/hw_escpos/controllers/main.py @@ -3,6 +3,7 @@ import commands import logging import simplejson import os +import os.path import io import base64 import openerp @@ -11,6 +12,8 @@ import random import math import md5 import openerp.addons.hw_proxy.controllers.main as hw_proxy +import pickle +import re import subprocess import traceback from threading import Thread, Lock @@ -45,10 +48,58 @@ 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 supported_devices.device_list: + for device in self.supported_devices(): if usb.core.find(idVendor=device['vendor'], idProduct=device['product']) != None: connected.append(device) return connected @@ -83,8 +134,9 @@ class EscposDriver(Thread): printer.cashdraw(5) def set_status(self, status, message = None): + _logger.info(status+' : '+ (message or 'no message')) if status == self.status['status']: - if message != None and message != self.status['messages'][-1]: + if message != None and (len(self.status['messages']) == 0 or message != self.status['messages'][-1]): self.status['messages'].append(message) else: self.status['status'] = status @@ -307,7 +359,22 @@ class EscposProxy(hw_proxy.Proxy): driver.push_task('receipt',receipt) @http.route('/hw_proxy/print_xml_receipt', type='json', auth='none', cors='*') - def print_receipt(self, receipt): + def print_xml_receipt(self, receipt): _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_proxy/controllers/main.py b/addons/hw_proxy/controllers/main.py index 9e94e4817a4..ea9b45c1b35 100644 --- a/addons/hw_proxy/controllers/main.py +++ b/addons/hw_proxy/controllers/main.py @@ -97,6 +97,22 @@ class Proxy(http.Controller): device_name = device[device.find('ID')+2:] resp+= "
"+device_name+"
\n" resp += "\n" + resp += """ +

Add New Printer

+

+ Copy and paste your printer's device description in the form below. You can find + your printer's description in the device list above. If you find that your printer works + well, please send your printer's description to + support@openerp.com so that we can add it to the default list of supported devices. +

+
+ + +
+

Reset To Defaults

+

If the added devices cause problems, you can Reset the + device list to factory default. This operation cannot be undone.

+ """ resp += "\n\n\n" return request.make_response(resp,{