|
| 1 | +# Copyright 2025 Zeppelin Bend Pty Ltd |
| 2 | +# |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +""" |
| 8 | +Example trace showing methods to traverse upstream of a given `IdentifiedObject` to find the first occurrence |
| 9 | + of another specified `IdentifiedObject` |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import json |
| 14 | + |
| 15 | +from zepben.evolve import NetworkStateOperators, NetworkTraceActionType, NetworkTraceStep, StepContext, \ |
| 16 | + NetworkConsumerClient, ConductingEquipment, connect_with_token |
| 17 | +from zepben.evolve import PowerTransformer, UsagePoint, Tracing, Switch |
| 18 | +from zepben.protobuf.nc.nc_requests_pb2 import INCLUDE_ENERGIZED_LV_FEEDERS |
| 19 | + |
| 20 | +with open("config.json") as f: |
| 21 | + c = json.loads(f.read()) |
| 22 | + |
| 23 | + |
| 24 | +def _trace(start_item, results, stop_condition): |
| 25 | + """Returns a `NetworkTrace` configured with our parameters""" |
| 26 | + |
| 27 | + def step_action(step: NetworkTraceStep, context: StepContext): |
| 28 | + if context.is_stopping: # if the trace is stopping, we have found the equipment we're looking for |
| 29 | + results.append(step.path.to_equipment) |
| 30 | + |
| 31 | + state_operators = NetworkStateOperators.NORMAL |
| 32 | + |
| 33 | + return ( |
| 34 | + Tracing.network_trace( |
| 35 | + network_state_operators=state_operators, |
| 36 | + action_step_type=NetworkTraceActionType.ALL_STEPS |
| 37 | + ).add_condition(state_operators.upstream()) |
| 38 | + .add_stop_condition(stop_condition) |
| 39 | + .add_step_action(step_action) |
| 40 | + .add_start_item(start_item) |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +async def main(mrid: str, feeder_mrid: str): |
| 45 | + channel = connect_with_token(host=c["host"], access_token=c["access_token"], rpc_port=c["rpc_port"]) |
| 46 | + client = NetworkConsumerClient(channel) |
| 47 | + await client.get_equipment_container(feeder_mrid, include_energized_containers=INCLUDE_ENERGIZED_LV_FEEDERS) |
| 48 | + network = client.service |
| 49 | + |
| 50 | + try: |
| 51 | + usage_point = network.get(mrid, UsagePoint) |
| 52 | + # get the `ConductingEquipment` from the `UsagePoint` |
| 53 | + start_item = next(filter(lambda ce: isinstance(ce, ConductingEquipment), usage_point.equipment)) |
| 54 | + except TypeError: |
| 55 | + start_item = network.get(mrid, ConductingEquipment) |
| 56 | + |
| 57 | + results = [] |
| 58 | + |
| 59 | + # Get DSUB from which any given customer is supplied from using a basic upstream trace |
| 60 | + def dsub_stop_condition(step: NetworkTraceStep, context: StepContext): |
| 61 | + return isinstance(step.path.to_equipment, PowerTransformer) |
| 62 | + |
| 63 | + # Get Circuit Breaker from which any given customer is supplied from using a basic upstream trace |
| 64 | + # Uncomment stop condition below to use |
| 65 | + def circuit_breaker_stop_condition(step: NetworkTraceStep, context: StepContext): |
| 66 | + return isinstance(step.path.to_equipment, Switch) |
| 67 | + |
| 68 | + await _trace( |
| 69 | + start_item=start_item, |
| 70 | + results=results, |
| 71 | + stop_condition=dsub_stop_condition, |
| 72 | + # stop_condition=circuit_breaker_stop_condition, |
| 73 | + ).run() |
| 74 | + |
| 75 | + print(results) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + # EnergyConsumer: 50763684 |
| 80 | + asyncio.run(main(mrid='4310990779', feeder_mrid='RW1292')) |
0 commit comments