|
@@ -1,101 +1,236 @@
|
|
|
|
|
|
(function () {
|
|
|
- if (!window.openerp) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!openerp.print_engine) {
|
|
|
- openerp.print_engine = {};
|
|
|
- }
|
|
|
+ var instance = openerp
|
|
|
+ openerp.print_engine = {}
|
|
|
|
|
|
- if (!openerp.print_engine.sockets) {
|
|
|
- openerp.print_engine.sockets = [];
|
|
|
- }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ 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 exception
|
|
|
+ }
|
|
|
+ },
|
|
|
+ reconnect: function () {
|
|
|
+ this.state = 'reconnecting';
|
|
|
+ var self = this;
|
|
|
+
|
|
|
+ if (this.attemps === 3) {
|
|
|
+ this.state = 'offline';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ setTimeout(function () {
|
|
|
+ self.connect();
|
|
|
+
|
|
|
+ self.attemps = self.attemps + 1;
|
|
|
+ }, 3000);
|
|
|
+ },
|
|
|
+ handleOpen: function (e) {
|
|
|
+ this.state = '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 (ok) {
|
|
|
+ console.log(ok);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ handleError: function (e) {
|
|
|
+ // Ignore 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',
|
|
|
+ data: data
|
|
|
+ }
|
|
|
+
|
|
|
+ $.post(url, json).done(function (response) {
|
|
|
+ defer.resolve(response);
|
|
|
+ }).fail(function (error) {
|
|
|
+ defer.reject(error);
|
|
|
+ });
|
|
|
+
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ discoveryPrinters: function () {
|
|
|
+ var commands = {
|
|
|
+ action: 'list'
|
|
|
+ }
|
|
|
|
|
|
- $.get('/print_engine/sockets', function (data) {
|
|
|
- for (var i = 0; i < data.sockets.length; i++) {
|
|
|
- connect(data.sockets[i]);
|
|
|
+ this.sendToSocket(commands);
|
|
|
+ },
|
|
|
+ print(printer, data) {
|
|
|
+ if (this.state !== 'online') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!printer) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!data) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var commands = {
|
|
|
+ action: 'print',
|
|
|
+ printer: printer,
|
|
|
+ data: data
|
|
|
+ }
|
|
|
+
|
|
|
+ this.sendToSocket(commands)
|
|
|
}
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param {*} options
|
|
|
*/
|
|
|
- var buildUrl = function(options) {
|
|
|
- return options.protocol + '://' + options.host + ':' + options.port + options.path
|
|
|
- }
|
|
|
+ instance.print_engine.PrinterTopWidget = instance.web.Widget.extend({
|
|
|
+ template: 'printEngine.PrinterTopWidget',
|
|
|
+ init: function (parent) {
|
|
|
+ this._super(parent)
|
|
|
+ },
|
|
|
+ start: function () {
|
|
|
+ console.log('started');
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
/**
|
|
|
- * Connect to websocket
|
|
|
*
|
|
|
- * @param {*} socket
|
|
|
+ *
|
|
|
*/
|
|
|
- var connect = function (socket) {
|
|
|
- var url = buildUrl(socket);
|
|
|
- socket = new WebSocket(url);
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {*} e
|
|
|
- */
|
|
|
- socket.onopen = function (e) {
|
|
|
- openerp.print_engine.sockets.push({
|
|
|
- instance: this,
|
|
|
- status: 'online'
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {*} e
|
|
|
- */
|
|
|
- socket.onclose = function (e) {
|
|
|
- console.log()
|
|
|
+ instance.print_engine.ping = function () {
|
|
|
+ if (!instance.print_engine.socket_manager) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {*} e
|
|
|
- */
|
|
|
- socket.onerror = function (e) {
|
|
|
- console.log(e);
|
|
|
+ openerp.web.notification.do_warn('Atención', 'pong')
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ instance.print_engine.discovery_printers = function (element, action) {
|
|
|
+ if (!instance.print_engine.socket_manager) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {*} e
|
|
|
- */
|
|
|
- socket.onmessage = function (e) {
|
|
|
- console.log(e);
|
|
|
- }
|
|
|
+ instance.print_engine.socket_manager.discoveryPrinters();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * Send action to printer
|
|
|
*
|
|
|
- * @param {*} data
|
|
|
*/
|
|
|
- var sendToSocket = function (data) {
|
|
|
- data = JSON.stringify(data);
|
|
|
+ instance.print_engine.test_printer = function (element, action) {
|
|
|
+ if (!instance.print_engine.socket_manager) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- console.log(data);
|
|
|
+ instance.print_engine.socket_manager.test();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Update printers on server
|
|
|
*
|
|
|
- * @param {*} data
|
|
|
*/
|
|
|
- var updatePrinters = function (printers) {
|
|
|
- var data = {
|
|
|
- jsonrpc: '2.0',
|
|
|
- method: 'call',
|
|
|
- data: printers
|
|
|
+ instance.print_engine.print = function (element, action) {
|
|
|
+ if (!instance.print_engine.socket_manager) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- $.post('/print_engine/update', data, function (response) {
|
|
|
- console.log(response);
|
|
|
+ instance.print_engine.socket_manager.print();
|
|
|
+ }
|
|
|
+
|
|
|
+ instance.print_engine.socket_manager = new instance.print_engine.SocketManager()
|
|
|
+
|
|
|
+ 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(openerp.web && openerp.web.UserMenu) {
|
|
|
+ openerp.web.UserMenu.include({
|
|
|
+ do_update: function(){
|
|
|
+ var printer = new openerp.print_engine.PrinterTopWidget(this);
|
|
|
+ printer.appendTo($('.oe_systray'));
|
|
|
+
|
|
|
+ return this._super.apply(this, arguments);
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
-}).call(this)
|
|
|
+})()
|