-
Notifications
You must be signed in to change notification settings - Fork 17
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
Battery status with channel communication #171
Battery status with channel communication #171
Conversation
6bd3146
to
3adb945
Compare
Unit tests are waiting for: frequenz-floss/frequenz-channels-python#68 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked the first 4 commits later (and one thing with the big commit).
I'll continue tomorrow.
class _ComponentData(Generic[T]): | ||
component_id: int | ||
receiver: Peekable[T] | ||
last_msg_timestamp: datetime = datetime.now(tz=timezone.utc) | ||
last_msg_correct: bool = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can now rename this class to _ComponentStreamStatus
? Also it is not generic anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -45,10 +40,25 @@ class BatteryStatus(Enum): | |||
"""Component is working""" | |||
|
|||
|
|||
@dataclass | |||
class RequestResult: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this have to go to microgrid/_battery
? I think this is specific to the power distributor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also rename this to SetPowerResult
or something? RequestResult
sounds too confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved whole file to frequez.sdk.actor.power_distributing.
Renamed RequestResult to SetPowerResult.
@@ -113,12 +123,10 @@ def is_blocked(self) -> bool: | |||
return self.blocked_until > datetime.now(tz=timezone.utc) | |||
|
|||
|
|||
class BatteryStatusTracker(AsyncConstructible): | |||
class BatteryStatusTracker: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This too, I think should sit outside the microgrid
package. The microgrid
package is supposed to be a thin wrapper providing access to data from the microgrid and for sending commands to the microgrid.
But this class is really stateful tracking logic, which should ideally be part of a higher level package, possibly the actor/power_distributing
. I see that this code was already merged, so I can move out later also, if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to actor/power_distributing
To create an instance of this class you should use `async_new` class method. | ||
Standard constructor (__init__) is not supported and using it will raise | ||
`NotSyncConstructible` error. | ||
Status in send only when it change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status in send only when it change. | |
Status updates are sent out only when there is a status change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
self.sender = channel.new_sender() | ||
|
||
|
||
class BatteriesStatus: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you're going to rename this to AllBatteryStatus
as discussed, and the file accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed to BatteryPoolStatus
once again.
Because it has battery_ids as argument, so it can be created for pool, too.
Also BatteryPoolStatus
sounds better after all.
|
||
self._batteries: Dict[str, BatteryStatusTracker] = {} | ||
|
||
# Receivers are needed to create Select object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Receivers are needed to create Select object | |
# Receivers for individual battery statuses are needed to create a `MergeNamed` object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
try: | ||
await self._update_status(self._battery_status_channel) | ||
except Exception as err: # pylint: disable=broad-except | ||
_logger.error("BatteriesStatus failed with error: %s. Restart.", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_logger.error("BatteriesStatus failed with error: %s. Restart.", err) | |
_logger.error("BatteriesStatus failed with error: %s. Restarting.", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
Raises: | ||
KeyError: If any battery in the given batteries is not in the pool. | ||
async def _update_status(self, status_channel: MergeNamed[BatteryStatus]) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe more readable to decompose to a Receiver
, like below, but it is ok to keep it as it is also.
async def _update_status(self, status_channel: MergeNamed[BatteryStatus]) -> None: | |
async def _update_status(self, status_channel: Receiver[tuple[str, BatteryStatus]]) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to leave it MergeNamed
.
It is clear and shorter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, why not just use self._battery_status_channel
and not pass anything at all?
"""Return status of the battery. | ||
async def stop(self) -> None: | ||
"""Stop tracking battery status.""" | ||
if self._select is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should probably also cancel_and_await(self._task)
here, before killing the Select
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
And simplify BatteriesStatus code. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
In the future commit blocking will be only in BatteryStatusTracker. All blocking related public functions will be removed. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Move getting status functionality to separate method. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
3adb945
to
b5a1219
Compare
BatteryStatusTracker receives messages from components, parse it. If status change BatteryStatusTracker sends new status using channel. BatteriesStatus is now only class that listen for change of status from any batteries and updates set of working batteries. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
... and stop MergeNamed object in BatteriesStatus. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
b5a1219
to
1821682
Compare
Move from frequenz/sdk/microgrid/_battery/_status.py to frequenz/sdk/actor/power_distributing/_battery_status.py Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Rename from batteries_status.py to battery_pool_status.py Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
... battery_status.py and battery_pool_status.py Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
BatteryStatus will be used outside, by users. Status is just an internal class. Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
7ce92ff
to
6a865d4
Compare
No description provided.