I have a mosquitto MQTT broker on a Raspberry Pi. I have some topics from a client in this format SolutionCommand/state as an array 110001.
In other hand I can invoke some publish topics like SolutionControl/WP it can be on or off. So the MQTT client would subscribe to SolutionCommand/state first of all and publish to SolutionControl/WP after processing the value of what was received. But, Here while processing how can I hold on the value of switch until receiving the current value from SolutionCommand/state this process takes 7s to received the value.
The question is how can I hold on the state after publish for 7s?
Here is my code bellow:
const client = new Paho.MQTT.Client(process.REACT_APP_HOST, Number(process.REACT_APP_PORT), clientID);
if(client){
client.connect({
cleanSession : false,
onSuccess : ....
});
}
}, [client])
const onConnect = () => {
client.subscribe(topic1, { qos: 1 });
};
const onMessageArrived = (payload)=> {
if(payload.payloadString[0] == 1)
{
setSolutionPump('ON')
setIsEnabled(true)
}
else if(payload.payloadString[0] == 0){
setSolutionPump('OFF')
setIsEnabled(false)
}
const toggleSwitch1 = () => {
const topic = "SolutionControl/WP";
if(!isEnabled1) {
setIsEnabled1(previousState => !previousState);
client.publish(topic, "on", 1);
}
else {
setIsEnabled1(previousState => !previousState);
client.publish(topic, "off", 1);
}
}