chenqiyang
2024-07-17 be56723cce4cd60ddc144ebe6ac20607675b3006
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
//
//  HDLSmartLinkTimerManager.m
//  HdlSmartIoT
//
//  Created by 陈启扬 on 2021/9/30.
//
 
#import "HDLLinphoneTimerManager.h"
@interface HDLLinphoneTimerManager()
@property (nonatomic, strong) NSMutableDictionary *sendTimers;//发送数据时的计时器
 
//@property (nonatomic, strong) NSMutableDictionary *timerOuts;//倒计时的timeOut(用于重发,key为timerName,value为对应timeOut)
//
//@property (nonatomic, assign) NSTimeInterval timeStamp;//时间戳(用于记录退出应用时的时间)
//
//@property (nonatomic, strong) NSNumber *timeOutNum;//
 
 
@end
@implementation HDLLinphoneTimerManager
 
+ (instancetype)sharedInstance {
    static HDLLinphoneTimerManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HDLLinphoneTimerManager alloc] init];
    });
    return instance;
}
 
-(instancetype)init{
    self = [super init];
    if (self) {
        self.sendTimers=[[NSMutableDictionary alloc] init];
        
//        [self observeApplicationActionNotification];
    }
    return self;
}
 
///*注册程序生命周期通知
// */
//- (void)observeApplicationActionNotification {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(applicationDidEnterBackground) name: UIApplicationDidEnterBackgroundNotification object:nil];
//}
//- (void)applicationDidEnterBackground {
//    _timeStamp = [NSDate date].timeIntervalSince1970;
//}
//- (void)applicationDidBecomeActive {
//    NSTimeInterval timeInterval = [NSDate date].timeIntervalSince1970-_timeStamp; //进行时间差计算操作
//    _timeStamp = 0;
//    for (int i=0; i<self.timerOuts.allKeys.count; i++) {
//        _timeOutNum=self.timerOuts.allValues[i];
//        NSTimeInterval ret = [_timeOutNum doubleValue] - timeInterval;
//        if (ret > 0) {
//            _timeOutNum = [NSNumber numberWithInteger:ret];
//        } else {
//            _timeOutNum = [NSNumber numberWithInteger:0];
//        }
//    }
//
//}
 
//倒计时
- (void)scheduleTimerWithInterval:(double)interval timerName:(NSString *)timerName count:(HDLLPCoutdownBlock)count
                           finished:(dispatch_block_t)finished
{
//    [self.sendTimerNames  addObject:timerName];
    __block double timeout =interval;
 
 
 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    [self.sendTimers setObject:timer forKey:timerName];
//    HDLNSLog(@"self.sendTimers:%@",self.sendTimers);
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);//
 
    dispatch_source_set_event_handler(timer, ^{
        @autoreleasepool {
            if (timeout<=0) {
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    finished();
                });
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    count(timeout);
                });
            }
            timeout--;
       }
    });
 
    dispatch_resume(timer);
}
 
///**
// 启动一个timer
// @param interval        执行的时间间隔(单位是秒)
// @param timerName       timer的名称,作为唯一标识。
// @param count           倒计时block
// @param finished        时间间隔到点时执行的block。
// */
//- (void)scheduleTimerWithInterval:(double)interval timerName:(NSString *)timerName count:(HDLOPPCoutdownBlock)count
//                           finished:(dispatch_block_t)finished
//{
////    [self.sendTimerNames  addObject:timerName];
//    __block NSNumber *timeout =[NSNumber numberWithDouble:interval];
//
//    [self.timerOuts setObject:timeout forKey:timerName];
//
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//    [self.sendTimers setObject:timer forKey:timerName];
////    HDLNSLog(@"self.sendTimers:%@",self.sendTimers);
//    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);//
//
//    dispatch_source_set_event_handler(timer, ^{
//        @autoreleasepool {
//            double timeoutD=[timeout doubleValue];
//            if (timeoutD<=0) {
//                dispatch_source_cancel(timer);
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    finished();
//                });
//            }else{
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    count(timeoutD);
//                });
//            }
//            timeoutD--;
//            timeout=[NSNumber numberWithDouble:timeoutD];
//            [self.timerOuts setObject:timeout forKey:timerName];
//       }
//    });
//
//    dispatch_resume(timer);
//}
 
/*可设置单位的倒计时
 @param interval 总时间
 @param unit 计时单位
 */
- (void)scheduleTimerWithInterval:(double)interval unit:(double)unit  timerName:(NSString *)timerName count:(HDLLPCoutdownBlock)count
                           finished:(dispatch_block_t)finished
{
    [self cancelTimerByName:timerName];
    __block double timeout =interval;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    [self.sendTimers setObject:timer forKey:timerName];
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),unit*NSEC_PER_SEC, 0);//
    
    dispatch_source_set_event_handler(timer, ^{
        @autoreleasepool {
            if (timeout<=0) {
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    finished();
                });
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    count(timeout);
                });
            }
            timeout-=unit;
       }
    });
 
    dispatch_resume(timer);
}
 
/**
 启动一个计时
 @param rateTime        执行的时间间隔(单位是秒)
 @param timerName       timer的名称,作为唯一标识。
 @param count           计时block
 */
- (void)scheduleTimerWithRateTime:(float)rateTime timerName:(NSString *)timerName count:(HDLLPCoutdownVoidBlock)count{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    [self.sendTimers setObject:timer forKey:timerName];
    
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),rateTime*NSEC_PER_SEC, 0);//
    
    dispatch_source_set_event_handler(timer, ^{
        @autoreleasepool {
 
            dispatch_async(dispatch_get_main_queue(), ^{
                count();
            });
       }
    });
 
    dispatch_resume(timer);
    
}
 
- (void)cancelTimerByName:(NSString *)timerName{
    if ([self.sendTimers objectForKey:timerName]) {
        dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
        dispatch_source_cancel(timer);
        [self.sendTimers removeObjectForKey:timerName];
 
    }
//    dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
    
//    if (timer){
//        dispatch_source_cancel(timer);
//        [self.sendTimers removeObjectForKey:timerName];
//    }
//    [self.sendTimerNames removeObject:timerName];
 
 
    
}
 
- (void)resumeTimerByName:(NSString *)timerName{
    dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
    
    if (!timer)
        return;
 
    dispatch_resume(timer);
 
}
 
- (void)suspendTimerByName:(NSString *)timerName{
    dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
    
    if (!timer)
        return;
 
    dispatch_suspend(timer);
}
 
/*获取timer
 @param timerName timer的名称。
 */
-(dispatch_source_t)timerByNmae:(NSString *)timerName{
    if ([self.sendTimers objectForKey:timerName]) {
        dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
        return timer;
    }else{
        return nil;
    }
}
@end