Odoo Python套接字和Ajax JsonRPC LongPolling

时间:2018-08-09 12:42:34

标签: python odoo-10

Am使用odoo10销售点模块。 Odoo10安装在远程服务器上。我有一台运行ubuntu 16.04桌面操作系统的台式计算机,计算机上有一个 java应用程序,该应用程序从主称重桥获取数据,并通过 tcp端口1300 输出重量数据(数字)。 。端口和防火墙已打开。任何设备都可以监听该端口并获得重量。

在我的远程odoo实例中,我有一个python套接字,该套接字通过台式机中暴露的tcp端口并通过ajax获取权重,然后将权重传递给销售点ui以计算定价。

一切正常,但是ajax长轮询打开了许多连接,消耗了很多资源,这有时会使浏览器变慢。

Python套接字源文件:

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import http
from odoo.http import request, route
import json
import socket
from threading import *


class WeighBridgeController(http.Controller):

@http.route('/pos/weight/bridge', type='json', auth="user", website="True")
def process_weight_data(self, **kw):
    input_data = kw.get('input_data')
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('197.237.228.190', 5866)
    print 'connecting to %s port %s' % server_address
    while True:
        try:
            sock.connect(server_address)
            break
        except socket.error as e:
            print 'error while connecting "%s"' % e
    try:
        message = 'CLIENT DATA'
        print 'sending...'
        sock.send(message)
        data = ''
        while True:
            data = sock.recv(2048)
            print 'received at new "%s"' % filter(str.isdigit, data)
            return {"ibos_weight": filter(str.isdigit, data)}
    except socket.gaierror:
        print 'Server cannot be reached!'
    finally:
        sock.close()

Javascript(backbonejs)ajax源文件:

var IbosWeightReaderPopupWidget = IbosPopupWidget.extend({
    template: 'IbosWeightReaderPopupWidget',
    show: function (options) {
        options = options || {};
        this._super(options);

        this.renderElement();
        setInterval(function () {
                  ajax.jsonRpc('/pos/weight/bridge','process_weight_data',{'input_data':0}).then(function (data) {
                    var weightage_bridge=0;
                      weightage_bridge=data['ibos_weight'];
                      document.getElementById('wrdr').value = weightage_bridge;
                    console.log('Ajax Json RPC Weight POPUP: ' + weightage_bridge);
                },function (err) {
                    console.log('err: ' + err.toString());
                });
        },1)
        //this.$('input,textarea').val(weight_value);
        this.$('input,textarea').focus();
    },
    click_confirm: function () {
        var value = this.$('input,textarea').val();
        this.gui.close_popup();
        if (this.options.confirm) {
            this.options.confirm.call(this, value);
        }
    },
});
gui.define_popup({name: 'ibosweightreader', widget: IbosWeightReaderPopupWidget});

如何使ajax轮询使用单个连接并防止其过度使用浏览器资源?

0 个答案:

没有答案