Skip to content

Commit

Permalink
Merge pull request #1002 from frogonwheels/duktape-metric-age
Browse files Browse the repository at this point in the history
Duktape - Add IsStale, IsFresh and Age to OvmsMetrics object
  • Loading branch information
dexterbg authored Apr 26, 2024
2 parents ab36d0f + bbd8cba commit 125e084
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/source/userguide/scripting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,15 @@ OvmsMetrics
- ``bool = OvmsMetrics.HasValue(metricname)``
Returns whether the specified metric has a defined value.
Returns undefined if metric is un-registered.
- ``bool = OvmsMetrics.IsStale(metricname)``
Returns whether the specified metric is "stale" (has been marked stale or has not been set within the staleness period).
Returns undefined if metric is un-registered.
- ``bool = OvmsMetrics.IsFresh(metricname)``
Returns whether the specified metric is "fresh" (defined, set since reboot, and not stale).
Returns undefined if metric is un-registered.
- ``num = OvmsMetrics.Age(metricname)``
Returns the age in (monotonic) seconds of the specified metric.
Returns undefined if metric is un-registered.
- ``str = OvmsMetrics.Value(metricname [,unitcode] [,decode])``
Returns the typed value (default) or string representation (with ``decode`` = false)
of the metric value optionally converted to the specified unit.
Expand Down
6 changes: 5 additions & 1 deletion vehicle/OVMS.V3/changes.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Open Vehicle Monitor System v3 - Change log

????-??-?? ??? ??????? OTA release

- Duktape support for Metric Age / Stale
New Duktape methods
OvmsMetrics.IsStale
OmvsMetrics.IsFresh
OvmsMetrics.Age
2024-03-23 MB 3.3.004 OTA release
- MG EV Added support for MG5 (2020 - 2023) Short Range
- MG EV Added support for MG ZS EV (2023 - ) and MG5 (2020 - 2023) Long Range
Expand Down
36 changes: 36 additions & 0 deletions vehicle/OVMS.V3/main/ovms_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,39 @@ static duk_ret_t DukOvmsMetricHasValue(duk_context *ctx)
return 1;
}

static duk_ret_t DukOvmsMetricIsStale(duk_context *ctx)
{
DukContext dc(ctx);
const char *mn = duk_to_string(ctx,0);
OvmsMetric *m = MyMetrics.Find(mn);
if (!m)
return 0;
dc.Push(m->IsStale());
return 1;
}

static duk_ret_t DukOvmsMetricIsFresh(duk_context *ctx)
{
DukContext dc(ctx);
const char *mn = duk_to_string(ctx,0);
OvmsMetric *m = MyMetrics.Find(mn);
if (!m)
return 0;
dc.Push(m->IsFresh());
return 1;
}

static duk_ret_t DukOvmsMetricAge(duk_context *ctx)
{
DukContext dc(ctx);
const char *mn = duk_to_string(ctx,0);
OvmsMetric *m = MyMetrics.Find(mn);
if (!m)
return 0;
dc.Push(m->Age());
return 1;
}

static duk_ret_t DukOvmsMetricValue(duk_context *ctx)
{
DukContext dc(ctx);
Expand Down Expand Up @@ -1078,6 +1111,9 @@ OvmsMetrics::OvmsMetrics()
ESP_LOGI(TAG, "Expanding DUKTAPE javascript engine");
DuktapeObjectRegistration* dto = new DuktapeObjectRegistration("OvmsMetrics");
dto->RegisterDuktapeFunction(DukOvmsMetricHasValue, 1, "HasValue");
dto->RegisterDuktapeFunction(DukOvmsMetricIsStale, 1, "IsStale");
dto->RegisterDuktapeFunction(DukOvmsMetricIsFresh, 1, "IsFresh");
dto->RegisterDuktapeFunction(DukOvmsMetricAge, 1, "Age");
dto->RegisterDuktapeFunction(DukOvmsMetricValue, 3, "Value");
dto->RegisterDuktapeFunction(DukOvmsMetricJSON, 1, "AsJSON");
dto->RegisterDuktapeFunction(DukOvmsMetricFloat, 2, "AsFloat");
Expand Down

0 comments on commit 125e084

Please sign in to comment.