objects.py 9.2 KB

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