// // IfishUDPBroadCastHelper.m // Ifish // // Created by imac on 16/9/12. // Copyright © 2016年 lianxiang. All rights reserved. // #import "IFishHotpotUDPHelper.h" #import "LxGetCurrentIp.h" #import "AppDelegate.h" #import "ConnectHotspotModel.h" #import "ResetDeviceModel.h" #import "RestartDeviceModel.h" #define K_ReSendTimeInterval 3 @interface IFishHotpotUDPHelper () { NSString * _ssidName; NSString *_pwd; } @property (nonatomic, strong) NSTimer *loopTimer; @property (nonatomic, assign) int tag; @property (nonatomic, assign) BOOL bindok; @property (nonatomic, copy) void(^closeBlock)(void); @end static IFishHotpotUDPHelper *sharedInstace = nil; @implementation IFishHotpotUDPHelper + (IFishHotpotUDPHelper *)sharedInstance{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstace = [[self alloc] init]; }); return sharedInstace; } - (instancetype)init { if (self = [super init]) { self.clientSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; self.bindok=NO; [self bind]; } return self; } -(void)bind { if (!self.bindok) { UInt16 bindPort = 9954; NSError *error; //本地接收端口 [self.clientSocket bindToPort:bindPort error:&error]; if (error) { [self postmsg:error.localizedDescription]; self.bindok=NO; } else { self.bindok=YES; } } } - (void)loopTimerTick { [self bind]; if (self.tag == 0) { [self sendConnectHotspotSoketdata]; } else if (self.tag == 1) { [self sendRestartSocketData]; } else if (self.tag == 2) { [self sendResetSocketData]; } } - (void)startLoopTimer { self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(loopTimerTick) userInfo:nil repeats:YES]; } - (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd { _ssidName = ssidName; _pwd = pwd; [self postmsg:@"命令已发出"]; [self sendConnectHotspotSoketdata]; [self startLoopTimer]; } - (void)broadCastRestCommand { [self postmsg:@"命令已发出"]; [self sendResetSocketData]; [self startLoopTimer]; } - (void)sendResetSocketData { NSString* hostS = @"192.168.4.1"; ResetDeviceModel *model = [ResetDeviceModel new]; NSString *request=[NSString stringWithFormat:@"%@", model.description]; NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; self.tag = 2; // [self.clientSocket receiveWithTimeout:-1 tag:self.tag]; [self.clientSocket beginReceiving:nil]; [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; } - (void)broadCastRestartCommand { [self postmsg:@"命令已发出"]; [self sendRestartSocketData]; [self startLoopTimer]; } - (void)sendRestartSocketData { NSString* hostS = @"192.168.4.1"; //120.55.190.56 服务器IP 指令中用 RestartDeviceModel *model = [RestartDeviceModel new]; NSString *request=[NSString stringWithFormat:@"%@", model.description]; NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; self.tag = 1; //[self.clientSocket receiveWithTimeout:-1 tag:self.tag]; [self.clientSocket beginReceiving:nil]; [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; } -(void)sendConnectHotspotSoketdata{ NSString* hostS = @"192.168.4.1"; //120.55.190.56 服务器IP 指令中用 ConnectHotspotModel *model = [ConnectHotspotModel new]; model.ssidName = _ssidName; model.pwd = _pwd; NSString *request=[NSString stringWithFormat:@"%@", model.description]; NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; self.tag = 0; // [self.clientSocket receiveWithTimeout:-1 tag:self.tag]; [self.clientSocket beginReceiving:nil]; //BOOL send= [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; // if (!send) // { // [self postmsg:@"发送失败"]; // } } - (void)closeComplete:(void (^)(void))block { self.closeBlock = block; if(self.loopTimer) { [self.loopTimer invalidate]; self.loopTimer=nil; } [self.clientSocket close]; } -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port { [self.loopTimer invalidate]; self.loopTimer = nil; NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data]; if ([model.result isEqualToString:@"01"]) { if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess:)]) { [self.delegate udpHelperCommandExecutedSuccess:model]; } } else { AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate; [app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]]; } return YES; } -(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext { [self.loopTimer invalidate]; self.loopTimer = nil; NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data]; if ([model.result isEqualToString:@"01"]) { if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess:)]) { [self.delegate udpHelperCommandExecutedSuccess:model]; } } else { AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate; [app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]]; } // return YES; } -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{ [self postmsg:@"连接关闭"]; self.bindok=NO; if (self.closeBlock) { self.closeBlock(); } } -(void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error { [self postmsg:@"连接关闭"]; self.bindok=NO; if (self.closeBlock) { self.closeBlock(); } } -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ NSLog(@"didSendData"); [self postmsg:@"指令发送"]; } -(void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag { NSLog(@"didSendData"); [self postmsg:@"指令发送"]; } -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{ NSLog(@"dueToError%@",error); [self postmsg:[NSString stringWithFormat:@"Socket连接失败:%@",error.localizedDescription]]; } -(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error { NSLog(@"dueToError%@",error); [self postmsg:[NSString stringWithFormat:@"Socket连接失败:%@",error.localizedDescription]]; } -(void)dealloc{ [self postmsg:@"dealloc"]; [self.clientSocket close]; } - (void)postmsg:(NSString *)msg { if ([self.delegate respondsToSelector:@selector(udpHelperMessage:)]) { [self.delegate udpHelperMessage:msg]; } } @end