1
wei
2021-01-21 62d098cb78296feaa6f786a20748921338db838c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// TYBLEMeshManager.h
// TuyaSmartBLEMeshKit
//
// Copyright (c) 2014-2021 Tuya Inc. (https://developer.tuya.com)
 
#ifndef TuyaSmart_TYBLEMeshManager
#define TuyaSmart_TYBLEMeshManager
 
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "TYBleMeshDeviceModel.h"
#import "TYBLEMeshCommand.h"
 
#define kInitMeshName @"out_of_mesh"
 
@class TYBLEMeshManager;
 
@protocol TYBLEMeshManagerDelegate <NSObject>
 
@optional
 
/// Bluetooth status callback.
/// @param status Bluetooth status.
- (void)centralManagerStatusChange:(CBManagerState)status;
 
/// Activate child device callback.
/// @param name The device name.
/// @param deviceId The device ID.
/// @param error Error in activation. If an error occurs, ` name 'and' deviceid 'are empty.
- (void)activeDeviceSuccessWithName:(NSString *)name deviceId:(NSString *)deviceId error:(NSError *)error;
 
/// Activate sub device successfully callback.
/// @param manager TYBLEMeshManager.
/// @param device TYBleMeshDeviceModel.
/// @param devId The device ID.
/// @param error Error in activation. If an error occurs, ` name 'and' deviceid 'are empty.
- (void)bleMeshManager:(TYBLEMeshManager *)manager didActiveSubDevice:(TYBleMeshDeviceModel *)device devId:(NSString *)devId error:(NSError *)error;
 
/// Activate gateway device callback.
/// @param name The device name.
/// @param address The device address.
/// @param mac The gateway mac.
/// @param error Error.
- (void)activeWifiDeviceWithName:(NSString *)name address:(NSInteger)address mac:(NSInteger)mac error:(NSError *)error;
 
/// Activate device failure callback.
/// @param manager TYBLEMeshManager.
/// @param device The device.
/// @param error Error.
- (void)bleMeshManager:(TYBLEMeshManager *)manager didFailToActiveDevice:(TYBleMeshDeviceModel *)device error:(NSError *)error;
 
/// Activate completion callback.
- (void)didFinishToActiveDevList;
 
/// Disconnect device callback.
- (void)notifyCentralManagerDidDisconnectPeripheral;
 
/// Version number callback.
/// @param version Version.
- (void)notifyFirmwareWithVersion:(NSString *)version;
 
/// Login success notification, upgrade required.
/// @param address Current access device address.
- (void)notifyLoginSuccessWithAddress:(uint32_t)address;
 
/// Group operation callbacks, such as group delete device and new device, will be informed which device is through this callback.
/// @param address The device address.
/// @param error Error during operation.
- (void)deviceAddGroupAddress:(uint32_t)address error:(NSError *)error;
 
/// Equipment scanned to the distribution network.
/// @param manager TYBLEMeshManager.
/// @param device TYBleMeshDeviceModel.
- (void)bleMeshManager:(TYBLEMeshManager *)manager didScanedDevice:(TYBleMeshDeviceModel *)device;
 
- (void)notifyQueryGroupAddress:(uint32_t)localId nodeId:(uint32_t)nodeId;
 
/// Mesh callback for successful connection, subsequent local communication can be done directly
- (void)didConnectMeshNodeAndLoginMesh;
 
@end
 
@interface TYBLEMeshManager : NSObject
 
+ (instancetype)sharedInstance;
 
@property (nonatomic, assign) BOOL isPoweredOn;
@property (nonatomic, assign) BOOL isLogin;
@property (nonatomic, assign) BOOL isWifiDevice;
@property (nonatomic, assign) uint32_t wifiAddress;
@property (nonatomic, assign) uint32_t otaAddress;
@property (nonatomic, strong) NSString *ssid;
@property (nonatomic, strong) NSString *password;
@property (nonatomic, strong) NSString *token;
@property (nonatomic, strong) NSString *authKey;
@property (nonatomic, strong) NSString *uuid;
@property (nonatomic, strong) NSString *productId;
@property (nonatomic, strong) NSString *version;
 
/// New Wi-Fi address.
@property (nonatomic, assign) uint32_t wifiMac;
 
@property (nonatomic, weak) id<TYBLEMeshManagerDelegate> delegate;
 
/// Mesh entrance.
/// If the operation is a distribution network, fill in the default mesh name and password. At this time, it will only pass through the
/// `-(void) blemeshmanager: (tyblemeshmanager *) manager didscaneddevice: (tyblemeshdevicemodel *) device; ` returns the scan result.
/// @param name The mesh name.
/// @param pwd The mesh passward.
/// @param active Is it activated for distribution network.
/// @param wifiAddress Wifi address, Gateway distribution network needs, the rest will be transmitted to 0.
/// @param otaAddress OTA device address, required for OTA upgrade, and 0 for the rest.
- (void)startScanWithName:(NSString *)name
                      pwd:(NSString *)pwd
                   active:(BOOL)active
              wifiAddress:(uint32_t)wifiAddress
               otaAddress:(uint32_t)otaAddress;
 
/// Activate device.
/// @param includeGateway Whether to activate the gateway. If it is' yes', the gateway device that has been scanned into the device will be activated, and the remaining sub devices will not be activated.
/// On the contrary, activate all the scanned ordinary mesh sub devices, and do not activate the gateway.
- (void)activeMeshDeviceIncludeGateway:(BOOL)includeGateway;
 
/// Activate a specific device.
/// @param deviceModel The device model.
- (void)activeMeshDevice:(TYBleMeshDeviceModel *)deviceModel;
 
/// Stop activating device.
- (void)stopActiveDevice;
 
- (void)getLightAllStatus;
- (BOOL)isConnected;
- (void)stopScan;
- (void)clearScanData;
 
/// Send command.
/// @param command Command.
- (void)sendCommand:(TYBLEMeshCommand *)command;
 
/// Write Wi Fi information to the gateway. This method will be called automatically after activation with SDK.
/// @param ssid Router hotspot name.
/// @param pwd Router hotspot password.
/// @param token Distribution network token.
- (void)startConfigWiFiWithSsid:(NSString *)ssid
                            pwd:(NSString *)pwd
                          token:(NSString *)token;
 
/// Read firmware version number.
- (void)readFirmwareFeature;
 
/// Send upgrade package.
/// @param address The device address.
/// @param version Version.
/// @param otaData Ota data.
/// @param success Called when the task finishes successfully.
/// @param failure Called when the task is interrupted by an error.
- (void)sendOTAPackWithAddress:(NSInteger)address
                       version:(NSString *)version
                       otaData:(NSData *)otaData
                       success:(TYSuccessHandler)success
                       failure:(TYFailureHandler)failure;
 
/// To get the mesh product name, you need to associate the product with the app in advance, otherwise it will return to null.
/// @param productId The product id.
/// @param completion Complete callback.
- (void)getProductNameByProductId:(NSString *)productId
                       completion:(void(^)(NSString *))completion;
 
@end
 
@interface TYBLEMeshManager (Command)
 
/// Set the mode of the lamp (exclusive for lighting class).
/// @param address Address.
/// @param type Device size class.
/// @param isGroup Is it a group.
/// @param param Param.
- (void)setLightModelWithAddress:(uint32_t)address
                            type:(NSString *)type
                         isGroup:(BOOL)isGroup
                           param:(NSArray<NSString *> *)param;
 
/// Read the scene mode of the lamp (lighting class exclusive).
/// @param address The device address.
/// @param type Device size class.
- (void)getLightSceneModelWithAddress:(uint32_t)address
                                 type:(NSString *)type;
 
/// Get low power (gate) state.
/// @param deviceAddress The device address.
/// @param dps Dps.
/// @param type Type.
- (void)getSensorStateWithAddress:(uint32_t)deviceAddress
                              dps:(NSArray *)dps
                             type:(NSString *)type;
 
/// Add device to group.
/// @param deviceAddress The device address.
/// @param type Type.
/// @param groupAddress The group address.
- (void)addDeviceAddress:(uint32_t)deviceAddress
                    type:(NSString *)type
            groupAddress:(uint32_t)groupAddress;
 
/// Remove device from group.
/// @param deviceAddress The device address.
/// @param type Type.
/// @param groupAddress The group Address.
- (void)deleteDeviceAddress:(uint32_t)deviceAddress
                       type:(NSString *)type
               groupAddress:(uint32_t)groupAddress;
 
/// Delete a group from mesh.
/// @param groupAddress The group address.
/// @param type The group type.
- (void)deleteGroupAddress:(uint32_t)groupAddress
                      type:(NSString *)type;
 
/// Get the group address corresponding to the device.
/// @param deviceAddress The device address.
/// @param type Type.
- (void)getGroupAddressWithDeviceAddress:(uint32_t)deviceAddress
                                    type:(NSString *)type;
 
/// Get all the devices in the group.
/// @param groupAddress The group address.
/// @param type The group type.
- (void)getDevicesAddressWithGroupAddress:(uint32_t)groupAddress
                                     type:(NSString *)type;
 
/// Remove a device from mesh network.
/// @param address The device address.
/// @param type Type.
- (void)kickoutLightWithAddress:(uint32_t)address
                           type:(NSString *)type;
 
/// Get the status of a device, such as the RGB of a light...
/// @param address The device address.
/// @param type Type.
- (void)getDeviceStatusAllWithAddress:(uint32_t)address
                                 type:(NSString *)type;
 
/// Get countdown data of equipment (exclusive for electricians).
/// @param address The device address.
/// @param type Type.
- (void)getDeviceCountdownWithAddress:(uint32_t)address
                                 type:(NSString *)type;
 
@end
 
@interface TYBLEMeshManager (Raw)
 
/** The commands here are issued and controlled under the gateway connection. */
 
/// Delete a group from mesh.
/// @param groupAddress The group address.
/// @param type Type.
- (NSString *)rawDataDeleteGroupAddress:(uint32_t)groupAddress
                                   type:(NSString *)type;
 
/// Remove a device from a group.
/// @param deviceAddress The device address.
/// @param groupAddress The group address.
/// @param type Type.
- (NSString *)rawDataDeleteDeviceAddress:(uint32_t)deviceAddress
                            groupAddress:(uint32_t)groupAddress
                                    type:(NSString *)type;
 
/// Add a device to a group.
/// @param deviceAddress The device address.
/// @param groupAddress The group address.
/// @param type Type.
- (NSString *)rawDataAddDeviceAddress:(uint32_t)deviceAddress
                         groupAddress:(uint32_t)groupAddress
                                 type:(NSString *)type;
 
/// Get all devices under the specified group.
/// @param groupAddress The group address.
/// @param type Type.
- (NSString *)rawDataGetDevicesAddressWithGroupAddress:(uint32_t)groupAddress
                                                  type:(NSString *)type;
 
/// Kick a device out of mesh.
/// @param address The device address.
/// @param type Type.
- (NSString *)rawDataKickoutLightWithAddress:(uint32_t)address
                                        type:(NSString *)type;
 
/// Get the corresponding device status.
/// @param address The device address.
/// @param type Type.
- (NSString *)rawDataGetStatusAllWithAddress:(uint32_t)address
                                        type:(NSString *)type;
 
/// Get device countdown (exclusive for electricians).
/// @param address Teh devce address.
/// @param type Type.
- (NSString *)rawDataCountDownWithAddress:(uint32_t)address
                                     type:(NSString *)type;
 
@end
 
#endif