objects.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. self.targets_by_type = utils.group_targets_by_type(list_of_targets)
  137. elif self.targets is None:
  138. self.targets = [Target(**target) for target in utils.ungroup_targets_by_type(self.targets_by_type)]
  139. elif self.targets is not None and self.targets_by_type is not None:
  140. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  141. if utils.group_targets_by_type(list_of_targets) != self.targets_by_type:
  142. raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
  143. "but the two were not consistent with each other. "
  144. f"You supplied 'targets' = {self.targets} and "
  145. f"'targets_by_type' = {self.targets_by_type}")
  146. @dataclass
  147. class Event:
  148. event_descriptor: EventDescriptor
  149. event_signals: List[EventSignal]
  150. targets: List[Target] = None
  151. targets_by_type: Dict = None
  152. active_period: ActivePeriod = None
  153. def __post_init__(self):
  154. if self.active_period is None:
  155. dtstart = min([i['dtstart']
  156. if isinstance(i, dict) else i.dtstart
  157. for s in self.event_signals for i in s.intervals])
  158. duration = max([i['dtstart'] + i['duration']
  159. if isinstance(i, dict) else i.dtstart + i.duration
  160. for s in self.event_signals for i in s.intervals]) - dtstart
  161. self.active_period = ActivePeriod(dtstart=dtstart,
  162. duration=duration)
  163. if self.targets is None and self.targets_by_type is None:
  164. raise ValueError("You must supply either 'targets' or 'targets_by_type'.")
  165. elif self.targets_by_type is None:
  166. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  167. self.targets_by_type = utils.group_targets_by_type(list_of_targets)
  168. elif self.targets is None:
  169. self.targets = [Target(**target) for target in utils.ungroup_targets_by_type(self.targets_by_type)]
  170. elif self.targets is not None and self.targets_by_type is not None:
  171. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  172. if utils.group_targets_by_type(list_of_targets) != self.targets_by_type:
  173. raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
  174. "but the two were not consistent with each other. "
  175. f"You supplied 'targets' = {self.targets} and "
  176. f"'targets_by_type' = {self.targets_by_type}")
  177. # Set the event status
  178. self.event_descriptor.event_status = utils.determine_event_status(self.active_period)
  179. @dataclass
  180. class Response:
  181. response_code: int
  182. response_description: str
  183. request_id: str
  184. @dataclass
  185. class ReportDescription:
  186. r_id: str # Identifies a specific datapoint in a report
  187. market_context: str
  188. reading_type: str
  189. report_subject: Target
  190. report_data_source: Target
  191. report_type: str
  192. sampling_rate: SamplingRate
  193. measurement: Measurement = None
  194. @dataclass
  195. class ReportPayload:
  196. r_id: str
  197. value: float
  198. confidence: int = None
  199. accuracy: int = None
  200. @dataclass
  201. class ReportInterval:
  202. dtstart: datetime
  203. report_payload: ReportPayload
  204. duration: timedelta = None
  205. @dataclass
  206. class Report:
  207. report_specifier_id: str # This is what the VEN calls this report
  208. report_name: str # Usually one of the default ones (enums.REPORT_NAME)
  209. report_request_id: str = None # Usually empty
  210. report_descriptions: List[ReportDescription] = None
  211. created_date_time: datetime = None
  212. dtstart: datetime = None # For delivering values
  213. duration: timedelta = None # For delivering values
  214. intervals: List[ReportInterval] = None # For delivering values
  215. data_collection_mode: str = 'incremental'
  216. def __post_init__(self):
  217. if self.created_date_time is None:
  218. self.created_date_time = datetime.now(timezone.utc)
  219. if self.report_descriptions is None:
  220. self.report_descriptions = []
  221. @dataclass
  222. class SpecifierPayload:
  223. r_id: str
  224. reading_type: str
  225. measurement: Measurement = None
  226. @dataclass
  227. class ReportSpecifier:
  228. report_specifier_id: str # This is what the VEN called this report
  229. granularity: timedelta
  230. specifier_payloads: List[SpecifierPayload]
  231. report_interval: Interval = None
  232. report_back_duration: timedelta = None
  233. @dataclass
  234. class ReportRequest:
  235. report_request_id: str
  236. report_specifier: ReportSpecifier