1
0

poller.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const parameterGroupService = require('./service.js')
  2. const POLL_TIMER_ID = '__parameterPoll'
  3. const MEMORY_AREA_ORDER = {
  4. DATA: 0,
  5. IDATA: 1,
  6. XDATA: 2,
  7. CODE: 3,
  8. BIT: 4
  9. }
  10. function getMemoryAreaOrder(group = {}) {
  11. const key = String(group.sourceMemoryArea || '').trim().toUpperCase()
  12. return Object.prototype.hasOwnProperty.call(MEMORY_AREA_ORDER, key) ? MEMORY_AREA_ORDER[key] : 99
  13. }
  14. function getGroupAddress(group = {}) {
  15. const sourceAddress = Number(group.sourceAddress)
  16. if (Number.isFinite(sourceAddress)) return Math.max(0, Math.floor(sourceAddress))
  17. const startAddress = Number(group.startAddress)
  18. return Number.isFinite(startAddress) ? Math.max(0, Math.floor(startAddress)) : 0
  19. }
  20. function shouldPoll(data) {
  21. return !!data
  22. && (data.activeParamView === 'parameterGroups' || data.activeParamView === 'parameterGroup')
  23. && !!data.connectedDevice
  24. && !!data.parameterAutoPollEnabled
  25. }
  26. function getParameterGroups(data = {}) {
  27. return data.parameterGroups || []
  28. }
  29. function getPollableGroups(data) {
  30. return getParameterGroups(data)
  31. .filter((group) => {
  32. if (!group || group.addressOverflow) return false
  33. if ((group.registers || []).some((register) => register && register.isDirty)) return false
  34. if (data.isStorageAccessProtocol) return !!group.sourceMemoryArea
  35. return !!group.functionCode
  36. })
  37. .sort((left, right) => (
  38. getMemoryAreaOrder(left) - getMemoryAreaOrder(right)
  39. || getGroupAddress(left) - getGroupAddress(right)
  40. || String(left.name || '').localeCompare(String(right.name || ''))
  41. ))
  42. }
  43. function createParameterGroupPoller(getData) {
  44. const timers = {}
  45. function clearTimer(timerId) {
  46. if (!timers[timerId]) return
  47. clearTimeout(timers[timerId])
  48. delete timers[timerId]
  49. }
  50. function clearAll() {
  51. Object.keys(timers).forEach(clearTimer)
  52. }
  53. function schedule(delay) {
  54. clearTimer(POLL_TIMER_ID)
  55. timers[POLL_TIMER_ID] = setTimeout(async () => {
  56. const data = getData()
  57. if (!shouldPoll(data)) {
  58. clearTimer(POLL_TIMER_ID)
  59. return
  60. }
  61. const protocolMode = data.protocolMode
  62. for (const group of getPollableGroups(data)) {
  63. const latestData = getData()
  64. if (!shouldPoll(latestData)) break
  65. if (latestData.protocolMode !== protocolMode) break
  66. await parameterGroupService.readGroup(group.id, {
  67. maxPacketLength: latestData.parameterMaxPacketLength,
  68. protocolMode,
  69. showModal: false
  70. })
  71. }
  72. const latestData = getData()
  73. if (shouldPoll(latestData)) {
  74. schedule(latestData.parameterPollInterval || 100)
  75. }
  76. }, delay)
  77. }
  78. function scheduleVisible() {
  79. const data = getData()
  80. if (!shouldPoll(data)) {
  81. clearAll()
  82. return
  83. }
  84. schedule(data.parameterPollInterval || 100)
  85. }
  86. return {
  87. clearAll,
  88. clearTimer,
  89. schedule,
  90. scheduleVisible
  91. }
  92. }
  93. module.exports = {
  94. createParameterGroupPoller
  95. }