1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
# SPDX-License-Identifier: Apache-2.0
# Copyright 2020 Contributors to OpenLEADR
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass, field, asdict, is_dataclass
from typing import List, Dict
from datetime import datetime, timezone, timedelta
from openleadr import utils
from openleadr import enums
@dataclass
class AggregatedPNode:
node: str
@dataclass
class EndDeviceAsset:
mrid: str
@dataclass
class MeterAsset:
mrid: str
@dataclass
class PNode:
node: str
@dataclass
class FeatureCollection:
id: str
location: dict
@dataclass
class ServiceArea:
feature_collection: FeatureCollection
@dataclass
class ServiceDeliveryPoint:
node: str
@dataclass
class ServiceLocation:
node: str
@dataclass
class TransportInterface:
point_of_receipt: str
point_of_delivery: str
@dataclass
class Target:
aggregated_p_node: AggregatedPNode = None
end_device_asset: EndDeviceAsset = None
meter_asset: MeterAsset = None
p_node: PNode = None
service_area: ServiceArea = None
service_delivery_point: ServiceDeliveryPoint = None
service_location: ServiceLocation = None
transport_interface: TransportInterface = None
group_id: str = None
group_name: str = None
resource_id: str = None
ven_id: str = None
party_id: str = None
def __repr__(self):
targets = {key: value for key, value in asdict(self).items() if value is not None}
targets_str = ", ".join(f"{key}={value}" for key, value in targets.items())
return f"Target('{targets_str}')"
@dataclass
class EventDescriptor:
event_id: str
modification_number: int
market_context: str
event_status: str
created_date_time: datetime = None
modification_date_time: datetime = None
priority: int = 0
test_event: bool = False
vtn_comment: str = None
def __post_init__(self):
if self.modification_date_time is None:
self.modification_date_time = datetime.now(timezone.utc)
if self.created_date_time is None:
self.created_date_time = datetime.now(timezone.utc)
if self.modification_number is None:
self.modification_number = 0
@dataclass
class ActivePeriod:
dtstart: datetime
duration: timedelta
tolerance: dict = None
notification_period: dict = None
ramp_up_period: dict = None
recovery_period: dict = None
@dataclass
class Interval:
dtstart: datetime
duration: timedelta
signal_payload: float
uid: int = None
@dataclass
class SamplingRate:
min_period: timedelta = None
max_period: timedelta = None
on_change: bool = False
@dataclass
class PowerAttributes:
hertz: int = 50
voltage: int = 230
ac: bool = True
@dataclass
class Measurement:
name: str
description: str
unit: str
acceptable_units: List[str] = field(repr=False, default_factory=list)
scale: str = None
power_attributes: PowerAttributes = None
pulse_factor: int = None
ns: str = 'power'
def __post_init__(self):
if self.name not in enums._MEASUREMENT_NAMESPACES:
self.name = 'customUnit'
self.ns = enums._MEASUREMENT_NAMESPACES[self.name]
@dataclass
class EventSignal:
intervals: List[Interval]
signal_name: str
signal_type: str
signal_id: str
current_value: float = None
targets: List[Target] = None
targets_by_type: Dict = None
measurement: Measurement = None
def __post_init__(self):
if self.signal_type not in enums.SIGNAL_TYPE.values:
raise ValueError(f"""The signal_type must be one of '{"', '".join(enums.SIGNAL_TYPE.values)}', """
f"""you specified: '{self.signal_type}'.""")
if self.signal_name not in enums.SIGNAL_NAME.values and not self.signal_name.startswith('x-'):
raise ValueError(f"""The signal_name must be one of '{"', '".join(enums.SIGNAL_TYPE.values)}', """
f"""or it must begin with 'x-'. You specified: '{self.signal_name}'""")
if self.targets is None and self.targets_by_type is None:
return
elif self.targets_by_type is None:
list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
targets_by_type = utils.group_targets_by_type(list_of_targets)
if len(targets_by_type) > 1:
raise ValueError("In OpenADR, the EventSignal target may only be of type endDeviceAsset. "
f"You provided types: '{', '.join(targets_by_type)}'")
elif self.targets is None:
self.targets = [Target(**target) for target in utils.ungroup_targets_by_type(self.targets_by_type)]
elif self.targets is not None and self.targets_by_type is not None:
list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
if utils.group_targets_by_type(list_of_targets) != self.targets_by_type:
raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
"but the two were not consistent with each other. "
f"You supplied 'targets' = {self.targets} and "
f"'targets_by_type' = {self.targets_by_type}")
@dataclass
class Event:
event_descriptor: EventDescriptor
event_signals: List[EventSignal]
targets: List[Target] = None
targets_by_type: Dict = None
active_period: ActivePeriod = None
response_required: str = 'always'
def __post_init__(self):
if self.active_period is None:
dtstart = min([i['dtstart']
if isinstance(i, dict) else i.dtstart
for s in self.event_signals for i in s.intervals])
duration = max([i['dtstart'] + i['duration']
if isinstance(i, dict) else i.dtstart + i.duration
for s in self.event_signals for i in s.intervals]) - dtstart
self.active_period = ActivePeriod(dtstart=dtstart,
duration=duration)
if self.targets is None and self.targets_by_type is None:
raise ValueError("You must supply either 'targets' or 'targets_by_type'.")
elif self.targets_by_type is None:
list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
self.targets_by_type = utils.group_targets_by_type(list_of_targets)
elif self.targets is None:
self.targets = [Target(**target) for target in utils.ungroup_targets_by_type(self.targets_by_type)]
elif self.targets is not None and self.targets_by_type is not None:
list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
if utils.group_targets_by_type(list_of_targets) != self.targets_by_type:
raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
"but the two were not consistent with each other. "
f"You supplied 'targets' = {self.targets} and "
f"'targets_by_type' = {self.targets_by_type}")
# Set the event status
self.event_descriptor.event_status = utils.determine_event_status(self.active_period)
@dataclass
class Response:
response_code: int
response_description: str
request_id: str
@dataclass
class ReportDescription:
r_id: str # Identifies a specific datapoint in a report
market_context: str
reading_type: str
report_subject: Target
report_data_source: Target
report_type: str
sampling_rate: SamplingRate
measurement: Measurement = None
@dataclass
class ReportPayload:
r_id: str
value: float
confidence: int = None
accuracy: int = None
@dataclass
class ReportInterval:
dtstart: datetime
report_payload: ReportPayload
duration: timedelta = None
@dataclass
class Report:
report_specifier_id: str # This is what the VEN calls this report
report_name: str # Usually one of the default ones (enums.REPORT_NAME)
report_request_id: str = None # Usually empty
report_descriptions: List[ReportDescription] = None
created_date_time: datetime = None
dtstart: datetime = None # For delivering values
duration: timedelta = None # For delivering values
intervals: List[ReportInterval] = None # For delivering values
data_collection_mode: str = 'incremental'
def __post_init__(self):
if self.created_date_time is None:
self.created_date_time = datetime.now(timezone.utc)
if self.report_descriptions is None:
self.report_descriptions = []
@dataclass
class SpecifierPayload:
r_id: str
reading_type: str
measurement: Measurement = None
@dataclass
class ReportSpecifier:
report_specifier_id: str # This is what the VEN called this report
granularity: timedelta
specifier_payloads: List[SpecifierPayload]
report_interval: Interval = None
report_back_duration: timedelta = None
@dataclass
class ReportRequest:
report_request_id: str
report_specifier: ReportSpecifier
|