要创建上行数据转换器,请进入Data Converters页面,点击Add new data converter -> Create new converter,将其命名为“Uplink data converter for Particle Integration”,并选择类型为Uplink。目前请使用调试模式。
// Decode an uplink message from a buffer// payload - array of bytes// metadata - key/value object/** Decoder **/// decode payload to JSONvardata=decodeToJson(payload);if(data.coreid!="api"){vardeviceName=data.coreid;vardeviceType='Photon';vargroupName='Particle devices';varattributes={integrationName:metadata['integrationName']};vartelemetry={};if(data.data=='online'||data.data=='offline'){attributes.status=data.data;}else{telemetry.rawData=data.data;}// Result object with device/asset attributes/telemetry datavarresult={// Use deviceName and deviceType or assetName and assetType, but not both.deviceName:deviceName,deviceType:deviceType,groupName:groupName,attributes:attributes,telemetry:telemetry};/** Helper functions 'decodeToString' and 'decodeToJson' are already built-in **/returnresult;}
// Decode an uplink message from a buffer// payload - array of bytes// metadata - key/value object/** Decoder **/// decode payload to JSONvardata=decodeToJson(payload);if(data.coreid!="api"){vardeviceName=data.coreid;vardeviceType='Photon';vargroupName='Particle devices';varattributes={integrationName:metadata['integrationName']};vartelemetry={};if(data.data=='online'||data.data=='offline'){attributes.status=data.data;}else{telemetry.rawData=data.data;}// Result object with device/asset attributes/telemetry datavarresult={// Use deviceName and deviceType or assetName and assetType, but not both.deviceName:deviceName,deviceType:deviceType,groupName:groupName,attributes:attributes,telemetry:telemetry};/** Helper functions 'decodeToString' and 'decodeToJson' are already built-in **/returnresult;}/** Helper functions **/functiondecodeToString(payload){returnString.fromCharCode.apply(String,payload);}functiondecodeToJson(payload){// covert payload to string.varstr=decodeToString(payload);// parse string to JSONvardata=JSON.parse(str);returndata;}
下行配置(可选)
创建下行数据转换器
下行数据转换器用于转换传出的RPC消息,然后集成将其发送至您的设备。
创建另一个转换器,命名为“Downlink data converter for Particle Integration”,类型选择Downlink。要查看事件,请启用调试模式。