node-red-contrib-mysensors
Version:
Mysensors related nodes, for decoding / encoding mysensors serial protocol and MQTT topic, and wrapping arbitrary messages into mysensors compatible messages
28 lines (25 loc) • 713 B
text/typescript
import {
IMysensorsMsg,
INodeMessage,
IStrongMysensorsMsg,
MsgOrigin,
MysensorsCommand,
validateStrongMysensorsMsg,
} from '../mysensors-msg';
import { MysensorsMqtt } from './mysensors-mqtt';
import { MysensorsSerial } from './mysensors-serial';
export async function AutoDecode(
msg: Readonly<IMysensorsMsg>
): Promise<IStrongMysensorsMsg<MysensorsCommand> | undefined> {
if (validateStrongMysensorsMsg(msg)) {
return {
...msg,
origin: MsgOrigin.decoded,
};
}
if (!msg.topic) {
return new MysensorsSerial().decode(msg as INodeMessage);
} else {
return new MysensorsMqtt().decode(msg as INodeMessage);
}
}