settings-view-model.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const settingsService = require('./settings-service')
  2. const themeService = require('./theme-service')
  3. const controlState = require('./control-page-state')
  4. const toolNavigation = require('./tool-navigation')
  5. function getModbusProtocolMeta(settingsState) {
  6. const modbusProtocolOptions = settingsService.MODBUS_PROTOCOL_OPTIONS
  7. const modbusProtocolIndex = Math.max(0, modbusProtocolOptions.findIndex((option) => (
  8. option.key === settingsState.modbusProtocolFilter
  9. )))
  10. const modbusProtocolText = (modbusProtocolOptions[modbusProtocolIndex] || modbusProtocolOptions[0]).label
  11. return {
  12. isGenericProtocol: settingsState.modbusProtocolFilter === 'generic',
  13. modbusProtocolIndex,
  14. modbusProtocolOptions,
  15. modbusProtocolText
  16. }
  17. }
  18. function getSettingsPageState(
  19. settingsState = settingsService.getState(),
  20. themeState = themeService.getState()
  21. ) {
  22. const nightModeEnabledSwitch = settingsState.nightModeFollowSystem
  23. ? themeState.themeMode === 'dark'
  24. : settingsState.nightModeEnabled
  25. return {
  26. ...settingsState,
  27. ...themeState,
  28. ...getModbusProtocolMeta(settingsState),
  29. genericModbusMinPacketLength: settingsService.GENERIC_MODBUS_MIN_PACKET_LENGTH,
  30. maxUserStatusCount: controlState.MAX_USER_STATUS_COUNT,
  31. nightModeEnabledSwitch,
  32. statusPollMaxInterval: settingsService.STATUS_POLL_MAX_INTERVAL,
  33. statusPollMinInterval: settingsService.STATUS_POLL_MIN_INTERVAL,
  34. toolEntries: toolNavigation.getToolEntries()
  35. }
  36. }
  37. module.exports = {
  38. getModbusProtocolMeta,
  39. getSettingsPageState
  40. }