1
0

register-groups.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const {
  2. parseHexInteger
  3. } = require('../../utils/base-utils.js')
  4. const {
  5. getRegisterCount
  6. } = require('./registers.js')
  7. function parseRegisterAddress(address) {
  8. return parseHexInteger(address)
  9. }
  10. function getAreaKey(item) {
  11. return (item.area && item.area.key) || item.areaKey || 'holding'
  12. }
  13. function getGroupItems(data, groupKey) {
  14. if (groupKey === 'vsp') return data.vspCurveRegisters.concat([data.speedSlopeRegister])
  15. if (groupKey === 'speedLoop') {
  16. return data.speedLoopInputDisplayRegisters
  17. .concat(data.speedLoopExtraDisplayRegisters)
  18. }
  19. if (groupKey === 'estimator') {
  20. return data.estimatorCalculatedDisplayRegisters.concat(data.atoBandwidthDisplayRegisters)
  21. }
  22. if (groupKey === 'tailwind') {
  23. return data.tailwindControlRegisters
  24. .concat(data.tailwindCalculatedDisplayRegisters, data.tailwindAtoBandwidthDisplayRegisters)
  25. }
  26. if (groupKey === 'preposition') return data.prepositionSwitchRegisters.concat(data.prepositionParameterDisplayRegisters)
  27. if (groupKey === 'oil') return data.oilParameterInputRegisters
  28. if (groupKey === 'dq') return data.dqGainDisplayRegisters
  29. if (groupKey === 'protection') return data.protectionSwitchRegisters.concat(data.protectionDisplayRegisters)
  30. return []
  31. }
  32. function expandAtoItems(item) {
  33. if (!item.kpAddress || !item.kiAddress) return [item]
  34. return [
  35. {
  36. address: item.kpAddress,
  37. areaKey: 'holding',
  38. name: `${item.name} KP`,
  39. type: 'uint16_t',
  40. writeValue: item.kpWriteValue
  41. },
  42. {
  43. address: item.kiAddress,
  44. areaKey: 'holding',
  45. name: `${item.name} KI`,
  46. type: 'uint16_t',
  47. writeValue: item.kiWriteValue
  48. }
  49. ]
  50. }
  51. function expandItems(items) {
  52. return items.reduce((result, item) => result.concat(expandAtoItems(item)), [])
  53. }
  54. function makeReadSpans(entries) {
  55. const sortedEntries = entries
  56. .map((item) => ({
  57. address: parseRegisterAddress(item.address),
  58. count: getRegisterCount(item)
  59. }))
  60. .filter((item) => Number.isFinite(item.address) && item.count > 0)
  61. .sort((left, right) => left.address - right.address)
  62. const spans = []
  63. sortedEntries.forEach((entry) => {
  64. const last = spans[spans.length - 1]
  65. if (last && entry.address <= last.address + last.quantity) {
  66. const end = Math.max(last.address + last.quantity, entry.address + entry.count)
  67. last.quantity = end - last.address
  68. return
  69. }
  70. spans.push({
  71. address: entry.address,
  72. quantity: entry.count
  73. })
  74. })
  75. return spans
  76. }
  77. function mergeReadValues(target, source) {
  78. if (!target || !source) return
  79. Object.assign(target.coils, source.coils || {})
  80. Object.assign(target.words, source.words || {})
  81. }
  82. module.exports = {
  83. expandItems,
  84. getAreaKey,
  85. getGroupItems,
  86. makeReadSpans,
  87. mergeReadValues,
  88. parseRegisterAddress
  89. }