mini_program_gizwits_sdk 1.0.0-bololo

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/.istanbul.yml ADDED
@@ -0,0 +1,4 @@
1
+ instrumentation:
2
+ root: . # 执行的根目录
3
+ extensions:
4
+ - .ts # 检查覆盖率的文件扩张名
package/.travis.yml ADDED
@@ -0,0 +1,23 @@
1
+ language: node_js
2
+ node_js:
3
+ - 10
4
+ branches:
5
+ only:
6
+ - master
7
+ install:
8
+ - npm install
9
+ script:
10
+ - npm run test
11
+ - npm run build
12
+ after_success:
13
+ - npm run coveralls
14
+ deploy:
15
+ provider: npm
16
+ email: rbwang@gizwits.com
17
+ api_key:
18
+ secure: GrkNtVOo77beuB+ZyMWIeXQa2rYJtpstJYBX7aLQlg7Fb/yvlOywIARI+sW4RmvnrIOVYwVwhX24m9Jq2B/IU6ToiH5LNKeqLVeBaZmcYE2ribl3XkImzO6ZJLDEvMA7/cW8ZP6Pqm5rd1H5xHLYrwe6cwyp4S2LH8FF8scf3HVlDLlkpK+umMgep6nMgXSV0GVz0lrHfFEECvEY4UAuS/gZb9bPk8N8WMW8oqIZHP+yJ/floDopAAEJ+iCvl6SdykZjwmIQf0ZI/l6glgEUITGiai3CYkGcoyENm/itQC0y+wtZhjQmKyHI0zznwswnj33DE2kUla0BLu0P2mW78hlHEKwgdpvbZGlPnS94MTZ7qPkDtHwujJz7/oy54AiNR5LWYdUxLby7n495PKO+eJjYa/zuwjMRavLY86bZIsHTzHTjOVutD9IfsbyeAOSGeowuA3sw5n24R7CmG2oDCbGaQznXayWCcnZteJqLmFmY5B15v3QfcOM5dX/QqLUEZOy6l10bXXElnGqsWLs8Te2a6WeT5TmxqAKA+ljPMzcp7E2jIAiLny7EcRN7yxnTXVO8cVJ42MHlFX93TlFNJlWBi663dV2imb6xi2gK6m66OWgH7pfx32l8PhsAtv9mI43ADRWrRd7E5/jy1hgPbar6dDaDK4tR/gtunWh6KgQ=
19
+ skip_cleanup: true
20
+ on:
21
+ branch: master
22
+ tag: latest
23
+ condition: "$TRAVIS_TAG =~ ^v[0-9]+.[0-9]+.[0-9]+$"
@@ -0,0 +1,19 @@
1
+ {
2
+ // 使用 IntelliSense 了解相关属性。
3
+ // 悬停以查看现有属性的描述。
4
+ // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Mocha Tests",
11
+ "url": "http://localhost:8080",
12
+ "webRoot": "${workspaceFolder}",
13
+ "skipFiles": [
14
+ "<node_internals>/**"
15
+ ],
16
+ "program":"${workspaceFolder}/node_modules/mocha/bin/_mocha -r ts-node/register tests/bleConfig/sdk.ble.spec.ts -file ./tests/setup-wx.js -timeout 60s"
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "mocha.enabled": true
3
+ }
package/README.md ADDED
@@ -0,0 +1,402 @@
1
+ <a href='https://coveralls.io/github/gizwits/mini_program_gizwits_sdk?branch=master'><img src='https://coveralls.io/repos/github/gizwits/mini_program_gizwits_sdk/badge.svg?branch=master' alt='Coverage Status' /></a><img alt="npm" src="https://img.shields.io/npm/v/mini_program_gizwits_sdk">[![Build Status](https://travis-ci.org/gizwits/mini_program_gizwits_sdk.svg?branch=master)](https://travis-ci.org/gizwits/mini_program_gizwits_sdk)
2
+
3
+ # 机智云小程序 SDK
4
+
5
+
6
+ ## ⚠️ 重要
7
+
8
+ 使用小程序 sdk 前,请在微信开发者后台-开发管理-开发设置-服务器域名添加以下请求地址。
9
+
10
+ - request 合法域名
11
+
12
+ ```txt
13
+ https://api.gizwits.com
14
+ https://site.gizwits.com
15
+ ```
16
+
17
+ - socket 合法域名
18
+
19
+ ```txt
20
+ wss://wxstage.gizwits.com
21
+ wss://wxm2m.gizwits.com
22
+ ```
23
+
24
+ - 关于services
25
+
26
+ 为了搜索效率,sdk会根据`services `过滤蓝牙设备。默认 ['ABF8', 'ABF0', 'F0AB', 'F8AB']。
27
+ 如果有特殊情况,不想用`services `过滤设备,可以
28
+
29
+ ```js
30
+ sdk.scanBleDevice(3000, [])
31
+ sdk.startAutoScan([])
32
+ ```
33
+
34
+
35
+ ## 🔧 安装
36
+
37
+ ```bash
38
+ npm install mini_program_gizwits_sdk
39
+ // or
40
+ yarn add mini_program_gizwits_sdk
41
+ ```
42
+
43
+ ## 📑 代码示例
44
+
45
+ > 采用小程序代码片段,安装新版小程序开发工具,点击链接即可打开
46
+
47
+ ### 蓝牙配网
48
+
49
+ [点击打开微信开发者工具](https://developers.weixin.qq.com/s/Wk0aZlmg7jOR)
50
+
51
+ ### 设备控制
52
+
53
+ [点击打开微信开发者工具](https://developers.weixin.qq.com/s/Kh0CMlmq77OA)
54
+
55
+ ## 🏍 初始化 Sdk
56
+
57
+ ```ts
58
+ import GizwitsSdk from 'mini_program_gizwits_sdk';
59
+ import { Component } from 'react';
60
+ import { View } from '@tarojs/components';
61
+
62
+ export default class Index extends Component {
63
+ private gizSdk: GizwitsSdk;
64
+
65
+ componentDidMount() {
66
+ this.gizSdk = new GizwitsSdk({
67
+ appID: 'appID',
68
+ appSecret: 'appSecret',
69
+ productInfo: [
70
+ {
71
+ productKey: 'productKey',
72
+ productSecret: 'productSecret',
73
+ },
74
+ ],
75
+ uid: 'giz_uid', // 必填
76
+ token: 'giz_token', // 必填
77
+ });
78
+ }
79
+
80
+ render() {
81
+ return <View>hello</View>;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ## 📋 日志
87
+ sdk.setLogLevel("debug")
88
+
89
+ ## 📖 API
90
+
91
+ 小程序相关 API。
92
+
93
+ ### sdk.initBle()
94
+
95
+ 初始化 sdk 蓝牙能力。使用其他`BLE API`前**必须**进行初始化。
96
+
97
+ ### sdk.scanBleDevice(delay?:number, services?: string[])
98
+
99
+ 搜索蓝牙设备。搜索到的蓝牙设备会被 [onScanListChange](#onScanListChange) 事件监听。
100
+
101
+ #### 参数
102
+
103
+ - delay: 超时时间。
104
+ - services: 过滤services的条件,不传默认 ['ABF8', 'ABF0', 'F0AB', 'F8AB']
105
+
106
+ ### sdk.startAutoScan(services?: string[])
107
+
108
+ 间隔自动搜索蓝牙设备 [onScanListChange](#onScanListChange) 事件监听。
109
+
110
+ #### 参数
111
+ - services: 过滤services的条件,不传默认 ['ABF8', 'ABF0', 'F0AB', 'F8AB']
112
+
113
+ ### sdk.deviceSafetyRegister(Object object)
114
+
115
+ 安全注册目标设备,蓝牙设备绑定前,需要调用这个接口,否则无法发起设备绑定。返回值中可以拿到注册结果。
116
+
117
+ #### 参数
118
+
119
+ - Object object
120
+
121
+ | 属性 | 类型 | 描述 |
122
+ | ---------- | ------ | ------------------ |
123
+ | mac | string | 待注册设备 mac |
124
+ | productKey | string | 待注册设备所属产品 |
125
+
126
+ ### sdk.bindRemoteDevice(Object object)
127
+
128
+ 绑定远程设备,否则无法发起设备订阅。返回值中可以拿到绑定结果。
129
+
130
+ ### sdk.unbindDevice({devices: [IDevice]})
131
+
132
+ 解绑设备
133
+
134
+ #### 参数
135
+
136
+ - Object object
137
+
138
+ | 属性 | 类型 | 描述 |
139
+ | ---------- | ------ | ------------------ |
140
+ | mac | string | 待绑定设备 mac |
141
+ | productKey | string | 待绑定设备所属产品 |
142
+
143
+ ### sdk.subscribe(device:[IDevice](#IDevice),connectType:[TConnectType](#TConnectType))
144
+
145
+ 订阅目标设备,订阅成功后可控制设备。返回值中可以拿到订阅结果。
146
+
147
+ ### sdk.getDeviceStatus(device:[IDevice](#IDevice), attrNames?: string[])
148
+
149
+ 查询目标设备当前状态,attrNames 不传则查询所有数据
150
+
151
+ ### sdk.write(device:[IDevice](#IDevice),cmd:{[key]:value})
152
+
153
+ 向目标设备发送指令。 其中`key`为数据点名称,value 为数据点的值。
154
+
155
+ ### sdk.unSubscribe(device:[IDevice](#IDevice))
156
+
157
+ 取消订阅目标设备。蓝牙设备会断开连接。返回值中可拿到取消订阅结果。
158
+
159
+ ### sdk.getDevices()
160
+ 刷新设备列表(同时也会在 GizDeviceListNotifications 事件里推送)
161
+ ```ts
162
+ const data = await sdk.getDevices()
163
+ console.log(data)
164
+ ```
165
+
166
+ ### sdk.stopDeviceOnboardingDeploy
167
+ 停止配网
168
+
169
+ ### sdk.setDeviceOnboardingDeploy
170
+ 开始配网
171
+
172
+ 详细参数请查阅类型定义
173
+
174
+ ### sdk.addEventListener(type,listener)
175
+
176
+ #### 概览
177
+
178
+ | 事件种类 type | 回调参数类型 | 回调参数描述 |
179
+ | --------------------------- | ------------------------------------- | -------------------- |
180
+ | GizDeviceListNotifications | [IDevice[]](#IDevice) | 设备列表变更 |
181
+ | GizDeviceStatusNotifications | [IDeviceStatusNoti](#IDeviceStatusNoti) | 设备在线状态变更 |
182
+ | GizDeviceAttrsNotifications | [IDeviceAttrsNoti](#IDeviceAttrsNoti) | 设备数据点变更通知 |
183
+ | onScanListChange | [IDevice[]](#IDevice) | 扫描设备列表变更通知 |
184
+ | onBleHandleError | [IError](#IError) | ble 错误通知 |
185
+
186
+ - <a id="GizDeviceListNotifications"> GizDeviceListNotifications </a>
187
+
188
+ ```ts
189
+ this.gizSdk.addEventListener('GizDeviceListNotifications', (deviceList: IDevice[]) => {
190
+ console.log('列表发生变化', deviceList);
191
+ });
192
+ ```
193
+
194
+ - <a id="GizDeviceStatusNotifications"> GizDeviceStatusNotifications </a>
195
+
196
+ ```ts
197
+ this.gizSdk.addEventListener('GizDeviceStatusNotifications', (e: IDeviceStatusNoti) => {
198
+ console.log('设备在线状态变化', e);
199
+ });
200
+ ```
201
+
202
+ - <a id="onScanListChange">onScanListChange</a>
203
+
204
+ ```ts
205
+ const initBle = () => {
206
+ this.gizSdk.initBle();
207
+ };
208
+
209
+ const scanBle = async () => {
210
+ const data = await this.gizSdk.scanBleDevice(60000);
211
+ console.log('scanBle', data);
212
+ };
213
+
214
+ this.gizSdk.addEventListener('onScanListChange', (deviceList: IDevice[]) => {
215
+ console.log('扫描列表发生变化', deviceList);
216
+ deviceListModal.updateScan(deviceList);
217
+ });
218
+
219
+ initBle();
220
+ scanBle();
221
+ ```
222
+
223
+ - <a id="GizDeviceAttrsNotifications">GizDeviceAttrsNotifications</a>
224
+
225
+ 监听设备上报数据。
226
+
227
+ ```ts
228
+ this.gizSdk.addEventListener(
229
+ 'GizDeviceAttrsNotifications',
230
+ ({ device, data }) => {
231
+ console.log("====设备状态发生变化====", device,data);
232
+ };
233
+ );
234
+ ```
235
+
236
+ #### 类型定义
237
+
238
+ - <a id="IDevice">IDevice</a>
239
+
240
+ ```ts
241
+ interface IDevice {
242
+ mac: string; // 设备mac
243
+ productKey: string; // 机智云pk
244
+ did?: string; // 云平台did
245
+ name: string; // 设备名称
246
+ isBind: boolean; // 是否绑定
247
+ bleDeviceID?: string; // 本地蓝牙设备id
248
+ isOnline: boolean; // 代表大循环在线
249
+ isLanOnline: boolean; // 代表小循环在线
250
+ isBleOnline: boolean; // 代表蓝牙在线
251
+ bleWorkStatus?: number; // 蓝牙设备状态 0 默认状态 1 配网状态
252
+ connectType: TConnectType; // 当前连接类型
253
+ }
254
+ ```
255
+
256
+ - <a id="TConnectType">TConnectType</a>
257
+
258
+ ```ts
259
+ type TConnectType = 'BLE' | 'BLE_BROADCAST' | 'LAN' | 'WAN' | 'NONE';
260
+ }
261
+ ```
262
+
263
+ - <a id="IDeviceAttrsNoti">IDeviceAttrsNoti</a>
264
+
265
+ ```ts
266
+ interface IDeviceAttrsNoti {
267
+ device: IDevice;
268
+ data: object; // 设备上报数据点
269
+ }
270
+ ```
271
+
272
+ - <a id="IDeviceStatusNoti">IDeviceStatusNoti</a>
273
+
274
+ ```ts
275
+ interface IDeviceStatusNoti {
276
+ device: IDevice;
277
+ connectType: TConnectType;
278
+ }
279
+ ```
280
+
281
+ - <a id="TConnectType">TConnectType</a>
282
+
283
+ ```ts
284
+ type TConnectType = 'BLE' | 'BLE_BROADCAST' | 'LAN' | 'WAN' | 'NONE';
285
+ ```
286
+
287
+ - <a id="IError">IError</a>
288
+
289
+ ```ts
290
+ interface IError {
291
+ errorCode: number;
292
+ errorMessage?: string; // 错误信息
293
+ }
294
+ ```
295
+
296
+ ### sdk.removeEventListener(type:K,func:TListenerType[K])
297
+
298
+ 销毁事件监听。
299
+
300
+ # 配网相关
301
+
302
+ ## 使用方法
303
+
304
+ ```javascript
305
+ import GizwitsSdk, { errorCode } from 'mini_program_gizwits_sdk';
306
+
307
+ const configData = {
308
+ appID: "xxx",
309
+ appSecret: "xxx",
310
+ productInfo: [
311
+ {
312
+ productKey: "xxx",
313
+ productSecret: "xxx",
314
+ },
315
+ ],
316
+ };
317
+ const sdk = new GizwitsSdk(configData);
318
+
319
+ try {
320
+ const data = await sdk.setDeviceOnboardingDeploy({
321
+ ssid: SSID,
322
+ password: password,
323
+ mode: 3, // 蓝牙配网
324
+ // mode:0 // softAp
325
+ timeout: 60,
326
+ softAPSSIDPrefix: 'XPG-GAgent-',
327
+ });
328
+ } catch (error) {
329
+ console.log(error);
330
+ }
331
+ ```
332
+
333
+ 其中`data`代表配网成功的设备。
334
+
335
+ 如果失败的话会返回数据
336
+
337
+ ```javascript
338
+ {
339
+ success: false,
340
+ err: {
341
+ errorCode: 'code',
342
+ errorMessage: 'message',
343
+ }
344
+ }
345
+ ```
346
+
347
+ ```ts
348
+ const errorCode: IErrorCode = {
349
+ GIZ_OPENAPI_DEVICE_NOT_BOUND: {
350
+ errorCode: 9017,
351
+ errorMessage: 'not bind',
352
+ },
353
+ GIZ_SDK_HTTP_REQUEST_FAILED: {
354
+ errorCode: 8099,
355
+ errorMessage: '网络请求失败',
356
+ },
357
+ GIZ_SDK_SDK_NOT_INITIALIZED: {
358
+ errorCode: 8300,
359
+ errorMessage: 'not init',
360
+ },
361
+ CONFIG_TIME_OUT: {
362
+ errorCode: 8023,
363
+ errorMessage: 'Config Timeout',
364
+ },
365
+ WECHAT_ERROR: {
366
+ errorCode: 10000,
367
+ errorMessage: 'wechat error',
368
+ },
369
+ GIZ_SDK_ONBOARDING_STOPPED: {
370
+ errorCode: 8318,
371
+ errorMessage: '配网中断',
372
+ },
373
+ GIZ_SDK_DEVICE_CONFIG_IS_RUNNING: {
374
+ errorCode: 8022,
375
+ errorMessage: '配置正在进行中',
376
+ },
377
+ GIZ_OPENAPI_TOKEN_INVALID: {
378
+ errorCode: 9004,
379
+ errorMessage: 'token 失效',
380
+ },
381
+ GIZ_SDK_BIND_DEVICE_FAILED: {
382
+ errorCode: 8039,
383
+ errorMessage: '设备绑定失败',
384
+ },
385
+ GIZ_SDK_BLE_BLUETOOTH_FUNCTION_NOT_TURNED_ON: {
386
+ errorCode: 8281,
387
+ errorMessage: '蓝牙未启用',
388
+ },
389
+ GIZ_SDK_OTHERWISE: {
390
+ errorCode: 8100,
391
+ errorMessage: '',
392
+ },
393
+ GIZ_SDK_DEVICE_NOT_SUBSCRIBED: {
394
+ errorCode: 8029,
395
+ errorMessage: '设备还未订阅',
396
+ },
397
+ GIZ_SDK_PRODUCTKEY_NOT_IN_SPECIFY: {
398
+ errorCode: 8250,
399
+ errorMessage: '产品类型不在指定范围内',
400
+ },
401
+ };
402
+ ```
@@ -0,0 +1,7 @@
1
+ import 'miniprogram-api-typings';
2
+ import gizwitsSdk from './src/sdk';
3
+ import errorCode from './src/utils/errorCode';
4
+ import { BleHandle } from './src/handler/ble';
5
+ export { errorCode, BleHandle };
6
+ export default gizwitsSdk;
7
+ export * from './src/types';