<commit_before>/**
 Copyright (c) 2016,2017,2018,2019 <NAME> (https://bianco-royal.com/)
 All rights reserved.
 node-red-contrib-modbus
 node-red-contrib-modbusio

 @author <a href="mailto:<EMAIL>"><NAME></a> (<NAME>)
 */
'use strict'
// SOURCE-MAP-REQUIRED

var de = de || { biancoroyal: { modbus: { queue: { core: {} } } } } // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.queue.core.internalDebug = de.biancoroyal.modbus.queue.core.internalDebug || require('debug')('contribModbus:queue:core') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.queue.core.core = de.biancoroyal.modbus.queue.core.core || require('./modbus-core') // eslint-disable-line no-use-before-define

de.biancoroyal.modbus.queue.core.initQueue = function (node) {
  node.bufferCommandList.clear()
  node.sendAllowed.clear()
  node.sendToDeviceAllowed = []
  node.unitSendingAllowed = []

  for (let step = 0; step <= 255; step++) {
    node.bufferCommandList.set(step, [])
    node.sendAllowed.set(step, true)
  }
}

de.biancoroyal.modbus.queue.core.checkQueuesAreEmpty = function (node) {
  let queueIsEmpty = true
  for (let step = 0; step <= 255; step++) {
    queueIsEmpty &= (node.bufferCommandList.get(step).length > 0)
  }
  return queueIsEmpty
}

de.biancoroyal.modbus.queue.core.sequentialDequeueCommand = function (node) {
  const nodesToWaitCount = node.unitSendingAllowed.length
  const serialUnit = parseInt(node.unitSendingAllowed.shift())
  let noneCommandToSent = true

  if (Number.isInteger(serialUnit) &&
    node.bufferCommandList.get(serialUnit).length > 0) {
    if (node.parallelUnitIdsAllowed) {
      noneCommandToSent = this.sendDataInParallel(node, serialUnit, nodesToWaitCount)
    } else {
      noneCommandToSent = this.sendDataPerDevice(node, serialUnit, nodesToWaitCount)
    }
  } else {
    node.queueLog(JSON.stringify({
      type: 'queue check is not a unit',
      unitid: serialUnit
    }))
  }

  if (noneCommandToSent) {
    node.stateService.send('EMPTY')
  }
}

de.biancoroyal.modbus.queue.core.sendDataInParallel = function (node, serialUnit, nodesToWaitCount) {
  let command = null
  let noneCommandToSent = true

  node.queueLog(JSON.stringify({
    type: 'queue check',
    unitid: serialUnit,
    sendAllowed: node.sendAllowed.get(serialUnit),
    queueLength: node.bufferCommandList.get(serialUnit).length
  }))

  if (node.sendAllowed.get(serialUnit)) {
    command = node.bufferCommandList.get(serialUnit).shift()
    if (command) {
      node.sendAllowed.set(serialUnit, false)
      node.queueLog(JSON.stringify({
        type: 'serial sending and wait per unitid',
        unitid: serialUnit,
        // commandData: command,
        queueLength: node.bufferCommandList.get(serialUnit).length,
        sendAllowedForNext: node.sendAllowed.get(serialUnit),
        delay: node.commandDelay
      }))

      if (node.bufferCommandList.get(serialUnit).length > 0) {
        node.unitSendingAllowed.push(serialUnit)
      }
      noneCommandToSent = false
      command.callModbus(node, command.msg, command.cb, command.cberr)
    }
  }

  return noneCommandToSent
}

de.biancoroyal.modbus.queue.core.sendDataPerDevice = function (node, serialUnit, nodesToWaitCount) {
  let command = null
  let noneCommandToSent = true

  node.queueLog(JSON.stringify({
    type: 'queue check',
    unitid: serialUnit,
    sendAllowed: node.sendAllowed.get(serialUnit),
    queueLength: node.bufferCommandList.get(serialUnit).length
  }))

  if (node.sendToDeviceAllowed.length === 0) {
    command = node.bufferCommandList.get(serialUnit).shift()
    if (command) {
      node.sendToDeviceAllowed.push(serialUnit)
      node.queueLog(JSON.stringify({
        type: 'serial sending and wait',
        unitid: serialUnit,
        // commandData: command,
        queueLength: node.bufferCommandList.get(serialUnit).length,
        sendAllowedForNext: node.sendToDeviceAllowed.length,
        delay: node.commandDelay
      }))

      if (node.bufferCommandList.get(serialUnit).length > 0) {
        node.unitSendingAllowed.push(serialUnit)
      }
      noneCommandToSent = false
      command.callModbus(node, command.msg, command.cb, command.cberr)
    }
  }

  return noneCommandToSent
}

de.biancoroyal.modbus.queue.core.dequeueCommand = function (node) {
  const state = node.actualServiceState

  if (node.messageAllowedStates.indexOf(state.value) === -1) {
    node.queueLog(JSON.stringify({
      state: state.value,
      message: 'dequeue command disallowed state',
      delay: node.commandDelay
    }))
  } else {
    node.queueLog(JSON.stringify({
      state: state.value,
      message: 'dequeue command ' + node.clienttype,
      delay: node.commandDelay
    }))

    this.sequentialDequeueCommand(node)
  }

  if (this.checkQueuesAreEmpty(node)) {
    node.stateService.send('EMPTY')
  }<commit_msg>chore: remove this for dev problems in debug<commit_after>/**
 Copyright (c) 2016,2017,2018,2019 <NAME> (https://bianco-royal.com/)
 All rights reserved.
 node-red-contrib-modbus
 node-red-contrib-modbusio

 @author <a href="mailto:<EMAIL>"><NAME></a> (B<NAME>)
 */
'use strict'
// SOURCE-MAP-REQUIRED

var de = de || { biancoroyal: { modbus: { queue: { core: {} } } } } // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.queue.core.internalDebug = de.biancoroyal.modbus.queue.core.internalDebug || require('debug')('contribModbus:queue:core') // eslint-disable-line no-use-before-define
de.biancoroyal.modbus.queue.core.core = de.biancoroyal.modbus.queue.core.core || require('./modbus-core') // eslint-disable-line no-use-before-define

de.biancoroyal.modbus.queue.core.initQueue = function (node) {
  node.bufferCommandList.clear()
  node.sendAllowed.clear()
  node.sendToDeviceAllowed = []
  node.unitSendingAllowed = []

  for (let step = 0; step <= 255; step++) {
    node.bufferCommandList.set(step, [])
    node.sendAllowed.set(step, true)
  }
}

de.biancoroyal.modbus.queue.core.checkQueuesAreEmpty = function (node) {
  let queueIsEmpty = true
  for (let step = 0; step <= 255; step++) {
    queueIsEmpty &= (node.bufferCommandList.get(step).length > 0)
  }
  return queueIsEmpty
}

de.biancoroyal.modbus.queue.core.sequentialDequeueCommand = function (node) {
  const queueCore = de.biancoroyal.modbus.queue.core
  const nodesToWaitCount = node.unitSendingAllowed.length
  const serialUnit = parseInt(node.unitSendingAllowed.shift())
  let noneCommandToSent = true

  if (Number.isInteger(serialUnit) &&
    node.bufferCommandList.get(serialUnit).length > 0) {
    if (node.parallelUnitIdsAllowed) {
      noneCommandToSent = queueCore.sendDataInParallel(node, serialUnit, nodesToWaitCount)
    } else {
      noneCommandToSent = queueCore.sendDataPerDevice(node, serialUnit, nodesToWaitCount)
    }
  } else {
    node.queueLog(JSON.stringify({
      type: 'queue check is not a unit',
      unitid: serialUnit
    }))
  }

  if (noneCommandToSent) {
    node.stateService.send('EMPTY')
  }
}

de.biancoroyal.modbus.queue.core.sendDataInParallel = function (node, serialUnit, nodesToWaitCount) {
  let command = null
  let noneCommandToSent = true

  node.queueLog(JSON.stringify({
    type: 'queue check',
    unitid: serialUnit,
    sendAllowed: node.sendAllowed.get(serialUnit),
    queueLength: node.bufferCommandList.get(serialUnit).length
  }))

  if (node.sendAllowed.get(serialUnit)) {
    command = node.bufferCommandList.get(serialUnit).shift()
    if (command) {
      node.sendAllowed.set(serialUnit, false)
      node.queueLog(JSON.stringify({
        type: 'serial sending and wait per unitid',
        unitid: serialUnit,
        // commandData: command,
        queueLength: node.bufferCommandList.get(serialUnit).length,
        sendAllowedForNext: node.sendAllowed.get(serialUnit),
        delay: node.commandDelay
      }))

      if (node.bufferCommandList.get(serialUnit).length > 0) {
        node.unitSendingAllowed.push(serialUnit)
      }
      noneCommandToSent = false
      command.callModbus(node, command.msg, command.cb, command.cberr)
    }
  }

  return noneCommandToSent
}

de.biancoroyal.modbus.queue.core.sendDataPerDevice = function (node, serialUnit, nodesToWaitCount) {
  let command = null
  let noneCommandToSent = true

  node.queueLog(JSON.stringify({
    type: 'queue check',
    unitid: serialUnit,
    sendAllowed: node.sendAllowed.get(serialUnit),
    queueLength: node.bufferCommandList.get(serialUnit).length
  }))

  if (node.sendToDeviceAllowed.length === 0) {
    command = node.bufferCommandList.get(serialUnit).shift()
    if (command) {
      node.sendToDeviceAllowed.push(serialUnit)
      node.queueLog(JSON.stringify({
        type: 'serial sending and wait',
        unitid: serialUnit,
        // commandData: command,
        queueLength: node.bufferCommandList.get(serialUnit).length,
        sendAllowedForNext: node.sendToDeviceAllowed.length,
        delay: node.commandDelay
      }))

      if (node.bufferCommandList.get(serialUnit).length > 0) {
        node.unitSendingAllowed.push(serialUnit)
      }
      noneCommandToSent = false
      command.callModbus(node, command.msg, command.cb, command.cberr)
    }
  }

  return noneCommandToSent
}

de.biancoroyal.modbus.queue.core.dequeueCommand = function (node) {
  const queueCore = de.biancoroyal.modbus.queue.core
  const state = node.actualServiceState

  if (node.messageAllowedStates.indexOf(state.value) === -1) {
    node.queueLog(JSON.stringify({
      state: state.value,
      message: 'dequeue command disallowed state',
      delay: node.commandDelay
    }))
  } else {
    node.queueLog(JSON.stringify({
      state: state.value,
      message: 'dequeue command ' + node.clienttype,
      delay: node.commandDelay
    }))

    queueCore.sequentialDequeueCommand(node)
  }

  if (queueCore.checkQueuesAreEmpty(node)) {
    node.stateService.send('EMPTY')
  }
{'fraction_non_alphanumeric': 0.12583518930957685, 'fraction_numerical': 0.006074104069649726, 'mean_word_length': 3.607742537313433, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2860043', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3168, 'n_words': 537}