From 780dc8fc7edc6fe656b3b7a9b6cb564657f487f7 Mon Sep 17 00:00:00 2001 From: Ksoy Date: Mon, 15 Mar 2021 15:48:15 +0800 Subject: [PATCH] allow user set the push interval as a callback function to get dynamic interval (#46) --- iottalkpy/dai.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/iottalkpy/dai.py b/iottalkpy/dai.py index bb63e64..d308ab7 100644 --- a/iottalkpy/dai.py +++ b/iottalkpy/dai.py @@ -64,6 +64,10 @@ def __init__(self, api_url, device_model, device_addr=None, self.device_features = device_features if device_features else {} self.flags = {} + def _get_interval(self, df_name): + interval = self.interval.get(df_name, self.push_interval) + return interval() if callable(interval) else interval + def push_data(self, df_name): if not self.device_features[df_name].push_data: return @@ -72,7 +76,7 @@ def push_data(self, df_name): _data = self.device_features[df_name].push_data() if not isinstance(_data, NoData) and _data is not NoData: self.dan.push(df_name, _data) - time.sleep(self.interval.get(df_name, self.push_interval)) + time.sleep(self._get_interval(df_name)) def on_signal(self, signal, df_list): log.info('Receive signal: \033[1;33m%s\033[0m, %s', signal, df_list)