Skip to content

Commit

Permalink
Merge pull request #20 from leandrolanzieri/pr/power_control/cached_l…
Browse files Browse the repository at this point in the history
…ocations

Power control: option to use cached locations
  • Loading branch information
MrKevinWeiss authored Jun 12, 2024
2 parents ac348f1 + f1fc265 commit 506aa68
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/inet_nm/power_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import sleep
from typing import List

import inet_nm.config as cfg
import inet_nm.locking as lck
import inet_nm.usb_ctrl as ucl
from inet_nm.data_types import NmNode
Expand All @@ -15,7 +16,13 @@ class PowerControl:
DEFAULT_POWER_OFF_WAIT = 1
MAX_ALLOWED_NODES = 256

def __init__(self, locations, nodes: List[NmNode], max_powered_devices=None):
def __init__(
self,
locations,
nodes: List[NmNode],
max_powered_devices=None,
config: Optional[str] = None,
):
self.logging = logging.getLogger(__name__)
self.id_path_to_node_uid = {}
self.powered_locations = {}
Expand All @@ -31,6 +38,11 @@ def __init__(self, locations, nodes: List[NmNode], max_powered_devices=None):
self._power_off_procs = []
self._powered_on = set()

if config is not None:
self._cache_config = cfg.LocationCache(config_dir=config).load()
else:
self._cache_config = None

def _available(self, powered_devs) -> int:
if self.max_powered_devices is not None:
return self.max_powered_devices - len(powered_devs)
Expand Down Expand Up @@ -149,6 +161,12 @@ def _map_id_path_to_node_uid(self):
uid = ucl.get_uid_from_id_path(id_path)
self.id_path_to_node_uid[id_path] = uid

if self._cache_config is not None:
for cache in self._cache_config:
if cache["id_path"] in self.id_path_to_node_uid:
continue
self.id_path_to_node_uid[cache["id_path"]] = cache["node_uid"]

def power_off_unused(self) -> None:
self.logging.debug("Powering off")
# check locked devices from lockfiles
Expand Down

0 comments on commit 506aa68

Please sign in to comment.