|
| 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 | + |
| 17 | +from abc import ABC |
| 18 | +from frost_sta_client.service.sensorthingsservice import SensorThingsService |
| 19 | + |
| 20 | + |
| 21 | +class Entity(ABC): |
| 22 | + """ |
| 23 | + An abstract representation of an entity. |
| 24 | + """ |
| 25 | + def __init__(self, |
| 26 | + id=None, |
| 27 | + self_link='', |
| 28 | + service=None): |
| 29 | + self.id = id |
| 30 | + self.self_link = self_link |
| 31 | + self.service = service |
| 32 | + |
| 33 | + @property |
| 34 | + def id(self): |
| 35 | + return self._id |
| 36 | + |
| 37 | + @id.setter |
| 38 | + def id(self, value): |
| 39 | + if value is None: |
| 40 | + self._id = None |
| 41 | + return |
| 42 | + if isinstance(value, int) or isinstance(value, str): |
| 43 | + self._id = value |
| 44 | + return |
| 45 | + raise ValueError('id of entity should be of type int or str!') |
| 46 | + |
| 47 | + @property |
| 48 | + def self_link(self): |
| 49 | + return self._self_link |
| 50 | + |
| 51 | + @self_link.setter |
| 52 | + def self_link(self, value): |
| 53 | + if not isinstance(value, str): |
| 54 | + raise ValueError('self_link should be of type str!') |
| 55 | + self._self_link = value |
| 56 | + |
| 57 | + @property |
| 58 | + def service(self): |
| 59 | + return self._service |
| 60 | + |
| 61 | + @service.setter |
| 62 | + def service(self, value): |
| 63 | + if value is None or isinstance(value, SensorThingsService): |
| 64 | + self._service = value |
| 65 | + return |
| 66 | + raise ValueError('service should be of type SensorThingsService') |
| 67 | + |
| 68 | + def set_service(self, service): |
| 69 | + if self.service != service: |
| 70 | + self.service = service |
| 71 | + self.ensure_service_on_children(service) |
| 72 | + |
| 73 | + @property |
| 74 | + def IOT_COUNT(self): |
| 75 | + return 'iot.count' |
| 76 | + |
| 77 | + @property |
| 78 | + def AT_IOT_COUNT(self): |
| 79 | + return '@iot.count' |
| 80 | + |
| 81 | + @property |
| 82 | + def IOT_NAVIGATION_LINK(self): |
| 83 | + return 'iot.navigationLink' |
| 84 | + |
| 85 | + @property |
| 86 | + def AT_IOT_NAVIGATION_LINK(self): |
| 87 | + return '@iot.navigationLink' |
| 88 | + |
| 89 | + @property |
| 90 | + def IOT_NEXT_LINK(self): |
| 91 | + return 'iot.nextLink' |
| 92 | + |
| 93 | + @property |
| 94 | + def AT_IOT_NEXT_LINK(self): |
| 95 | + return '@iot.nextLink' |
| 96 | + |
| 97 | + @property |
| 98 | + def IOT_SELF_LINK(self): |
| 99 | + return 'iot.selfLink' |
| 100 | + |
| 101 | + @property |
| 102 | + def AT_IOT_SELF_LINK(self): |
| 103 | + return '@iot.selfLink' |
| 104 | + |
| 105 | + def __eq__(self, other): |
| 106 | + if other is None: |
| 107 | + return False |
| 108 | + if not isinstance(other, type(self)): |
| 109 | + return False |
| 110 | + if id(self) == id(other): |
| 111 | + return True |
| 112 | + if self.id is not None and other.id is not None: |
| 113 | + if self.id != other.id: |
| 114 | + return False |
| 115 | + return True |
| 116 | + |
| 117 | + def __ne__(self, other): |
| 118 | + return not self == other |
| 119 | + |
| 120 | + def __getstate__(self): |
| 121 | + data = {} |
| 122 | + if self.id is not None and self.id != '': |
| 123 | + data['@iot.id'] = self.id |
| 124 | + return data |
| 125 | + |
| 126 | + def __setstate__(self, state): |
| 127 | + self.id = state.get('@iot.id', None) |
| 128 | + self.self_link = state.get('@iot.selfLink', '') |
0 commit comments