feat(mqtt): update notification use websocket

This commit is contained in:
zain94rif
2026-01-14 11:26:39 +07:00
parent c0fe3aaca1
commit 2b3d8ea3d2
2 changed files with 30 additions and 3 deletions

View File

@@ -10,7 +10,8 @@ const topics = [
'PIU_COD/COMPRESSOR/OVERVIEW',
'PIU_COD/COMPRESSOR/COMPRESSOR_A',
'PIU_COD/COMPRESSOR/COMPRESSOR_B',
'PIU_COD/COMPRESSOR/COMPRESSOR_C'
'PIU_COD/COMPRESSOR/COMPRESSOR_C',
'PIU_COD/ERROR_CODE/SIM',
];
const options = {
keepalive: 30,
@@ -98,4 +99,22 @@ const setValSvg = (listenTopic, svg) => {
});
};
export { publishMessage, listenMessage, setValSvg };
// === NOTIFICATION LISTENER ===
const notifListeners = [];
const onNotifUpdate = (callback) => {
notifListeners.push(callback);
};
client.on('message', (topic, message) => {
if (topic === import.meta.env.VITE_MQTT_TOPIC_COD) {
try {
const payload = JSON.parse(message.toString());
notifListeners.forEach((cb) => cb(payload));
} catch (err) {
console.error('Invalid notif payload', err);
}
}
});
export { publishMessage, listenMessage, setValSvg, onNotifUpdate };