(function () { var instance = openerp openerp.print_engine = {} /** * */ instance.print_engine.SocketManager = instance.web.Class.extend({ init: function () { this.socket = null; this.state = 'offline'; this.attemps = 0; this.start(); }, start: function () { var self = this; this.getSockets().then(function () { self.connect(); }); }, getSockets: function () { var defer = $.Deferred(); var self = this; $.get('/print_engine/sockets').done(function (data) { self.sockets = data.sockets; defer.resolve(data.sockets); }).fail(function (error){ defer.reject(error); }); return defer; }, urlize: function (info) { return info.protocol + '://' + info.host + ':' + info.port + info.path; }, connect: function () { if (this.sockets.length === 0) { return } var self = this; var url = this.urlize(this.sockets[0]); if (this.socket) { this.socket = null; } try { this.socket = new WebSocket(url); this.socket.onopen = function (e) { self.handleOpen(e); }; this.socket.onclose = function (e) { self.handleClose(e); }; this.socket.onerror = function (e) { self.handleError(e); }; this.socket.onmessage = function (e) { self.handleMessage(e); }; } catch(e) { // Ignore this error } }, reconnect: function () { this.state = 'reconnecting'; $('#printer-status').removeClass(); $('#printer-status').addClass('printer-status-reconnect'); var self = this; if (this.attemps === 3) { this.state = 'offline'; $('#printer-status').removeClass(); $('#printer-status').addClass('printer-status-offline'); return; } setTimeout(function () { self.connect(); self.attemps = self.attemps + 1; }, 3000); }, handleOpen: function (e) { this.state = 'online'; $('#printer-status').removeClass(); $('#printer-status').addClass('printer-status-online'); }, handleClose: function (e) { this.reconnect(); }, handleMessage: function (e) { var obj = JSON.parse(e.data); if (obj.printers) { this.sendToServer('/print_engine/update', e.data).then(function () { instance.client.action_manager.inner_widget.views['form'].controller.reload(); instance.web.notification.do_notify('Información', 'Impresoras actualizadas con éxito'); }); } }, handleError: function (e) { // Ignore this error }, sendToSocket: function (data) { if (this.state !== 'online') { return; } if (!data) { return; } this.socket.send(JSON.stringify(data)); }, sendToServer: function (url, data) { var defer = $.Deferred(); var json = { jsonrpc: '2.0', method: 'call', params: { data: data } } $.ajax({ type: 'POST', url: url, dataType: 'json', data: JSON.stringify(json), beforeSend: function(xhr) { xhr.setRequestHeader('Content-Type', 'application/json'); }, success: function (ok) { defer.resolve(ok); }, }); return defer; }, discoveryPrinters: function () { var commands = { action: 'list' } this.sendToSocket(commands); }, print(printer, data) { if (this.state !== 'online') { instance.web.notification.do_warn('Atención', 'La impresora está fuera de servicio'); return; } if (!printer) { instance.web.notification.do_warn('Atención', 'La impresora no se encuentra'); return; } if (!data) { instance.web.notification.do_warn('Atención', 'No se encuentran datos para imprimir'); return; } var commands = { action: 'print', printer: printer, data: data } this.sendToSocket(commands) } }); /** * */ instance.print_engine.PrinterTopWidget = instance.web.Widget.extend({ template: 'printEngine.PrinterTopWidget', init: function (parent) { this._super(parent) }, start: function () { this.$el.click(this, this.selectDefaultPrinter); }, selectDefaultPrinter: function (e) { e.preventDefault(); var self = e.data; instance.client.action_manager.do_action({ context: self.session.user_context, name: 'Impresora por defecto', type: 'ir.actions.act_window', res_model: 'print.engine.printer', views: [[false, 'list'], [false, 'form']], domain : [['socket_id.user_id', '=', self.session.user_context.uid]], target: 'new', flags: { action_buttons: true } }) } }) /** * * */ instance.print_engine.ping = function () { if (!instance.print_engine.socket_manager) { return; } instance.web.notification.do_notify('Información', 'Pong') } /** * */ instance.print_engine.discovery_printers = function (element, action) { if (!instance.print_engine.socket_manager) { return; } if(!confirm('Ésta acción quitará la impresora por defecto')) { return; } instance.print_engine.socket_manager.discoveryPrinters(); } /** * */ instance.print_engine.test_printer = function (element, action) { if (!instance.print_engine.socket_manager) { return; } instance.print_engine.socket_manager.test(); } /** * */ instance.print_engine.print = function (element, action) { if (!instance.print_engine.socket_manager) { return; } instance.print_engine.socket_manager.print(); } instance.web.client_actions.add('print_engine.ping', 'instance.print_engine.ping') instance.web.client_actions.add('print_engine.discovery_printers', 'instance.print_engine.discovery_printers') instance.web.client_actions.add('print_engine.test_printer', 'instance.print_engine.test_printer') if (instance.web && instance.web.UserMenu) { instance.web.UserMenu.include({ do_update: function(){ var printer = new openerp.print_engine.PrinterTopWidget(this); printer.appendTo($('.oe_systray')); instance.print_engine.socket_manager = new instance.print_engine.SocketManager() return this._super.apply(this, arguments); } }); } })()