-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] web_widget_remote_measure: add tcp direct connection
TT47128
- Loading branch information
1 parent
411eef2
commit 6a01615
Showing
5 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters