Skip to content

Commit

Permalink
Merge pull request #97 from sHedC/sHedC/issue96
Browse files Browse the repository at this point in the history
Add offset to last update and full data refresh time
  • Loading branch information
sHedC authored Mar 23, 2023
2 parents 7e80252 + 0201d9a commit 543c83e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 60 deletions.
46 changes: 45 additions & 1 deletion .devcontainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,48 @@ _If you don't see this notification, open the command palette and select `Remote

### Step by Step debugging

You can run the Unit Tests or if you open the Main Folder you can use Run->Start Debugging to debug the main folder.
You can run the Unit Tests or if you open the Main Folder you can use Run->Start Debugging to debug the main folder, but to do this effectively you need to have a launch.json file in your .vscode folder. this is an example for debuging using the main command line function.

This will also disable coverage reporting in the testing within VS Code.


```
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": [
"debug-test"
],
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"PYTEST_ADDOPTS": "--no-cov"
},
"args": [
"get",
"--user",
"demo",
"--password",
"mt-demo",
"--api-ver",
"v1",
"data",
"0",
"1"
]
},
{
"name": "Python: Attach using Process Id",
"type": "python",
"request": "attach",
"processId": "${command:pickProcess}",
"justMyCode": true
}
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Ignore local files
.devcontainer/devcontainer.json
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
38 changes: 0 additions & 38 deletions .vscode/launch.json

This file was deleted.

9 changes: 0 additions & 9 deletions .vscode/settings.json

This file was deleted.

57 changes: 46 additions & 11 deletions masterthermconnect/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def __init__(
username, password, session, api_version=api_version
)
self.__api_connected = False
self.__info_update_minutes = 30
self.__data_update_seconds = 60
self.__info_update_min = None
self.__data_update_sec = None
self.__data_offset_sec = None
self.__full_refresh_min = None
self.set_refresh_rate()

# The device structure is held as a dictionary with the following format:
# {
Expand Down Expand Up @@ -196,7 +199,7 @@ async def __get_hp_updates(self, full_load: bool = False) -> None:
if (
last_info_update is None
or datetime.now()
>= last_info_update + timedelta(minutes=self.__info_update_minutes)
>= last_info_update + timedelta(minutes=self.__info_update_min)
):
device_info = await self.__api.get_device_info(module_id, unit_id)
if device_info["returncode"] == "0":
Expand All @@ -206,16 +209,24 @@ async def __get_hp_updates(self, full_load: bool = False) -> None:
# Refresh the Device Data, refresh rate of this data is restricted by default
# to try and keep frequency of requests down.all(iterable)
last_data_update = None
last_update_time = "0"
last_update_time = 0
if "last_data_update" in device and not full_load:
last_data_update = device["last_data_update"]
last_update_time = device["last_update_time"]

last_update_time = last_update_time - self.__data_offset_sec
if (
last_data_update is None
or datetime.now()
>= last_data_update + timedelta(seconds=self.__data_update_seconds)
>= last_data_update + timedelta(seconds=self.__data_update_sec)
):
# Check to see if full data load is required.
if (
last_data_update is None
or datetime.now()
>= last_data_update + timedelta(minutes=self.__full_refresh_min)
):
last_update_time = None

try:
# Refresh Device Data.
device_data = await self.__api.get_device_data(
Expand Down Expand Up @@ -279,7 +290,11 @@ async def connect(self, reload_modules: bool = False) -> bool:
return self.__api_connected

def set_refresh_rate(
self, info_refresh_minutes: int = 30, data_refresh_seconds: int = 60
self,
info_refresh_minutes: int = -1,
data_refresh_seconds: int = -1,
data_offset_seconds: int = -1,
full_refresh_minutes: int = -1,
) -> None:
"""Set the Refresh Rates allowed, caution should be taken as too frequent requests
could cause lock-out on the new servers. Additionally the system seems not to update
Expand All @@ -289,11 +304,31 @@ def set_refresh_rate(
reach out to the servers to update.
Parameters:
info_refresh_minutes - The refresh rate in minutes default is 30, should be left
data_refresh_seconds - Default is 60 seconds but could be reducded with care.
info_refresh_minutes - Default is 30 minutes, delay between refresh of info
data_refresh_seconds - Default is 60 seconds, delay between refresh of data
data_offset_seconds - Default is 0, offset in the past from last update time
full_refresh_minutes - Default is 15, minutes between doing updates and full data refresh.
"""
self.__info_update_minutes = info_refresh_minutes
self.__data_update_seconds = data_refresh_seconds
if info_refresh_minutes < 0:
if self.__info_update_min is None:
self.__info_update_min = 30
else:
self.__info_update_min = info_refresh_minutes
if data_refresh_seconds < 0:
if self.__data_update_sec is None:
self.__data_update_sec = 60
else:
self.__data_update_sec = data_refresh_seconds
if data_offset_seconds < 0:
if self.__data_offset_sec is None:
self.__data_offset_sec = 0
else:
self.__data_offset_sec = data_offset_seconds
if full_refresh_minutes < 0:
if self.__full_refresh_min is None:
self.__full_refresh_min = 15
else:
self.__full_refresh_min = full_refresh_minutes

async def refresh(self, full_load: bool = False) -> bool:
"""Refresh data and information for all the devices, info refresh is restricted
Expand Down
2 changes: 1 addition & 1 deletion tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ async def test_getdata_update():
side_effect=mockconnect.get_device_data,
) as mock_get_device_data:
assert await controller.connect() is True
controller.set_refresh_rate(data_refresh_seconds=0)
controller.set_refresh_rate(data_refresh_seconds=0, data_offset_seconds=0)
assert await controller.refresh() is True

data = controller.get_device_data("1234", "1")
Expand Down

0 comments on commit 543c83e

Please sign in to comment.