1
0

settings-store.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const {
  2. clampInteger,
  3. getWxApi,
  4. toFiniteNumber
  5. } = require('../utils/base-utils.js')
  6. const {
  7. normalizeHexByte,
  8. parseHexByte
  9. } = require('../utils/validation.js')
  10. const {
  11. DEFAULT_PROTOCOL_MODE,
  12. PROTOCOL_MODE,
  13. PROTOCOL_OPTIONS,
  14. isModbusProtocolMode,
  15. isNoProtocolMode,
  16. isStorageAccessProtocolMode,
  17. normalizeProtocolMode
  18. } = require('../domain/protocol-mode.js')
  19. const STORAGE_KEY = 'app-settings'
  20. const DEFAULT_SETTINGS = {
  21. modbusSlaveAddress: 'F0',
  22. nightModeEnabled: false,
  23. nightModeFollowSystem: true,
  24. parameterAutoPollEnabled: false,
  25. parameterMaxPacketLength: 64,
  26. parameterPollInterval: 100,
  27. protocolMode: DEFAULT_PROTOCOL_MODE
  28. }
  29. const STATUS_POLL_MIN_INTERVAL = 100
  30. const STATUS_POLL_MAX_INTERVAL = 3000
  31. const PARAMETER_MIN_PACKET_LENGTH = 32
  32. const state = {
  33. ...DEFAULT_SETTINGS
  34. }
  35. let initialized = false
  36. const subscribers = []
  37. function normalizeParameterPacketLength(value, fallback = DEFAULT_SETTINGS.parameterMaxPacketLength) {
  38. const numberValue = toFiniteNumber(value, NaN)
  39. if (!Number.isFinite(numberValue)) return fallback
  40. const rounded = Math.round(numberValue)
  41. if (rounded <= 0) return 0
  42. return Math.max(rounded, PARAMETER_MIN_PACKET_LENGTH)
  43. }
  44. function isModbusProtocol(value = state.protocolMode) {
  45. return isModbusProtocolMode(value)
  46. }
  47. function isStorageAccessProtocol(value = state.protocolMode) {
  48. return isStorageAccessProtocolMode(value)
  49. }
  50. function isNoProtocol(value = state.protocolMode) {
  51. return isNoProtocolMode(value)
  52. }
  53. function normalizeSettings(settings = {}) {
  54. const protocolMode = normalizeProtocolMode(settings.protocolMode)
  55. const parameterAutoPollEnabled = !!settings.parameterAutoPollEnabled
  56. const parameterMaxPacketLength = normalizeParameterPacketLength(
  57. settings.parameterMaxPacketLength,
  58. DEFAULT_SETTINGS.parameterMaxPacketLength
  59. )
  60. const parameterPollInterval = clampInteger(
  61. settings.parameterPollInterval,
  62. STATUS_POLL_MIN_INTERVAL,
  63. STATUS_POLL_MAX_INTERVAL,
  64. DEFAULT_SETTINGS.parameterPollInterval
  65. )
  66. return {
  67. modbusSlaveAddress: normalizeHexByte(settings.modbusSlaveAddress),
  68. nightModeEnabled: !!settings.nightModeEnabled,
  69. nightModeFollowSystem: settings.nightModeFollowSystem !== false,
  70. parameterAutoPollEnabled,
  71. parameterMaxPacketLength,
  72. parameterPollInterval,
  73. protocolMode
  74. }
  75. }
  76. function readStoredSettings() {
  77. const wxApi = getWxApi()
  78. if (typeof wxApi.getStorageSync !== 'function') return {}
  79. try {
  80. return wxApi.getStorageSync(STORAGE_KEY) || {}
  81. } catch (error) {
  82. return {}
  83. }
  84. }
  85. function persistSettings() {
  86. const wxApi = getWxApi()
  87. if (typeof wxApi.setStorageSync !== 'function') return
  88. try {
  89. wxApi.setStorageSync(STORAGE_KEY, getState())
  90. } catch (error) {}
  91. }
  92. function notify() {
  93. const nextState = getState()
  94. subscribers.slice().forEach((subscriber) => {
  95. subscriber(nextState)
  96. })
  97. }
  98. function setState(changedData, options = {}) {
  99. Object.assign(state, normalizeSettings({
  100. ...state,
  101. ...changedData
  102. }))
  103. if (options.persist !== false) {
  104. persistSettings()
  105. }
  106. notify()
  107. }
  108. function init() {
  109. if (initialized) return
  110. Object.assign(state, normalizeSettings(readStoredSettings()))
  111. initialized = true
  112. }
  113. function getState() {
  114. return {
  115. ...state
  116. }
  117. }
  118. function subscribe(subscriber) {
  119. if (typeof subscriber !== 'function') return () => {}
  120. init()
  121. subscribers.push(subscriber)
  122. subscriber(getState())
  123. return () => {
  124. const index = subscribers.indexOf(subscriber)
  125. if (index >= 0) subscribers.splice(index, 1)
  126. }
  127. }
  128. function setNightModeEnabled(value) {
  129. init()
  130. setState({
  131. nightModeEnabled: !!value
  132. })
  133. }
  134. function setNightModeFollowSystem(value) {
  135. init()
  136. setState({
  137. nightModeFollowSystem: !!value
  138. })
  139. }
  140. function setModbusSlaveAddress(value) {
  141. init()
  142. setState({
  143. modbusSlaveAddress: normalizeHexByte(value, state.modbusSlaveAddress)
  144. })
  145. }
  146. function setProtocolMode(value) {
  147. init()
  148. setState({
  149. protocolMode: normalizeProtocolMode(value)
  150. })
  151. }
  152. function getModbusSlaveAddress() {
  153. init()
  154. return parseHexByte(state.modbusSlaveAddress, 'Modbus从机地址')
  155. }
  156. function setParameterAutoPollEnabled(value) {
  157. init()
  158. setState({
  159. parameterAutoPollEnabled: !!value
  160. })
  161. }
  162. function setParameterMaxPacketLength(value) {
  163. init()
  164. setState({
  165. parameterMaxPacketLength: normalizeParameterPacketLength(value, state.parameterMaxPacketLength)
  166. })
  167. }
  168. function setParameterPollInterval(value) {
  169. init()
  170. setState({
  171. parameterPollInterval: value
  172. })
  173. }
  174. module.exports = {
  175. PARAMETER_MIN_PACKET_LENGTH,
  176. PROTOCOL_MODE,
  177. PROTOCOL_OPTIONS,
  178. STATUS_POLL_MAX_INTERVAL,
  179. STATUS_POLL_MIN_INTERVAL,
  180. getModbusSlaveAddress,
  181. getState,
  182. init,
  183. isNoProtocol,
  184. isModbusProtocol,
  185. isStorageAccessProtocol,
  186. setModbusSlaveAddress,
  187. setNightModeEnabled,
  188. setNightModeFollowSystem,
  189. setParameterAutoPollEnabled,
  190. setParameterMaxPacketLength,
  191. setParameterPollInterval,
  192. setProtocolMode,
  193. normalizeProtocolMode,
  194. subscribe
  195. }