Skip to content

Home Information - Your home, your data, organized.

Python

Now displaying SVG styles on first load to match polling.

Anthony Cassandra13de0002f673
diff --git a/src/hi/apps/common/svg_models.py b/src/hi/apps/common/svg_models.py
index f5c88e69..cf465d4b 100644
--- a/src/hi/apps/common/svg_models.py
+++ b/src/hi/apps/common/svg_models.py
@@ -96,6 +96,7 @@ class SvgIconItem:
html_id : str
css_class : str
+ status_value : str
template_name : str
bounding_box : SvgViewBox
position_x : float
@@ -178,3 +179,21 @@ class SvgDecimalField( models.DecimalField ):
return value.quantize( precision, rounding = ROUND_HALF_UP )
+@dataclass
+class SvgStatusStyle:
+
+ status_value : str
+ stroke_color : str
+ stroke_width : float
+ fill_color : str
+ fill_opacity : float
+
+ def to_dict(self):
+ return {
+ 'status': self.status_value,
+ 'stroke': self.stroke_color,
+ 'stroke-width': self.stroke_width,
+ 'fill': self.fill_color,
+ 'fill-opacity': self.fill_opacity,
+ }
+
diff --git a/src/hi/apps/location/location_view_data.py b/src/hi/apps/location/location_view_data.py
index 9d65b1ef..e2cd2698 100644
--- a/src/hi/apps/location/location_view_data.py
+++ b/src/hi/apps/location/location_view_data.py
@@ -3,8 +3,9 @@ from typing import Dict, Generator, List, Set
from hi.apps.collection.models import Collection, CollectionPath, CollectionPosition
from hi.apps.common.svg_models import SvgIconItem, SvgPathItem
-from hi.apps.entity.models import Entity, EntityPosition, EntityPath, EntityState
+from hi.apps.entity.models import Entity, EntityPosition, EntityPath
from hi.apps.location.svg_item_factory import SvgItemFactory
+from hi.apps.monitor.status_display_data import StatusDisplayData
from hi.apps.monitor.transient_models import EntityStateStatusData
from .models import LocationView
@@ -27,28 +28,35 @@ class LocationViewData:
def __post_init__(self):
self._svg_item_factory = SvgItemFactory()
+ self._css_class_map = self._get_css_class_map()
+ self._latest_entity_state_status_data_map = self._get_latest_entity_state_status_data_map()
return
def svg_icon_items(self) -> Generator[ SvgIconItem, None, None ]:
for entity_position in self.entity_positions:
- entity_state_status_data_list = self.entity_to_entity_state_status_data_list.get(
+
+ css_class = self._css_class_map.get( entity_position.entity, '' )
+ latest_entity_state_status_data = self._latest_entity_state_status_data_map.get(
entity_position.entity,
)
- if entity_state_status_data_list is None:
- entity_state_status_data_list = list()
- entity_states = [ x.entity_state for x in entity_state_status_data_list ]
- if entity_states:
- css_class = ' '.join([ x.css_class for x in entity_states ])
+ if latest_entity_state_status_data:
+ status_display_data = StatusDisplayData(
+ entity_state_status_data = latest_entity_state_status_data,
+ )
+ svg_status_style = status_display_data.svg_status_style
else:
- css_class = ''
+ svg_status_style = None
+
svg_icon_item = self._svg_item_factory.create_svg_icon_item(
item = entity_position.entity,
position = entity_position,
css_class = css_class,
+ svg_status_style = svg_status_style,
)
yield svg_icon_item
continue
+
for collection_position in self.collection_positions:
svg_icon_item = self._svg_item_factory.create_svg_icon_item(
item = collection_position.collection,
@@ -62,23 +70,28 @@ class LocationViewData:
def svg_path_items(self) -> Generator[ SvgPathItem, None, None ]:
for entity_path in self.entity_paths:
- entity_state_status_data_list = self.entity_to_entity_state_status_data_list.get(
+
+ css_class = self._css_class_map.get( entity_path.entity, '' )
+ latest_entity_state_status_data = self._latest_entity_state_status_data_map.get(
entity_path.entity,
)
- if entity_state_status_data_list is None:
- entity_state_status_data_list = list()
- entity_states = [ x.entity_state for x in entity_state_status_data_list ]
- if entity_states:
- css_class = ' '.join([ x.css_class for x in entity_states ])
+ if latest_entity_state_status_data:
+ status_display_data = StatusDisplayData(
+ entity_state_status_data = latest_entity_state_status_data,
+ )
+ svg_status_style = status_display_data.svg_status_style
else:
- css_class = ''
+ svg_status_style = None
+
svg_path_item = self._svg_item_factory.create_svg_path_item(
item = entity_path.entity,
path = entity_path,
css_class = css_class,
+ svg_status_style = svg_status_style,
)
yield svg_path_item
continue
+
for collection_path in self.collection_paths:
svg_path_item = self._svg_item_factory.create_svg_path_item(
item = collection_path.collection,
@@ -89,4 +102,26 @@ class LocationViewData:
continue
return
-
+ def _get_css_class_map(self):
+ css_class_map = dict()
+ for entity, entity_state_status_data_list in self.entity_to_entity_state_status_data_list.items():
+ if not entity_state_status_data_list:
+ continue
+ entity_states = [ x.entity_state for x in entity_state_status_data_list ]
+ css_class_map[entity] = ' '.join([ x.css_class for x in entity_states ])
+ continue
+ return css_class_map
+
+ def _get_latest_entity_state_status_data_map(self):
+ latest_entity_state_status_data_map = dict()
+ for entity, entity_state_status_data_list in self.entity_to_entity_state_status_data_list.items():
+ if not entity_state_status_data_list:
+ continue
+ with_responses_list = [ x for x in entity_state_status_data_list if x.latest_sensor_response ]
+ if not with_responses_list:
+ continue
+ with_responses_list.sort( key = lambda item : item.latest_sensor_response.timestamp,
+ reverse = True )
+ latest_entity_state_status_data_map[entity] = with_responses_list[0]
+ continue
+ return latest_entity_state_status_data_map
diff --git a/src/hi/apps/location/svg_item_factory.py b/src/hi/apps/location/svg_item_factory.py
index c8fef292..36632032 100644
--- a/src/hi/apps/location/svg_item_factory.py
+++ b/src/hi/apps/location/svg_item_factory.py
@@ -1,8 +1,9 @@
from hi.apps.common.singleton import Singleton
-from hi.apps.common.svg_models import SvgIconItem, SvgPathItem, SvgViewBox
+from hi.apps.common.svg_models import SvgIconItem, SvgPathItem, SvgStatusStyle, SvgViewBox
from hi.apps.collection.models import Collection
from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity
+from hi.apps.monitor.status_display_data import StatusStyle
from .enums import SvgItemType
from .models import (
@@ -21,13 +22,17 @@ class SvgItemFactory( Singleton ):
return
def create_svg_icon_item( self,
- item : LocationItemModelMixin,
- position : LocationItemPositionModel,
- css_class : str ) -> SvgIconItem:
-
+ item : LocationItemModelMixin,
+ position : LocationItemPositionModel,
+ css_class : str,
+ svg_status_style : SvgStatusStyle = None ) -> SvgIconItem:
+ if not svg_status_style:
+ svg_status_style = StatusStyle.default()
+
return SvgIconItem(
html_id = item.html_id,
css_class = css_class,
+ status_value = svg_status_style.status_value,
position_x = float( position.svg_x ),
position_y = float( position.svg_y ),
rotate = float( position.svg_rotate ),
@@ -37,18 +42,21 @@ class SvgItemFactory( Singleton ):
)
def create_svg_path_item( self,
- item : LocationItemModelMixin,
- path : LocationItemPathModel,
- css_class : str ) -> SvgPathItem:
-
+ item : LocationItemModelMixin,
+ path : LocationItemPathModel,
+ css_class : str,
+ svg_status_style : SvgStatusStyle = None ) -> SvgPathItem:
+ if not svg_status_style:
+ svg_status_style = StatusStyle.default()
+
return SvgPathItem(
html_id = item.html_id,
css_class = css_class,
svg_path = path.svg_path,
- stroke_color = '#40f040',
- stroke_width = 5.0,
- fill_color = 'white',
- fill_opacity = 0.0,
+ stroke_color = svg_status_style.stroke_color,
+ stroke_width = svg_status_style.stroke_width,
+ fill_color = svg_status_style.fill_color,
+ fill_opacity = svg_status_style.fill_opacity,
)
def get_svg_item_type( self, obj ) -> SvgItemType:
diff --git a/src/hi/apps/monitor/status_display_data.py b/src/hi/apps/monitor/status_display_data.py
index e98bc35f..46b41276 100644
--- a/src/hi/apps/monitor/status_display_data.py
+++ b/src/hi/apps/monitor/status_display_data.py
@@ -1,5 +1,5 @@
import hi.apps.common.datetimeproxy as datetimeproxy
-from dataclasses import dataclass
+from hi.apps.common.svg_models import SvgStatusStyle
from hi.apps.entity.enums import EntityStateType
from hi.apps.sense.enums import SensorValue
@@ -7,112 +7,109 @@ from hi.apps.sense.enums import SensorValue
from .transient_models import EntityStateStatusData
-@dataclass
-class SvgStatusStyle:
-
- status_value : str
- stroke_color : str
- stroke_width : float
- fill_color : str
- fill_opacity : float
-
- def to_dict(self):
- return {
- 'status': self.status_value,
- 'stroke': self.stroke_color,
- 'stroke-width': self.stroke_width,
- 'fill': self.fill_color,
- 'fill-opacity': self.fill_opacity,
- }
-
-
class StatusStyle:
+ DEFAULT_STATUS_VALUE = ''
+ DEFAULT_STROKE_COLOR = '#40f040'
+ DEFAULT_STROKE_WIDTH = 5.0
+ DEFAULT_FILL_COLOR = 'white'
+ DEFAULT_FILL_OPACITY = 0.0
+
MovementActive = SvgStatusStyle(
status_value = 'active',
stroke_color = 'red',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'red',
fill_opacity = 0.5,
)
MovementRecent = SvgStatusStyle(
status_value = 'recent',
stroke_color = 'orange',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'orange',
fill_opacity = 0.5,
)
MovementPast = SvgStatusStyle(
status_value = 'past',
stroke_color = 'yellow',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'yellow',
fill_opacity = 0.5,
)
MovementIdle = SvgStatusStyle(
status_value = 'idle',
stroke_color = '#888888',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'white',
fill_opacity = 0.0,
)
On = SvgStatusStyle(
status_value = 'on',
stroke_color = 'green',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'green',
fill_opacity = 0.5,
)
Off = SvgStatusStyle(
status_value = 'off',
stroke_color = '#888888',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = '#888888',
fill_opacity = 0.5,
)
Open = SvgStatusStyle(
status_value = 'open',
stroke_color = 'green',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'green',
fill_opacity = 0.5,
)
Closed = SvgStatusStyle(
status_value = 'closed',
stroke_color = '#888888',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = '#888888',
fill_opacity = 0.5,
)
Connected = SvgStatusStyle(
status_value = 'connected',
stroke_color = 'green',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'green',
fill_opacity = 0.5,
)
Disconnected = SvgStatusStyle(
status_value = 'disconnected',
stroke_color = 'red',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'red',
fill_opacity = 0.5,
)
High = SvgStatusStyle(
status_value = 'high',
stroke_color = 'green',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'green',
fill_opacity = 0.5,
)
Low = SvgStatusStyle(
status_value = 'low',
stroke_color = 'red',
- stroke_width = None,
+ stroke_width = DEFAULT_STROKE_WIDTH,
fill_color = 'red',
fill_opacity = 0.5,
)
+ @classmethod
+ def default(cls):
+ return SvgStatusStyle(
+ status_value = cls.DEFAULT_STATUS_VALUE,
+ stroke_color = cls.DEFAULT_STROKE_COLOR,
+ stroke_width = cls.DEFAULT_STROKE_WIDTH,
+ fill_color = cls.DEFAULT_FILL_COLOR,
+ fill_opacity = cls.DEFAULT_FILL_OPACITY,
+ )
+
class StatusDisplayData:
@@ -135,7 +132,11 @@ class StatusDisplayData:
@property
def controller_data_list(self):
return self._controller_data_list
-
+
+ @property
+ def svg_status_style(self):
+ return self._svg_status_style
+
@property
def should_skip(self):
return bool( self._svg_status_style is None )
@@ -200,13 +201,17 @@ class StatusDisplayData:
# EntityStateType.TEMPERATURE
# EntityStateType.WATER_FLOW
# EntityStateType.WIND_SPEED
-
+
+ status_value = self.latest_sensor_value
+ if not status_value:
+ status_value = StatusStyle.DEFAULT_STATUS_VALUE
+
return SvgStatusStyle(
- status_value = self.latest_sensor_value,
- stroke_color = None,
- stroke_width = None,
- fill_color = None,
- fill_opacity = None,
+ status_value = status_value,
+ stroke_color = StatusStyle.DEFAULT_STROKE_COLOR,
+ stroke_width = StatusStyle.DEFAULT_STROKE_WIDTH,
+ fill_color = StatusStyle.DEFAULT_FILL_COLOR,
+ fill_opacity = StatusStyle.DEFAULT_FILL_OPACITY,
)
def _get_movement_status_style( self ):
diff --git a/src/hi/apps/monitor/transient_models.py b/src/hi/apps/monitor/transient_models.py
index 5592d44b..5f0935ad 100644
--- a/src/hi/apps/monitor/transient_models.py
+++ b/src/hi/apps/monitor/transient_models.py
@@ -17,7 +17,7 @@ class EntityStateStatusData:
if self.sensor_response_list:
return self.sensor_response_list[0]
return None
-
+
@dataclass
class EntityStatusData:
diff --git a/src/hi/enums.py b/src/hi/enums.py
index 3f5aa71b..c1cb6827 100644
--- a/src/hi/enums.py
+++ b/src/hi/enums.py
@@ -44,7 +44,7 @@ class ItemType(LabeledEnum):
# the dimensions being overlapping between subsets. This is mostly
# used on the front-ends Javascript where we try to hide model details
# and use more a more generic data model for 'items' that could be more
- # than backend model one type. When the Javascript needs to informt
+ # than one backend model type. When the Javascript needs to inform
# the server of an action, the server needs to be able to map that
# Javascript concept back to the specifics class.
diff --git a/src/hi/static/js/polling.js b/src/hi/static/js/polling.js
index 758c5f5a..324d8a57 100644
--- a/src/hi/static/js/polling.js
+++ b/src/hi/static/js/polling.js
@@ -10,7 +10,7 @@
window.Hi.polling = HiPolling;
window.addEventListener('load', function() {
- HiPolling.init();
+ HiPolling.init();
});
/*