Home Information - Your home, your data, organized.
Python
Model refinements. WIP
Anthony Cassandra19ecc12c74c5
diff --git a/src/hi/apps/control/enums.py b/src/hi/apps/control/enums.pyindex eff7bf3e..cf1e1cdf 100644--- a/src/hi/apps/control/enums.py+++ b/src/hi/apps/control/enums.py@@ -3,6 +3,10 @@ from hi.apps.common.enums import LabeledEnumclass ControllerType(LabeledEnum):+ # !!!! Some of these should change to be view concerns, not model concerns+++SWITCH = ( 'Switch', '' ) # Boolean on/off valueSET_STRING = ( 'Set Value', '' ) # Generic, discrete value controlSET_INTEGER = ( 'Set Value', '' ) # Generic, discrete value controldiff --git a/src/hi/apps/control/models.py b/src/hi/apps/control/models.pyindex 7d618c0d..5537c844 100644--- a/src/hi/apps/control/models.py+++ b/src/hi/apps/control/models.py@@ -1,6 +1,6 @@from django.db import models-from hi.apps.entity.models import Entity+from hi.apps.device.models import DeviceStatefrom .enums import ControllerType, ControlledAreaType@@ -18,10 +18,10 @@ class Controller( models.Model ):max_length = 64,null = False, blank = False,)- entity = models.ForeignKey(- Entity,+ device_state = models.ForeignKey(+ DeviceState,related_name = 'controllers',- verbose_name = 'Entity',+ verbose_name = 'Device State',on_delete=models.CASCADE,)controller_type_str = models.CharField(@@ -43,7 +43,9 @@ class Controller( models.Model ):self.controller_type_str = str(controller_type)return-++# !!!! This is not a thing anymore: either controlling its own state ot that of an area or other device as defined by relationsnhip of state variable.+class ControlledEntity(models.Model):controller = models.ForeignKey(@@ -63,7 +65,8 @@ class ControlledEntity(models.Model):verbose_name = 'Controlled Entity'verbose_name_plural = 'Controlled Entities'-++# !!! Not a thing anymore: state variables this controls are related to areasclass ControlledArea(models.Model):"""For controls that exert influence over an area. The SVG styling willcome from the ControlledAreaType.diff --git a/src/hi/apps/entity/__init__.py b/src/hi/apps/device/__init__.pysimilarity index 100%rename from src/hi/apps/entity/__init__.pyrename to src/hi/apps/device/__init__.pydiff --git a/src/hi/apps/entity/admin.py b/src/hi/apps/device/admin.pysimilarity index 100%rename from src/hi/apps/entity/admin.pyrename to src/hi/apps/device/admin.pydiff --git a/src/hi/apps/entity/apps.py b/src/hi/apps/device/apps.pysimilarity index 61%rename from src/hi/apps/entity/apps.pyrename to src/hi/apps/device/apps.pyindex b5b798fe..529f81ed 100644--- a/src/hi/apps/entity/apps.py+++ b/src/hi/apps/device/apps.py@@ -1,6 +1,6 @@from django.apps import AppConfig-class EntityConfig(AppConfig):+class DeviceConfig(AppConfig):default_auto_field = "django.db.models.BigAutoField"- name = "hi.apps.entity"+ name = "hi.apps.device"diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/device/enums.pysimilarity index 84%rename from src/hi/apps/entity/enums.pyrename to src/hi/apps/device/enums.pyindex 638e62fd..e00b67d7 100644--- a/src/hi/apps/entity/enums.py+++ b/src/hi/apps/device/enums.py@@ -1,10 +1,10 @@from hi.apps.common.enums import LabeledEnum-class EntityType(LabeledEnum):+class DeviceType(LabeledEnum):"""- SVG File needed for each (by convention): templates/entity/svg/{name.lower()}.svg+ SVG File needed for each (by convention): templates/device/svg/{name.lower()}.svg"""AIR_CONDITIONER = ( 'Air_Conditioner', '' ) # Controls area@@ -47,6 +47,7 @@ class EntityType(LabeledEnum):WATER_LINE = ( 'Water Line', '' )WATER_METER = ( 'Water_Meter', '' )WATER_SHUTOFF_VALVE = ( 'Water Shutoff Valve', '' )+ WEATHER_STATION = ( 'Weather Station', '' )@propertydef svg_icon_name(self):@@ -55,7 +56,22 @@ class EntityType(LabeledEnum):@propertydef svg_path_style(self):raise NotImplementedError()-+++class DeviceStateType(LabeledEnum):++ DICRETE = ( 'Dicrete', '' )+ CONTINUOUS = ( 'Continuous', '' )+ BLOB = ( 'Blob', '' )+ MOVEMENT = ( 'Movement', '' )++ TEMPERATURE = ( 'Temperature', '' )+ HUMIDITY = ( 'Humidity', '' )+ LIGHT_LEVEL = ( 'Light Level', '' )+ SOUND_LEVEL = ( 'Sound Level', '' )+ MOISTURE = ( 'Moisture', '' )+ WIND_SPEED = ( 'Wind Speed', '' )+class AttributeValueType(LabeledEnum):diff --git a/src/hi/apps/entity/migrations/__init__.py b/src/hi/apps/device/migrations/__init__.pysimilarity index 100%rename from src/hi/apps/entity/migrations/__init__.pyrename to src/hi/apps/device/migrations/__init__.pydiff --git a/src/hi/apps/entity/models.py b/src/hi/apps/device/models.pysimilarity index 57%rename from src/hi/apps/entity/models.pyrename to src/hi/apps/device/models.pyindex b547fa97..d744fcca 100644--- a/src/hi/apps/entity/models.py+++ b/src/hi/apps/device/models.py@@ -2,29 +2,34 @@ from django.db import modelsfrom hi.apps.location.models import Location-from .enums import EntityType, AttributeValueType, AttributeSourceType+from .enums import (+ DeviceType,+ DeviceStateType,+ AttributeValueType,+ AttributeSourceType,+)-class Entity( models.Model ):+class Device( models.Model ):"""- - A physical object or space.+ - A physical or software artifact.- May have a fixed physical location or can just be part of a collection.- Maybe be located at a specific point or defined by an SVG path (e.g., wire runs, areas)- Contains zero or more sensors- Contains zero or more controllers- May be controlled by zero or more controllers- - It may have zero or more states.+ - It may have zero or more DeviceStates.- The state's actual value is always hidden.- A state may have zero of more sensors to report the state values.- Each sensor reports the value for a single state.- Each sensors reports state from a space of discrete or continuous values (or a blob).- - Its 'EntityType' determines is visual appearance.- - An entity can have zero or more staticly defined attributes (for information and configuration)+ - Its 'DeviceType' determines is visual appearance.+ - An device can have zero or more staticly defined attributes (for information and configuration)"""location = models.ForeignKey(Location,- related_name = 'entities',+ related_name = 'devices',verbose_name = 'Location',on_delete = models.CASCADE,null = False, blank = False,@@ -34,8 +39,8 @@ class Entity( models.Model ):max_length = 64,null = False, blank = False,)- entity_type_str = models.CharField(- 'Entity Type',+ device_type_str = models.CharField(+ 'Device Type',max_length = 32,null = False, blank = False,)@@ -50,30 +55,66 @@ class Entity( models.Model ):)class Meta:- verbose_name = 'Entity'- verbose_name_plural = 'Entities'+ verbose_name = 'Device'+ verbose_name_plural = 'Devices'@property- def entity_type(self):- return EntityType.from_name_safe( self.entity_type_str )+ def device_type(self):+ return DeviceType.from_name_safe( self.device_type_str )- @entity_type.setter- def entity_type( self, entity_type : EntityType ):- self.entity_type_str = str(entity_type)+ @device_type.setter+ def device_type( self, device_type : DeviceType ):+ self.device_type_str = str(device_type)return--class EntityPosition(models.Model):- """For entities represented by an icon. This is the most common case. The- icon and its styling determined by the EntityType. An Entity is not- required to have an EntityPosition.+class DeviceState(models.Model):+++ # !!! Consider how to model an area with multiple temp sensors and multiple controllers to alter the temp.++ device = models.ForeignKey(+ Device,+ related_name = 'states',+ verbose_name = 'Device',+ on_delete=models.CASCADE,+ )+ name = models.CharField(+ 'Name',+ max_length = 64,+ null = False, blank = False,+ )+ device_state_type_str = models.CharField(+ 'Device State Type',+ max_length = 32,+ null = False, blank = False,+ )++ class Meta:+ verbose_name = 'DeviceState'+ verbose_name_plural = 'DeviceStates'++ @property+ def device_state_type(self):+ return DeviceStateType.from_name_safe( self.device_state_type_str )++ @device_state_type.setter+ def device_state_type( self, device_state_type : DeviceStateType ):+ self.device_state_type_str = str(device_state_type)+ return++++class DevicePosition(models.Model):+ """For devices represented by an icon. This is the most common case. The+ icon and its styling determined by the DeviceType. An Device is not+ required to have an DevicePosition."""- entity = models.OneToOneField(- Entity,+ device = models.OneToOneField(+ Device,related_name = 'position',- verbose_name = 'Entity',+ verbose_name = 'Device',on_delete = models.CASCADE,)svg_x = models.DecimalField(@@ -100,21 +141,21 @@ class EntityPosition(models.Model):)class Meta:- verbose_name = 'EntityPosition'- verbose_name_plural = 'EntitiePositionss'+ verbose_name = 'DevicePosition'+ verbose_name_plural = 'DevicePositionss'-class EntityPath(models.Model):- """For entities represented by an arbitary SVG path. e.g., The path of a- utility line, The styling of the path is determined by the EntityType. An Entity is not- required to have an EntityPath.+class DevicePath(models.Model):+ """For devices represented by an arbitary SVG path. e.g., The path of a+ utility line, The styling of the path is determined by the DeviceType. An Device is not+ required to have an DevicePath."""- entity = models.OneToOneField(- Entity,+ device = models.OneToOneField(+ Device,related_name = 'path',- verbose_name = 'Entity',+ verbose_name = 'Device',on_delete = models.CASCADE,)svg_path = models.TextField(@@ -123,19 +164,19 @@ class EntityPath(models.Model):)class Meta:- verbose_name = 'EntityPath'- verbose_name_plural = 'EntitiePaths'+ verbose_name = 'DevicePath'+ verbose_name_plural = 'DevicePaths'class Attribute(models.Model):"""- Information related to an entity, e.g., specs, docs, notes, configs)+ Information related to an device, e.g., specs, docs, notes, configs)"""- entity = models.ForeignKey(- Entity,+ device = models.ForeignKey(+ Device,related_name = 'attributes',- verbose_name = 'Attributes',+ verbose_name = 'Device',on_delete=models.CASCADE,)attribute_value_type_str = models.CharField(diff --git a/src/hi/apps/sense/models.py b/src/hi/apps/sense/models.pyindex dc25af9e..5725bf66 100644--- a/src/hi/apps/sense/models.py+++ b/src/hi/apps/sense/models.py@@ -1,14 +1,14 @@from django.db import models-from hi.apps.entity.models import Entity+from hi.apps.devices.models import DeviceStatefrom .enums import SensorType, SensedAreaTypeclass Sensor( models.Model ):"""- - Represents an observed state of an entity.- - An entity state's is hidden and a sensor's value may not be true state (sensors can fail).+ - Represents an observed state of an device.+ - An devices state's is hidden and a sensor's value may not be true state (sensors can fail).- May sense zero or more Entities- When sensing multiple Entities, the sensed value is a single value, aggregated from all.- The SensorType defines the type of sensed value.@@ -21,10 +21,10 @@ class Sensor( models.Model ):max_length = 64,null = False, blank = False,)- entity = models.ForeignKey(- Entity,+ device_state = models.ForeignKey(+ DeviceState,related_name = 'sensors',- verbose_name = 'Entity',+ verbose_name = 'Device State',on_delete=models.CASCADE,)sensor_type_str = models.CharField(@@ -46,7 +46,8 @@ class Sensor( models.Model ):self.sensor_type_str = str(sensor_type)return-++# !!! Not a thing anymore: state variables this senses are related to areasclass SensedEntity(models.Model):sensor = models.ForeignKey(@@ -67,6 +68,7 @@ class SensedEntity(models.Model):verbose_name_plural = 'Sensed Entities'+# !!! Not a thing anymore: state variables this senses are related to areasclass SensedArea(models.Model):"""For sensors that are detection area-wide conditions. The SVG stylingwill come from the SensedAreaType.diff --git a/src/hi/settings/base.py b/src/hi/settings/base.pyindex d0c14138..6e7631e3 100644--- a/src/hi/settings/base.py+++ b/src/hi/settings/base.py@@ -137,7 +137,7 @@ INSTALLED_APPS = ['constance','hi.apps.common','hi.apps.location',- 'hi.apps.entity',+ 'hi.apps.device','hi.apps.collection','hi.apps.sense','hi.apps.control',