Initial commit
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// Socketsingleton.h
|
||||
// Ifish
|
||||
//
|
||||
// Created by imac on 15/10/17.
|
||||
// Copyright © 2015年 imac. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AsyncSocket.h"
|
||||
|
||||
#import "manulswitchMSModel.h"
|
||||
#import "XuToLightModel.h"
|
||||
#import "JiaReWenDuModel.h"
|
||||
//#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
|
||||
//static dispatch_once_t onceToken = 0; \
|
||||
//__strong static id sharedInstance = nil; \
|
||||
//dispatch_once(&onceToken, ^{ \
|
||||
//sharedInstance = block(); \
|
||||
//}); \
|
||||
//return sharedInstance; \
|
||||
|
||||
//enum{
|
||||
// SocketOfflineByServer,// 服务器掉线,默认为0
|
||||
// SocketOfflineByUser, // 用户主动cut
|
||||
//};
|
||||
//typedef NS_ENUM(NSInteger,soketofflineStyle){
|
||||
// SocketOfflineByServer,// 服务器掉线,默认为0
|
||||
// SocketOfflineByUser // 用户主动cut
|
||||
//};
|
||||
//监听soket
|
||||
|
||||
@protocol IfishCommuniteDelegate <NSObject>
|
||||
|
||||
//设备登陆成功后返回数据
|
||||
-(void)socketDidGetBackmsgData:(NSData *)data onsoket:(AsyncSocket *)sock;
|
||||
|
||||
//已断开连接
|
||||
|
||||
- (void)ifishSocketDidDisconnect:(AsyncSocket *)sock;
|
||||
|
||||
//已经连接成功
|
||||
|
||||
-(void)ifishSocket:(AsyncSocket *)sock ifishSocketdidConnectToHost:(NSString *)host port:(UInt16)port;
|
||||
|
||||
//发送数据完成
|
||||
|
||||
- (void)ifishSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;
|
||||
|
||||
//错误断开
|
||||
|
||||
-(void)ifishSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
|
||||
|
||||
// 将要连接
|
||||
|
||||
- (BOOL)ifishSocketWillConnect:(AsyncSocket *)sock;
|
||||
|
||||
//新的soket
|
||||
|
||||
- (void)ifishSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket;
|
||||
|
||||
//设备登陆成功
|
||||
|
||||
-(void)ifishDeviceLogInSuccees;
|
||||
|
||||
//设备登陆失败
|
||||
|
||||
-(void)ifishDeviceLogInFail;
|
||||
|
||||
@end
|
||||
|
||||
@interface Socketsingleton : NSObject
|
||||
|
||||
@property (nonatomic, strong) AsyncSocket *clientSocket; // socket
|
||||
@property (nonatomic, copy ) NSString *socketHost; // socket的Host
|
||||
@property (nonatomic, assign) UInt16 socketPort; // socket的prot
|
||||
@property (nonatomic, retain) NSTimer *connectTimer; // 计时器 //心跳
|
||||
@property (nonatomic, assign) id<IfishCommuniteDelegate>
|
||||
|
||||
communiteDelegate;
|
||||
|
||||
+ (Socketsingleton *)sharedInstance;
|
||||
|
||||
-(void)cutOffSocket; // 断开socket连接
|
||||
|
||||
-(void)socketConnectHost;
|
||||
|
||||
@property(nonatomic,copy) NSString *macAddress;
|
||||
|
||||
|
||||
//将通信指令写入soket
|
||||
|
||||
|
||||
-(void)soketWriteData:(NSData *)data;
|
||||
|
||||
|
||||
-(void)setSoketLightDataWith:(manulswitchMSModel *)model;
|
||||
|
||||
-(void)setXuToSoketLightDataWith:( XuToLightModel *)model;
|
||||
-(void)setXuToJiaReBangDataWith:( JiaReWenDuModel *)model;
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
//
|
||||
// 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 disconnect];
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#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 ifishSocket:sock willDisconnectWithError:err];
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock{
|
||||
if (self.communiteDelegate) {
|
||||
[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
|
||||
Reference in New Issue
Block a user