diff --git a/addons/hw_proxy/controllers/main.py b/addons/hw_proxy/controllers/main.py index 9b8e87bb4ac..1c14cb36126 100644 --- a/addons/hw_proxy/controllers/main.py +++ b/addons/hw_proxy/controllers/main.py @@ -20,7 +20,6 @@ class Proxy(http.Controller): def __init__(self): self.scale = 'closed' self.scale_weight = 0.0 - pass def connected_usb_devices(self,devices): connected = [] diff --git a/addons/hw_scanner/controllers/main.py b/addons/hw_scanner/controllers/main.py index 82e940cc5c3..ec56a733671 100644 --- a/addons/hw_scanner/controllers/main.py +++ b/addons/hw_scanner/controllers/main.py @@ -86,11 +86,11 @@ class ScannerDriver(hw_proxy.Proxy): else: return None - @http.route('/hw_proxy/is_scanner_connected', type='http', auth='admin') + @http.route('/hw_proxy/is_scanner_connected', type='json', auth='admin') def is_scanner_connected(self): return self.get_device() != None - @http.route('/hw_proxy/scanner', type='http', auth='admin') + @http.route('/hw_proxy/scanner', type='json', auth='admin') def scanner(self): device = self.get_device() barcode = [] @@ -100,14 +100,14 @@ class ScannerDriver(hw_proxy.Proxy): else: device.grab() while True: - r,w,x = select([device],[],[],10) + r,w,x = select([device],[],[],5) if len(r) == 0: # timeout device.ungrab() return '' for event in device.read(): if event.type == ecodes.EV_KEY: if event.value == 1: # keydown events - print categorize(event) + #print categorize(event) if event.code in self.keymap: if shift: barcode.append(self.keymap[event.code][1]) @@ -117,7 +117,8 @@ class ScannerDriver(hw_proxy.Proxy): shift = True elif event.code == 28: # ENTER device.ungrab() - return ''.join(barcode); + barcode = ''.join(barcode) + return barcode elif event.value == 0: #keyup events if event.code == 42 or event.code == 54: # LEFT SHIFT shift = False diff --git a/addons/point_of_sale/static/src/js/devices.js b/addons/point_of_sale/static/src/js/devices.js index a443c824087..379d368c875 100644 --- a/addons/point_of_sale/static/src/js/devices.js +++ b/addons/point_of_sale/static/src/js/devices.js @@ -106,18 +106,21 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal this.connection = new instance.web.Session(undefined,url); this.connection.session_id = _.uniqueId('posproxy'); + this.connected = new $.Deferred(); + this.status = 'disconnected'; this.test_connection(); window.proxy = this; }, close: function(){ this.connection.destroy(); + this.status = 'disconnected'; }, message : function(name,params){ var callbacks = this.notifications[name] || []; for(var i = 0; i < callbacks.length; i++){ callbacks[i](params); } - if(this.connected){ + if(this.status !== 'disconnected'){ return this.connection.rpc('/hw_proxy/' + name, params || {}); }else{ return (new $.Deferred()).reject(); @@ -125,9 +128,13 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal }, test_connection: function(){ var self = this; - this.connected = true; - return this.message('test_connection').fail(function(){ - self.connected = false; + this.status = 'connecting'; + return this.message('test_connection').then(function(){ + self.status = 'connected'; + self.connected.resolve(); + },function(){ + self.status = 'disconnected'; + self.connected.reject(); console.error('Could not connect to the Proxy'); }); }, @@ -331,7 +338,6 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal * } */ print_receipt: function(receipt){ - console.log('PRINT RECEIPT:', receipt); return this.message('print_receipt',{receipt: receipt}); }, @@ -360,6 +366,7 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal init: function(attributes){ this.pos = attributes.pos; this.action_callback = {}; + this.proxy = attributes.proxy; this.action_callback_stack = []; @@ -516,16 +523,20 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal return parse_result; }, - scan: function(type,code){ - if (type === 'ean13'){ + scan: function(code){ + if(code.length < 3){ + return; + }else if(code.length === 13 && /^\d+$/.test(code)){ var parse_result = this.parse_ean(code); - }else if(type === 'reference'){ + }else if(this.pos.db.get_product_by_reference(code)){ var parse_result = { encoding: 'reference', type: 'unit', code: code, prefix: '', }; + }else{ + return; } if (parse_result.type === 'error') { //most likely a checksum error, raise warning @@ -541,12 +552,6 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal } }, - on_reference: function(code){ - if(this.action_callback['reference']){ - this.action_callback['reference'](code); - } - }, - // starts catching keyboard events and tries to interpret codebar // calling the callbacks when needed. connect: function(){ @@ -583,11 +588,7 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal // Internal Ref 5449 vs EAN13 5449000... timeout = setTimeout(function(){ - if(code.length === 13 && onlynumbers){ - self.scan('ean13',code); - }else if(code.length >= 3 && self.pos.db.get_product_by_reference(code)){ - self.scan('reference',code); - } + self.scan(code); code = ""; onlynumbers = true; },100); @@ -599,6 +600,27 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal // stops catching keyboard events disconnect: function(){ $('body').off('keypress', this.handler) + this.remote_scanning = false; + }, + connect_to_proxy: function(){ + var self = this; + this.remote_scanning = true; + + this.proxy.connected.then(function waitforbarcode(){ + if(!self.remote_scanning){ + return; + } + return self.proxy.connection.rpc('/hw_proxy/scanner',{}).then(function(barcode){ + if(!self.remote_scanning){ + return; + } + self.scan(barcode); + waitforbarcode(); + }, + function(){ + setTimeout(waitforbarcode,5000); + }); + }); }, }); diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 7ac2f42212e..e9940535d42 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -21,8 +21,9 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal this.session = session; this.flush_mutex = new $.Mutex(); // used to make sure the orders are sent to the server once at time - this.barcode_reader = new module.BarcodeReader({'pos': this}); // used to read barcodes this.proxy = new module.ProxyDevice(); // used to communicate to the hardware devices via a local proxy + this.barcode_reader = new module.BarcodeReader({'pos': this, proxy:this.proxy}); // used to read barcodes + this.barcode_reader.connect_to_proxy(); this.proxy_queue = new module.JobQueue(); // used to prevent parallels communications to the proxy this.db = new module.PosDB(); // a local database used to search trough products and categories & store pending orders this.debug = jQuery.deparam(jQuery.param.querystring()).debug !== undefined; //debug mode