Skip to content

Commit

Permalink
[IMP] web_widget_remote_measure: non stop read
Browse files Browse the repository at this point in the history
TT49230
  • Loading branch information
chienandalu committed Jun 12, 2024
1 parent c603beb commit 3a7505d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions web_widget_remote_measure/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def _get_weight(self, data, host, port, time_out=1):
device.connect((host, port))
try:
if data:
if isinstance(data, str):
data = data.encode("utf-8")
device.sendall(data)
# Get in one shot. The info won't be longer than a few bytes
response = device.recv(64)
Expand Down
7 changes: 7 additions & 0 deletions web_widget_remote_measure/models/remote_measure_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ class RemoteMeasureDevice(models.Model):
required=True,
)
host = fields.Char(required=True)
instant_read = fields.Boolean(help="Read right on as the widget gets rendered")
non_stop_read = fields.Boolean(
help="Don't stop reading until the widget is disposed"
)
read_interval = fields.Integer(
help="(0 for no sleep between reads) Miliseconds to wait between device reads"
)
test_measure = fields.Float(default=0.0)
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,21 @@ export const RemoteMeasureMixin = {
_connect_to_webservices() {
return;
},
/**
* Send read params to the remote device
* @returns {Object}
*/
_read_from_device_tcp_params() {
return {command: false};
},
/**
* Process call
* @returns {Number}
*/
async _read_from_device_tcp() {
const data = await this._rpc({
route: `/remote_measure_device/${this.remote_device_data.id}` || [],
params: this._read_from_device_tcp_params(),
});
if (!data) {
return null;
Expand All @@ -106,8 +114,18 @@ export const RemoteMeasureMixin = {
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--) {
// Used to set the read interval if any
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
// Don't keep going forever unless non stop reading
for (
let attemps_left = this.remote_device_data.non_stop_read ? Infinity : 1000;
attemps_left > 0;
attemps_left--
) {
// Allow to break the loop manually
if (this.stop) {
break;
}
const processed_data = await this._read_from_device_tcp();
if (!processed_data) {
continue;
Expand All @@ -123,13 +141,20 @@ export const RemoteMeasureMixin = {
this._awaitingMeasure();
this._recordMeasure();
break;
} else if (this.remote_device_data.non_stop_read) {
stream_success_counter = 20;
this._recordMeasure();
}
if (stream_success_counter) {
--stream_success_counter;
}
icon = this._nextStateIcon(icon);
this.amount = processed_data.value;
this._setMeasure();
// Set sleep interval
if (this.remote_device_data.read_interval) {
await timer(this.remote_device_data.read_interval);
}
}
},
/**
Expand Down Expand Up @@ -216,6 +241,7 @@ export const RemoteMeasureMixin = {
* Request measure to remote device
*/
measure() {
this.stop = false;
this.$start_measure.addClass("d-none");
this.$stop_measure.removeClass("d-none");
this.$icon = this.$stop_measure.find("i");
Expand All @@ -226,6 +252,7 @@ export const RemoteMeasureMixin = {
*/
measure_stop() {
this._closeSocket();
this.stop = true;
this._awaitingMeasure();
this._recordMeasure();
},
Expand Down Expand Up @@ -402,6 +429,17 @@ export const RemoteMeasure = FieldFloat.extend(RemoteMeasureMixin, {
this.$el.prepend(this.$start_measure, this.$stop_measure);
return def;
},
/**
* Read right on if configured
* @override
*/
start() {
this._super(...arguments).then(() => {
if (this.remote_device_data.instant_read) {
this.measure();
}
});
},
/**
* Ensure that the socket is allways closed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<field name="connection_mode" />
</group>
</group>
<group>
<group string="Read behavior">
<field name="instant_read" />
<field name="non_stop_read" />
<field name="read_interval" />
</group>
</group>
</sheet>
</form>
</field>
Expand Down

0 comments on commit 3a7505d

Please sign in to comment.