290 lines
7.2 KiB
Objective-C
290 lines
7.2 KiB
Objective-C
//
|
|
// Socketsingleton.m
|
|
// Ifish
|
|
//
|
|
// Created by imac on 15/10/17.
|
|
// Copyright © 2015年 imac. All rights reserved.
|
|
|
|
|
|
#import "Socketsingleton.h"
|
|
#import "appLogin.h"
|
|
#import "searchDeviceModel.h"
|
|
|
|
@implementation Socketsingleton
|
|
|
|
+(Socketsingleton *) sharedInstance
|
|
{
|
|
|
|
static Socketsingleton *sharedInstace = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstace = [[self alloc] init];
|
|
|
|
|
|
});
|
|
|
|
return sharedInstace;
|
|
}
|
|
// socket连接
|
|
|
|
-(void)socketConnectHost{
|
|
|
|
|
|
|
|
//每次请求建立连接前先断开
|
|
// if (self.clientSocket.isConnected) {
|
|
// [self.clientSocket disconnect];
|
|
// }
|
|
|
|
|
|
if (self.clientSocket.canSafelySetDelegate) {
|
|
NSLog(@"self.clientSocket.canSafelySetDelegateYES");
|
|
|
|
}else{
|
|
NSLog(@"self.clientSocket.canSafelySetDelegateNO");
|
|
}
|
|
|
|
if (self.clientSocket) {
|
|
|
|
self.clientSocket =nil;
|
|
}
|
|
|
|
|
|
self.clientSocket=[[AsyncSocket alloc] initWithDelegate:self];
|
|
|
|
[self.clientSocket connectToHost:SOCKETPORT onPort:9955 error:nil];
|
|
|
|
}
|
|
|
|
|
|
-(void)cutOffSocket{
|
|
|
|
if (self.clientSocket.isConnected) {
|
|
|
|
self.clientSocket.userData = SocketOfflineByUser;// 声明是由用户主动切断
|
|
|
|
[self.clientSocket disconnectAfterReadingAndWriting];
|
|
self.clientSocket=nil;
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#pragma mark AsyncSocketDelegate
|
|
|
|
#pragma mark当已经和服务端断开连接后调用该代理方法
|
|
//断线重连功能需要在这里进行
|
|
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
|
|
{
|
|
|
|
|
|
if (self.clientSocket.userData==SocketOfflineByUser) {
|
|
//主动断开
|
|
NSLog(@"SocketOfflineByUser");
|
|
}else{
|
|
//[self showTitle:@"" messsage:@"已经和服务端断开连接"];
|
|
NSLog(@"SocketOfflineByServer");
|
|
}
|
|
|
|
if (!self.communiteDelegate) {
|
|
NSLog(@"self.communiteDelegate%@",self.communiteDelegate);
|
|
}
|
|
|
|
if (self.communiteDelegate ) {
|
|
|
|
[self.communiteDelegate ifishSocketDidDisconnect:sock];
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
#pragma mark soket错误断开
|
|
|
|
-(void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
|
|
NSLog(@"soket错误断开");
|
|
NSLog(@"存留数据%@",[sock unreadData]);
|
|
if (self.communiteDelegate&&[self.communiteDelegate respondsToSelector:@selector(ifishSocket:willDisconnectWithError:)]) {
|
|
[self.communiteDelegate ifishSocket:sock willDisconnectWithError:err];
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock{
|
|
if (self.communiteDelegate&&[self.communiteDelegate respondsToSelector:@selector(ifishSocketWillConnect:)]) {
|
|
[self.communiteDelegate ifishSocketWillConnect:sock];
|
|
};
|
|
return YES;
|
|
}
|
|
|
|
#pragma mark 当已经与服务端建立连接后调用的代理方法
|
|
|
|
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
|
|
{
|
|
|
|
//[self requesDeviceData];
|
|
//设备登陆
|
|
[self deviceLogInWith:self.macAddress];
|
|
if (self.communiteDelegate) {
|
|
[self.communiteDelegate ifishSocket:sock ifishSocketdidConnectToHost:host port:port];
|
|
}
|
|
|
|
NSLog(@"comm已经与服务器建立连接%d macAddress%@", self.clientSocket.isConnected,self.macAddress);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
|
|
NSLog(@" 新socket");
|
|
if (self.communiteDelegate) {
|
|
[self.communiteDelegate ifishSocket:sock didAcceptNewSocket:newSocket];
|
|
};
|
|
}
|
|
|
|
//发送完成处理
|
|
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
|
|
{
|
|
//[self endRefreshing];
|
|
if (self.communiteDelegate) {
|
|
[self.communiteDelegate ifishSocket:sock didWriteDataWithTag:tag];
|
|
|
|
}
|
|
NSLog(@"发送数据完成");
|
|
|
|
}
|
|
|
|
#pragma mark 收到服务端的回执之后,调用的代理方法
|
|
|
|
|
|
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
|
|
{
|
|
|
|
|
|
[sock readDataWithTimeout:-1 tag:0];
|
|
NSString*string1=[dataContorl dataToHexString:data];
|
|
NSString*string2=[string1 substringWithRange:NSMakeRange(30, 2)];
|
|
if ([string2 isEqualToString:@"01"]&&string1.length==52) {
|
|
NSLog(@"app登录成功");
|
|
// 查询
|
|
|
|
// AppDelegate*delegate=[[UIApplication sharedApplication]delegate];
|
|
// [delegate.window makeToast:@"设备登陆成功"];
|
|
|
|
[self getDeviceInfo];
|
|
if (self.communiteDelegate) {
|
|
[self.communiteDelegate ifishDeviceLogInSuccees];
|
|
}
|
|
|
|
|
|
}else if([string2 isEqualToString:@"00"]&&string1.length==52){
|
|
|
|
AppDelegate*delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
|
|
[delegate.window makeToast:@"设备已离线"];
|
|
|
|
if (self.communiteDelegate) {
|
|
[self.communiteDelegate ifishDeviceLogInFail];
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
|
|
if (self.communiteDelegate) {
|
|
|
|
[self.communiteDelegate socketDidGetBackmsgData:data onsoket:sock];
|
|
|
|
}
|
|
|
|
NSLog(@"登陆成功后收到服务器的回执--->%@",data);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
#pragma mark -设备登陆服务器指令
|
|
|
|
-(void)deviceLogInWith:(NSString *)macaddress{
|
|
|
|
|
|
appLogin *logModel=[[appLogin alloc]init];
|
|
logModel.sendmacId = macaddress;
|
|
logModel.resavemacId = macaddress;
|
|
|
|
// 十六进制转NSdata
|
|
|
|
NSData*data=[dataContorl stringToHexData:logModel.description];//1337AE5
|
|
|
|
[ self.clientSocket writeData:data withTimeout:-1 tag:0 ];
|
|
//为了得到服务端的回执,这里等待接受数据
|
|
[ self.clientSocket readDataWithTimeout:-1 tag:0];
|
|
if (self.clientSocket.isConnected) {
|
|
//soket 未断开时显示
|
|
//[_indicatorView startAnimating];
|
|
}
|
|
|
|
}
|
|
|
|
#pragma mark - 查询设备信息指令
|
|
|
|
-(void)getDeviceInfo{
|
|
|
|
searchDeviceModel*xinxiModel=[[searchDeviceModel alloc]init];
|
|
xinxiModel.resavemacId = self.macAddress;
|
|
xinxiModel.sendmacId = self.macAddress;
|
|
NSString*requestStr=xinxiModel.description;
|
|
NSData*data1=[dataContorl stringToHexData:requestStr];
|
|
[ self.clientSocket writeData:data1 withTimeout:-1 tag:0];
|
|
[ self.clientSocket readDataWithTimeout:-1 tag:0];
|
|
|
|
}
|
|
|
|
#pragma mark - 写入通信指令
|
|
|
|
-(void)soketWriteData:(NSData *)data{
|
|
|
|
[ self.clientSocket writeData:data withTimeout:-1 tag:0];
|
|
NSLog(@"manulData:%@",data);
|
|
|
|
[self.clientSocket readDataWithTimeout:-1 tag:0];// 会出现接受服务器心跳包粘包问题
|
|
//[ self.singletonSocket.clientSocket readDataToLength:99 withTimeout:-1 tag:0];// 防止心跳包粘包
|
|
|
|
}
|
|
|
|
#pragma mark 设置灯控指令mac
|
|
|
|
-(void)setSoketLightDataWith:( manulswitchMSModel *)model{
|
|
|
|
model.sendmacId = self.macAddress;
|
|
model.resavemacId = self.macAddress;
|
|
|
|
}
|
|
|
|
|
|
#pragma mark 设置灯控指令mac 炫多
|
|
|
|
-(void)setXuToSoketLightDataWith:( XuToLightModel *)model{
|
|
model.sendmacId = self.macAddress;
|
|
model.resavemacId = self.macAddress;
|
|
|
|
}
|
|
-(void)setXuToJiaReBangDataWith:( JiaReWenDuModel *)model{
|
|
|
|
model.sendmacId = self.macAddress;
|
|
model.resavemacId = self.macAddress;
|
|
}
|
|
|
|
#pragma mark- 设置开启关闭报警
|
|
|
|
|
|
|
|
@end
|