|
| 1 | +# Copyright (C) 2021 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 |
| 2 | +# Karlsruhe, Germany. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU Lesser General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +from enum import Enum |
| 17 | + |
| 18 | +import frost_sta_client |
| 19 | +import datetime |
| 20 | + |
| 21 | + |
| 22 | +class DataArrayValue: |
| 23 | + |
| 24 | + class Property(Enum): |
| 25 | + ID = 1 |
| 26 | + PHENOMENON_TIME = 2 |
| 27 | + RESULT = 3 |
| 28 | + RESULT_TIME = 4 |
| 29 | + RESULT_QUALITY = 5 |
| 30 | + VALID_TIME = 6 |
| 31 | + PARAMETERS = 7 |
| 32 | + FEATURE_OF_INTEREST = 8 |
| 33 | + |
| 34 | + def to_string(self): |
| 35 | + if self is DataArrayValue.Property.ID: |
| 36 | + return "id" |
| 37 | + if self is DataArrayValue.Property.PHENOMENON_TIME: |
| 38 | + return "phenomenonTime" |
| 39 | + if self is DataArrayValue.Property.RESULT: |
| 40 | + return "result" |
| 41 | + if self is DataArrayValue.Property.RESULT_TIME: |
| 42 | + return "resultTime" |
| 43 | + if self is DataArrayValue.Property.RESULT_QUALITY: |
| 44 | + return "resultQuality" |
| 45 | + if self is DataArrayValue.Property.VALID_TIME: |
| 46 | + return "validTime" |
| 47 | + if self is DataArrayValue.Property.PARAMETERS: |
| 48 | + return "parameters" |
| 49 | + if self is DataArrayValue.Property.FEATURE_OF_INTEREST: |
| 50 | + return "FeatureOfInterest/id" |
| 51 | + |
| 52 | + |
| 53 | + class VisibleProperties: |
| 54 | + def __init__(self, all_values: bool): |
| 55 | + self.id = all_values |
| 56 | + self.phenomenon_time = all_values |
| 57 | + self.result = all_values |
| 58 | + self.result_time = all_values |
| 59 | + self.result_quality = all_values |
| 60 | + self.valid_time = all_values |
| 61 | + self.parameters = all_values |
| 62 | + self.feature_of_interest = all_values |
| 63 | + |
| 64 | + def __init__(self): |
| 65 | + self = DataArrayValue.VisibleProperties(False) |
| 66 | + |
| 67 | + def __init__(self, select): |
| 68 | + self.id = DataArrayValue.Property.ID in select |
| 69 | + self.phenomenon_time = DataArrayValue.Property.PHENOMENON_TIME in select |
| 70 | + self.result = DataArrayValue.Property.RESULT in select |
| 71 | + self.result_time = DataArrayValue.Property.RESULT_TIME in select |
| 72 | + self.result_quality = DataArrayValue.Property.RESULT_QUALITY in select |
| 73 | + self.valid_time = DataArrayValue.Property.VALID_TIME in select |
| 74 | + self.parameters = DataArrayValue.Property.PARAMETERS in select |
| 75 | + self.feature_of_interest = DataArrayValue.Property.FEATURE_OF_INTEREST in select |
| 76 | + |
| 77 | + def get_components(self): |
| 78 | + components = [] |
| 79 | + if self.id: |
| 80 | + components.append(DataArrayValue.Property.ID.to_string()) |
| 81 | + if self.phenomenon_time: |
| 82 | + components.append(DataArrayValue.Property.PHENOMENON_TIME.to_string()) |
| 83 | + if self.result: |
| 84 | + components.append(DataArrayValue.Property.RESULT.to_string()) |
| 85 | + if self.result_time: |
| 86 | + components.append(DataArrayValue.Property.RESULT_TIME.to_string()) |
| 87 | + if self.result_quality: |
| 88 | + components.append(DataArrayValue.Property.RESULT_QUALITY.to_string()) |
| 89 | + if self.valid_time: |
| 90 | + components.append(DataArrayValue.Property.VALID_TIME.to_string()) |
| 91 | + if self.parameters: |
| 92 | + components.append(DataArrayValue.Property.PARAMETERS.to_string()) |
| 93 | + if self.feature_of_interest: |
| 94 | + components.append(DataArrayValue.Property.FEATURE_OF_INTEREST.to_string()) |
| 95 | + return components |
| 96 | + |
| 97 | + |
| 98 | + def from_observation(self, o: frost_sta_client.Observation): |
| 99 | + value = [] |
| 100 | + if self.id: |
| 101 | + value.append(o.id) |
| 102 | + if self.phenomenon_time: |
| 103 | + if type(o.phenomenon_time) == str: |
| 104 | + value.append(o.phenomenon_time) |
| 105 | + if type(o.phenomenon_time) == datetime.datetime: |
| 106 | + value.append(o.phenomenon_time.isoformat()) |
| 107 | + if self.result: |
| 108 | + value.append(o.result) |
| 109 | + if self.result_time: |
| 110 | + value.append(o.result_time) |
| 111 | + if self.result_quality: |
| 112 | + value.append(o.result_quality) |
| 113 | + if self.valid_time: |
| 114 | + value.append(o.valid_time) |
| 115 | + if self.parameters: |
| 116 | + value.append(o.parameters) |
| 117 | + if self.feature_of_interest: |
| 118 | + value.append(o.feature_of_interest.id) |
| 119 | + return value |
| 120 | + |
| 121 | + |
| 122 | + def __init__(self): |
| 123 | + self.datastream = None |
| 124 | + self.multi_datastream = None |
| 125 | + self.visible_properties = None |
| 126 | + self.data_array = [] |
| 127 | + self.components = None |
| 128 | + self.observations = [] |
| 129 | + |
| 130 | + |
| 131 | + @property |
| 132 | + def datastream(self): |
| 133 | + return self._datastream |
| 134 | + |
| 135 | + @datastream.setter |
| 136 | + def datastream(self, value): |
| 137 | + self._datastream = value |
| 138 | + |
| 139 | + @property |
| 140 | + def components(self): |
| 141 | + return self._components |
| 142 | + |
| 143 | + @components.setter |
| 144 | + def components(self, properties): |
| 145 | + if properties is None: |
| 146 | + self._components = None |
| 147 | + return |
| 148 | + if len(self.data_array) >= 1: |
| 149 | + raise ValueError("Can not change components after adding Observations") |
| 150 | + self.visible_properties = self.VisibleProperties(properties) |
| 151 | + self._components = self.visible_properties.get_components() |
| 152 | + |
| 153 | + @property |
| 154 | + def data_array(self): |
| 155 | + return self._data_array |
| 156 | + |
| 157 | + @data_array.setter |
| 158 | + def data_array(self, value): |
| 159 | + self._data_array = value |
| 160 | + |
| 161 | + def add_observation(self, o): |
| 162 | + self.data_array.append(self.visible_properties.from_observation(o)) |
| 163 | + self.observations.append(o) |
| 164 | + |
| 165 | + def __getstate__(self): |
| 166 | + data = {"Datastream": { |
| 167 | + "@iot.id": self.datastream.id |
| 168 | + }, |
| 169 | + "components": self.components, |
| 170 | + "dataArray": self.data_array} |
| 171 | + return data |
| 172 | + |
0 commit comments