feat(mqtt): update notification use websocket
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user