1
1
"""Handle OTA software version endpoints of the DCL."""
2
2
3
3
import logging
4
- from typing import Any
4
+ from typing import Any , cast
5
5
6
6
from aiohttp import ClientError , ClientSession
7
7
10
10
LOGGER = logging .getLogger (__name__ )
11
11
12
12
13
- async def get_software_versions (node_id : int , vid : int , pid : int ) -> Any :
13
+ async def get_software_versions (vid : int , pid : int ) -> Any :
14
14
"""Check DCL if there are updates available for a particular node."""
15
15
async with ClientSession (raise_for_status = True ) as http_session :
16
16
# fetch the paa certificates list
@@ -20,9 +20,7 @@ async def get_software_versions(node_id: int, vid: int, pid: int) -> Any:
20
20
return await response .json ()
21
21
22
22
23
- async def get_software_version (
24
- node_id : int , vid : int , pid : int , software_version : int
25
- ) -> Any :
23
+ async def get_software_version (vid : int , pid : int , software_version : int ) -> Any :
26
24
"""Check DCL if there are updates available for a particular node."""
27
25
async with ClientSession (raise_for_status = True ) as http_session :
28
26
# fetch the paa certificates list
@@ -32,27 +30,47 @@ async def get_software_version(
32
30
return await response .json ()
33
31
34
32
35
- async def check_updates (
36
- node_id : int , vid : int , pid : int , current_software_version : int
33
+ async def check_for_update (
34
+ vid : int , pid : int , current_software_version : int
37
35
) -> None | dict :
38
36
"""Check if there is a newer software version available on the DCL."""
39
37
try :
40
- versions = await get_software_versions (node_id , vid , pid )
38
+ versions = await get_software_versions (vid , pid )
41
39
42
- software_versions : list [int ] = versions ["modelVersions" ]["softwareVersions" ]
43
- latest_software_version = max (software_versions )
44
- if latest_software_version <= current_software_version :
40
+ all_software_versions : list [int ] = versions ["modelVersions" ]["softwareVersions" ]
41
+ newer_software_versions = [
42
+ version
43
+ for version in all_software_versions
44
+ if version > current_software_version
45
+ ]
46
+
47
+ # Check if there is a newer software version available
48
+ if not newer_software_versions :
49
+ LOGGER .info ("No newer software version available." )
45
50
return None
46
51
47
- version : dict = await get_software_version (
48
- node_id , vid , pid , latest_software_version
49
- )
50
- if isinstance (version , dict ) and "modelVersion" in version :
51
- result : Any = version ["modelVersion" ]
52
- if isinstance (result , dict ):
53
- return result
52
+ # Check if latest firmware is applicable, and backtrack from there
53
+ for version in sorted (newer_software_versions , reverse = True ):
54
+ version_res : dict = await get_software_version (vid , pid , version )
55
+ if not isinstance (version_res , dict ):
56
+ raise TypeError ("Unexpected DCL response." )
57
+
58
+ if "modelVersion" not in version_res :
59
+ raise ValueError ("Unexpected DCL response." )
60
+
61
+ version_candidate : dict = cast (dict , version_res ["modelVersion" ])
62
+
63
+ # Check minApplicableSoftwareVersion/maxApplicableSoftwareVersion
64
+ min_sw_version = version_candidate ["minApplicableSoftwareVersion" ]
65
+ max_sw_version = version_candidate ["maxApplicableSoftwareVersion" ]
66
+ if (
67
+ current_software_version < min_sw_version
68
+ or current_software_version > max_sw_version
69
+ ):
70
+ LOGGER .debug ("Software version %d not applicable." , version )
71
+ continue
54
72
55
- logging . error ( "Unexpected DCL response." )
73
+ return version_candidate
56
74
return None
57
75
58
76
except (ClientError , TimeoutError ) as err :
0 commit comments