diff --git a/pyscada/device.py b/pyscada/device.py index 8dbaf9d4..bc14c572 100644 --- a/pyscada/device.py +++ b/pyscada/device.py @@ -48,6 +48,7 @@ def connect(self): return False # self.accessibility() + return True def accessibility(self): if self.inst is not None: @@ -67,8 +68,7 @@ def accessibility(self): def disconnect(self): if self.inst is not None: self.inst = None - return True - return False + return True def before_read(self): """ @@ -98,13 +98,13 @@ def read_data_and_time(self, variable_instance): return self.read_data(variable_instance), self.time() - def read_data_all(self, variables_dict): + def read_data_all(self, variables_dict, erase_cache=True): output = [] if self.before_read(): for item in variables_dict.values(): value, read_time = self.read_data_and_time(item) - if value is not None and item.update_values([value], [read_time]): + if value is not None and read_time is not None and item.update_values(value, read_time, erase_cache=erase_cache): output.append(item) self.after_read() return output diff --git a/pyscada/models.py b/pyscada/models.py index 074b14b6..b64bbd0f 100755 --- a/pyscada/models.py +++ b/pyscada/models.py @@ -1916,7 +1916,11 @@ def _update_value(self, value=None, timestamp=None): self.value = value else: self.value = self.scaling.scale_value(value) - self.timestamp = timestamp + try: + self.timestamp = float(timestamp) + except ValueError as e: + logger.error(f"{self} - wrong timestamp : {e}") + return False self.store_value = False if self.prev_value is None: # no prev value in the cache, always store the value @@ -1947,18 +1951,28 @@ def update_values(self, value_list, timestamp_list, erase_cache=True): if erase_cache: self.cached_values_to_write = [] has_value = False - if isinstance(value_list.__class__, list): - logger.error( - f"{self} - Value list must be a list, it is a {type(value_list)}" - ) - return False - if isinstance(timestamp_list.__class__, list): - logger.error( - f"{self} - Timestamp list must be a list, it is a {type(timestamp_list)}" - ) - return False + if not isinstance(value_list, list): + if isinstance(value_list, bool) or isinstance(value_list, int) or isinstance(value_list, float) or isinstance(value_list, str): + value_list = [value_list] + else: + logger.warning( + f"{self} - Value list wrong type : {type(value_list)}" + ) + return False + if not isinstance(timestamp_list, list): + if isinstance(timestamp_list, bool) or isinstance(timestamp_list, int) or isinstance(timestamp_list, float) or isinstance(timestamp_list, str): + try: + timestamp_list = [float(timestamp_list)] + except ValueError as e: + logger.warning(f"{self} - wrong timestamp : {e}") + return False + else: + logger.warning( + f"{self} - Timestamp list wrong type : {type(timestamp_list)}" + ) + return False if len(value_list) != len(timestamp_list): - logger.error( + logger.warning( f"{self} - value and timestamp lists have not the same length ({len(value_list)}, {len(timestamp_list)})" ) return False