useReceiveMessage.ts 626 Bytes
import { ReceiveTsSubCmdsGroupMessageType } from './socket/useSocket.type';

export const useReceiveMessage = () => {
  const forEachGroupMessage = (
    message: ReceiveTsSubCmdsGroupMessageType,
    deviceId: string,
    attributes: string[],
    Fn: (attribute: string, value: any, timespan: number) => void
  ) => {
    const { data = {} } = message;
    const bindMessage = data[deviceId];

    for (const attribute of attributes) {
      const [latest] = bindMessage[attribute] || [];
      const [timespan, value] = latest || [];
      Fn?.(attribute, value, timespan);
    }
  };

  return { forEachGroupMessage };
};