summaryrefslogtreecommitdiff
path: root/addons/pos_epson_printer/static/src/js/printers.js
blob: c396c611fd7bc3fc5f3d799356b76016ae63b52d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

odoo.define('pos_epson_printer.Printer', function (require) {
"use strict";

const { Gui } = require('point_of_sale.Gui');
var core = require('web.core');
var PrinterMixin = require('point_of_sale.Printer').PrinterMixin;

var _t = core._t;

var EpsonPrinter = core.Class.extend(PrinterMixin, {
    init: function (ip, pos) {
        PrinterMixin.init.call(this, arguments);
        this.pos = pos;
        this.ePOSDevice = new epson.ePOSDevice();
        var port = window.location.protocol === 'http:' ? '8008' : '8043';
        this.ePOSDevice.connect(ip, port, this.callback_connect.bind(this), {eposprint: true});
    },

    callback_connect: function (resultConnect) {
        var deviceId = 'local_printer';
        var options = {'crypto' : false, 'buffer' : false};
        if ((resultConnect == 'OK') || (resultConnect == 'SSL_CONNECT_OK')) {
            this.ePOSDevice.createDevice(deviceId, this.ePOSDevice.DEVICE_TYPE_PRINTER, options, this.callback_createDevice.bind(this));
        } else {
            Gui.showPopup('ErrorPopup', {
                'title': _t('Connection to the printer failed'),
                'body':  _t('Please check if the printer is still connected, if the configured IP address is correct and if your printer supports the ePOS protocol.'),
            });
        }
    },

    callback_createDevice: function (deviceObj, errorCode) {
        if (deviceObj === null) {
            Gui.showPopup('ErrorPopup', {
                'title': _t('Connection to the printer failed'),
                'body':  _t('Please check if the printer is still connected. Error code: ') + errorCode,
            });
            return;
        }
        this.printer = deviceObj;
        this.printer.onreceive = function(response){
            if (!response.success) {
                Gui.showPopup('ErrorPopup', {
                    'title': _t('Epson ePOS Error'),
                    'body':  _t('An error happened while sending data to the printer. Error code: ') + response.code,
                });
            }
        };
    },

    /**
     * Create the print request for webPRNT from a canvas
     * 
     * @override
     */
    process_canvas: function (canvas) {
        if (this.printer) {
            this.printer.addTextAlign(this.printer.ALIGN_CENTER);
            this.printer.addImage(canvas.getContext('2d'), 0, 0, canvas.width, canvas.height);
            this.printer.addCut();
        }
    },

    /**
     * @override
     */
    open_cashbox: function () {
        if (this.printer) {
            this.printer.addPulse();
            this.printer.send();
        }
    },

    /**
     * @override
     */
    send_printing_job: function () {
        if (this.printer) {
            this.printer.send();
            return {
                result: true
            };
        }
    },

    //--------------------------------------------------------------------------
    // Handlers
    //--------------------------------------------------------------------------

    /**
     * Not applicable to Epson ePOS
     * @override
     */
    _onIoTActionFail: function () {},
    _onIoTActionResult: function () {},
});

return EpsonPrinter;

});