objects.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.utils import group_targets_by_type, ungroup_targets_by_type
  16. @dataclass
  17. class AggregatedPNode:
  18. node: str
  19. @dataclass
  20. class EndDeviceAsset:
  21. mrid: str
  22. @dataclass
  23. class MeterAsset:
  24. mrid: str
  25. @dataclass
  26. class PNode:
  27. node: str
  28. @dataclass
  29. class FeatureCollection:
  30. id: str
  31. location: dict
  32. @dataclass
  33. class ServiceArea:
  34. feature_collection: FeatureCollection
  35. @dataclass
  36. class ServiceDeliveryPoint:
  37. node: str
  38. @dataclass
  39. class ServiceLocation:
  40. node: str
  41. @dataclass
  42. class TransportInterface:
  43. point_of_receipt: str
  44. point_of_delivery: str
  45. @dataclass
  46. class Target:
  47. aggregated_p_node: AggregatedPNode = None
  48. end_device_asset: EndDeviceAsset = None
  49. meter_asset: MeterAsset = None
  50. p_node: PNode = None
  51. service_area: ServiceArea = None
  52. service_delivery_point: ServiceDeliveryPoint = None
  53. service_location: ServiceLocation = None
  54. transport_interface: TransportInterface = None
  55. group_id: str = None
  56. group_name: str = None
  57. resource_id: str = None
  58. ven_id: str = None
  59. party_id: str = None
  60. def __repr__(self):
  61. targets = {key: value for key, value in asdict(self).items() if value is not None}
  62. targets_str = ", ".join(f"{key}={value}" for key, value in targets.items())
  63. return f"Target('{targets_str}')"
  64. @dataclass
  65. class EventDescriptor:
  66. event_id: int
  67. modification_number: int
  68. market_context: str
  69. event_status: str
  70. created_date_time: datetime = None
  71. modification_date_time: datetime = None
  72. priority: int = 0
  73. test_event: bool = False
  74. vtn_comment: str = None
  75. def __post_init__(self):
  76. if self.modification_date_time is None:
  77. self.modification_date_time = datetime.now(timezone.utc)
  78. if self.created_date_time is None:
  79. self.created_date_time = datetime.now(timezone.utc)
  80. if self.modification_number is None:
  81. self.modification_number = 0
  82. @dataclass
  83. class ActivePeriod:
  84. dtstart: datetime
  85. duration: timedelta
  86. tolerance: dict = None
  87. notification: dict = None
  88. ramp_up: dict = None
  89. recovery: dict = None
  90. @dataclass
  91. class Interval:
  92. dtstart: datetime
  93. duration: timedelta
  94. signal_payload: float
  95. uid: int = None
  96. @dataclass
  97. class EventSignal:
  98. intervals: List[Interval]
  99. signal_name: str
  100. signal_type: str
  101. signal_id: str
  102. current_value: float = None
  103. targets: List[Target] = None
  104. targets_by_type: Dict = None
  105. def __post_init__(self):
  106. if self.targets is None and self.targets_by_type is None:
  107. return
  108. elif self.targets_by_type is None:
  109. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  110. self.targets_by_type = group_targets_by_type(list_of_targets)
  111. elif self.targets is None:
  112. self.targets = [Target(**target) for target in ungroup_targets_by_type(self.targets_by_type)]
  113. elif self.targets is not None and self.targets_by_type is not None:
  114. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  115. if group_targets_by_type(list_of_targets) != self.targets_by_type:
  116. raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
  117. "but the two were not consistent with each other. "
  118. f"You supplied 'targets' = {self.targets} and "
  119. f"'targets_by_type' = {self.targets_by_type}")
  120. @dataclass
  121. class Event:
  122. event_descriptor: EventDescriptor
  123. event_signals: List[EventSignal]
  124. targets: List[Target] = None
  125. targets_by_type: Dict = None
  126. active_period: ActivePeriod = None
  127. def __post_init__(self):
  128. if self.active_period is None:
  129. dtstart = min([i['dtstart']
  130. if isinstance(i, dict) else i.dtstart
  131. for s in self.event_signals for i in s.intervals])
  132. duration = max([i['dtstart'] + i['duration']
  133. if isinstance(i, dict) else i.dtstart + i.duration
  134. for s in self.event_signals for i in s.intervals]) - dtstart
  135. self.active_period = ActivePeriod(dtstart=dtstart,
  136. duration=duration)
  137. if self.targets is None and self.targets_by_type is None:
  138. raise ValueError("You must supply either 'targets' or 'targets_by_type'.")
  139. elif self.targets_by_type is None:
  140. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  141. self.targets_by_type = group_targets_by_type(list_of_targets)
  142. elif self.targets is None:
  143. self.targets = [Target(**target) for target in ungroup_targets_by_type(self.targets_by_type)]
  144. elif self.targets is not None and self.targets_by_type is not None:
  145. list_of_targets = [asdict(target) if is_dataclass(target) else target for target in self.targets]
  146. if group_targets_by_type(list_of_targets) != self.targets_by_type:
  147. raise ValueError("You assigned both 'targets' and 'targets_by_type' in your event, "
  148. "but the two were not consistent with each other. "
  149. f"You supplied 'targets' = {self.targets} and "
  150. f"'targets_by_type' = {self.targets_by_type}")
  151. @dataclass
  152. class Response:
  153. response_code: int
  154. response_description: str
  155. request_id: str
  156. @dataclass
  157. class SamplingRate:
  158. min_period: timedelta = None
  159. max_period: timedelta = None
  160. on_change: bool = False
  161. @dataclass
  162. class PowerAttributes:
  163. hertz: int = 50
  164. voltage: int = 230
  165. ac: bool = True
  166. @dataclass
  167. class Measurement:
  168. name: str
  169. description: str
  170. unit: str
  171. acceptable_units: List[str] = field(repr=False, default_factory=list)
  172. scale: str = None
  173. power_attributes: PowerAttributes = None
  174. def __post_init__(self):
  175. if self.name not in ('voltage', 'energyReal', 'energyReactive',
  176. 'energyApparent', 'powerReal', 'powerApparent',
  177. 'powerReactive', 'frequency', 'pulseCount', 'temperature',
  178. 'therm', 'currency', 'currencyPerKW', 'currencyPerKWh',
  179. 'currencyPerTherm'):
  180. self.name = 'customUnit'
  181. @dataclass
  182. class ReportDescription:
  183. r_id: str # Identifies a specific datapoint in a report
  184. market_context: str
  185. reading_type: str
  186. report_subject: Target
  187. report_data_source: Target
  188. report_type: str
  189. sampling_rate: SamplingRate
  190. measurement: Measurement = None
  191. @dataclass
  192. class ReportPayload:
  193. r_id: str
  194. value: float
  195. confidence: int = None
  196. accuracy: int = None
  197. @dataclass
  198. class ReportInterval:
  199. dtstart: datetime
  200. report_payload: ReportPayload
  201. duration: timedelta = None
  202. @dataclass
  203. class Report:
  204. report_specifier_id: str # This is what the VEN calls this report
  205. report_name: str # Usually one of the default ones (enums.REPORT_NAME)
  206. report_request_id: str = None # Usually empty
  207. report_descriptions: List[ReportDescription] = None
  208. created_date_time: datetime = None
  209. dtstart: datetime = None # For delivering values
  210. duration: timedelta = None # For delivering values
  211. intervals: List[ReportInterval] = None # For delivering values
  212. data_collection_mode: str = 'incremental'
  213. def __post_init__(self):
  214. if self.created_date_time is None:
  215. self.created_date_time = datetime.now(timezone.utc)
  216. if self.report_descriptions is None:
  217. self.report_descriptions = []
  218. @dataclass
  219. class SpecifierPayload:
  220. r_id: str
  221. reading_type: str
  222. measurement: Measurement = None
  223. @dataclass
  224. class ReportSpecifier:
  225. report_specifier_id: str # This is what the VEN called this report
  226. granularity: timedelta
  227. specifier_payloads: List[SpecifierPayload]
  228. report_interval: Interval = None
  229. report_back_duration: timedelta = None
  230. @dataclass
  231. class ReportRequest:
  232. report_request_id: str
  233. report_specifier: ReportSpecifier