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
//
//  TuyaSmartMQTTChannel.h
//  TuyaSmart
//
//  Created by xucheng on 15/5/30.
//  Copyright (c) 2015年 Tuya. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "TuyaSmartMQTTConfigModel.h"
 
@interface TuyaSmartPublishMessageModel : NSObject
 
@property (nonatomic, strong) NSString          *devId;
 
@property (nonatomic, assign) NSTimeInterval    time; // time stamp
@property (nonatomic, assign) NSInteger         protocol; // protocol
@property (nonatomic, assign) double            pv; // version
@property (nonatomic, strong) NSDictionary      *body; // body
@property (nonatomic, strong) NSString          *localKey; // local key
@property (nonatomic, assign) NSInteger         publishS;// sequence
@property (nonatomic, assign) NSInteger         publishR;// publish Id
 
@end
 
@interface TuyaSmartResponseMessageModel : NSObject
 
@property (nonatomic, strong) NSString          *devId;
@property (nonatomic, strong) id                message;  // mesh array
@property (nonatomic, assign) NSInteger         protocol; // protocol
@property (nonatomic, strong) NSString          *type;
@property (nonatomic, assign) NSInteger         responseS;// sequence
@property (nonatomic, assign) NSInteger         responseR;// response Id
@property (nonatomic, assign) NSTimeInterval    time;
 
@end
 
@class TuyaSmartMQTTChannel;
 
/**
 mqtt connect state
 */
typedef NS_ENUM (NSInteger, TuyaSmartMqttConnectState){
    TuyaSmartMqttConnectStateCreated,
    TuyaSmartMqttConnectStateConnecting,
    TuyaSmartMqttConnectStateConnected,
    TuyaSmartMqttConnectStateDisconnecting,
    TuyaSmartMqttConnectStateClose,
    TuyaSmartMqttConnectStateError,
};
 
@protocol TuyaSmartMQTTChannelDelegate <NSObject>
 
@optional
 
/**
 *  mqtt connection channel state changes
 *  mqtt 连接的状态改变回调
 */
- (void)mqttChannel:(TuyaSmartMQTTChannel *)mqttChannel connectState:(TuyaSmartMqttConnectState)connectState error:(NSError *)error;
 
/**
 *  Receive mqtt data
 *  收到mqtt消息上报
 */
- (void)mqttChannel:(TuyaSmartMQTTChannel *)mqttChannel didReceiveMessage:(TuyaSmartResponseMessageModel *)message topic:(NSString *)topic;
 
@end
 
 
@interface TuyaSmartMQTTChannel : NSObject
 
+ (instancetype)sharedInstance;
 
/**
 *  connect mqtt host
 *  建立 mqtt 连接
 *
 *  @param mqttConfig mqttConfig
 */
- (void)startConnectToHostWithMqttConfig:(TuyaSmartMQTTConfigModel *)mqttConfig;
 
/**
 *  close mqtt host
 *  关闭mqtt连接
 */
- (void)close;
 
/**
 *  mqtt connect state
 *  mqtt 连接状态
 */
- (TuyaSmartMqttConnectState)connectState;
 
/**
 *  subscribe topic
 *  订阅主题
 *
 *  @param topic   Topic
 *  @param success Success block
 *  @param failure Failure block
 */
- (void)subscribeToTopic:(NSString *)topic
                 devInfo:(NSDictionary *)devInfo
                 success:(TYSuccessHandler)success
                 failure:(TYFailureError)failure;
 
/**
 *  unsubscribe topic
 *  取消订阅主题
 *
 *  @param topic   Topic
 *  @param success Success block
 *  @param failure Failure block
 */
- (void)unsubscribeToTopic:(NSString *)topic
                   success:(TYSuccessHandler)success
                   failure:(TYFailureError)failure;
 
/**
 *  推送mqtt消息
 *  publish mqtt data
 *
 *  @param data    Data
 *  @param topic   Topic
 *  @param success Success block
 *  @param failure Failure block
 *  @return the Message Identifier of the publish message. Zero if qos 0. If qos 1 or 2, zero was publish faliure
 */
- (UInt16)publishMessage:(NSData *)data
                   topic:(NSString *)topic
                 success:(TYSuccessHandler)success
                 failure:(TYFailureError)failure;
 
/**
 *  推送mqtt消息
 *  publish mqtt data
 *
 *  @param messageModel MessageModel
 *  @param topic        Topic
 *  @param success      Success block
 *  @param failure      Failure block
 */
- (void)publishMessageWithMessageModel:(TuyaSmartPublishMessageModel *)messageModel
                                 topic:(NSString *)topic
                               success:(TYSuccessHandler)success
                               failure:(TYFailureError)failure;
 
/**
 *  add mqtt channel delegate
 *  添加mqtt长连接的代理
 *
 *  @param delegate Delegate
 */
- (void)addDelegate:(id<TuyaSmartMQTTChannelDelegate>)delegate;
 
/**
 *  remove mqtt channel delegate
 *  删除mqtt长连接的代理
 *
 *  @param delegate Delegate
 */
- (void)removeDelegate:(id<TuyaSmartMQTTChannelDelegate>)delegate;
 
@end