Skip to content

Commit

Permalink
[IMP] web_widget_remote_measure: add tcp direct connection
Browse files Browse the repository at this point in the history
TT47128
  • Loading branch information
chienandalu committed Apr 24, 2024
1 parent 411eef2 commit 1b33bfd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions web_widget_remote_measure/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import controllers
from . import models
1 change: 1 addition & 0 deletions web_widget_remote_measure/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
42 changes: 42 additions & 0 deletions web_widget_remote_measure/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import socket

from odoo.http import Controller, request, route


class RemoteDeviceTcpConnection(Controller):
"""Connect directly to a remote device"""

def _get_weight(self, data, host, port, time_out=1):
"""Direct tcp connection to the remote device"""
response = None
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as device:
device.settimeout(time_out)
device.connect((host, port))
try:
if data:
device.sendall(data)
# Get in one shot. The info won't be longer than a few bytes
response = device.recv(64)
except Exception as e:
raise (e)
finally:
device.close()
return response

@route(
"/remote_measure_device/<int:device>", type="json", auth="user", sitemap=False
)
def request_weight(self, device=None, command=None, **kw):
"""Meant be called from the remote scale widget js code"""
device = request.env["remote.measure.device"].browse(device)
command = command or b""
kw.get("timeout", 1)
response = b""
host, port = device.host.split(":")
try:
response = self._get_weight(command, host, int(port))
except socket.timeout:
response = b"timeout"
return response
1 change: 1 addition & 0 deletions web_widget_remote_measure/models/remote_measure_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RemoteMeasureDevice(models.Model):
selection=[
("websockets", "Web Sockets"),
("webservices", "Web Services"),
("tcp", "Direct connection"),
],
required=True,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ export const RemoteMeasureMixin = {
_connect_to_webservices() {
return;
},
/**
* Connect to the local controller, which makes the direct connection to the
* scale.
*/
async _connect_to_tcp() {
var icon = "fa-thermometer-empty";
var stream_success_counter = 20;
this._unstableMeasure();
// Don't keep going forever
for (let attemps_left = 1000; attemps_left > 0; attemps_left--) {
const data = await this._rpc({
route: `/remote_measure_device/${this.remote_device_data.id}` || [],
});
if (!data) {
continue;
}
const processed_data = this[`_proccess_msg_${this.protocol}`](data);
if (processed_data.stable) {
this._stableMeasure();
} else {
this._unstableMeasure();
stream_success_counter = 20;
}
if (processed_data.stable && stream_success_counter <= 0) {
this._stableMeasure();
this._awaitingMeasure();
this._recordMeasure();
break;
}
if (stream_success_counter) {
--stream_success_counter;
}
icon = this._nextStateIcon(icon);
this.amount = processed_data.value;
this._setMeasure();
}
},
/**
* Convert the measured units to the units expecte by the record if different
* @param {Number} amount
Expand Down

0 comments on commit 1b33bfd

Please sign in to comment.