Skip to content

Commit

Permalink
stateengine: improve visu compactness
Browse files Browse the repository at this point in the history
  • Loading branch information
onkelandy committed Nov 26, 2024
1 parent cf64c25 commit dc9a928
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
60 changes: 40 additions & 20 deletions stateengine/StateEngineWebif.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@
class WebInterface(StateEngineTools.SeItemChild):
# Constructor
# abitem: parent SeItem instance

@property
def width(self):
return self.__widthfactor

@width.setter
def width(self, value):
self.__widthfactor = value

@property
def height(self):
return self.__heightfactor

@height.setter
def height(self, value):
self.__heightfactor = value

def __init__(self, abitem):
super().__init__(abitem)

Expand All @@ -48,11 +65,13 @@ def __init__(self, abitem):
self.__graph = pydotplus.Dot('StateEngine', graph_type='digraph', splines='false',
overlap='scale', compound='false', imagepath='{}'.format(self.__img_path))
self.__graph.set_node_defaults(color='lightgray', style='filled', shape='box',
fontname='Helvetica', fontsize='10')
fontname='Helvetica', fontsize='10', pin='true')
self.__graph.set_edge_defaults(color='darkgray', style='filled', shape='box',
fontname='Helvetica', fontsize='10')
fontname='Helvetica', fontsize='10', len=0.1)
self.__graph.set_graph_defaults(sep=+0)
self.__nodes = {}
self.__scalefactor = 0.1
self.__heightfactor = 1
self.__widthfactor = 1
self.__textlimit = 105
self.__conditionset_count = 0

Expand Down Expand Up @@ -314,7 +333,7 @@ def _add_actioncondition(self, state, conditionset, action_type, new_y, cond1, c

label = 'first enter' if action_type in ['actions_enter', 'actions_enter_or_stay'] else 'staying at state'

position = '{},{}!'.format(0.38, new_y)
position = '{},{}!'.format(0.25 * self.__widthfactor, new_y)
color = color_enter if label == 'first enter' else color_stay
self.__nodes['{}_{}_state_{}'.format(state, conditionset, action_type)] = \
pydotplus.Node('{}_{}_state_{}'.format(state, conditionset, action_type), style="filled", fillcolor=color,
Expand Down Expand Up @@ -360,7 +379,7 @@ def drawgraph(self, filename):
color = "olivedrab" if state == self.__active_state \
else "gray" if i > list_index else "indianred2"

new_y -= 1 * self.__scalefactor
new_y -= 0.03 * self.__heightfactor
position = '{},{}!'.format(0, new_y)
if not i == 0:
condition_node = 'pass' if self.__nodes.get('{}_pass'.format(previous_state)) \
Expand All @@ -370,7 +389,7 @@ def drawgraph(self, filename):
self.__nodes['{}_above'.format(state)] = pydotplus.Node('{}_above'.format(state), pos=position,
shape="square", width="0", label="")
self.__graph.add_node(self.__nodes['{}_above'.format(state)])
position = '{},{}!'.format(0.3, new_y)
position = '{},{}!'.format(0.2 * self.__widthfactor, new_y)
self.__nodes['{}_above_right'.format(state)] = pydotplus.Node('{}_above_right'.format(state),
pos=position, shape="square", width="0", label="")
self.__graph.add_node(self.__nodes['{}_above_right'.format(state)])
Expand All @@ -381,7 +400,7 @@ def drawgraph(self, filename):
style='bold', color='black', label="False ", dir="none"))
self.__graph.add_edge(pydotplus.Edge(state, self.__nodes['{}_above'.format(state)], style='bold',
color='black', label="", dir="back"))
new_y -= 1 * self.__scalefactor
new_y -= 0.03 * self.__heightfactor
position = '{},{}!'.format(0, new_y)
#self._log_debug('state: {} {}',state, position)

Expand All @@ -390,7 +409,7 @@ def drawgraph(self, filename):
label='<<table border="0"><tr><td>{}</td></tr><hr/><tr>'
'<td>{}</td></tr></table>>'.format(
state, self.__states[state]['name']))
position = '{},{}!'.format(0.3, new_y)
position = '{},{}!'.format(0.2 * self.__widthfactor, new_y)
self.__nodes['{}_right'.format(state)] = pydotplus.Node('{}_right'.format(state), pos=position,
shape="square", width="0", label="")
self.__graph.add_node(self.__nodes[state])
Expand Down Expand Up @@ -464,17 +483,18 @@ def drawgraph(self, filename):
actionlist_pass, action_tooltip_pass, action_tooltip_count_pass = \
self._actionlabel(state, 'actions_pass', conditionset, active)

new_y -= 1 * self.__scalefactor if j == 0 else 2 * self.__scalefactor
position = '{},{}!'.format(0.3, new_y)

conditionset_positions.append(new_y)
conditionlist, condition_tooltip, condition_tooltip_count = self._conditionlabel(state, conditionset)

nodeheight = len(re.findall(r"<tr>", conditionlist)) * 0.004
new_y -= (0.01 + nodeheight) * self.__heightfactor
position = '{},{}!'.format(0.2 * self.__widthfactor, new_y)
label = 'no condition' if conditionset == '' else conditionset
self.__nodes['{}_{}'.format(state, conditionset)] = pydotplus.Node(
'{}_{}'.format(state, conditionset), style="filled", fillcolor=color, shape="diamond",
label=label, pos=position)
#self._log_debug('Node {} {} drawn. Conditionlist {}', state, conditionset, conditionlist)
position = '{},{}!'.format(0.1, new_y)
position = '{},{}!'.format(0.06 * self.__widthfactor, new_y)
xlabel = '1 tooltip' if condition_tooltip_count == 1\
else '{} tooltips'.format(condition_tooltip_count)\
if condition_tooltip_count > 1 else ''
Expand All @@ -488,7 +508,7 @@ def drawgraph(self, filename):
self.__graph.add_edge(parenthesis_edge)
self.__graph.add_node(self.__nodes['{}_{}'.format(state, conditionset)])

new_x = 0.55
new_x = 0.35 * self.__widthfactor
if not actionlist_enter == '':
position = '{},{}!'.format(new_x, new_y)
xlabel = '1 tooltip' if action_tooltip_count_enter == 1\
Expand All @@ -503,7 +523,7 @@ def drawgraph(self, filename):
self._add_actioncondition(state, conditionset, 'actions_enter', new_y, cond1, cond2)

if not actionlist_stay == '':
new_y -= 0.05 if not actionlist_enter == '' else 0
new_y -= 0.03 * self.__heightfactor if not actionlist_enter == '' else 0
position = '{},{}!'.format(new_x, new_y)
xlabel = '1 tooltip' if action_tooltip_count_stay == 1\
else '{} tooltips'.format(action_tooltip_count_stay)\
Expand All @@ -516,7 +536,7 @@ def drawgraph(self, filename):
self.__graph.add_node(self.__nodes['{}_{}_actions_stay'.format(state, conditionset)])
self._add_actioncondition(state, conditionset, 'actions_stay', new_y, cond1, cond2)

position = '{},{}!'.format(0.55, new_y)
position = '{},{}!'.format(0.35 * self.__widthfactor, new_y)
cond1 = self.__nodes.get('{}_{}_actions_enter'.format(state, conditionset)) is None
cond2 = self.__nodes.get('{}_{}_actions_stay'.format(state, conditionset)) is None
cond3 = self.__nodes.get('{}_{}_actions_leave'.format(state, conditionset)) is None
Expand Down Expand Up @@ -550,8 +570,8 @@ def drawgraph(self, filename):
previous_conditionset = self.__nodes['{}_{}'.format(state, conditionset)]

if len(actions_leave) > 0:
new_y -= 1 * self.__scalefactor if j == 0 else 2 * self.__scalefactor
position = '{},{}!'.format(0.3, new_y)
new_y -= 0.06 * self.__heightfactor if j == 0 else 0.08 * self.__heightfactor
position = '{},{}!'.format(0.2 * self.__widthfactor, new_y)
try:
cond2 = i >= list(self.__states.keys()).index(self.__active_state)
except Exception as ex:
Expand Down Expand Up @@ -583,8 +603,8 @@ def drawgraph(self, filename):
previous_conditionset = self.__nodes['{}_leave'.format(state)]

if len(actions_pass) > 0:
new_y -= 1 * self.__scalefactor if j == 0 else 2 * self.__scalefactor
position = '{},{}!'.format(0.3, new_y)
new_y -= 0.06 * self.__heightfactor if j == 0 else 0.08 * self.__heightfactor
position = '{},{}!'.format(0.2 * self.__widthfactor, new_y)
try:
cond2 = i >= list(self.__states.keys()).index(self.__active_state)
except Exception:
Expand Down Expand Up @@ -616,5 +636,5 @@ def drawgraph(self, filename):

previous_state = state

result = self.__graph.write_svg(filename, prog='fdp')
result = self.__graph.write_svg(filename, prog='neato')
return result
6 changes: 4 additions & 2 deletions stateengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@


class StateEngine(SmartPlugin):
PLUGIN_VERSION = '2.2.0'
PLUGIN_VERSION = '2.2.1'

# Constructor
# noinspection PyUnusedLocal,PyMissingConstructor
Expand Down Expand Up @@ -207,10 +207,12 @@ def get_items(self):
finallist.append(self._items[i])
return finallist

def get_graph(self, abitem, graphtype='link'):
def get_graph(self, abitem, graphtype='link', width=1, height=1):
if isinstance(abitem, str):
abitem = self._items[abitem]
webif = StateEngineWebif.WebInterface(abitem)
webif.width = width
webif.height = height
try:
os.makedirs(self.path_join(self.get_plugin_dir(), 'webif/static/img/visualisations/'))
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions stateengine/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ plugin:
- Python Modul pydotplus: ``pip3 install pydotplus``\n
'
maintainer: onkelandy
tester: 'Morg42'
tester: 'Morg42, bmxp'
state: ready
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/1303071-stateengine-plugin-support

version: '2.2.0'
version: '2.2.1'
sh_minversion: '1.6'
multi_instance: False
classname: StateEngine
Expand Down
4 changes: 2 additions & 2 deletions stateengine/webif/templates/visu.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
border: 3px solid white;
padding: 10px;
overflow: visible;
width:100%;
width:98%;
height:100%;
z-index: -1;
visibility: hidden;
Expand Down Expand Up @@ -298,7 +298,7 @@

<div class="container-fluid m-2" id="visu_div">

{{ p.get_graph(item, 'graph') }}
{{ p.get_graph(item, 'graph', 1, 1) }}
</div>
{% else %}
<div style="margin-top:15px;">
Expand Down

0 comments on commit dc9a928

Please sign in to comment.