objects.py 9.0 KB

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