Skip to content
Snippets Groups Projects
sensiplan.js 1.46 KiB
Newer Older
function detectTemperatureShift(temperaturesOfCycle) {
  // sensiplan rounds temps to the nearest 0.05
  const tempValues = temperaturesOfCycle.map(val => rounded(val, 0.05))
Julia Friesel's avatar
Julia Friesel committed
  let detectingPotentialHighLevel = false

  return tempValues.reduce((acc, curr) => {
    // if we don't yet have 6 lower temps, we just collect
    // if no shift has been detected, we collect low temps
    // after the shift has been detected, we count them as part
    // of the higher temperature phase
Julia Friesel's avatar
Julia Friesel committed
    if (acc.low.length < 7) {
      acc.low.push(curr)
      acc.ltl = Math.max(...acc.low.slice(-6))
      // TODO these are the same
    } else if (curr <= acc.ltl && !detectingPotentialHighLevel && !acc.shiftDetected) {
      acc.low.push(curr)
      acc.ltl = Math.max(...acc.low.slice(-6))
    } else {
Julia Friesel's avatar
Julia Friesel committed
      detectingPotentialHighLevel = true
Julia Friesel's avatar
Julia Friesel committed
    // regular rule
    // we round the difference because of JS decimal weirdness
    if (acc.high.length === 3 && rounded(curr - acc.ltl, 0.01) >= 0.2) {
      acc.shiftDetected = true
Julia Friesel's avatar
Julia Friesel committed
      detectingPotentialHighLevel = false
Julia Friesel's avatar
Julia Friesel committed
    // 1st exception rule
    if (acc.high.length === 4 && curr > acc.ltl) {
      acc.shiftDetected = true
Julia Friesel's avatar
Julia Friesel committed
      detectingPotentialHighLevel = false

    return acc
  }, {
    low: [],
    high: [],
    ltl: null,
    shiftDetected: false
  })
}

function rounded(val, step) {
  const inverted = 1 / step
  return Math.round(val * inverted) / inverted
}

export {
  detectTemperatureShift