Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default handler: update values checks and functions return #124

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pyscada/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def connect(self):
return False

# self.accessibility()
return True

def accessibility(self):
if self.inst is not None:
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down
38 changes: 26 additions & 12 deletions pyscada/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down