From 4f589b3359fdda3361242969830cb0a5212ab913 Mon Sep 17 00:00:00 2001 From: Holger Bruch Date: Mon, 26 Aug 2024 16:58:06 +0200 Subject: [PATCH] fix: switch from fetch_minimum_timestamp to fetch_maximum_timestamp and fix fetch_minimum_timestamp --- templates/weather/weather.geojson | 2 +- thingsboard-json-enhancer | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/templates/weather/weather.geojson b/templates/weather/weather.geojson index 558b6d8..0a2b45e 100644 --- a/templates/weather/weather.geojson +++ b/templates/weather/weather.geojson @@ -10,7 +10,7 @@ "roadTemperatureC": {{ fetch_timeseries(id, 'act_temp_road_surf') }}, "precipitationType": {{ fetch_timeseries(id, 'precip_type') }}, "roadCondition": {{ fetch_timeseries(id, 'act_road_condition') }}, - "updatedAt": "{{ fetch_minimum_timestamp(id) }}" + "updatedAt": "{{ fetch_maximum_timestamp(id) }}" }, "geometry": { "type": "Point", diff --git a/thingsboard-json-enhancer b/thingsboard-json-enhancer index a802262..539db3c 100755 --- a/thingsboard-json-enhancer +++ b/thingsboard-json-enhancer @@ -71,7 +71,17 @@ def fetch_minimum_timestamp(id, keys = None): timestamp = ts[key][0]['ts']/1000 if min_timestamp is None or timestamp < min_timestamp: min_timestamp = timestamp - return time.strftime('%Y-%m-%dT%H:%M:%S%z', time.localtime(timestamp)) + return time.strftime('%Y-%m-%dT%H:%M:%S%z', time.localtime(min_timestamp)) + +def fetch_maximum_timestamp(id, keys = None): + ts = _fetch_timeseries(id) + max_timestamp = None + for key in ts.keys(): + if keys is None or key in keys: + timestamp = ts[key][0]['ts']/1000 + if max_timestamp is None or timestamp > max_timestamp: + max_timestamp = timestamp + return time.strftime('%Y-%m-%dT%H:%M:%S%z', time.localtime(max_timestamp)) def fetch_attribute(id, key): return _get_attribute(_fetch_attributes(id), key) @@ -96,6 +106,7 @@ def transform_file(input_file, output_file): template.globals['fetch_timeseries'] = fetch_timeseries_value template.globals['fetch_timeseries_map'] = fetch_timeseries_map template.globals['fetch_device_timeseries'] = fetch_device_timeseries_value + template.globals['fetch_maximum_timestamp'] = fetch_maximum_timestamp template.globals['fetch_minimum_timestamp'] = fetch_minimum_timestamp template.globals['now'] = now_iso