Skip to content

Commit

Permalink
make sure MQTT topic is updated correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
3ll3d00d committed Dec 28, 2023
1 parent a67996f commit acaaecd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
23 changes: 9 additions & 14 deletions cmdserver/infoprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, config: Config, ws_server: WsServer, mqtt: Optional[MQTT]):
'port': self.__ms.port,
'token': '',
'ssl': False,
'alive': True
'alive': False
},
'zones': {}
}
Expand All @@ -61,7 +61,7 @@ def __init__(self, config: Config, ws_server: WsServer, mqtt: Optional[MQTT]):
self.__mqtt.offline(self.__mqtt_name)

def __state_to_str(self) -> str:
return json.dumps(self.__current_state, ensure_ascii=False)
return json.dumps(self.info, ensure_ascii=False)

@property
def info(self):
Expand Down Expand Up @@ -91,7 +91,8 @@ def __async_refresh(self):
else:
zd = self.__zone_to_dict(z, None)
zones_data[z.id] = zd
playing = self.__current_state.get('playingCommand', {})
playing = self.info.get('playingCommand', {})
was_alive = self.info['config']['alive']
self.__current_state = {
'config': {
'host': self.__ms.local_ip,
Expand All @@ -104,11 +105,12 @@ def __async_refresh(self):
'playingCommand': playing
}
last_active_zone_id = playing.get('id', '')
playing['active'] = self.get_active_command(self.__current_state['zones'].get(active_zone.id, None))
playing['active'] = self.get_active_command(zones_data.get(active_zone.id, None))
if active_zone.id:
playing['id'] = active_zone.id
self.__ws_server.broadcast(self.__state_to_str())
self.__broadcast_up()
if not was_alive:
self.__mqtt.online(self.__mqtt_name)
if active_zone.id != last_active_zone_id:
self.__mqtt.publish(self.__mqtt_name, json.dumps({'zone': active_zone.id}))
except ConnectTimeout as e:
Expand All @@ -133,15 +135,8 @@ def __async_refresh(self):
logger.exception(f"Unexpected failure to refresh current state of {self.__ms.address()}")
self.__broadcast_down()

def __broadcast_up(self):
was_alive = not self.__current_state or self.__current_state.get('config', {}).get('alive', True) is True
if not was_alive:
logger.warning(f"Broadcasting MC is UP")
if self.__mqtt:
self.__mqtt.online(self.__mqtt_name)

def __broadcast_down(self):
was_alive = not self.__current_state or self.__current_state.get('config', {}).get('alive', True) is True
was_alive = self.__current_state['config']['alive'] is True
if was_alive:
logger.warning(f"Broadcasting MC is DOWN")
self.__current_state = {
Expand All @@ -153,7 +148,7 @@ def __broadcast_down(self):
'alive': False
},
'zones': self.__current_state['zones'] if self.__current_state else {},
'playingCommand': {'active': DEFAULT_ACTIVE_COMMAND}
'playingCommand': {'active': DEFAULT_ACTIVE_COMMAND, 'id': ''}
}
self.__ws_server.broadcast(self.__state_to_str())
if self.__mqtt:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ezmote-cmdserver"
version = "1.1.11"
version = "1.1.12"
description = "A small webapp which can be used for web based home cinema automation"
authors = ["3ll3d00d <mattkhan+cmdserver@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit acaaecd

Please # to comment.