Home Information - Your home, your data, organized.
Python
Finished refactoring and cleanup to isolate SVG display concerns.
Anthony Cassandraa3a556563455
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.pyindex 2a7fdd81..1b737ea8 100644--- a/src/hi/apps/collection/collection_manager.py+++ b/src/hi/apps/collection/collection_manager.py@@ -6,6 +6,7 @@ from django.http import HttpRequestfrom hi.apps.common.singleton import Singletonfrom hi.apps.entity.models import Entityfrom hi.apps.location.models import Location, LocationView+from hi.apps.location.svg_item_factory import SvgItemFactoryfrom .collection_data import CollectionDatafrom .enums import CollectionType@@ -20,8 +21,6 @@ from .models import (class CollectionManager(Singleton):- PATH_EDIT_NEW_PATH_RADIUS_PERCENT = 5.0 # Preferrable if this matches Javascript new path sizing.-def __init_singleton__(self):return@@ -86,10 +85,12 @@ class CollectionManager(Singleton):with transaction.atomic():# Need to make sure it has some visible representation in the view if none exists.- if collection.collection_type.is_path:+ svg_item_type = SvgItemFactory().get_svg_item_type( collection )+ if svg_item_type.is_path:self.add_collection_path_if_needed(collection = collection,location_view = location_view,+ is_path_closed = svg_item_type.is_path_closed,)else:self.add_collection_position_if_needed(@@ -157,8 +158,6 @@ class CollectionManager(Singleton):def add_collection_position_if_needed( cls,collection : Collection,location_view : LocationView ) -> CollectionPosition:- assert not collection.collection_type.is_path-try:_ = CollectionPosition.objects.get(location = location_view.location,@@ -183,10 +182,9 @@ class CollectionManager(Singleton):return collection_positiondef add_collection_path_if_needed( self,- collection : Collection,- location_view : LocationView ) -> CollectionPath:- assert collection.collection_type.is_path-+ collection : Collection,+ location_view : LocationView,+ is_path_closed : bool ) -> CollectionPath:try:_ = CollectionPath.objects.get(location = location_view.location,@@ -196,35 +194,10 @@ class CollectionManager(Singleton):except CollectionPath.DoesNotExist:pass- # Note that this server-side creation of a new path is just one- # place new paths can be created. During client-side path editing,- # the Javascript code also uses logic to add new path segments.- # These do not have to behave identical, but it is preferrable for- # there to be some consistency.-- # Default display a line or rectangle in middle of current view with radius X% of viewbox- center_x = location_view.svg_view_box.x + ( location_view.svg_view_box.width / 2.0 )- center_y = location_view.svg_view_box.y + ( location_view.svg_view_box.height / 2.0 )- radius_x = location_view.svg_view_box.width * ( self.PATH_EDIT_NEW_PATH_RADIUS_PERCENT / 100.0 )- radius_y = location_view.svg_view_box.height * ( self.PATH_EDIT_NEW_PATH_RADIUS_PERCENT / 100.0 )-- if collection.collection_type.is_path_closed:- top_left_x = center_x - radius_x- top_left_y = center_y - radius_y- top_right_x = center_x + radius_x- top_right_y = center_y - radius_y- bottom_right_x = center_x + radius_x- bottom_right_y = center_y + radius_y- bottom_left_x = center_x - radius_x- bottom_left_y = center_y + radius_y- svg_path = f'M {top_left_x},{top_left_y} L {top_right_x},{top_right_y} L {bottom_right_x},{bottom_right_y} L {bottom_left_x},{bottom_left_y} Z'- else:- start_x = center_x - radius_x- start_y = center_y- end_x = start_x + radius_x- end_y = start_y- svg_path = f'M {start_x},{start_y} L {end_x},{end_y}'-+ svg_path = SvgItemFactory().get_default_svg_path_str(+ location_view = location_view,+ is_path_closed = is_path_closed,+ )collection_path = CollectionPath.objects.create(collection = collection,location = location_view.location,diff --git a/src/hi/apps/collection/enums.py b/src/hi/apps/collection/enums.pyindex a689d42c..32f9d8da 100644--- a/src/hi/apps/collection/enums.py+++ b/src/hi/apps/collection/enums.py@@ -9,15 +9,6 @@ class CollectionType(LabeledEnum):- SVG file is needed for each of these, else will use a default.- SVG filename is by convention:"""- def __init__( self,- label : str,- description : str,- is_path : bool = True,- is_path_closed : bool = True ):- super().__init__( label, description )- self.is_path = is_path- self.is_path_closed = is_path_closed- returnAPPLIANCES = ( 'Appliances', '' )DEVICES = ( 'Devices', '' )@@ -30,7 +21,3 @@ class CollectionType(LabeledEnum):@classmethoddef default(cls):return cls.OTHER-- @property- def is_icon(self):- return bool( not self.is_path )diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.pyindex f34b4f34..e39028a3 100644--- a/src/hi/apps/entity/entity_manager.py+++ b/src/hi/apps/entity/entity_manager.py@@ -5,6 +5,7 @@ from django.db import transactionfrom hi.apps.common.singleton import Singletonfrom hi.integrations.core.enums import IntegrationTypefrom hi.apps.location.models import Location, LocationView+from hi.apps.location.svg_item_factory import SvgItemFactoryfrom .enums import (EntityType,@@ -19,8 +20,6 @@ from .models import (class EntityManager(Singleton):- PATH_EDIT_NEW_PATH_RADIUS_PERCENT = 5.0 # Preferrable if this matches Javascript new path sizing.-def __init_singleton__(self):return@@ -84,10 +83,12 @@ class EntityManager(Singleton):with transaction.atomic():# Need to make sure it has some visible representation in the view if none exists.- if entity.entity_type.is_path:+ svg_item_type = SvgItemFactory().get_svg_item_type( entity )+ if svg_item_type.is_path:self.add_entity_path_if_needed(entity = entity,location_view = location_view,+ is_path_closed = svg_item_type.is_path_closed,)else:self.add_entity_position_if_needed(@@ -119,9 +120,9 @@ class EntityManager(Singleton):return- def add_entity_position_if_needed( self, entity : Entity, location_view : LocationView ) -> EntityPosition:- assert not entity.entity_type.is_path-+ def add_entity_position_if_needed( self,+ entity : Entity,+ location_view : LocationView ) -> EntityPosition:try:_ = EntityPosition.objects.get(location = location_view.location,@@ -145,9 +146,10 @@ class EntityManager(Singleton):)return entity_position- def add_entity_path_if_needed( self, entity : Entity, location_view : LocationView ) -> EntityPath:- assert entity.entity_type.is_path-+ def add_entity_path_if_needed( self,+ entity : Entity,+ location_view : LocationView,+ is_path_closed : bool ) -> EntityPath:try:_ = EntityPath.objects.get(location = location_view.location,@@ -157,35 +159,10 @@ class EntityManager(Singleton):except EntityPath.DoesNotExist:pass- # Note that this server-side creation of a new path is just one- # place new paths can be created. During client-side path editing,- # the Javascript code also uses logic to add new path segments.- # These do not have to behave identical, but it is preferrable for- # there to be some consistency.-- # Default display a line or rectangle in middle of current view with radius X% of viewbox- center_x = location_view.svg_view_box.x + ( location_view.svg_view_box.width / 2.0 )- center_y = location_view.svg_view_box.y + ( location_view.svg_view_box.height / 2.0 )- radius_x = location_view.svg_view_box.width * ( self.PATH_EDIT_NEW_PATH_RADIUS_PERCENT / 100.0 )- radius_y = location_view.svg_view_box.height * ( self.PATH_EDIT_NEW_PATH_RADIUS_PERCENT / 100.0 )-- if entity.entity_type.is_path_closed:- top_left_x = center_x - radius_x- top_left_y = center_y - radius_y- top_right_x = center_x + radius_x- top_right_y = center_y - radius_y- bottom_right_x = center_x + radius_x- bottom_right_y = center_y + radius_y- bottom_left_x = center_x - radius_x- bottom_left_y = center_y + radius_y- svg_path = f'M {top_left_x},{top_left_y} L {top_right_x},{top_right_y} L {bottom_right_x},{bottom_right_y} L {bottom_left_x},{bottom_left_y} Z'- else:- start_x = center_x - radius_x- start_y = center_y- end_x = start_x + radius_x- end_y = start_y- svg_path = f'M {start_x},{start_y} L {end_x},{end_y}'-+ svg_path = SvgItemFactory().get_default_svg_path_str(+ location_view = location_view,+ is_path_closed = is_path_closed,+ )entity_path = EntityPath.objects.create(entity = entity,location = location_view.location,diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/entity/enums.pyindex 09493315..6bd70269 100644--- a/src/hi/apps/entity/enums.py+++ b/src/hi/apps/entity/enums.py@@ -8,33 +8,24 @@ class EntityType(LabeledEnum):- SVG file is needed for each of these, else will use a default.- SVG filename is by convention:"""- def __init__( self,- label : str,- description : str,- is_path : bool = False,- is_path_closed : bool = False ):- super().__init__( label, description )- self.is_path = is_path- self.is_path_closed = is_path_closed- returnAIR_CONDITIONER = ( 'Air Conditioner', '' ) # Controls areaAPPLIANCE = ( 'Appliance', '' )- AREA = ( 'Area', '', True, True )+ AREA = ( 'Area', '' )AUDIO_AMPLIFIER = ( 'Audio Amplifier', '' ) # Controls SpeakerAUDIO_PLAYER = ( 'Audio Player', '' )BAROMETER = ( 'Barometer', '' )CAMERA = ( 'Camera', '' )COMPUTER = ( 'Computer', '' )CONSUMABLE = ( 'Consumable', '' )- CONTROL_WIRE = ( 'Control Wire', '', True )+ CONTROL_WIRE = ( 'Control Wire', '' )DISPLAY = ( 'Display', '' )DOOR = ( 'Door', '' )DOOR_LOCK = ( 'Door Lock', '' ) # Controls doorsELECTRICAL_OUTLET = ( 'Electrical Outlet', '' )ELECTRICY_METER = ( 'Electric Meter', '' )ELECTRIC_PANEL = ( 'Electric Panel', '' )- ELECTRIC_WIRE = ( 'Electric Wire', '', True )+ ELECTRIC_WIRE = ( 'Electric Wire', '' )HEALTHCHECK = ( 'Healthcheck', '' )HEATER = ( 'Heater', '' ) # Controls areaHVAC_AIR_HANDLER = ( 'HVAC Air Handler', '' ) # Controls area@@ -48,16 +39,16 @@ class EntityType(LabeledEnum):OPEN_CLOSE_DETECTOR = ( 'Open/Close Sensor', '' )OTHER = ( 'Other', '' ) # Will use generic visual elementPRESENCE_SENSOR = ( 'Presence Sensor', '' )- SEWER_LINE = ( 'Sewer Wire', '', True )+ SEWER_LINE = ( 'Sewer Wire', '' )SHOWER = ( 'Shower', '' )SINK = ( 'Sink', '' )SPEAKER = ( 'Speaker', '' )SPINKLER_CONTROLLER = ( 'Spinkler Controller', '' )SPINKLER_VALVE = ( 'Spinkler Valve', '' ) # Controls sprinkler headsSPRINKLER_HEAD = ( 'Sprinkler Head', '' )- SPRINKLER_WIRE = ( 'Sprinkler Wire', '', True )+ SPRINKLER_WIRE = ( 'Sprinkler Wire', '' )TELECOM_BOX = ( 'Telecom Box', '' )- TELECOM_WIRE = ( 'Telecom Wire', '', True )+ TELECOM_WIRE = ( 'Telecom Wire', '' )THERMOMETER = ( 'Thermometer', '' )THERMOSTAT = ( 'Thermostat', '' )TIME_SOURCE = ( 'Time Source', '' )@@ -65,9 +56,9 @@ class EntityType(LabeledEnum):TOOL = ( 'Tool', '' )VIDEO_PLAYER = ( 'Video Player', '' )WALL_SWITCH = ( 'Wall Switch', '' )- WASTE_PIPE = ( 'Waste Pipe', '', True )+ WASTE_PIPE = ( 'Waste Pipe', '' )WATER_HEATER = ( 'Water Heater', '' )- WATER_LINE = ( 'Water Line', '', True )+ WATER_LINE = ( 'Water Line', '' )WATER_METER = ( 'Water Meter', '' )WATER_SHUTOFF_VALVE = ( 'Water Shutoff Valve', '' )WEATHER_STATION = ( 'Weather Station', '' )@@ -76,10 +67,6 @@ class EntityType(LabeledEnum):@classmethoddef default(cls):return cls.OTHER-- @property- def is_icon(self):- return bool( not self.is_path )class EntityStateType(LabeledEnum):diff --git a/src/hi/apps/location/enums.py b/src/hi/apps/location/enums.pyindex d39429f0..bde8c239 100644--- a/src/hi/apps/location/enums.py+++ b/src/hi/apps/location/enums.py@@ -5,3 +5,24 @@ class LocationViewType(LabeledEnum):CONTROL = ('Control', '' )INFORMATION = ('Information', '' )+++class SvgItemType(LabeledEnum):++ ICON = ( 'Icon', '' )+ OPEN_PATH = ( 'Open Path ', '' )+ CLOSED_PATH = ( 'Closed Path ', '' )++ @property+ def is_icon(self):+ return bool( self == SvgItemType.ICON )++ @property+ def is_path(self):+ return bool( self in [ SvgItemType.OPEN_PATH, SvgItemType.CLOSED_PATH ] )++ @property+ def is_path_closed(self):+ return bool( self == SvgItemType.CLOSED_PATH )++diff --git a/src/hi/apps/location/svg_item_factory.py b/src/hi/apps/location/svg_item_factory.pyindex f5018547..90410b36 100644--- a/src/hi/apps/location/svg_item_factory.py+++ b/src/hi/apps/location/svg_item_factory.py@@ -1,14 +1,22 @@from hi.apps.common.singleton import Singletonfrom hi.apps.common.svg_models import SvgIconItem, SvgPathItem, SvgViewBox+from hi.apps.collection.enums import CollectionType+from hi.apps.collection.models import Collection+from hi.apps.entity.enums import EntityType+from hi.apps.entity.models import Entity+from hi.apps.location.enums import SvgItemTypefrom hi.apps.location.models import (LocationItemModelMixin,LocationItemPositionModel,LocationItemPathModel,+ LocationView,)class SvgItemFactory( Singleton ):+ NEW_PATH_RADIUS_PERCENT = 5.0 # Preferrable if this matches Javascript new path sizing.+def __init_singleton__(self):return@@ -35,3 +43,64 @@ class SvgItemFactory( Singleton ):stroke_width = 5.0,fill_color = 'none',)++ def get_svg_item_type( self, obj ) -> SvgItemType:+ if isinstance( obj, Entity ):+ entity_type = obj.entity_type++ if entity_type in [ EntityType.CONTROL_WIRE,+ EntityType.ELECTRIC_WIRE,+ EntityType.SEWER_LINE,+ EntityType.SPRINKLER_WIRE,+ EntityType.TELECOM_WIRE,+ EntityType.WASTE_PIPE,+ EntityType.WATER_LINE, ]:+ return SvgItemType.OPEN_PATH++ if entity_type in [ EntityType.AREA ]:+ return SvgItemType.CLOSED_PATH++ return SvgItemType.ICON++ elif isinstance( obj, Collection ):+ # Future colection types could leverage other SVG item types+ return SvgItemType.CLOSED_PATH++ else:+ return SvgItemType.ICON++ def get_default_svg_path_str( self,+ location_view : LocationView,+ is_path_closed : bool ) -> str:++ # Note that this server-side creation of a new path is just one+ # place new paths can be created. During client-side path editing,+ # the Javascript code also uses logic to add new path segments.+ # These do not have to behave identical, but it is preferrable for+ # there to be some consistency.++ # Default display a line or rectangle in middle of current view with radius X% of viewbox+ center_x = location_view.svg_view_box.x + ( location_view.svg_view_box.width / 2.0 )+ center_y = location_view.svg_view_box.y + ( location_view.svg_view_box.height / 2.0 )+ radius_x = location_view.svg_view_box.width * ( self.NEW_PATH_RADIUS_PERCENT / 100.0 )+ radius_y = location_view.svg_view_box.height * ( self.NEW_PATH_RADIUS_PERCENT / 100.0 )++ if is_path_closed:+ top_left_x = center_x - radius_x+ top_left_y = center_y - radius_y+ top_right_x = center_x + radius_x+ top_right_y = center_y - radius_y+ bottom_right_x = center_x + radius_x+ bottom_right_y = center_y + radius_y+ bottom_left_x = center_x - radius_x+ bottom_left_y = center_y + radius_y+ svg_path = f'M {top_left_x},{top_left_y} L {top_right_x},{top_right_y} L {bottom_right_x},{bottom_right_y} L {bottom_left_x},{bottom_left_y} Z'+ else:+ start_x = center_x - radius_x+ start_y = center_y+ end_x = start_x + radius_x+ end_y = start_y+ svg_path = f'M {start_x},{start_y} L {end_x},{end_y}'++ return svg_path+