createDpKit(option?: CreateDpKitOptions)

Requires:
@ray-js/panel-sdk≥1.7.0

Description

Create a DP protocol suite that provides structured DP parsing, send interception, and enhancements such as throttling/debouncing

Parameters

ParameterTypeRequiredDescription
Nodp-kit configuration options

Return Value

Type: CreateDpKitResult
dp-kit instance, including methods such as init, interceptors, and destroy

Referenced Types

typeCreateDpKitResultThe result of createDpKit, containing a set of interceptors that can be attached directly to SmartDeviceModel, plus the `init` / `destroy` lifecycle methods required to initialize structured DP state.
PropertyTypeDescription
init(device: any) => voidInitialize device state; when used with useStructuredProps, call after the device model has finished initializing.
interceptorsRecord<string, any>dp-kit default interceptor configuration
onDeviceInfoUpdated(ctx: any) => (next: (data: any) => any) => (data: any) => anydp-kit onDeviceInfoUpdated interceptor; can be used standalone if needed.
onDpDataChange(ctx: any) => (next: (data: any) => any) => (data: any) => anydp-kit onDpDataChange interceptor; can be used standalone if needed.
publishDps(ctx: any) => (next: (dpState: Record<string, any>, options?: SendDpOption) => any) => (dpState: Record<string, any>, options?: SendDpOption) => anydp-kit publishDps interceptor; can be used standalone if needed.
destroy() => voidDestroy dp-kit and clean up resources such as flush timers and throttling/debouncing maps. Should be called when SmartDeviceModel is destroyed (e.g., in SdmProvider's useEffect cleanup).
typeDpValueDatapoint value type, which can be boolean, number, or string.
export type DpValue = boolean | number | string;

Examples

Basic usage.

import { SmartDeviceModel, createDpKit } from '@ray-js/panel-sdk';

const schema = [
  { code: 'switch_1', type: 'bool' },
  { code: 'colour_data', type: 'raw' },
] as const;

const dpKit = createDpKit<typeof schema>({
  protocols: {
    colour_data: {
      parser: raw => JSON.parse(raw),
      formatter: structured => JSON.stringify(structured),
    },
  },
});

const sdm = new SmartDeviceModel<typeof schema>({
  interceptors: dpKit.interceptors,
});

await sdm.init();
dpKit.init(sdm);

Includes protocol parsing and throttling

const dpKit = createDpKit<typeof schema>({
  protocols: {
    colour_data: {
      parser: raw => JSON.parse(raw),
      formatter: value => JSON.stringify(value),
    },
  },
  sendDpOption: {
    throttle: 300,
  },
});

const device = new SmartDeviceModel<typeof schema>({
  interceptors: dpKit.interceptors,
});

await device.init();
dpKit.init(device);

// All subsequent sends inherit 300ms throttling
await device.publishDps({
  colour_data: { hue: 240, saturation: 1000, value: 1000 },
});

Ignore device report responses

const dpKit = createDpKit<typeof schema>({
  sendDpOption: {
    immediate: true,
    ignoreDpDataResponse: {
      timeout: 5000,
      whiteDpCodes: ['switch_1'],
    },
  },
});

const device = new SmartDeviceModel<typeof schema>({
  interceptors: dpKit.interceptors,
});

Use with hooks

import { useStructuredActions, useStructuredProps } from '@ray-js/panel-sdk';

const colour = useStructuredProps(props => props.colour_data);
const actions = useStructuredActions();

actions.colour_data.set({
  hue: colour.hue,
  saturation: colour.saturation,
  value: 800,
});