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 May 24, 2024
1 parent c603beb commit b844018
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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 @@ -106,8 +106,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 +133,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 +233,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 +244,7 @@ export const RemoteMeasureMixin = {
*/
measure_stop() {
this._closeSocket();
this.stop = true;
this._awaitingMeasure();
this._recordMeasure();
},
Expand Down Expand Up @@ -402,6 +421,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 b844018

Please sign in to comment.