1
0

settings-store.js 5.7 KB

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