@gct-paas/core 0.1.4-dev.9 → 0.1.5-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.min.js +1 -0
- package/es/constants/core.mjs +5 -4
- package/es/constants/default-date-type/default-date-type.d.ts +70 -0
- package/es/constants/default-date-type/default-date-type.mjs +74 -0
- package/es/constants/editor-register/editor-register.mjs +17 -5
- package/es/constants/expression-type/BuiltOperators.mjs +152 -144
- package/es/constants/expression-type/FunctionKeys.mjs +75 -79
- package/es/constants/expression-type/editor.mjs +10 -3
- package/es/constants/expression-type/function.mjs +602 -613
- package/es/constants/expression-type/index.mjs +111 -113
- package/es/constants/expression-type/modeCfg.mjs +256 -263
- package/es/constants/expression-type/variable.mjs +43 -39
- package/es/constants/form-container-type/form-container-type.mjs +17 -5
- package/es/constants/index.d.ts +7 -1
- package/es/constants/index.mjs +32 -10
- package/es/constants/page-designer/model.d.ts +4 -0
- package/es/constants/page-designer/model.mjs +17 -0
- package/es/constants/page-designer/regex.d.ts +2 -0
- package/es/constants/page-designer/regex.mjs +5 -0
- package/es/create-app-vue.mjs +15 -9
- package/es/enums/app-designer/index.d.ts +19 -0
- package/es/enums/app-designer/index.mjs +131 -63
- package/es/enums/appEnum.mjs +681 -360
- package/es/enums/appStateEnum.mjs +17 -10
- package/es/enums/authActionEnum.mjs +63 -51
- package/es/enums/breakpointEnum.mjs +27 -26
- package/es/enums/cacheEnum.d.ts +0 -1
- package/es/enums/cacheEnum.mjs +22 -22
- package/es/enums/designEnum.d.ts +7 -1
- package/es/enums/designEnum.mjs +110 -68
- package/es/enums/developer-center/integration.mjs +7 -6
- package/es/enums/exceptionEnum.mjs +21 -17
- package/es/enums/expressionEnum.d.ts +2 -2
- package/es/enums/expressionEnum.mjs +81 -74
- package/es/enums/globalEnum.d.ts +15 -0
- package/es/enums/globalEnum.mjs +60 -25
- package/es/enums/httpEnum.mjs +71 -61
- package/es/enums/index.mjs +44 -14
- package/es/enums/menuEnum.mjs +42 -38
- package/es/enums/page-designer/designer.d.ts +1 -8
- package/es/enums/page-designer/designer.mjs +403 -407
- package/es/enums/page-designer/index.d.ts +1 -0
- package/es/enums/page-designer/index.mjs +5 -0
- package/es/enums/page-designer/layout.d.ts +12 -0
- package/es/enums/page-designer/layout.mjs +17 -0
- package/es/enums/page-designer/panel.mjs +341 -213
- package/es/enums/page-designer/toolkit.mjs +13 -12
- package/es/enums/page-designer/widget.d.ts +5 -0
- package/es/enums/page-designer/widget.mjs +426 -334
- package/es/enums/pageEnum.mjs +14 -13
- package/es/enums/plugin-enum.d.ts +8 -4
- package/es/enums/plugin-enum.mjs +27 -7
- package/es/enums/processEnum.mjs +95 -60
- package/es/enums/roleEnum.mjs +7 -6
- package/es/enums/sizeEnum.d.ts +18 -0
- package/es/enums/sizeEnum.mjs +28 -8
- package/es/global/gct/gct.d.ts +240 -4
- package/es/global/gct/gct.mjs +98 -90
- package/es/global/index.mjs +1 -0
- package/es/hooks/index.d.ts +0 -2
- package/es/hooks/index.mjs +4 -0
- package/es/hooks/use-app-inst/use-app-inst.mjs +11 -5
- package/es/hooks/use-modal/use-modal.mjs +16 -10
- package/es/hooks/use-sub-app-utils/use-sub-app-utils.d.ts +8 -12
- package/es/hooks/use-sub-app-utils/use-sub-app-utils.mjs +45 -25
- package/es/hooks/useCopyToClipboard.mjs +50 -50
- package/es/index.d.ts +1 -3
- package/es/index.mjs +128 -132
- package/es/interface/i-dictionary/i-dictionary-item.d.ts +6 -0
- package/es/interface/i-global-register/i-global-register.d.ts +8 -0
- package/es/interface/i-kit-info/i-kit-info.d.ts +20 -0
- package/es/interface/i-pinia-action/i-global-actions.d.ts +4 -6
- package/es/interface/i-pinia-getter/i-global-getters.d.ts +2 -0
- package/es/interface/i-pinia-state/i-global-state.d.ts +12 -12
- package/es/interface/index.d.ts +4 -6
- package/es/interface/model-designer/entity.d.ts +110 -0
- package/es/interface/provider/index.d.ts +0 -2
- package/es/locale/index.d.ts +11 -3
- package/es/locale/index.mjs +75 -57
- package/es/modules/env-manager/env-manager.const.mjs +16 -8
- package/es/modules/env-manager/env-manager.d.ts +2 -0
- package/es/modules/env-manager/env-manager.interface.d.ts +2 -0
- package/es/modules/env-manager/env-manager.interface.mjs +14 -7
- package/es/modules/env-manager/env-manager.mjs +108 -107
- package/es/modules/error-handler/error-handler.const.mjs +11 -16
- package/es/modules/error-handler/error-strategy.mjs +199 -175
- package/es/modules/error-handler/index.mjs +120 -106
- package/es/modules/mqtt/constants/mqtt.const.d.ts +33 -0
- package/es/modules/mqtt/constants/mqtt.const.mjs +24 -0
- package/es/modules/mqtt/enums/mqtt-status.enum.d.ts +20 -0
- package/es/modules/mqtt/enums/mqtt-status.enum.mjs +24 -0
- package/es/modules/mqtt/index.d.ts +5 -0
- package/es/modules/mqtt/index.mjs +4 -0
- package/es/modules/mqtt/interfaces/i-mqtt.d.ts +139 -0
- package/es/modules/mqtt/mqtt-client.d.ts +117 -0
- package/es/modules/mqtt/mqtt-client.mjs +229 -0
- package/es/modules/mqtt/mqtt-manager.d.ts +87 -0
- package/es/modules/mqtt/mqtt-manager.mjs +118 -0
- package/es/modules/platform-config/constants/index.mjs +4 -0
- package/es/modules/platform-config/constants/login.const.mjs +48 -45
- package/es/modules/platform-config/constants/org.const.mjs +57 -52
- package/es/modules/platform-config/constants/theme.const.mjs +11 -4
- package/es/modules/platform-config/constants/watermark.const.mjs +17 -12
- package/es/modules/platform-config/enums/deploy.enum.mjs +13 -6
- package/es/modules/platform-config/enums/login.enum.mjs +29 -15
- package/es/modules/platform-config/enums/platform.enum.d.ts +0 -15
- package/es/modules/platform-config/enums/platform.enum.mjs +17 -26
- package/es/modules/platform-config/index.d.ts +1 -1
- package/es/modules/platform-config/index.mjs +9 -0
- package/es/modules/platform-config/interfaces/index.d.ts +0 -1
- package/es/modules/platform-config/store.d.ts +5 -1
- package/es/modules/platform-config/store.mjs +68 -76
- package/es/modules/platform-config/useBasicSetting.mjs +47 -35
- package/es/modules/platform-config/useDeploySetting.mjs +29 -21
- package/es/modules/platform-config/useLoginSetting.mjs +151 -173
- package/es/modules/platform-config/useOrgSetting.mjs +63 -62
- package/es/modules/platform-config/useProjectSetting.mjs +14 -6
- package/es/modules/platform-config/useSecuritySetting.d.ts +1 -0
- package/es/modules/platform-config/useSecuritySetting.mjs +81 -56
- package/es/modules/platform-config/useThemeSetting.mjs +78 -59
- package/es/modules/platform-config/useWatermarkSetting.mjs +37 -23
- package/es/modules/user-stores/index.mjs +3 -0
- package/es/modules/user-stores/store/permission.store.mjs +27 -36
- package/es/modules/user-stores/store/tenant.store.mjs +20 -22
- package/es/modules/user-stores/store/user.store.d.ts +149 -1
- package/es/modules/user-stores/store/user.store.mjs +97 -101
- package/es/modules/user-stores/types/user.d.ts +1 -0
- package/es/router/index.mjs +25 -28
- package/es/setup-app.d.ts +1 -1
- package/es/setup-app.mjs +22 -28
- package/es/state/global-pinia-state/global-pinia-state.d.ts +15 -7
- package/es/state/global-pinia-state/global-pinia-state.mjs +43 -57
- package/es/state/index.d.ts +0 -9
- package/es/state/index.mjs +1 -0
- package/es/store/global-store.mjs +44 -57
- package/es/store/index.d.ts +8 -0
- package/es/store/index.mjs +35 -19
- package/es/store/storage-store.d.ts +9 -0
- package/es/store/storage-store.mjs +10 -0
- package/es/types/index.d.ts +18 -0
- package/es/utils/axios/interceptors.mjs +22 -17
- package/es/utils/cache-adapter/cache-adapter.mjs +61 -56
- package/es/utils/css-loader/css-loader.d.ts +36 -0
- package/es/utils/css-loader/css-loader.mjs +82 -0
- package/es/utils/design/design.d.ts +11 -0
- package/es/utils/design/design.mjs +21 -0
- package/es/utils/deviceFingerprint.mjs +77 -63
- package/es/utils/dictionary/dictionary.mjs +142 -145
- package/es/utils/file/base64Conver.d.ts +9 -0
- package/es/utils/file/base64Conver.mjs +40 -0
- package/es/utils/file/download.d.ts +44 -0
- package/es/utils/file/download.mjs +101 -0
- package/es/utils/file/parser.d.ts +3 -0
- package/es/utils/file/parser.mjs +29 -0
- package/es/utils/i18n/index.mjs +14 -3
- package/es/utils/index.d.ts +14 -4
- package/es/utils/index.mjs +34 -0
- package/es/utils/is-sort-filed/is-sort-filed.d.ts +10 -0
- package/es/utils/is-sort-filed/is-sort-filed.mjs +45 -0
- package/es/utils/kit-pkg-util/kit-pkg-util.d.ts +32 -0
- package/es/utils/kit-pkg-util/kit-pkg-util.mjs +73 -0
- package/es/utils/linked-list/linked-list.mjs +115 -118
- package/es/utils/linked-node/linked-node.mjs +41 -31
- package/es/utils/lowcode/utils.mjs +13 -18
- package/es/utils/lowcode/validate.mjs +20 -26
- package/es/utils/mitt/mitt.mjs +4 -4
- package/es/utils/model-loader/model-loader.mjs +288 -325
- package/es/utils/namespace/namespace.mjs +178 -164
- package/es/utils/openUtil/index.mjs +2 -0
- package/es/utils/openUtil/modal/modal.mjs +72 -79
- package/es/utils/openUtil/overlay-container/overlay-container.mjs +144 -138
- package/es/utils/permission.d.ts +19 -0
- package/es/utils/permission.mjs +34 -0
- package/es/utils/plugin-pkg-util/plugin-pkg-util.d.ts +63 -0
- package/es/utils/plugin-pkg-util/plugin-pkg-util.mjs +119 -0
- package/es/utils/plugin-static-resource/plugin-static-resource.d.ts +2 -27
- package/es/utils/plugin-static-resource/plugin-static-resource.mjs +19 -42
- package/es/utils/style/index.mjs +94 -138
- package/es/utils/tools/tools.d.ts +12 -10
- package/es/utils/tools/tools.mjs +128 -57
- package/es/utils/treeHelper/treeHelper.d.ts +3 -9
- package/es/utils/treeHelper/treeHelper.mjs +205 -233
- package/es/utils/uploader/uploader.d.ts +20 -0
- package/es/utils/uploader/uploader.mjs +86 -0
- package/es/utils/useUUid.d.ts +41 -0
- package/es/utils/useUUid.mjs +85 -0
- package/es/utils/util.mjs +55 -35
- package/es/utils/uuid/uuid.d.ts +2 -0
- package/es/utils/uuid/uuid.mjs +33 -43
- package/es/utils/validate.d.ts +5 -0
- package/es/utils/validate.mjs +21 -0
- package/es/utils/value-helper/value-helper.mjs +113 -98
- package/package.json +18 -27
- package/dist/index.esm.min.mjs +0 -7973
- package/dist/index.min.cjs +0 -386
- package/dist/index.system.min.js +0 -386
- package/es/constants/editor-type/editor-type.d.ts +0 -244
- package/es/constants/editor-type/editor-type.mjs +0 -63
- package/es/controller/edit-form/edit-form.controller.d.ts +0 -28
- package/es/controller/edit-form/edit-form.controller.mjs +0 -47
- package/es/controller/editor/color-editor.controller.d.ts +0 -13
- package/es/controller/editor/color-editor.controller.mjs +0 -6
- package/es/controller/editor/length-unit-editor.controller.d.ts +0 -13
- package/es/controller/editor/length-unit-editor.controller.mjs +0 -6
- package/es/controller/editor-item/editor-item.controller.d.ts +0 -14
- package/es/controller/editor-item/editor-item.controller.mjs +0 -8
- package/es/controller/form/form.controller.d.ts +0 -76
- package/es/controller/form/form.controller.mjs +0 -147
- package/es/controller/form-collapse/form-collapse.controller.d.ts +0 -18
- package/es/controller/form-collapse/form-collapse.controller.mjs +0 -14
- package/es/controller/form-collapse-pane/form-collapse-pane.controller.d.ts +0 -18
- package/es/controller/form-collapse-pane/form-collapse-pane.controller.mjs +0 -14
- package/es/controller/form-edit-item/form-edit-item.controller.d.ts +0 -27
- package/es/controller/form-edit-item/form-edit-item.controller.mjs +0 -74
- package/es/controller/form-group/form-group.controller.d.ts +0 -18
- package/es/controller/form-group/form-group.controller.mjs +0 -14
- package/es/controller/form-item/form-item.controller.d.ts +0 -21
- package/es/controller/form-item/form-item.controller.mjs +0 -43
- package/es/controller/form-item-basic/form-item-basic.controller.d.ts +0 -57
- package/es/controller/form-item-basic/form-item-basic.controller.mjs +0 -49
- package/es/controller/form-item-hidden/form-item-hidden.controller.d.ts +0 -17
- package/es/controller/form-item-hidden/form-item-hidden.controller.mjs +0 -7
- package/es/controller/form-tab/form-tab.controller.d.ts +0 -18
- package/es/controller/form-tab/form-tab.controller.mjs +0 -14
- package/es/controller/form-tab-pane/form-tab-pane.controller.d.ts +0 -18
- package/es/controller/form-tab-pane/form-tab-pane.controller.mjs +0 -14
- package/es/controller/gct-form/gct-form.controller.d.ts +0 -101
- package/es/controller/gct-form/gct-form.controller.mjs +0 -214
- package/es/controller/index.d.ts +0 -15
- package/es/hooks/use-form/use-form.d.ts +0 -10
- package/es/hooks/use-form/use-form.mjs +0 -8
- package/es/hooks/use-gct-form-value/use-gct-form-value.d.ts +0 -22
- package/es/hooks/use-gct-form-value/use-gct-form-value.mjs +0 -89
- package/es/hooks/use-overlay-scrollbars/use-overlay-scrollbars.d.ts +0 -11
- package/es/interface/controller/edit-form/edit-form.controller.d.ts +0 -26
- package/es/interface/controller/editor/i-color-editor.controller.d.ts +0 -11
- package/es/interface/controller/editor/i-length-unit-editor.controller.d.ts +0 -11
- package/es/interface/controller/editor-item/editor-item.controller.d.ts +0 -19
- package/es/interface/controller/form/form.controller.d.ts +0 -121
- package/es/interface/controller/form-collapse/form-collapse.controller.d.ts +0 -23
- package/es/interface/controller/form-collapse-pane/form-collapse-pane.controller.d.ts +0 -23
- package/es/interface/controller/form-edit-item/form-edit-item.controller.d.ts +0 -59
- package/es/interface/controller/form-group/form-group.controller.d.ts +0 -24
- package/es/interface/controller/form-hidden-item/form-hidden-item.controller.d.ts +0 -24
- package/es/interface/controller/form-item/form-item.controller.d.ts +0 -40
- package/es/interface/controller/form-item-basic/form-item-basic.controller.d.ts +0 -51
- package/es/interface/controller/form-tab/form-tab.controller.d.ts +0 -23
- package/es/interface/controller/form-tab-pane/form-tab-pane.controller.d.ts +0 -23
- package/es/interface/controller/index.d.ts +0 -14
- package/es/interface/events/form/i-form.event.d.ts +0 -11
- package/es/interface/events/index.d.ts +0 -1
- package/es/interface/form/i-editor/i-check-switch.d.ts +0 -29
- package/es/interface/form/i-editor/i-checkbox-editor.d.ts +0 -9
- package/es/interface/form/i-editor/i-color-editor.d.ts +0 -13
- package/es/interface/form/i-editor/i-editor-basic.d.ts +0 -87
- package/es/interface/form/i-editor/i-icon-select-editor.d.ts +0 -21
- package/es/interface/form/i-editor/i-info-editor.d.ts +0 -21
- package/es/interface/form/i-editor/i-length-unit-editor.d.ts +0 -13
- package/es/interface/form/i-editor/i-model-field-select-editor.d.ts +0 -18
- package/es/interface/form/i-editor/i-number-editor.d.ts +0 -21
- package/es/interface/form/i-editor/i-picker-editor.d.ts +0 -30
- package/es/interface/form/i-editor/i-radio-editor.d.ts +0 -21
- package/es/interface/form/i-editor/i-select-editor.d.ts +0 -14
- package/es/interface/form/i-editor/i-span-editor.d.ts +0 -30
- package/es/interface/form/i-editor/i-switch-editor.d.ts +0 -14
- package/es/interface/form/i-editor/i-text-editor.d.ts +0 -46
- package/es/interface/form/i-editor/i-textarea-editor.d.ts +0 -25
- package/es/interface/form/i-form/i-edit-form.d.ts +0 -48
- package/es/interface/form/i-form/i-form-collapse-pane.d.ts +0 -30
- package/es/interface/form/i-form/i-form-collapse.d.ts +0 -35
- package/es/interface/form/i-form/i-form-container.d.ts +0 -53
- package/es/interface/form/i-form/i-form-edit-item.d.ts +0 -140
- package/es/interface/form/i-form/i-form-group-container.d.ts +0 -24
- package/es/interface/form/i-form/i-form-group.d.ts +0 -35
- package/es/interface/form/i-form/i-form-hidden-item.d.ts +0 -11
- package/es/interface/form/i-form/i-form-item-basic.d.ts +0 -126
- package/es/interface/form/i-form/i-form-item.d.ts +0 -11
- package/es/interface/form/i-form/i-form-tab-pane.d.ts +0 -23
- package/es/interface/form/i-form/i-form-tab.d.ts +0 -21
- package/es/interface/form/i-form/i-form.d.ts +0 -79
- package/es/interface/form/index.d.ts +0 -29
- package/es/interface/layout/i-flex-layout/i-flex-container.d.ts +0 -75
- package/es/interface/layout/i-flex-layout/i-flex-item.d.ts +0 -51
- package/es/interface/layout/i-grid-layout/i-grid-container.d.ts +0 -29
- package/es/interface/layout/i-grid-layout/i-grid-item-span.d.ts +0 -54
- package/es/interface/layout/i-grid-layout/i-grid-item.d.ts +0 -37
- package/es/interface/layout/i-layout-basis/i-layout-container-basis.d.ts +0 -11
- package/es/interface/layout/index.d.ts +0 -6
- package/es/interface/provider/i-editor-provider/i-editor-provider.d.ts +0 -24
- package/es/interface/provider/i-form-item-provider/i-form-item-provider.d.ts +0 -44
- package/es/interface/state/form/form.state.d.ts +0 -53
- package/es/interface/state/form-collapse/form-collapse.state.d.ts +0 -11
- package/es/interface/state/form-collapse-pane/form-collapse-pane.state.d.ts +0 -11
- package/es/interface/state/form-edit-item/form-edit-item.state.d.ts +0 -38
- package/es/interface/state/form-group/form-group.state.d.ts +0 -21
- package/es/interface/state/form-hidden-item/form-hidden-item.state.d.ts +0 -5
- package/es/interface/state/form-item/form-item.state.d.ts +0 -20
- package/es/interface/state/form-item-basic/form-item-basic.state.d.ts +0 -63
- package/es/interface/state/form-tab/form-tab.state.d.ts +0 -19
- package/es/interface/state/form-tab-pane/form-tab-pane.state.d.ts +0 -11
- package/es/interface/state/index.d.ts +0 -10
- package/es/modules/platform-config/interfaces/global-setting.interface.d.ts +0 -7
- package/es/register/editor-register/editor-register.d.ts +0 -49
- package/es/register/editor-register/editor-register.mjs +0 -57
- package/es/register/form-item-register/form-item-register.d.ts +0 -36
- package/es/register/form-item-register/form-item-register.mjs +0 -42
- package/es/register/index.d.ts +0 -14
- package/es/state/form/form.state.d.ts +0 -24
- package/es/state/form/form.state.mjs +0 -16
- package/es/state/form-collapse/form-collapse.state.d.ts +0 -14
- package/es/state/form-collapse/form-collapse.state.mjs +0 -6
- package/es/state/form-collapse-pane/form-collapse-pane.state.d.ts +0 -14
- package/es/state/form-collapse-pane/form-collapse-pane.state.mjs +0 -6
- package/es/state/form-edit-item/form-edit-item.state.d.ts +0 -17
- package/es/state/form-edit-item/form-edit-item.state.mjs +0 -10
- package/es/state/form-group/form-group.state.d.ts +0 -21
- package/es/state/form-group/form-group.state.mjs +0 -14
- package/es/state/form-item/form-item.state.d.ts +0 -14
- package/es/state/form-item/form-item.state.mjs +0 -6
- package/es/state/form-item-basic/form-item-basic.state.d.ts +0 -17
- package/es/state/form-item-basic/form-item-basic.state.mjs +0 -9
- package/es/state/form-tab/form-tab.state.d.ts +0 -15
- package/es/state/form-tab/form-tab.state.mjs +0 -7
- package/es/state/form-tab-pane/form-tab-pane.state.d.ts +0 -14
- package/es/state/form-tab-pane/form-tab-pane.state.mjs +0 -6
- package/es/utils/props/gct-form-item/gct-form-item.props.d.ts +0 -63
- package/es/utils/props/gct-form-item/gct-form-item.props.mjs +0 -66
- package/es/utils/props/index.d.ts +0 -1
- package/es/utils/width-install/width-install.d.ts +0 -17
- package/es/utils/width-install/width-install.mjs +0 -23
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MQTT 连接状态枚举
|
|
3
|
+
*
|
|
4
|
+
* @description 描述 MQTT 客户端当前的连接生命周期状态
|
|
5
|
+
*
|
|
6
|
+
* @export
|
|
7
|
+
* @enum {string}
|
|
8
|
+
*/
|
|
9
|
+
export declare enum MqttConnectionStatus {
|
|
10
|
+
/** 正在连接中 */
|
|
11
|
+
CONNECTING = "connecting",
|
|
12
|
+
/** 已成功连接 */
|
|
13
|
+
CONNECTED = "connected",
|
|
14
|
+
/** 正在断开连接中 */
|
|
15
|
+
DISCONNECTING = "disconnecting",
|
|
16
|
+
/** 已断开连接 */
|
|
17
|
+
DISCONNECTED = "disconnected",
|
|
18
|
+
/** 正在重连中 */
|
|
19
|
+
RECONNECTING = "reconnecting"
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/modules/mqtt/enums/mqtt-status.enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* MQTT 连接状态枚举
|
|
4
|
+
*
|
|
5
|
+
* @description 描述 MQTT 客户端当前的连接生命周期状态
|
|
6
|
+
*
|
|
7
|
+
* @export
|
|
8
|
+
* @enum {string}
|
|
9
|
+
*/
|
|
10
|
+
var MqttConnectionStatus = /* @__PURE__ */ function(MqttConnectionStatus) {
|
|
11
|
+
/** 正在连接中 */
|
|
12
|
+
MqttConnectionStatus["CONNECTING"] = "connecting";
|
|
13
|
+
/** 已成功连接 */
|
|
14
|
+
MqttConnectionStatus["CONNECTED"] = "connected";
|
|
15
|
+
/** 正在断开连接中 */
|
|
16
|
+
MqttConnectionStatus["DISCONNECTING"] = "disconnecting";
|
|
17
|
+
/** 已断开连接 */
|
|
18
|
+
MqttConnectionStatus["DISCONNECTED"] = "disconnected";
|
|
19
|
+
/** 正在重连中 */
|
|
20
|
+
MqttConnectionStatus["RECONNECTING"] = "reconnecting";
|
|
21
|
+
return MqttConnectionStatus;
|
|
22
|
+
}({});
|
|
23
|
+
//#endregion
|
|
24
|
+
export { MqttConnectionStatus };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { IClientOptions } from 'mqtt';
|
|
2
|
+
import { MqttConnectionStatus } from '../enums/mqtt-status.enum';
|
|
3
|
+
/**
|
|
4
|
+
* MQTT 消息回调函数类型
|
|
5
|
+
*
|
|
6
|
+
* @param topic 消息所属的主题
|
|
7
|
+
* @param payload 消息内容
|
|
8
|
+
*/
|
|
9
|
+
export type IMqttMessageCallback = (topic: string, payload: Buffer) => void;
|
|
10
|
+
/**
|
|
11
|
+
* MQTT 客户端事件回调函数类型映射
|
|
12
|
+
*/
|
|
13
|
+
export interface IMqttClientEventMap {
|
|
14
|
+
/** 连接成功事件 */
|
|
15
|
+
connect: () => void;
|
|
16
|
+
/** 断开连接事件 */
|
|
17
|
+
disconnect: () => void;
|
|
18
|
+
/** 重连中事件 */
|
|
19
|
+
reconnect: () => void;
|
|
20
|
+
/** 连接错误事件 */
|
|
21
|
+
error: (err: Error) => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* MQTT 连接选项接口
|
|
25
|
+
*
|
|
26
|
+
* @description 用于创建 MQTT 客户端实例时传入的配置参数
|
|
27
|
+
*
|
|
28
|
+
* @interface IMqttConnectOptions
|
|
29
|
+
*/
|
|
30
|
+
export type IMqttConnectOptions = Partial<IClientOptions>;
|
|
31
|
+
/**
|
|
32
|
+
* MQTT 消息发布选项接口
|
|
33
|
+
*
|
|
34
|
+
* @interface IMqttPublishOptions
|
|
35
|
+
*/
|
|
36
|
+
export interface IMqttPublishOptions {
|
|
37
|
+
/** 服务质量等级:0 最多一次, 1 至少一次, 2 恰好一次,默认 0 */
|
|
38
|
+
qos?: 0 | 1 | 2;
|
|
39
|
+
/** 是否保留消息,默认 false */
|
|
40
|
+
retain?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* MQTT 客户端接口
|
|
44
|
+
*
|
|
45
|
+
* @description 封装单个 MQTT 连接的操作
|
|
46
|
+
*
|
|
47
|
+
* @interface IMqttClient
|
|
48
|
+
*/
|
|
49
|
+
export interface IMqttClient {
|
|
50
|
+
/** 客户端唯一标识 */
|
|
51
|
+
readonly instanceId: string;
|
|
52
|
+
/** 当前连接状态 */
|
|
53
|
+
readonly status: MqttConnectionStatus;
|
|
54
|
+
/**
|
|
55
|
+
* 建立 MQTT 连接
|
|
56
|
+
*/
|
|
57
|
+
connect(): void;
|
|
58
|
+
/**
|
|
59
|
+
* 断开 MQTT 连接
|
|
60
|
+
*/
|
|
61
|
+
disconnect(): void;
|
|
62
|
+
/**
|
|
63
|
+
* 订阅一个或多个主题
|
|
64
|
+
*
|
|
65
|
+
* @param topics 主题或主题数组
|
|
66
|
+
* @param callback 消息回调函数
|
|
67
|
+
* @returns 取消订阅函数
|
|
68
|
+
*/
|
|
69
|
+
subscribe(topics: string | string[], callback: IMqttMessageCallback): () => void;
|
|
70
|
+
/**
|
|
71
|
+
* 取消订阅一个或多个主题
|
|
72
|
+
*
|
|
73
|
+
* @param topics 主题或主题数组
|
|
74
|
+
*/
|
|
75
|
+
unsubscribe(topics: string | string[]): void;
|
|
76
|
+
/**
|
|
77
|
+
* 向指定主题发布消息
|
|
78
|
+
*
|
|
79
|
+
* @param topic 目标主题
|
|
80
|
+
* @param payload 消息内容(字符串或 Buffer)
|
|
81
|
+
* @param options 发布选项
|
|
82
|
+
*/
|
|
83
|
+
publish(topic: string, payload: string | Buffer, options?: IMqttPublishOptions): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* 监听连接级事件
|
|
86
|
+
*
|
|
87
|
+
* @param event 事件名
|
|
88
|
+
* @param listener 监听函数
|
|
89
|
+
*/
|
|
90
|
+
on<E extends keyof IMqttClientEventMap>(event: E, listener: IMqttClientEventMap[E]): void;
|
|
91
|
+
/**
|
|
92
|
+
* 取消监听连接级事件
|
|
93
|
+
*
|
|
94
|
+
* @param event 事件名
|
|
95
|
+
* @param listener 监听函数
|
|
96
|
+
*/
|
|
97
|
+
off<E extends keyof IMqttClientEventMap>(event: E, listener: IMqttClientEventMap[E]): void;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* MQTT 管理器接口
|
|
101
|
+
*
|
|
102
|
+
* @description 管理多个 MQTT 客户端实例的生命周期
|
|
103
|
+
*
|
|
104
|
+
* @interface IMqttManager
|
|
105
|
+
*/
|
|
106
|
+
export interface IMqttManager {
|
|
107
|
+
/**
|
|
108
|
+
* 创建并注册一个新的 MQTT 客户端实例
|
|
109
|
+
*
|
|
110
|
+
* @param instanceId 实例唯一标识
|
|
111
|
+
* @param options 连接选项
|
|
112
|
+
* @returns 已创建的客户端实例
|
|
113
|
+
* @throws 若 instanceId 已存在则抛出错误
|
|
114
|
+
*/
|
|
115
|
+
create(instanceId: string, options: IMqttConnectOptions): IMqttClient;
|
|
116
|
+
/**
|
|
117
|
+
* 获取已注册的 MQTT 客户端实例
|
|
118
|
+
*
|
|
119
|
+
* @param instanceId 实例唯一标识
|
|
120
|
+
* @returns 客户端实例,若不存在则返回 undefined
|
|
121
|
+
*/
|
|
122
|
+
get(instanceId: string): IMqttClient | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* 判断指定实例是否已存在
|
|
125
|
+
*
|
|
126
|
+
* @param instanceId 实例唯一标识
|
|
127
|
+
*/
|
|
128
|
+
has(instanceId: string): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* 销毁指定的 MQTT 客户端实例(会先断开连接)
|
|
131
|
+
*
|
|
132
|
+
* @param instanceId 实例唯一标识
|
|
133
|
+
*/
|
|
134
|
+
destroy(instanceId: string): void;
|
|
135
|
+
/**
|
|
136
|
+
* 销毁全部 MQTT 客户端实例
|
|
137
|
+
*/
|
|
138
|
+
destroyAll(): void;
|
|
139
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { MqttConnectionStatus } from './enums/mqtt-status.enum';
|
|
2
|
+
import { IMqttClient, IMqttClientEventMap, IMqttConnectOptions, IMqttMessageCallback, IMqttPublishOptions } from './interfaces/i-mqtt';
|
|
3
|
+
/**
|
|
4
|
+
* MQTT 客户端
|
|
5
|
+
*
|
|
6
|
+
* @description 封装单个 MQTT 连接,提供连接、断连、订阅、取消订阅、发布等功能。
|
|
7
|
+
* 重连逻辑完全委托给 mqtt.js 内置机制(reconnectPeriod),本类仅负责
|
|
8
|
+
* 状态同步、消息分发与事件转发。
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @class MqttClient
|
|
12
|
+
* @implements {IMqttClient}
|
|
13
|
+
*/
|
|
14
|
+
export declare class MqttClient implements IMqttClient {
|
|
15
|
+
/** 客户端唯一标识 */
|
|
16
|
+
readonly instanceId: string;
|
|
17
|
+
/** 连接 URL(即 brokerUrl) */
|
|
18
|
+
readonly url: string;
|
|
19
|
+
/** 内部 mqtt.js 客户端实例 */
|
|
20
|
+
private _client;
|
|
21
|
+
/** 当前连接状态 */
|
|
22
|
+
private _status;
|
|
23
|
+
/** topic -> 消息回调集合 */
|
|
24
|
+
private readonly _subscriptions;
|
|
25
|
+
/** 用户注册的连接级事件监听器集合 */
|
|
26
|
+
private readonly _eventListeners;
|
|
27
|
+
/** 内部保存的连接选项 */
|
|
28
|
+
private readonly _options;
|
|
29
|
+
/**
|
|
30
|
+
* 创建 MQTT 客户端实例
|
|
31
|
+
*
|
|
32
|
+
* @param instanceId 实例唯一标识
|
|
33
|
+
* @param options 连接选项
|
|
34
|
+
*/
|
|
35
|
+
constructor(instanceId: string, url: string, options: IMqttConnectOptions);
|
|
36
|
+
/** 获取当前连接状态 */
|
|
37
|
+
get status(): MqttConnectionStatus;
|
|
38
|
+
/**
|
|
39
|
+
* 建立 MQTT 连接
|
|
40
|
+
*
|
|
41
|
+
* @description 若当前已连接或正在连接中则不重复建链
|
|
42
|
+
*/
|
|
43
|
+
connect(): void;
|
|
44
|
+
/**
|
|
45
|
+
* 断开 MQTT 连接
|
|
46
|
+
*
|
|
47
|
+
* @description 主动断开并停止 mqtt.js 内置重连
|
|
48
|
+
*/
|
|
49
|
+
disconnect(): void;
|
|
50
|
+
/**
|
|
51
|
+
* 订阅一个或多个主题
|
|
52
|
+
*
|
|
53
|
+
* @param topics 主题或主题数组
|
|
54
|
+
* @param callback 消息回调函数
|
|
55
|
+
* @returns 取消订阅函数
|
|
56
|
+
*/
|
|
57
|
+
subscribe(topics: string | string[], callback: IMqttMessageCallback): () => void;
|
|
58
|
+
/**
|
|
59
|
+
* 取消订阅一个或多个主题
|
|
60
|
+
*
|
|
61
|
+
* @param topics 主题或主题数组
|
|
62
|
+
*/
|
|
63
|
+
unsubscribe(topics: string | string[]): void;
|
|
64
|
+
/**
|
|
65
|
+
* 向指定主题发布消息
|
|
66
|
+
*
|
|
67
|
+
* @param topic 目标主题
|
|
68
|
+
* @param payload 消息内容
|
|
69
|
+
* @param options 发布选项
|
|
70
|
+
* @returns Promise,发布成功后 resolve
|
|
71
|
+
*/
|
|
72
|
+
publish(topic: string, payload: string | Buffer, options?: IMqttPublishOptions): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* 监听连接级事件
|
|
75
|
+
*
|
|
76
|
+
* @param event 事件名
|
|
77
|
+
* @param listener 监听函数
|
|
78
|
+
*/
|
|
79
|
+
on<E extends keyof IMqttClientEventMap>(event: E, listener: IMqttClientEventMap[E]): void;
|
|
80
|
+
/**
|
|
81
|
+
* 取消监听连接级事件
|
|
82
|
+
*
|
|
83
|
+
* @param event 事件名
|
|
84
|
+
* @param listener 监听函数
|
|
85
|
+
*/
|
|
86
|
+
off<E extends keyof IMqttClientEventMap>(event: E, listener: IMqttClientEventMap[E]): void;
|
|
87
|
+
/**
|
|
88
|
+
* 绑定 mqtt.js 内部事件
|
|
89
|
+
*
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
private _bindClientEvents;
|
|
93
|
+
/**
|
|
94
|
+
* 向所有已订阅该 topic 的回调函数分发消息
|
|
95
|
+
*
|
|
96
|
+
* @private
|
|
97
|
+
* @param topic 消息主题
|
|
98
|
+
* @param payload 消息内容
|
|
99
|
+
*/
|
|
100
|
+
private _dispatchMessage;
|
|
101
|
+
/**
|
|
102
|
+
* MQTT 通配符主题匹配
|
|
103
|
+
*
|
|
104
|
+
* @private
|
|
105
|
+
* @param pattern 订阅时使用的通配符 pattern(支持 + 和 #)
|
|
106
|
+
* @param topic 实际消息的主题
|
|
107
|
+
*/
|
|
108
|
+
private _matchTopic;
|
|
109
|
+
/**
|
|
110
|
+
* 触发连接级事件
|
|
111
|
+
*
|
|
112
|
+
* @private
|
|
113
|
+
* @param event 事件名
|
|
114
|
+
* @param arg 可选参数(如 Error)
|
|
115
|
+
*/
|
|
116
|
+
private _emitEvent;
|
|
117
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { MQTT_CLIENT_EVENT, MQTT_DEFAULT_CONNECT_OPTIONS } from "./constants/mqtt.const.mjs";
|
|
2
|
+
import { MqttConnectionStatus } from "./enums/mqtt-status.enum.mjs";
|
|
3
|
+
import mqtt from "mqtt";
|
|
4
|
+
//#region src/modules/mqtt/mqtt-client.ts
|
|
5
|
+
/**
|
|
6
|
+
* MQTT 客户端
|
|
7
|
+
*
|
|
8
|
+
* @description 封装单个 MQTT 连接,提供连接、断连、订阅、取消订阅、发布等功能。
|
|
9
|
+
* 重连逻辑完全委托给 mqtt.js 内置机制(reconnectPeriod),本类仅负责
|
|
10
|
+
* 状态同步、消息分发与事件转发。
|
|
11
|
+
*
|
|
12
|
+
* @export
|
|
13
|
+
* @class MqttClient
|
|
14
|
+
* @implements {IMqttClient}
|
|
15
|
+
*/
|
|
16
|
+
var MqttClient = class {
|
|
17
|
+
/** 客户端唯一标识 */
|
|
18
|
+
instanceId;
|
|
19
|
+
/** 连接 URL(即 brokerUrl) */
|
|
20
|
+
url;
|
|
21
|
+
/** 内部 mqtt.js 客户端实例 */
|
|
22
|
+
_client = null;
|
|
23
|
+
/** 当前连接状态 */
|
|
24
|
+
_status = MqttConnectionStatus.DISCONNECTED;
|
|
25
|
+
/** topic -> 消息回调集合 */
|
|
26
|
+
_subscriptions = /* @__PURE__ */ new Map();
|
|
27
|
+
/** 用户注册的连接级事件监听器集合 */
|
|
28
|
+
_eventListeners = {
|
|
29
|
+
connect: /* @__PURE__ */ new Set(),
|
|
30
|
+
disconnect: /* @__PURE__ */ new Set(),
|
|
31
|
+
reconnect: /* @__PURE__ */ new Set(),
|
|
32
|
+
error: /* @__PURE__ */ new Set()
|
|
33
|
+
};
|
|
34
|
+
/** 内部保存的连接选项 */
|
|
35
|
+
_options;
|
|
36
|
+
/**
|
|
37
|
+
* 创建 MQTT 客户端实例
|
|
38
|
+
*
|
|
39
|
+
* @param instanceId 实例唯一标识
|
|
40
|
+
* @param options 连接选项
|
|
41
|
+
*/
|
|
42
|
+
constructor(instanceId, url, options) {
|
|
43
|
+
if (!instanceId) throw new Error("MqttClient: instanceId 不能为空");
|
|
44
|
+
if (!url) throw new Error("MqttClient: url 不能为空");
|
|
45
|
+
if (!options.clientId) throw new Error("MqttClient: options.clientId 不能为空");
|
|
46
|
+
this.instanceId = instanceId;
|
|
47
|
+
this.url = url;
|
|
48
|
+
this._options = {
|
|
49
|
+
...options,
|
|
50
|
+
keepalive: options.keepalive ?? MQTT_DEFAULT_CONNECT_OPTIONS.keepalive,
|
|
51
|
+
connectTimeout: options.connectTimeout ?? MQTT_DEFAULT_CONNECT_OPTIONS.connectTimeout,
|
|
52
|
+
reconnectPeriod: options.reconnectPeriod ?? MQTT_DEFAULT_CONNECT_OPTIONS.reconnectPeriod,
|
|
53
|
+
clean: options.clean ?? MQTT_DEFAULT_CONNECT_OPTIONS.clean
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** 获取当前连接状态 */
|
|
57
|
+
get status() {
|
|
58
|
+
return this._status;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 建立 MQTT 连接
|
|
62
|
+
*
|
|
63
|
+
* @description 若当前已连接或正在连接中则不重复建链
|
|
64
|
+
*/
|
|
65
|
+
connect() {
|
|
66
|
+
if (this._status === MqttConnectionStatus.CONNECTED || this._status === MqttConnectionStatus.CONNECTING) return;
|
|
67
|
+
this._status = MqttConnectionStatus.CONNECTING;
|
|
68
|
+
this._client = mqtt.connect(this.url, this._options);
|
|
69
|
+
this._bindClientEvents();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 断开 MQTT 连接
|
|
73
|
+
*
|
|
74
|
+
* @description 主动断开并停止 mqtt.js 内置重连
|
|
75
|
+
*/
|
|
76
|
+
disconnect() {
|
|
77
|
+
if (!this._client) {
|
|
78
|
+
this._status = MqttConnectionStatus.DISCONNECTED;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
this._status = MqttConnectionStatus.DISCONNECTING;
|
|
82
|
+
this._client.end(false, {}, () => {
|
|
83
|
+
this._status = MqttConnectionStatus.DISCONNECTED;
|
|
84
|
+
this._emitEvent(MQTT_CLIENT_EVENT.DISCONNECT);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 订阅一个或多个主题
|
|
89
|
+
*
|
|
90
|
+
* @param topics 主题或主题数组
|
|
91
|
+
* @param callback 消息回调函数
|
|
92
|
+
* @returns 取消订阅函数
|
|
93
|
+
*/
|
|
94
|
+
subscribe(topics, callback) {
|
|
95
|
+
const topicList = Array.isArray(topics) ? topics : [topics];
|
|
96
|
+
topicList.forEach((topic) => {
|
|
97
|
+
if (!this._subscriptions.has(topic)) {
|
|
98
|
+
this._subscriptions.set(topic, /* @__PURE__ */ new Set());
|
|
99
|
+
if (this._client && this._status === MqttConnectionStatus.CONNECTED) this._client.subscribe(topic);
|
|
100
|
+
}
|
|
101
|
+
this._subscriptions.get(topic).add(callback);
|
|
102
|
+
});
|
|
103
|
+
return () => this.unsubscribe(topicList);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 取消订阅一个或多个主题
|
|
107
|
+
*
|
|
108
|
+
* @param topics 主题或主题数组
|
|
109
|
+
*/
|
|
110
|
+
unsubscribe(topics) {
|
|
111
|
+
(Array.isArray(topics) ? topics : [topics]).forEach((topic) => {
|
|
112
|
+
this._subscriptions.delete(topic);
|
|
113
|
+
if (this._client && this._status === MqttConnectionStatus.CONNECTED) this._client.unsubscribe(topic);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 向指定主题发布消息
|
|
118
|
+
*
|
|
119
|
+
* @param topic 目标主题
|
|
120
|
+
* @param payload 消息内容
|
|
121
|
+
* @param options 发布选项
|
|
122
|
+
* @returns Promise,发布成功后 resolve
|
|
123
|
+
*/
|
|
124
|
+
publish(topic, payload, options) {
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
if (!this._client || this._status !== MqttConnectionStatus.CONNECTED) {
|
|
127
|
+
reject(/* @__PURE__ */ new Error(`MqttClient [${this.instanceId}]: 尚未建立连接,无法发布消息`));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const publishOptions = {
|
|
131
|
+
qos: options?.qos ?? 0,
|
|
132
|
+
retain: options?.retain ?? false
|
|
133
|
+
};
|
|
134
|
+
this._client.publish(topic, payload, publishOptions, (err) => {
|
|
135
|
+
if (err) reject(err);
|
|
136
|
+
else resolve();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 监听连接级事件
|
|
142
|
+
*
|
|
143
|
+
* @param event 事件名
|
|
144
|
+
* @param listener 监听函数
|
|
145
|
+
*/
|
|
146
|
+
on(event, listener) {
|
|
147
|
+
this._eventListeners[event].add(listener);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 取消监听连接级事件
|
|
151
|
+
*
|
|
152
|
+
* @param event 事件名
|
|
153
|
+
* @param listener 监听函数
|
|
154
|
+
*/
|
|
155
|
+
off(event, listener) {
|
|
156
|
+
this._eventListeners[event].delete(listener);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 绑定 mqtt.js 内部事件
|
|
160
|
+
*
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
_bindClientEvents() {
|
|
164
|
+
if (!this._client) return;
|
|
165
|
+
this._client.on("connect", (_connack) => {
|
|
166
|
+
this._status = MqttConnectionStatus.CONNECTED;
|
|
167
|
+
this._emitEvent(MQTT_CLIENT_EVENT.CONNECT);
|
|
168
|
+
});
|
|
169
|
+
this._client.on("reconnect", () => {
|
|
170
|
+
this._status = MqttConnectionStatus.RECONNECTING;
|
|
171
|
+
this._emitEvent(MQTT_CLIENT_EVENT.RECONNECT);
|
|
172
|
+
});
|
|
173
|
+
this._client.on("error", (err) => {
|
|
174
|
+
this._emitEvent(MQTT_CLIENT_EVENT.ERROR, err);
|
|
175
|
+
});
|
|
176
|
+
this._client.on("close", () => {
|
|
177
|
+
if (this._status !== MqttConnectionStatus.DISCONNECTING) this._status = MqttConnectionStatus.DISCONNECTED;
|
|
178
|
+
});
|
|
179
|
+
this._client.on("message", (topic, payload) => {
|
|
180
|
+
this._dispatchMessage(topic, payload);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 向所有已订阅该 topic 的回调函数分发消息
|
|
185
|
+
*
|
|
186
|
+
* @private
|
|
187
|
+
* @param topic 消息主题
|
|
188
|
+
* @param payload 消息内容
|
|
189
|
+
*/
|
|
190
|
+
_dispatchMessage(topic, payload) {
|
|
191
|
+
const callbacks = this._subscriptions.get(topic);
|
|
192
|
+
if (callbacks) callbacks.forEach((cb) => cb(topic, payload));
|
|
193
|
+
this._subscriptions.forEach((cbSet, pattern) => {
|
|
194
|
+
if (pattern !== topic && this._matchTopic(pattern, topic)) cbSet.forEach((cb) => cb(topic, payload));
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* MQTT 通配符主题匹配
|
|
199
|
+
*
|
|
200
|
+
* @private
|
|
201
|
+
* @param pattern 订阅时使用的通配符 pattern(支持 + 和 #)
|
|
202
|
+
* @param topic 实际消息的主题
|
|
203
|
+
*/
|
|
204
|
+
_matchTopic(pattern, topic) {
|
|
205
|
+
const patternSegments = pattern.split("/");
|
|
206
|
+
const topicSegments = topic.split("/");
|
|
207
|
+
for (let i = 0; i < patternSegments.length; i++) {
|
|
208
|
+
const p = patternSegments[i];
|
|
209
|
+
if (p === "#") return true;
|
|
210
|
+
if (p !== "+" && p !== topicSegments[i]) return false;
|
|
211
|
+
}
|
|
212
|
+
return patternSegments.length === topicSegments.length;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 触发连接级事件
|
|
216
|
+
*
|
|
217
|
+
* @private
|
|
218
|
+
* @param event 事件名
|
|
219
|
+
* @param arg 可选参数(如 Error)
|
|
220
|
+
*/
|
|
221
|
+
_emitEvent(event, arg) {
|
|
222
|
+
if (event === MQTT_CLIENT_EVENT.ERROR && arg) this._eventListeners.error.forEach((fn) => fn(arg));
|
|
223
|
+
else if (event === MQTT_CLIENT_EVENT.CONNECT) this._eventListeners.connect.forEach((fn) => fn());
|
|
224
|
+
else if (event === MQTT_CLIENT_EVENT.DISCONNECT) this._eventListeners.disconnect.forEach((fn) => fn());
|
|
225
|
+
else if (event === MQTT_CLIENT_EVENT.RECONNECT) this._eventListeners.reconnect.forEach((fn) => fn());
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
//#endregion
|
|
229
|
+
export { MqttClient };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { IMqttClient, IMqttConnectOptions, IMqttManager } from './interfaces/i-mqtt';
|
|
2
|
+
/**
|
|
3
|
+
* MQTT 实例管理中心
|
|
4
|
+
*
|
|
5
|
+
* @description 管理多个 MQTT 客户端实例的生命周期,提供创建、获取、销毁等操作。
|
|
6
|
+
* 该类通常作为全局单例挂载在 window._gct.mqtt 上,供各业务模块共享使用。
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // 获取全局管理器
|
|
11
|
+
* const manager = window._gct.mqtt;
|
|
12
|
+
*
|
|
13
|
+
* // 创建一个 MQTT 客户端实例
|
|
14
|
+
* const client = manager.create('device-001', {
|
|
15
|
+
* brokerUrl: 'ws://localhost:8083/mqtt',
|
|
16
|
+
* username: 'admin',
|
|
17
|
+
* password: 'password',
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // 连接
|
|
21
|
+
* client.connect();
|
|
22
|
+
*
|
|
23
|
+
* // 订阅
|
|
24
|
+
* const unsubscribe = client.subscribe('sensor/+/temperature', (topic, payload) => {
|
|
25
|
+
* console.log(topic, payload.toString());
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // 发布
|
|
29
|
+
* await client.publish('sensor/001/cmd', 'on');
|
|
30
|
+
*
|
|
31
|
+
* // 取消订阅
|
|
32
|
+
* unsubscribe();
|
|
33
|
+
*
|
|
34
|
+
* // 销毁实例
|
|
35
|
+
* manager.destroy('device-001');
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @export
|
|
39
|
+
* @class MqttManager
|
|
40
|
+
* @implements {IMqttManager}
|
|
41
|
+
*/
|
|
42
|
+
export declare class MqttManager implements IMqttManager {
|
|
43
|
+
/**
|
|
44
|
+
* 管理器唯一标识
|
|
45
|
+
*
|
|
46
|
+
* @private
|
|
47
|
+
*/
|
|
48
|
+
private readonly uuid;
|
|
49
|
+
/** 已注册的 MQTT 客户端实例注册表,key 为 instanceId */
|
|
50
|
+
private readonly _clients;
|
|
51
|
+
/**
|
|
52
|
+
* 创建并注册一个新的 MQTT 客户端实例
|
|
53
|
+
*
|
|
54
|
+
* @param instanceId 实例唯一标识,全局唯一
|
|
55
|
+
* @param options MQTT 连接选项
|
|
56
|
+
* @returns 创建的 MqttClient 实例
|
|
57
|
+
* @throws 若 instanceId 已存在则抛出错误
|
|
58
|
+
*/
|
|
59
|
+
create(instanceId: string, options: IMqttConnectOptions): IMqttClient;
|
|
60
|
+
/**
|
|
61
|
+
* 获取已注册的 MQTT 客户端实例
|
|
62
|
+
*
|
|
63
|
+
* @param instanceId 实例唯一标识
|
|
64
|
+
* @returns 客户端实例,若不存在则返回 undefined
|
|
65
|
+
*/
|
|
66
|
+
get(instanceId: string): IMqttClient | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* 判断指定实例是否已存在
|
|
69
|
+
*
|
|
70
|
+
* @param instanceId 实例唯一标识
|
|
71
|
+
* @returns 是否存在
|
|
72
|
+
*/
|
|
73
|
+
has(instanceId: string): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* 销毁指定的 MQTT 客户端实例
|
|
76
|
+
*
|
|
77
|
+
* @description 会先断开连接,再从管理器中移除
|
|
78
|
+
* @param instanceId 实例唯一标识
|
|
79
|
+
*/
|
|
80
|
+
destroy(instanceId: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* 销毁全部 MQTT 客户端实例
|
|
83
|
+
*
|
|
84
|
+
* @description 依次断开所有连接并清空注册表
|
|
85
|
+
*/
|
|
86
|
+
destroyAll(): void;
|
|
87
|
+
}
|