Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80099656
gui.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Aug 29, 21:25
Size
7 KB
Mime Type
text/x-python
Expires
Sat, Aug 31, 21:25 (2 d)
Engine
blob
Format
Raw Data
Handle
20295976
Attached To
R3852 EMS for Smart-Building
gui.py
View Options
__author__
=
'Olivier Van Cutsem'
from
multiprocessing
import
Queue
import
matplotlib.pyplot
as
plt
from
building_data_management.category_management.category_config
import
*
from
ems_config
import
*
class
EMS_gui
():
"""
User interface for EMS class
"""
MSG_TYPE_KEY
=
'MSG_TYPE'
MSG_TYPE_INIT
=
'init_gui'
MSG_TYPE_ADD
=
'new_data'
MSG_TYPE_PLOT
=
'plot_data'
MSG_CONTENT_DATA
=
'data'
MSG_CONTENT_TIME
=
'timestamp'
MSG_EMS_STATE_STRUCT_ENERGY
=
'energy'
MSG_EMS_STATE_STRUCT_COMFORT
=
'comfort'
MSG_EMS_STATE_STRUCT_ENV
=
'environment'
def
__init__
(
self
):
self
.
data_storage
=
EMS_gui
.
get_data_format
()
# this variable storage all the incoming data
def
process_message
(
self
,
msg
):
"""
:param msg:
:return:
"""
if
msg
is
None
or
EMS_gui
.
MSG_TYPE_KEY
not
in
msg
:
return
type_msg
=
msg
[
self
.
MSG_TYPE_KEY
]
if
type_msg
==
self
.
MSG_TYPE_ADD
:
#print("GUI received: {0}".format(msg))
self
.
analyse_data
(
msg
[
self
.
MSG_CONTENT_TIME
],
msg
[
self
.
MSG_CONTENT_DATA
])
elif
type_msg
==
self
.
MSG_TYPE_PLOT
:
self
.
show_data
()
def
analyse_data
(
self
,
t
,
data
):
"""
Store incoming data
:param data:
:return:
"""
# Analyse the received ems state data
# Energy data
if
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
in
data
.
keys
():
energy_data
=
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
]
obj_energy
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
]
for
type_entity
in
energy_data
.
keys
():
#TODO: remove the if ... else !!
if
type_entity
!=
EMS_CATEGORY_ENTITY_STORAGE
:
# just power
for
name_entity
in
energy_data
[
type_entity
]
.
keys
():
new_data
=
(
t
,
energy_data
[
type_entity
][
name_entity
])
# pair (time, value)
if
name_entity
in
obj_energy
[
type_entity
]
.
keys
():
# already registered entity
obj_energy
[
type_entity
][
name_entity
]
.
append
(
new_data
)
else
:
# otherwise create a new list
obj_energy
[
type_entity
][
name_entity
]
=
[
new_data
]
else
:
for
name_entity
in
energy_data
[
type_entity
][
EMS_ENERGY_power
]
.
keys
():
new_data_p
=
(
t
,
energy_data
[
type_entity
][
EMS_ENERGY_power
][
name_entity
])
# pair (time, value)
if
name_entity
in
obj_energy
[
type_entity
][
EMS_ENERGY_power
]
.
keys
():
# already registered entity
obj_energy
[
type_entity
][
EMS_ENERGY_power
][
name_entity
]
.
append
(
new_data_p
)
else
:
# otherwise create a new list
obj_energy
[
type_entity
][
EMS_ENERGY_power
][
name_entity
]
=
[
new_data_p
]
for
name_entity
in
energy_data
[
type_entity
][
EMS_ENERGY_soc
]
.
keys
():
new_data_soc
=
(
t
,
energy_data
[
type_entity
][
EMS_ENERGY_soc
][
name_entity
])
# pair (time, value)
if
name_entity
in
obj_energy
[
type_entity
][
EMS_ENERGY_soc
]
.
keys
():
# already registered entity
obj_energy
[
type_entity
][
EMS_ENERGY_soc
][
name_entity
]
.
append
(
new_data_soc
)
else
:
# otherwise create a new list
obj_energy
[
type_entity
][
EMS_ENERGY_soc
][
name_entity
]
=
[
new_data_soc
]
# Comfort data
if
EMS_gui
.
MSG_EMS_STATE_STRUCT_COMFORT
in
data
.
keys
():
comfort_data
=
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_COMFORT
]
obj_comfort
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_COMFORT
]
for
room_name
in
comfort_data
.
keys
():
if
room_name
not
in
obj_comfort
.
keys
():
# first time seeing this room?
obj_comfort
[
room_name
]
=
{}
for
comfort_type
in
comfort_data
[
room_name
]
.
keys
():
new_data
=
(
t
,
comfort_data
[
room_name
][
comfort_type
])
# pair (time, value)
if
comfort_type
in
obj_comfort
[
room_name
]
.
keys
():
# already registered entity
obj_comfort
[
room_name
][
comfort_type
]
.
append
(
new_data
)
else
:
# otherwise create a new list
obj_comfort
[
room_name
][
comfort_type
]
=
[
new_data
]
# Environment data
if
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENV
in
data
.
keys
():
env_data
=
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENV
]
obj_env
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENV
]
for
env_data_type
in
env_data
.
keys
():
new_data_env
=
(
t
,
env_data
[
env_data_type
])
# pair (time, value)
if
env_data_type
in
obj_env
.
keys
():
# already registered entity
obj_env
[
env_data_type
]
.
append
(
new_data_env
)
else
:
# otherwise create a new list
obj_env
[
env_data_type
]
=
[
new_data_env
]
def
show_data
(
self
):
"""
Plot the accumulated data
:return:
"""
# Print all the load consumption, storage energy and generation production
loads_list
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_LOAD
]
for
l_name
in
loads_list
.
keys
():
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
([
t
for
(
t
,
v
)
in
loads_list
[
l_name
]],
[
v
for
(
t
,
v
)
in
loads_list
[
l_name
]])
plt
.
title
(
l_name
)
generation_list
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_GENERATION
]
for
g_name
in
generation_list
.
keys
():
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
([
t
for
(
t
,
v
)
in
generation_list
[
g_name
]],
[
v
for
(
t
,
v
)
in
generation_list
[
g_name
]])
plt
.
title
(
g_name
)
storage_list
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_STORAGE
][
EMS_ENERGY_soc
]
for
s_name
in
storage_list
.
keys
():
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
([
t
for
(
t
,
v
)
in
storage_list
[
s_name
]],
[
v
for
(
t
,
v
)
in
storage_list
[
s_name
]])
plt
.
title
(
s_name
)
# Show the temperature and luminosity
room_list
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_COMFORT
]
for
r_name
in
room_list
.
keys
():
for
comf_type
in
room_list
[
r_name
]:
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
([
t
for
(
t
,
v
)
in
room_list
[
r_name
][
comf_type
]],
[
v
for
(
t
,
v
)
in
room_list
[
r_name
][
comf_type
]])
plt
.
title
(
r_name
+
"."
+
comf_type
)
# Show the price of electricity
env_data
=
self
.
data_storage
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENV
]
for
env_data_type
in
env_data
.
keys
():
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
([
t
for
(
t
,
v
)
in
env_data
[
env_data_type
]],
[
v
for
(
t
,
v
)
in
env_data
[
env_data_type
]])
plt
.
title
(
env_data_type
)
plt
.
show
()
# DELETE THE DATA
self
.
data_storage
=
self
.
get_data_format
()
@staticmethod
def
get_data_format
():
data
=
dict
()
# loads, storage, generation
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
]
=
{}
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_LOAD
]
=
{}
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_STORAGE
]
=
{}
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_STORAGE
][
EMS_ENERGY_power
]
=
{}
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_STORAGE
][
EMS_ENERGY_soc
]
=
{}
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENERGY
][
EMS_CATEGORY_ENTITY_GENERATION
]
=
{}
# rooms
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_COMFORT
]
=
{}
# Environement
data
[
EMS_gui
.
MSG_EMS_STATE_STRUCT_ENV
]
=
{}
return
data
Event Timeline
Log In to Comment