From 1028ffa9ac59080308eee8efa69892d58c62774f Mon Sep 17 00:00:00 2001 From: xuemh Date: Mon, 6 Aug 2018 22:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E5=90=AF=E3=80=81?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=87=BA=E5=8E=82=E7=9A=84udp=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ifish.xcodeproj/project.pbxproj | 12 ++++ .../mineControllers/RestartViewController.m | 19 +++--- .../mineControllers/RestartViewController.xib | 3 + .../SettingResetViewController.m | 20 +++--- .../SettingResetViewController.xib | 3 + .../leftcontrollers/ConnectHotspotModel.m | 26 +++++++- .../leftcontrollers/IFishHotpotUDPHelper.h | 6 +- .../leftcontrollers/IFishHotpotUDPHelper.m | 65 ++++++++++++++++++- .../leftcontrollers/ResetDeviceModel.h | 13 ++++ .../leftcontrollers/ResetDeviceModel.m | 28 ++++++++ .../leftcontrollers/RestartDeviceModel.h | 13 ++++ .../leftcontrollers/RestartDeviceModel.m | 28 ++++++++ 12 files changed, 209 insertions(+), 27 deletions(-) create mode 100644 Ifish/controllers/leftcontrollers/ResetDeviceModel.h create mode 100644 Ifish/controllers/leftcontrollers/ResetDeviceModel.m create mode 100644 Ifish/controllers/leftcontrollers/RestartDeviceModel.h create mode 100644 Ifish/controllers/leftcontrollers/RestartDeviceModel.m diff --git a/Ifish.xcodeproj/project.pbxproj b/Ifish.xcodeproj/project.pbxproj index 37fc2db..c67abe2 100644 --- a/Ifish.xcodeproj/project.pbxproj +++ b/Ifish.xcodeproj/project.pbxproj @@ -2466,6 +2466,8 @@ E5212236210760A700BA24C5 /* SettingResetViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E5212234210760A600BA24C5 /* SettingResetViewController.xib */; }; E52B400521042ED2003BBA3E /* ConnectHotspotTipViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E52B400321042ED2003BBA3E /* ConnectHotspotTipViewController.m */; }; E52B400621042ED2003BBA3E /* ConnectHotspotTipViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E52B400421042ED2003BBA3E /* ConnectHotspotTipViewController.xib */; }; + E5317A1321188F170014AFDD /* ResetDeviceModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E5317A1221188F170014AFDD /* ResetDeviceModel.m */; }; + E5317A1621188F8A0014AFDD /* RestartDeviceModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E5317A1521188F8A0014AFDD /* RestartDeviceModel.m */; }; E547870B20728F390060C2C2 /* back_btn@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E547870A20728F380060C2C2 /* back_btn@2x.png */; }; E547870F2073192E0060C2C2 /* introlImage_iphonex_1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = E547870C2073192D0060C2C2 /* introlImage_iphonex_1.jpg */; }; E54787102073192E0060C2C2 /* introlImage_iphonex_2.jpg in Resources */ = {isa = PBXBuildFile; fileRef = E547870D2073192D0060C2C2 /* introlImage_iphonex_2.jpg */; }; @@ -6265,6 +6267,10 @@ E52B400221042ED2003BBA3E /* ConnectHotspotTipViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ConnectHotspotTipViewController.h; sourceTree = ""; }; E52B400321042ED2003BBA3E /* ConnectHotspotTipViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ConnectHotspotTipViewController.m; sourceTree = ""; }; E52B400421042ED2003BBA3E /* ConnectHotspotTipViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConnectHotspotTipViewController.xib; sourceTree = ""; }; + E5317A1121188F170014AFDD /* ResetDeviceModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ResetDeviceModel.h; sourceTree = ""; }; + E5317A1221188F170014AFDD /* ResetDeviceModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ResetDeviceModel.m; sourceTree = ""; }; + E5317A1421188F8A0014AFDD /* RestartDeviceModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RestartDeviceModel.h; sourceTree = ""; }; + E5317A1521188F8A0014AFDD /* RestartDeviceModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RestartDeviceModel.m; sourceTree = ""; }; E547870A20728F380060C2C2 /* back_btn@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "back_btn@2x.png"; sourceTree = ""; }; E547870C2073192D0060C2C2 /* introlImage_iphonex_1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = introlImage_iphonex_1.jpg; sourceTree = ""; }; E547870D2073192D0060C2C2 /* introlImage_iphonex_2.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = introlImage_iphonex_2.jpg; sourceTree = ""; }; @@ -9642,6 +9648,10 @@ 883E781C1D48A5E50030E075 /* SecondConnectWifiController.xib */, 883E781D1D48A5E50030E075 /* SelectDeviceViewController.h */, 883E781E1D48A5E50030E075 /* SelectDeviceViewController.m */, + E5317A1421188F8A0014AFDD /* RestartDeviceModel.h */, + E5317A1521188F8A0014AFDD /* RestartDeviceModel.m */, + E5317A1121188F170014AFDD /* ResetDeviceModel.h */, + E5317A1221188F170014AFDD /* ResetDeviceModel.m */, 883E781F1D48A5E50030E075 /* SetViewController.h */, 883E78201D48A5E50030E075 /* SetViewController.m */, E567026B21187E7C00E61993 /* ConnectHotspotModel.h */, @@ -16118,6 +16128,7 @@ 8829582B1DC18CD100E9DDD7 /* AFNOHeaderHttpTool.m in Sources */, E52122302107609500BA24C5 /* RestartViewController.m in Sources */, 886185CD1EF255F900459AD1 /* XuToControlNameViewController.m in Sources */, + E5317A1321188F170014AFDD /* ResetDeviceModel.m in Sources */, 881671FC1EA5DE2B00BEBF23 /* UMAssetsCollectionCheckMarkView.m in Sources */, 881477E61E71091300BFB79C /* IfishTaskModel.m in Sources */, 88C1B2BE1DD1749F00C2C99A /* IfishKanKanListViewController.m in Sources */, @@ -16325,6 +16336,7 @@ 88602A3C1E10BC3500F3DB5D /* NIMCellLayoutConfig.m in Sources */, 882C85A61EB03759001CC414 /* GoldConvertDetailViewCell.m in Sources */, 881477EA1E711B4D00BFB79C /* IfishTaskViewCell.m in Sources */, + E5317A1621188F8A0014AFDD /* RestartDeviceModel.m in Sources */, 88602A591E10BC3500F3DB5D /* NIMTeamCardRowItem.m in Sources */, 886EAC5C1E2DC87E00A4DF27 /* IfishShopInfo.m in Sources */, 886028711E10BB1100F3DB5D /* NSNumberFormatter+CTAssetsPickerController.m in Sources */, diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m index 8f129ca..74fa16f 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.m @@ -7,8 +7,9 @@ // #import "RestartViewController.h" +#import "IFishHotpotUDPHelper.h" -@interface RestartViewController () +@interface RestartViewController () @end @@ -19,19 +20,15 @@ self.title = @"重启设备"; } -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. +- (IBAction)restart:(id)sender { + [IFishHotpotUDPHelper sharedInstance].delegate = self; + [[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand]; } -/* -#pragma mark - Navigation +#pragma mark - IFishHotpotUDPHelperDelegate -// In a storyboard-based application, you will often want to do a little preparation before navigation -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - // Get the new view controller using [segue destinationViewController]. - // Pass the selected object to the new view controller. +- (void)udpHelperCommandExecutedSuccess { + [self.view makeToast:@"设备即将重启"]; } -*/ @end diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.xib b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.xib index 9a5b22b..4c69251 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.xib +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/RestartViewController.xib @@ -112,6 +112,9 @@ + + + diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m index c5e2b39..0533e53 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.m @@ -7,8 +7,9 @@ // #import "SettingResetViewController.h" +#import "IFishHotpotUDPHelper.h" -@interface SettingResetViewController () +@interface SettingResetViewController () @end @@ -19,20 +20,15 @@ self.title = @"恢复出厂设置"; } - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. +- (IBAction)reset:(id)sender { + [IFishHotpotUDPHelper sharedInstance].delegate = self; + [[IFishHotpotUDPHelper sharedInstance] broadCastRestCommand]; } -/* -#pragma mark - Navigation +#pragma mark - IFishHotpotUDPHelperDelegate -// In a storyboard-based application, you will often want to do a little preparation before navigation -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - // Get the new view controller using [segue destinationViewController]. - // Pass the selected object to the new view controller. +- (void)udpHelperCommandExecutedSuccess { + [self.view makeToast:@"设备即将重置"]; } -*/ @end diff --git a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.xib b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.xib index ed7c240..8daf633 100644 --- a/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.xib +++ b/Ifish/controllers/IfishTabControllers/我的/mineControllers/SettingResetViewController.xib @@ -112,6 +112,9 @@ + + + diff --git a/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m b/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m index 881c17e..3d68dc2 100644 --- a/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m +++ b/Ifish/controllers/leftcontrollers/ConnectHotspotModel.m @@ -13,12 +13,34 @@ - (NSString *)description{ self.messagetype=@"00"; - self.massagelegth=@"48"; + self.massagelegth=@"39"; self.functionCode=@"12"; self.sendmacId = @"000000000000"; self.resavemacId = @"000000000000"; - return [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@", self.messagetype,self.functionCode,self.sendmacId,self.resavemacId,self.massagelegth,[dataContorl hexStringFromString:self.ssidName], [dataContorl hexStringFromString:self.pwd],[self crcCode]]; + return [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@", self.messagetype,self.functionCode,self.sendmacId,self.resavemacId,self.massagelegth,[self zeroLeftPadding:[dataContorl hexStringFromString:self.ssidName]], [self zeroLeftPadding:[dataContorl hexStringFromString:self.pwd]],[self crcCode]]; +} + +- (NSString *)ssidName { + return [NSString stringWithFormat:@"%@\0", _ssidName]; +} + +- (NSString *)pwd { + return [NSString stringWithFormat:@"%@\0", _pwd]; +} + +- (NSString *)zeroLeftPadding:(NSString *)origin { + if (origin.length >= 40) { + return [origin substringToIndex:40]; + } else { + NSInteger left = 40 - origin.length; + NSMutableString *str = [NSMutableString string]; + for (NSInteger index = 0; index < left; index++) { + [str appendString:@"0"]; + } + [str appendString:origin]; + return [str copy]; + } } - (NSString *)crcCode { diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h index cbf3a99..e769b56 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.h @@ -11,7 +11,9 @@ @protocol IFishHotpotUDPHelperDelegate +@optional - (void)udpHelperMessage:(NSString *)msg; +- (void)udpHelperCommandExecutedSuccess; @end @@ -25,6 +27,8 @@ @property (nonatomic, weak) id delegate; --(void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd; +- (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd; +- (void)broadCastRestartCommand; +- (void)broadCastRestCommand; @end diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m index 2dac777..f1edd77 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m @@ -10,6 +10,8 @@ #import "LxGetCurrentIp.h" #import "AppDelegate.h" #import "ConnectHotspotModel.h" +#import "ResetDeviceModel.h" +#import "RestartDeviceModel.h" @interface IFishHotpotUDPHelper () { @@ -38,6 +40,65 @@ self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendConnectHotspotSoketdata) userInfo:nil repeats:YES]; } +- (void)broadCastRestCommand { + self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendRestSocketData) userInfo:nil repeats:YES]; +} + +- (void)sendRestSocketData { + NSString* hostS = @"192.168.4.1"; + + self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; + //120.55.190.56 服务器IP 指令中用 + ResetDeviceModel *model = [ResetDeviceModel new]; + NSString *request=[NSString stringWithFormat:@"%@", model.description]; + + NSData *data=[dataContorl stringToHexData:request]; + + UInt16 port = 333; + UInt16 bindPort = 9954; + + NSError *error; + + [self.clientSocket enableBroadcast:YES error:&error]; + + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1]; + + //本地接收端口 + [self.clientSocket bindToPort:bindPort error:&error]; + + [self.clientSocket receiveWithTimeout:-1 tag:0]; +} + +- (void)broadCastRestartCommand { + self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendRestartSocketData) userInfo:nil repeats:YES]; + +} + +- (void)sendRestartSocketData { + NSString* hostS = @"192.168.4.1"; + + self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; + //120.55.190.56 服务器IP 指令中用 + RestartDeviceModel *model = [RestartDeviceModel new]; + NSString *request=[NSString stringWithFormat:@"%@", model.description]; + + NSData *data=[dataContorl stringToHexData:request]; + + UInt16 port = 333; + UInt16 bindPort = 9954; + + NSError *error; + + [self.clientSocket enableBroadcast:YES error:&error]; + + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1]; + + //本地接收端口 + [self.clientSocket bindToPort:bindPort error:&error]; + + [self.clientSocket receiveWithTimeout:-1 tag:0]; +} + -(void)sendConnectHotspotSoketdata{ NSString* hostS = @"192.168.4.1"; @@ -76,6 +137,9 @@ NSLog(@"port%hu",port); AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate; [app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]]; + if ([self.delegate respondsToSelector:@selector(udpHelperCommandExecutedSuccess)]) { + [self.delegate udpHelperCommandExecutedSuccess]; + } [self.loopTimer invalidate]; return YES; @@ -91,7 +155,6 @@ -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ NSLog(@"didSendData"); - [self postmsg:@"发送成功"]; } diff --git a/Ifish/controllers/leftcontrollers/ResetDeviceModel.h b/Ifish/controllers/leftcontrollers/ResetDeviceModel.h new file mode 100644 index 0000000..d1ff31c --- /dev/null +++ b/Ifish/controllers/leftcontrollers/ResetDeviceModel.h @@ -0,0 +1,13 @@ +// +// ResetDeviceModel.h +// Ifish +// +// Created by Minghao Xue on 2018/8/6. +// Copyright © 2018年 lianlian. All rights reserved. +// + +#import "baseModel.h" + +@interface ResetDeviceModel : baseModel + +@end diff --git a/Ifish/controllers/leftcontrollers/ResetDeviceModel.m b/Ifish/controllers/leftcontrollers/ResetDeviceModel.m new file mode 100644 index 0000000..04ad00e --- /dev/null +++ b/Ifish/controllers/leftcontrollers/ResetDeviceModel.m @@ -0,0 +1,28 @@ +// +// ResetDeviceModel.m +// Ifish +// +// Created by Minghao Xue on 2018/8/6. +// Copyright © 2018年 lianlian. All rights reserved. +// + +#import "ResetDeviceModel.h" + +@implementation ResetDeviceModel + +- (NSString *)description{ + + self.messagetype=@"00"; + self.massagelegth=@"11"; + self.functionCode=@"14"; + self.sendmacId = @"000000000000"; + self.resavemacId = @"000000000000"; + + return [NSString stringWithFormat:@"%@%@%@%@%@%@", self.messagetype,self.functionCode,self.sendmacId,self.resavemacId,self.massagelegth,[self crcCode]]; +} + +- (NSString *)crcCode { + return @"0000"; +} + +@end diff --git a/Ifish/controllers/leftcontrollers/RestartDeviceModel.h b/Ifish/controllers/leftcontrollers/RestartDeviceModel.h new file mode 100644 index 0000000..3a0a587 --- /dev/null +++ b/Ifish/controllers/leftcontrollers/RestartDeviceModel.h @@ -0,0 +1,13 @@ +// +// RestartDeviceModel.h +// Ifish +// +// Created by Minghao Xue on 2018/8/6. +// Copyright © 2018年 lianlian. All rights reserved. +// + +#import "baseModel.h" + +@interface RestartDeviceModel : baseModel + +@end diff --git a/Ifish/controllers/leftcontrollers/RestartDeviceModel.m b/Ifish/controllers/leftcontrollers/RestartDeviceModel.m new file mode 100644 index 0000000..f40db8a --- /dev/null +++ b/Ifish/controllers/leftcontrollers/RestartDeviceModel.m @@ -0,0 +1,28 @@ +// +// RestartDeviceModel.m +// Ifish +// +// Created by Minghao Xue on 2018/8/6. +// Copyright © 2018年 lianlian. All rights reserved. +// + +#import "RestartDeviceModel.h" + +@implementation RestartDeviceModel + +- (NSString *)description{ + + self.messagetype=@"00"; + self.massagelegth=@"11"; + self.functionCode=@"13"; + self.sendmacId = @"000000000000"; + self.resavemacId = @"000000000000"; + + return [NSString stringWithFormat:@"%@%@%@%@%@%@", self.messagetype,self.functionCode,self.sendmacId,self.resavemacId,self.massagelegth,[self crcCode]]; +} + +- (NSString *)crcCode { + return @"0000"; +} + +@end