Décodage des payloads - Community Edition

Décodage des payloads - Community Edition

  1. // Decoder function for Heywaste Fill Level Sensor (community edition)
  2. // working with EU868
  3. // https://www.heyliot.com
  4. //

  5. // Your container configuration is stored here, you can pull it from your database...
  6. const containerConfiguration = {
  7.     maxMeasure: 3000, // measure when container is empty = container depth (in millimeters)
  8.     minMeasure: 0 // measure when container is full (in millimeters)
  9. };

  10. //This object will be filled by the payloadDecoder function
  11. let decodedPayload = {
  12.     payloadType: null,
  13.     nbrMeasure: null,
  14.     measure: null,
  15.     fillLevel: null,
  16.     rangeStatus: null,
  17. };

  18. const extractPayloadType = (payload) => {
  19.     const type = payload.substr(0, 1);

  20.     switch (type) {
  21.         case '1':
  22.         case '3': // Periodic measure will be send in a regular interval according to the sensor configuration
  23.             return "PERIODIC_MEASURE";
  24.         case '2': // Manual measure need a push button
  25.             return "MANUAL_MEASURE"
  26.         case '0': // First uplink - not available in community edition
  27.         case '4': // Daily configuration - not available in community edition
  28.         default:
  29.             return null;
  30.     }
  31. }

  32. /**
  33.  * By default, the sensor will send its measurements in millimeters
  34.  * If you want to estimate a fill level in percentage, you have two options :
  35.  * 1) use the raw measure in millimeters and calculate the fill level by yourself in your own app or IoT dashboard
  36.  * 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)
  37.  *
  38.  * @param payload {string}
  39.  * @param detectFillLevel {boolean} - If true we are going to d
  40.  */
  41. const payloadDecoder = (payload, detectFillLevel = true) => {

  42.     decodedPayload.payloadType = extractPayloadType(payload);
  43.     decodedPayload.nbrMeasure = parseInt(payload.substr(1, 1));
  44.     decodedPayload.rangeStatus = parseInt(payload.substr(2, 1));
  45.     decodedPayload.measure = parseInt(payload.substr(3, 3), 16) * 2;

  46.     if (detectFillLevel) {
  47.         const maxMeasureOfContainer = containerConfiguration.maxMeasure;
  48.         const minMeasureOfContainer = containerConfiguration.minMeasure;

  49.         const distanceBetweenMinAndMax = maxMeasureOfContainer - minMeasureOfContainer;

  50.         decodedPayload.fillLevel = 100 - Math.round((decodedPayload.measure - minMeasureOfContainer) * 100 / distanceBetweenMinAndMax);
  51.     }

  52.     return decodedPayload;
  53. };

  54. /**
  55.  * You will get outputs :
  56.  *
  57.  * console.log(payload1) -->
  58.  * {
  59.  *   payloadType: 'PERIODIC_MEASURE',
  60.  *   nbrMeasure: 1,
  61.  *   measure: 70,
  62.  *   fillLevel: null,
  63.  *   rangeStatus: 0
  64.  * }
  65.  *
  66.  * console.log(payload2) -->
  67.  * {
  68.  *   payloadType: 'MANUAL_MEASURE',
  69.  *   nbrMeasure: 2,
  70.  *   measure: 70,
  71.  *   fillLevel: 98,
  72.  *   rangeStatus: 0
  73.  * }
  74.  */

  75. const payload1 = payloadDecoder("11023", false);
  76. console.log(payload1);

  77. const payload2 = payloadDecoder("22023");
  78. console.log(payload2);
    • Related Articles

    • Comment s'authentifier

      Si vous souhaitez bénéficier d'une authentification par clé API, vous pouvez en faire la demande à votre contact Heyliot. Si vous bénéficiez déjà d'une clé d'API, cette section ne vous concerne pas. Préambule Le système d'authentification d'Heyliot ...
    • Récupérer les niveaux de remplissages de vos conteneurs

      Récupérer les niveaux de remplissages de vos conteneurs Accédez à la documentation complète de l'API V2 sur https://docs.heyliot.app/?api=v2 On ne parle pas ici de mesure mais bien de niveau de remplissage, depuis le 1er Février 2024, c'est cette ...