Skip to content

Home Information - Your home, your data, organized.

Python

Core entity, sensor, control models. WIP. No migrations created.

Anthony Cassandra447476328a52
diff --git a/src/hi/apps/collection/__init__.py b/src/hi/apps/collection/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/collection/admin.py b/src/hi/apps/collection/admin.py
new file mode 100644
index 00000000..0d7f2a45
--- /dev/null
+++ b/src/hi/apps/collection/admin.py
@@ -0,0 +1,16 @@
+from django.contrib import admin
+
+from .models import Collection, CollectionRelation
+
+
+class CollectionRelationInline(admin.TabularInline):
+ model = CollectionRelation
+ extra = 1 # Number of empty forms to display in the inline
+
+
[email protected](Collection)
+class CollectionAdmin(admin.ModelAdmin):
+ inlines = [ CollectionRelationInline ]
+ list_display = ('name',)
+ search_fields = ('name',)
+ ordering = ('name',)
diff --git a/src/hi/apps/collection/apps.py b/src/hi/apps/collection/apps.py
new file mode 100644
index 00000000..eaa0d3c1
--- /dev/null
+++ b/src/hi/apps/collection/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class CollectionConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "hi.apps.collection"
diff --git a/src/hi/apps/collection/enums.py b/src/hi/apps/collection/enums.py
new file mode 100644
index 00000000..228e1618
--- /dev/null
+++ b/src/hi/apps/collection/enums.py
@@ -0,0 +1,8 @@
+from hi.apps.common.enums import LabeledEnum
+
+
+class CollectionType(LabeledEnum):
+
+ LIST = ( 'List', '' )
+
+
diff --git a/src/hi/apps/collection/migrations/__init__.py b/src/hi/apps/collection/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/collection/models.py b/src/hi/apps/collection/models.py
new file mode 100644
index 00000000..ae2bfbf2
--- /dev/null
+++ b/src/hi/apps/collection/models.py
@@ -0,0 +1,61 @@
+from django.db import models
+
+from hi.apps.entity.models import Entity
+
+from .enums import CollectionType
+
+
+class Collection( models.Model ):
+
+ name = models.CharField(
+ 'Name',
+ max_length = 64
+ )
+ entities = models.ManyToManyField(
+ Entity,
+ through = 'CollectionRelation',
+ related_name = 'collections'
+
+ )
+ collection_type_str = models.CharField(
+ 'Collection Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Collection'
+ verbose_name_plural = 'Collections'
+
+ @property
+ def collection_type(self):
+ return CollectionType.from_name_safe( self.collection_type_str )
+
+ @collection_type.setter
+ def collection_type( self, collection_type : CollectionType ):
+ self.collection_type_str = str(collection_type)
+ return
+
+
+class CollectionRelation(models.Model):
+
+ entity = models.ForeignKey(
+ Entity,
+ verbose_name = 'Entity',
+ on_delete=models.CASCADE,
+ )
+ collection = models.ForeignKey(
+ Collection,
+ verbose_name = 'Collection',
+ on_delete=models.CASCADE,
+ )
+ order_id = models.PositiveIntegerField(
+ 'Order Id',
+ default = 0,
+ )
+
+ class Meta:
+ ordering = [ 'order_id' ]
+ indexes = [
+ models.Index( fields=['entity', 'collection' ] ),
+ ]
diff --git a/src/hi/apps/common/models.py b/src/hi/apps/common/models.py
index 37a4d3a5..45adc2f3 100644
--- a/src/hi/apps/common/models.py
+++ b/src/hi/apps/common/models.py
@@ -3,6 +3,8 @@ import json
import logging
from typing import Dict
+from django.db import models
+
logger = logging.getLogger(__name__)
@@ -68,4 +70,3 @@ class Histogram:
choices.append( ( category, f'{category} ({value})' ) )
continue
return choices
-
diff --git a/src/hi/apps/control/__init__.py b/src/hi/apps/control/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/control/admin.py b/src/hi/apps/control/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/src/hi/apps/control/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/src/hi/apps/control/apps.py b/src/hi/apps/control/apps.py
new file mode 100644
index 00000000..bb675875
--- /dev/null
+++ b/src/hi/apps/control/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ControlConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "hi.apps.control"
diff --git a/src/hi/apps/control/enums.py b/src/hi/apps/control/enums.py
new file mode 100644
index 00000000..5b22bc6a
--- /dev/null
+++ b/src/hi/apps/control/enums.py
@@ -0,0 +1,13 @@
+from hi.apps.common.enums import LabeledEnum
+
+
+class ControlType(LabeledEnum):
+
+ CONTINUOUS = ( 'Continuous', '' )
+ DISCRETE = ( 'Dicrete', '' )
+
+
+class ControlledAreaType(LabeledEnum):
+
+ LIGHT = ( 'Light', '' )
+ IRRIGATION = ( 'Irrigation', '' )
diff --git a/src/hi/apps/control/migrations/__init__.py b/src/hi/apps/control/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/control/models.py b/src/hi/apps/control/models.py
new file mode 100644
index 00000000..b7187991
--- /dev/null
+++ b/src/hi/apps/control/models.py
@@ -0,0 +1,94 @@
+from django.db import models
+
+from hi.apps.entity.models import Entity
+
+from .enums import ControlType, ControlledAreaType
+
+
+class Control( models.Model ):
+
+ name = models.CharField(
+ 'Name',
+ max_length = 64,
+ null = False, blank = False,
+ )
+ entity = models.ForeignKey(
+ Entity,
+ related_name = 'controls',
+ verbose_name = 'Entity',
+ on_delete=models.CASCADE,
+ )
+ control_type_str = models.CharField(
+ 'Control Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Control'
+ verbose_name_plural = 'Controls'
+
+ @property
+ def control_type(self):
+ return ControlType.from_name_safe( self.control_type_str )
+
+ @control_type.setter
+ def control_type( self, control_type : ControlType ):
+ self.control_type_str = str(control_type)
+ return
+
+
+class ControlledEntity(models.Model):
+
+ control = models.ForeignKey(
+ Entity,
+ related_name = 'controlled_entities',
+ verbose_name = 'Control',
+ on_delete=models.CASCADE,
+ )
+ controlled_entity = models.ForeignKey(
+ Entity,
+ related_name = 'controlling_source',
+ verbose_name = 'Controlled Entity',
+ on_delete=models.CASCADE,
+ )
+
+ class Meta:
+ verbose_name = 'Controlled Entity'
+ verbose_name_plural = 'Controlled Entities'
+
+
+class ControlledArea(models.Model):
+ """For controls that exert influence over an area. The SVG styling will
+ come from the ControlledAreaType.
+
+ """
+
+ control = models.ForeignKey(
+ Entity,
+ related_name = 'controlled_areas',
+ verbose_name = 'Control',
+ on_delete=models.CASCADE,
+ )
+ svg_path = models.TextField(
+ 'Path',
+ null = False, blank = False,
+ )
+ controlled_area_type_str = models.CharField(
+ 'Control Area Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Controlled Area'
+ verbose_name_plural = 'Controlled Areas'
+
+ @property
+ def controlled_area_type(self):
+ return ControlledAreaType.from_name_safe( self.controlled_area_type_str )
+
+ @controlled_area_type.setter
+ def controlled_area_type( self, controlled_area_type : ControlledAreaType ):
+ self.controlled_area_type_str = str(control_coverage_type)
+ return
diff --git a/src/hi/apps/entity/__init__.py b/src/hi/apps/entity/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/entity/admin.py b/src/hi/apps/entity/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/src/hi/apps/entity/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/src/hi/apps/entity/apps.py b/src/hi/apps/entity/apps.py
new file mode 100644
index 00000000..b5b798fe
--- /dev/null
+++ b/src/hi/apps/entity/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class EntityConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "hi.apps.entity"
diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/entity/enums.py
new file mode 100644
index 00000000..1dab001f
--- /dev/null
+++ b/src/hi/apps/entity/enums.py
@@ -0,0 +1,53 @@
+from hi.apps.common.enums import LabeledEnum
+
+
+class EntityType(LabeledEnum):
+
+ """
+ SVG File needed for each (by convention): templates/entity/svg/{name.lower()}.svg
+ """
+
+ CAMERA = ( 'Camera', '' )
+ MOTION_SENSOR = ( 'Motion Sensor', '' )
+ LIGHT_SWITCH = ( 'Light Switch', '' )
+ LIGHT = ( 'Light', '' )
+ ELECTRICAL_OUTLET = ( 'Electrical Outlet', '' )
+ CONTACT_SENSOR = ( 'Contact Sensor', '' )
+ DOOR = ( 'Door', '' )
+ INTERNET_CONNECTION = ( 'Internet Connection', '' )
+ ELECTRIC_WIRE = ( 'Electric Wire', '' )
+ TELECOM_WIRE = ( 'Telecom Wire', '' )
+ TELECOM_BOX = ( 'Telecom Box', '' )
+ WATER_LINE = ( 'Water Line', '' )
+ SEWER_LINE = ( 'Sewer Wire', '' )
+ ELECTRIC_PANEL = ( 'Electric_Panel', '' )
+ WATER_SHUTOFF_VALVE = ( 'Water Shutoff Valve', '' )
+ WATER_METER = ( 'Water_Meter', '' )
+ DOOR_LOCK = ( 'Door Lock', '' )
+ THERMOSTAT = ( 'Thermostat', '' )
+ THERMOMETER = ( 'Thermometer', '' )
+ HYGROMETER = ( 'Hygrometer', '' )
+ BAROMETER = ( 'Barometer', '' )
+ HEATER = ( 'Heater', '' )
+ AIR_CONDITIONER = ( 'Air_Conditioner', '' )
+ HUMIDIFIER = ( 'Humidifier', '' )
+ COMPUTER = ( 'Computer', '' )
+ APPLIANCE = ( 'Appliance', '' )
+ TOOL = ( 'Tool', '' )
+ SWITCH = ( 'Switch', '' )
+ SPINKLER_CONTROLLER = ( 'Spinkler Controller', '' )
+ SPINKLER_VALVE = ( 'Spinkler Valve', '' )
+ SPRINKLER_HEAD = ( 'Sprinkler Head', '' )
+
+
+class EntityAttributeType(LabeledEnum):
+
+ STRING = ('String', '' )
+ INTEGER = ('Integer', '' )
+ FLOAT = ('Float', '' )
+ TEXT = ('Text', '' ) # relative filename in MEDIA_ROOT
+ PDF = ('PDF', '' ) # relative filename in MEDIA_ROOT
+ IMAGE = ('Image', '' ) # relative filename in MEDIA_ROOT
+ VIDEO = ('Video', '' ) # relative filename in MEDIA_ROOT
+ AUDIO = ('Audio', '' ) # relative filename in MEDIA_ROOT
+ LINK = ('Link', '' )
diff --git a/src/hi/apps/entity/migrations/__init__.py b/src/hi/apps/entity/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
new file mode 100644
index 00000000..742ea406
--- /dev/null
+++ b/src/hi/apps/entity/models.py
@@ -0,0 +1,140 @@
+from django.db import models
+
+from hi.apps.location.models import Location
+
+from .enums import EntityType, EntityAttributeType
+
+
+class Entity( models.Model ):
+
+ location = models.ForeignKey(
+ Location,
+ related_name = 'entities',
+ verbose_name = 'Location',
+ on_delete = models.CASCADE,
+ null = False, blank = False,
+ )
+ name = models.CharField(
+ 'Name',
+ max_length = 64,
+ null = False, blank = False,
+ )
+ entity_type_str = models.CharField(
+ 'Entity Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Entity'
+ verbose_name_plural = 'Entities'
+
+ @property
+ def entity_type(self):
+ return EntityType.from_name_safe( self.entity_type_str )
+
+ @entity_type.setter
+ def entity_type( self, entity_type : EntityType ):
+ self.entity_type_str = str(entity_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.
+
+ """
+
+ entity = models.OneToOneField(
+ Entity,
+ related_name = 'position',
+ verbose_name = 'Entity',
+ on_delete = models.CASCADE,
+ )
+ svg_x = models.DecimalField(
+ 'X',
+ max_digits = 12,
+ decimal_places = 6,
+ )
+ svg_y = models.DecimalField(
+ 'Y',
+ max_digits = 12,
+ decimal_places = 6,
+ )
+ svg_scale = models.DecimalField(
+ 'Scale',
+ max_digits = 12,
+ decimal_places = 6,
+ default = 1.0,
+ )
+ svg_rotation = models.DecimalField(
+ 'Rotation',
+ max_digits = 12,
+ decimal_places = 6,
+ default = 0.0,
+ )
+
+ class Meta:
+ verbose_name = 'EntityPosition'
+ verbose_name_plural = 'EntitiePositionss'
+
+
+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.
+
+ """
+
+ entity = models.OneToOneField(
+ Entity,
+ related_name = 'path',
+ verbose_name = 'Entity',
+ on_delete = models.CASCADE,
+ )
+ svg_path = models.TextField(
+ 'Path',
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'EntityPath'
+ verbose_name_plural = 'EntitiePaths'
+
+
+class EntityAttribute(models.Model):
+
+ entity = models.ForeignKey(
+ Entity,
+ related_name = 'attributes',
+ verbose_name = 'Attributes',
+ on_delete=models.CASCADE,
+ )
+ entity_attribute_type_str = models.CharField(
+ 'Attribute Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+ name = models.CharField(
+ 'Name',
+ max_length = 64,
+ )
+ value = models.CharField(
+ 'Value',
+ max_length = 255
+ )
+
+ class Meta:
+ verbose_name = 'Attribute'
+ verbose_name_plural = 'Attributes'
+
+ @property
+ def entity_attribute_type(self):
+ return EntityAttributeType.from_name_safe( self.entity_attribute_type_str )
+
+ @entity_attribute_type.setter
+ def entity_attribute_type( self, entity_attribute_type : EntityAttributeType ):
+ self.entity_attribute_type_str = str(entity_attribute_type)
+ return
+
diff --git a/src/hi/apps/location/models.py b/src/hi/apps/location/models.py
index 3e0fd784..33fa8b1f 100644
--- a/src/hi/apps/location/models.py
+++ b/src/hi/apps/location/models.py
@@ -34,7 +34,7 @@ class Location(models.Model):
max_digits = 9,
decimal_places = 3,
)
- order_id = models.IntegerField(
+ order_id = models.PositiveIntegerField(
'Order Id',
default = 0,
)
@@ -64,7 +64,7 @@ class LocationView(models.Model):
location = models.ForeignKey(
Location,
related_name = 'views',
- verbose_name = 'View',
+ verbose_name = 'Location',
on_delete = models.CASCADE,
null = False, blank = False,
)
@@ -84,7 +84,7 @@ class LocationView(models.Model):
max_digits = 9,
decimal_places = 6,
)
- order_id = models.IntegerField(
+ order_id = models.PositiveIntegerField(
'Order Id',
default = 0,
)
diff --git a/src/hi/apps/sensor/__init__.py b/src/hi/apps/sensor/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/sensor/admin.py b/src/hi/apps/sensor/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/src/hi/apps/sensor/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/src/hi/apps/sensor/apps.py b/src/hi/apps/sensor/apps.py
new file mode 100644
index 00000000..a03def4c
--- /dev/null
+++ b/src/hi/apps/sensor/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class SensorConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "hi.apps.sensor"
diff --git a/src/hi/apps/sensor/enums.py b/src/hi/apps/sensor/enums.py
new file mode 100644
index 00000000..4bf0fc97
--- /dev/null
+++ b/src/hi/apps/sensor/enums.py
@@ -0,0 +1,17 @@
+from hi.apps.common.enums import LabeledEnum
+
+
+class SensorType(LabeledEnum):
+
+ CONTINUOUS = ( 'Continuous', '' )
+ DISCRETE = ( 'Dicrete', '' )
+
+
+class SensedAreaType(LabeledEnum):
+
+ VISIBLE = ( 'Visible', '' )
+ MOTION = ( 'Motion', '' )
+ PRESENCE = ( 'Presence', '' )
+ TEMPERATURE = ( 'Temperature', '' )
+ HUMIDITY = ( 'Humidity', '' )
+ AIR_PRESSURE = ( 'Air Pressure', '' )
diff --git a/src/hi/apps/sensor/migrations/__init__.py b/src/hi/apps/sensor/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/sensor/models.py b/src/hi/apps/sensor/models.py
new file mode 100644
index 00000000..4f99607f
--- /dev/null
+++ b/src/hi/apps/sensor/models.py
@@ -0,0 +1,93 @@
+from django.db import models
+
+from hi.apps.entity.models import Entity
+
+from .enums import SensorType, SensedAreaType
+
+
+class Sensor( models.Model ):
+
+ name = models.CharField(
+ 'Name',
+ max_length = 64,
+ null = False, blank = False,
+ )
+ entity = models.ForeignKey(
+ Entity,
+ related_name = 'sensors',
+ verbose_name = 'Entity',
+ on_delete=models.CASCADE,
+ )
+ sensor_type_str = models.CharField(
+ 'Sensor Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Sensor'
+ verbose_name_plural = 'Sensors'
+
+ @property
+ def sensor_type(self):
+ return SensorType.from_name_safe( self.sensor_type_str )
+
+ @sensor_type.setter
+ def sensor_type( self, sensor_type : SensorType ):
+ self.sensor_type_str = str(sensor_type)
+ return
+
+
+class SensedEntity(models.Model):
+
+ sensor = models.ForeignKey(
+ Entity,
+ related_name = 'sensed_entities',
+ verbose_name = 'Sensor',
+ on_delete=models.CASCADE,
+ )
+ sensed_entity = models.ForeignKey(
+ Entity,
+ related_name = 'sensing_source',
+ verbose_name = 'Sensed Entity',
+ on_delete=models.CASCADE,
+ )
+
+ class Meta:
+ verbose_name = 'Sensed Entity'
+ verbose_name_plural = 'Sensed Entities'
+
+
+class SensedArea(models.Model):
+ """For sensors that are detection area-wide conditions. The SVG styling
+ will come from the SensedAreaType.
+
+ """
+ sensor = models.ForeignKey(
+ Entity,
+ related_name = 'sensed_areas',
+ verbose_name = 'Sensor',
+ on_delete=models.CASCADE,
+ )
+ svg_path = models.TextField(
+ 'Path',
+ null = False, blank = False,
+ )
+ sensed_area_type_str = models.CharField(
+ 'Sensed Area Type',
+ max_length = 32,
+ null = False, blank = False,
+ )
+
+ class Meta:
+ verbose_name = 'Sensed Area'
+ verbose_name_plural = 'Sensed Areas'
+
+ @property
+ def sensed_area_type(self):
+ return SensedAreaType.from_name_safe( self.sensed_area_type_str )
+
+ @sensed_area_type.setter
+ def sensed_area_type( self, sensed_area_type : SensedAreaType ):
+ self.sensed_area_type_str = str(sensed_area_type)
+ return
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index ec0bb063..87ea6c0c 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -137,6 +137,10 @@ INSTALLED_APPS = [
'constance',
'hi.apps.common',
'hi.apps.location',
+ 'hi.apps.entity',
+ 'hi.apps.collection',
+ 'hi.apps.sensor',
+ 'hi.apps.control',
]
MIDDLEWARE = [
diff --git a/src/hi/views.py b/src/hi/views.py
index b642b323..a3dad398 100644
--- a/src/hi/views.py
+++ b/src/hi/views.py
@@ -1,6 +1,3 @@
-import os
-
-from django.conf import settings
from django.shortcuts import render
from django.views.generic import View