text: "use strict";
  
  /*
  Copyright (c) 2017-2020 Marcel Mol
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
  
  /*
    Auriol Protocol
  */
  
  const utils = require('utils');
  
  function crc8(message, nBytes, polynomial, init) {
      let remainder = init;
  
      for (let byte = 0; byte < nBytes; ++byte) {
          remainder ^= message[byte];
          for (let bit = 0; bit < 8; ++bit) {
              let xor = remainder & 0x80;
              remainder = (remainder << 1) & 0xff;
              if (xor) {
                  remainder ^= polynomial;
              }
          }
      }
      return remainder;
  }
  
  /*
      # 4-bit nibbles: [id0] [id1], [stat], [temp0] [temp1] [temp2], [unk], [hum0], [hum2]
      #   The id changes when the battery is changed
      #   stat  bits: [bat ? chan chan]  bat = 1 when battery ok
      #   unk seems always to be 'F'
      #   temp is 12 bit 2-complements value scaled by 10
      #   hum is 8 bit humidity in %
  */
  class Auriol1 extends utils.WeatherSignal {
  
    constructor() {
      super({
        id: 'auriol1',
        name: 'Auriol',
        signal: 'AuriolZ31130',
  			hint: {
  				en: 'Support for Auriol IAN85059 Z31130-TX and others',
  				nl: 'Decodeert Auriol IAN85059 Z31130-TX en andere',
  			}
      })
    }
  
    parse(payLoad) {
    	let result;
    	let decVal = payLoad.join('');
      let check = decVal.substr(24, 4);
      if (check === '1111') {
    		result = this.decode(decVal);
    	} else {
    		result = new Error('Not a valid Auriol message: Check error (not F): ' + check);
    	}
    	return result;
    }
  
    decode(data) {
      let id = utils.bin2dec(data.substr(0, 8));
      let batt = data.substr(8, 1);
      let channel = Number(utils.bin2dec(data.substr(10, 2))) + 1;
      let result = {
        id: id,
    		channel: channel
      }
      let value = utils.bin2dec(data.substr(12, 12));
      if (value & 0x800) {
    		value ^= 0xfff;
    		value = -value - 1;
    	}
      result.data = { temperature: value / 10 };
      let humidity = utils.bin2dec(data.substr(28, 8));
      if (humidity > 0) { // temp. only sensor if humidity is zero
        result.data.humidity = Number(humidity);
      }
      result.data.lowbattery = (batt === '0');
  
      return result;
    }
  
  }
  
  /*
    Auriol protocol as decribed here: https://github.com/merbanan/rtl_433/blob/master/src/devices/auriol_hg02832.c
  */
  class Auriol2 extends utils.WeatherSignal {
  
    constructor() {
      super({
        id: 'auriol2',
        name: 'Auriol v2',
        signal: 'AuriolHG',
        hint: {
          en: 'Support for Auriol IAN321304 HG05124A-DCF-TX and others',
  				nl: 'Decodeert Auriol IAN321304 HG05124A-DCF-TX en andere',
        }
      })
    }
  
    parse(payLoad)  {
      let result;
    	let decVal = payLoad.join('');
  
      let crc = Number(utils.bin2dec(decVal.substr(32, 7)));
      // They tried to implement CRC-8 poly 0x31, but (accidentally?) reset the key every new byte.
      // (equivalent key stream is 7a 3d 86 43 b9 c4 62 31 repeated 4 times.)
      let msg = [ Number(utils.bin2dec(decVal.slice(0, 8))) ^ Number(utils.bin2dec(decVal.slice(8, 16))) ^
                  Number(utils.bin2dec(decVal.slice(16, 24))) ^ Number(utils.bin2dec(decVal.slice(24, 32))) ];
      // Somehow we miss the last bit, so exclude this in the check
      let chk = crc8(msg, 1, 0x31, 0x53) >> 1;
      if (crc === chk) {
        result = this.decode(decVal);
      } else {
        result = new Error('CRC error: expected ' + crc + ', but got ' + chk);
      }
  
      return result;
    }
  
    decode(data) {
      let id = utils.bin2dec(data.substr(0, 8));
      let batt = data.substr(16, 1);
      let channel = Number(utils.bin2dec(data.substr(18, 2))) + 1;
      let result = {
        id: id,
    		channel: channel
      }
      let value = utils.bin2dec(data.substr(20, 12));
      if (value & 0x800) {
    		value ^= 0xfff;
    		value = -value - 1;
    	}
      result.data = { temperature: value / 10 };
      let humidity = utils.bin2dec(data.substr(8, 8));
      if (humidity > 0) { // temp. only sensor if humidity is zero
        result.data.humidity = Number(humidity);
      }
      result.data.lowbattery = (batt === '1')
  
      return result;
    }
  }
  
  module.exports = {
    auriol1: Auriol1,
    auriol2: Auriol2
  };
  