objects.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. @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 = utils.group_targets_by_type(list_of_targets)
  111. elif self.targets is None:
  112. self.targets = [Target(**target) for target in utils.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 utils.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 = utils.group_targets_by_type(list_of_targets)
  142. elif self.targets is None:
  143. self.targets = [Target(**target) for target in utils.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 utils.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. # Set the event status
  152. self.event_descriptor.event_status = utils.determine_event_status(self.active_period)
  153. @dataclass
  154. class Response:
  155. response_code: int
  156. response_description: str
  157. request_id: str
  158. @dataclass
  159. class SamplingRate:
  160. min_period: timedelta = None
  161. max_period: timedelta = None
  162. on_change: bool = False
  163. @dataclass
  164. class PowerAttributes:
  165. hertz: int = 50
  166. voltage: int = 230
  167. ac: bool = True
  168. @dataclass
  169. class Measurement:
  170. name: str
  171. description: str
  172. unit: str
  173. acceptable_units: List[str] = field(repr=False, default_factory=list)
  174. scale: str = None
  175. power_attributes: PowerAttributes = None
  176. def __post_init__(self):
  177. if self.name not in ('voltage', 'energyReal', 'energyReactive',
  178. 'energyApparent', 'powerReal', 'powerApparent',
  179. 'powerReactive', 'frequency', 'pulseCount', 'temperature',
  180. 'therm', 'currency', 'currencyPerKW', 'currencyPerKWh',
  181. 'currencyPerTherm'):
  182. self.name = 'customUnit'
  183. @dataclass
  184. class ReportDescription:
  185. r_id: str # Identifies a specific datapoint in a report
  186. market_context: str
  187. reading_type: str
  188. report_subject: Target
  189. report_data_source: Target
  190. report_type: str
  191. sampling_rate: SamplingRate
  192. measurement: Measurement = None
  193. @dataclass
  194. class ReportPayload:
  195. r_id: str
  196. value: float
  197. confidence: int = None
  198. accuracy: int = None
  199. @dataclass
  200. class ReportInterval:
  201. dtstart: datetime
  202. report_payload: ReportPayload
  203. duration: timedelta = None
  204. @dataclass
  205. class Report:
  206. report_specifier_id: str # This is what the VEN calls this report
  207. report_name: str # Usually one of the default ones (enums.REPORT_NAME)
  208. report_request_id: str = None # Usually empty
  209. report_descriptions: List[ReportDescription] = None
  210. created_date_time: datetime = None
  211. dtstart: datetime = None # For delivering values
  212. duration: timedelta = None # For delivering values
  213. intervals: List[ReportInterval] = None # For delivering values
  214. data_collection_mode: str = 'incremental'
  215. def __post_init__(self):
  216. if self.created_date_time is None:
  217. self.created_date_time = datetime.now(timezone.utc)
  218. if self.report_descriptions is None:
  219. self.report_descriptions = []
  220. @dataclass
  221. class SpecifierPayload:
  222. r_id: str
  223. reading_type: str
  224. measurement: Measurement = None
  225. @dataclass
  226. class ReportSpecifier:
  227. report_specifier_id: str # This is what the VEN called this report
  228. granularity: timedelta
  229. specifier_payloads: List[SpecifierPayload]
  230. report_interval: Interval = None
  231. report_back_duration: timedelta = None
  232. @dataclass
  233. class ReportRequest:
  234. report_request_id: str
  235. report_specifier: ReportSpecifier