Skip to content

Commit

Permalink
Merge pull request #69 from MrAsterisco/handle-error-in-mqtt-callback
Browse files Browse the repository at this point in the history
Handle error in mqtt callback
  • Loading branch information
LeLunZ authored Sep 3, 2024
2 parents b244c71 + 0de516b commit 8cbe4a4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
10 changes: 8 additions & 2 deletions src/hisenseMQTTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class HisenseMQTTClient {

public mqttClient: mqtt.MqttClient;

constructor(public deviceConfig: Pick<DeviceConfig, 'sslmode' | 'ipaddress' | 'sslcertificate' | 'sslprivatekey'>, macaddress: string) {
constructor(public deviceConfig: Pick<DeviceConfig, 'sslmode' | 'ipaddress' | 'sslcertificate' | 'sslprivatekey'>, macaddress: string, private log: {error: (message: string) => void}) {
this._BASE_TOPIC = path.join('/', 'remoteapp', 'mobile');
this._STATE_TOPIC = path.join(this._BASE_TOPIC, 'broadcast', 'ui_service', 'state');
this._DEVICE_TOPIC = `${macaddress.toUpperCase()}$normal`;
Expand Down Expand Up @@ -52,7 +52,13 @@ export class HisenseMQTTClient {

public callService(service: string, action: string, payload?: string) {
const topic = path.join('/', 'remoteapp', 'tv', service, this._DEVICE_TOPIC, 'actions', action);
this.mqttClient.publish(topic, payload ?? '');
if(this.mqttClient.disconnected || this.mqttClient.disconnecting) {
this.log.error('Sending message to TV failed - MQTT client is disconnected');
this.log.error(Error().stack ?? '');
return;
}else {
this.mqttClient.publish(topic, payload ?? '');
}
}

public changeSource(sourceId: string) {
Expand Down
54 changes: 29 additions & 25 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class HiSenseTVAccessory {
// Create "Unknown" source.
this.createHomeSource();

this.mqttHelper = new HisenseMQTTClient(this.deviceConfig, accessory.context.macaddress);
this.mqttHelper = new HisenseMQTTClient(this.deviceConfig, accessory.context.macaddress, this.log);
this.setupMqtt();

// set the counter threshold based on the polling interval
Expand Down Expand Up @@ -163,32 +163,36 @@ export class HiSenseTVAccessory {
});


//
this.mqttHelper.mqttClient.on('message', (topic, message) => {
this.log.debug(`Received message from TV (${topic}):` + message.toString());
const parsedMessage = JSON.parse(message.toString());
switch (topic) {
case this.mqttHelper._STATE_TOPIC:
// handle tvType fakeSleep differently as it has a different state
if (this.deviceConfig.tvType === 'fakeSleep') {
// setCurrentInput will be called in setAlwaysOnFakeSleepState
this.setAlwaysOnFakeSleepState(parsedMessage);
} else {
this.setCurrentInput(parsedMessage);
}
break;
case this.mqttHelper._SOURCE_LIST_TOPIC:
this.createSources(parsedMessage, this.availableApps);
break;
case this.mqttHelper._PICTURE_SETTINGS_TOPIC:
this.setAlwaysOnPictureSettingsPowerState(parsedMessage);
break;
case this.mqttHelper._APP_LIST_TOPIC:
this.createSources(this.inputSources, parsedMessage);
break;
default:
this.log.debug('Received unknown message from TV. Topic: ' + topic + ' Message: ' + message.toString());
break;
try{
const parsedMessage = JSON.parse(message.toString());
switch (topic) {
case this.mqttHelper._STATE_TOPIC:
// handle tvType fakeSleep differently as it has a different state
if (this.deviceConfig.tvType === 'fakeSleep') {
// setCurrentInput will be called in setAlwaysOnFakeSleepState
this.setAlwaysOnFakeSleepState(parsedMessage);
} else {
this.setCurrentInput(parsedMessage);
}
break;
case this.mqttHelper._SOURCE_LIST_TOPIC:
this.createSources(parsedMessage, this.availableApps);
break;
case this.mqttHelper._PICTURE_SETTINGS_TOPIC:
this.setAlwaysOnPictureSettingsPowerState(parsedMessage);
break;
case this.mqttHelper._APP_LIST_TOPIC:
this.createSources(this.inputSources, parsedMessage);
break;
default:
this.log.debug('Received unknown message from TV. Topic: ' + topic + ' Message: ' + message.toString());
break;
}
}catch(e: unknown){
this.log.error('Error occurred while handling new message from TV: ' + message.toString());
this.log.error((e as Error).stack ?? (e as Error).toString());
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/alwaysOnTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let pictureSettingsOff: null|PictureSetting = null;
await rl.question('Turn your TV off now and press enter when ready: ');
rl.write('Wait for a few seconds...');
try {
const mqttHelper = new HisenseMQTTClient({sslmode: sslMode, ipaddress: hostname, sslcertificate: sslCertificate, sslprivatekey: sslPrivateKey}, macaddress);
const mqttHelper = new HisenseMQTTClient({sslmode: sslMode, ipaddress: hostname, sslcertificate: sslCertificate, sslprivatekey: sslPrivateKey}, macaddress, console);
const timeout = setTimeout(() => {
mqttHelper.mqttClient.end(true);
rl.write('Could not detect always on TV');
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const macaddress = values['mac'] as string;
const hostname = values['hostname'] as string;

try {
const mqttHelper = new HisenseMQTTClient({sslmode: sslMode, ipaddress: hostname, sslcertificate: sslCertificate, sslprivatekey: sslPrivateKey}, macaddress);
const mqttHelper = new HisenseMQTTClient({sslmode: sslMode, ipaddress: hostname, sslcertificate: sslCertificate, sslprivatekey: sslPrivateKey}, macaddress, console);

mqttHelper.mqttClient.on('connect', () => {
mqttHelper.callService('ui_service', 'gettvstate');
Expand Down

0 comments on commit 8cbe4a4

Please sign in to comment.