settings-store.js 5.2 KB

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