Décodage des payloads - Community Edition
- // Decoder function for Heywaste Fill Level Sensor (community edition)
- // working with EU868
- // https://www.heyliot.com
- //
- // Your container configuration is stored here, you can pull it from your database...
- const containerConfiguration = {
- maxMeasure: 3000, // measure when container is empty = container depth (in millimeters)
- minMeasure: 0 // measure when container is full (in millimeters)
- };
- //This object will be filled by the payloadDecoder function
- let decodedPayload = {
- payloadType: null,
- nbrMeasure: null,
- measure: null,
- fillLevel: null,
- rangeStatus: null,
- };
- const extractPayloadType = (payload) => {
- const type = payload.substr(0, 1);
- switch (type) {
- case '1':
- case '3': // Periodic measure will be send in a regular interval according to the sensor configuration
- return "PERIODIC_MEASURE";
- case '2': // Manual measure need a push button
- return "MANUAL_MEASURE"
- case '0': // First uplink - not available in community edition
- case '4': // Daily configuration - not available in community edition
- default:
- return null;
- }
- }
- /**
- * By default, the sensor will send its measurements in millimeters
- * If you want to estimate a fill level in percentage, you have two options :
- * 1) use the raw measure in millimeters and calculate the fill level by yourself in your own app or IoT dashboard
- * 2) you can activate the fill level calculation function in this decoder by providing the max measure (= 0% ; container depth in millimeters) and the min measure (= measure when container is full)
- *
- * @param payload {string}
- * @param detectFillLevel {boolean} - If true we are going to d
- */
- const payloadDecoder = (payload, detectFillLevel = true) => {
- decodedPayload.payloadType = extractPayloadType(payload);
- decodedPayload.nbrMeasure = parseInt(payload.substr(1, 1));
- decodedPayload.rangeStatus = parseInt(payload.substr(2, 1));
- decodedPayload.measure = parseInt(payload.substr(3, 3), 16) * 2;
- if (detectFillLevel) {
- const maxMeasureOfContainer = containerConfiguration.maxMeasure;
- const minMeasureOfContainer = containerConfiguration.minMeasure;
- const distanceBetweenMinAndMax = maxMeasureOfContainer - minMeasureOfContainer;
- decodedPayload.fillLevel = 100 - Math.round((decodedPayload.measure - minMeasureOfContainer) * 100 / distanceBetweenMinAndMax);
- }
- return decodedPayload;
- };
- /**
- * You will get outputs :
- *
- * console.log(payload1) -->
- * {
- * payloadType: 'PERIODIC_MEASURE',
- * nbrMeasure: 1,
- * measure: 70,
- * fillLevel: null,
- * rangeStatus: 0
- * }
- *
- * console.log(payload2) -->
- * {
- * payloadType: 'MANUAL_MEASURE',
- * nbrMeasure: 2,
- * measure: 70,
- * fillLevel: 98,
- * rangeStatus: 0
- * }
- */
- const payload1 = payloadDecoder("11023", false);
- console.log(payload1);
- const payload2 = payloadDecoder("22023");
- console.log(payload2);