-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yuanwei
committed
Jun 26, 2014
1 parent
ffe842d
commit 81be9d1
Showing
6 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// HKNFCReader2.h | ||
// hkutil-static | ||
// | ||
// Created by akwei on 6/25/14. | ||
// Copyright (c) 2014 huoku. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HKNFCReader2 : NSObject | ||
@property(nonatomic,assign)NSTimeInterval writeTimeout; | ||
@property(nonatomic,assign)NSTimeInterval readTimeout; | ||
@property(nonatomic,strong)void (^openedBlock)(void); | ||
@property(nonatomic,strong)void (^closedBlock)(void); | ||
@property(nonatomic,strong)void (^getNFCBlock)(NSString* nfc); | ||
@property(nonatomic,strong)void (^failBlock)(NSError* error); | ||
|
||
-(instancetype)initWithHost:(NSString*)host | ||
port:(NSUInteger)port | ||
timeout:(NSTimeInterval)timeout; | ||
|
||
-(BOOL)test; | ||
|
||
-(void)open; | ||
|
||
-(BOOL)isConnected; | ||
|
||
-(void)swipe; | ||
|
||
-(void)stop; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// HKNFCReader2.m | ||
// hkutil-static | ||
// | ||
// Created by akwei on 6/25/14. | ||
// Copyright (c) 2014 huoku. All rights reserved. | ||
// | ||
|
||
#import "HKNFCReader2.h" | ||
#import "HKQueue.h" | ||
#import "HKSocket.h" | ||
#import "HKAsyncSocket.h" | ||
|
||
@interface HKNFCReader2 () | ||
@property(nonatomic,strong)HKSocket* socket; | ||
@property(nonatomic,strong)HKAsyncSocket* asyncSocket; | ||
@property(nonatomic,strong)HKQueue* queue; | ||
@end | ||
|
||
@implementation HKNFCReader2 | ||
-(instancetype)initWithHost:(NSString*)host | ||
port:(NSUInteger)port | ||
timeout:(NSTimeInterval)timeout{ | ||
self = [super init]; | ||
if (self) { | ||
__weak HKNFCReader2* me = self; | ||
self.socket = [[HKSocket alloc] initWithHost:host port:port timeout:timeout]; | ||
self.socket.debug = YES; | ||
self.asyncSocket = [[HKAsyncSocket alloc] initWithHost:host port:port timeout:timeout]; | ||
self.asyncSocket.debug = YES; | ||
self.readTimeout = -1; | ||
[self.asyncSocket setSuccessBlock:^(long tag, NSDictionary *info) { | ||
switch (tag) { | ||
case 0: | ||
//open socket and can write command | ||
if (me.openedBlock) { | ||
me.openedBlock(); | ||
} | ||
break; | ||
case 1:{ | ||
//close socket | ||
if (me.closedBlock) { | ||
me.closedBlock(); | ||
} | ||
break; | ||
} | ||
case 2:{ | ||
//write | ||
[me.asyncSocket readLineDataWithTag:3]; | ||
break; | ||
} | ||
case 3:{ | ||
//read | ||
NSData* data = [info valueForKey:@"data"]; | ||
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | ||
NSString* uid = nil; | ||
//nfcuid: | ||
if ([str length] > 7) { | ||
uid = [str substringFromIndex:7]; | ||
} | ||
if (me.getNFCBlock) { | ||
me.getNFCBlock(uid); | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
}]; | ||
} | ||
return self; | ||
} | ||
|
||
-(void)setFailBlock:(void (^)(NSError *))failBlock{ | ||
_failBlock = failBlock; | ||
self.asyncSocket.failBlock = _failBlock; | ||
} | ||
|
||
-(void)setReadTimeout:(NSTimeInterval)readTimeout{ | ||
_readTimeout = readTimeout; | ||
_asyncSocket.readTimeout = readTimeout; | ||
|
||
} | ||
|
||
-(void)setWriteTimeout:(NSTimeInterval)writeTimeout{ | ||
_writeTimeout = writeTimeout; | ||
_asyncSocket.writeTimeout = writeTimeout; | ||
} | ||
|
||
-(BOOL)test{ | ||
@try { | ||
[self.socket open]; | ||
[self.socket close]; | ||
return YES; | ||
} | ||
@catch (NSException *exception) { | ||
return NO; | ||
} | ||
@finally{ | ||
[self.socket close]; | ||
} | ||
} | ||
|
||
-(void)open{ | ||
[self.asyncSocket open]; | ||
} | ||
|
||
-(BOOL)isConnected{ | ||
return [self.asyncSocket isConnected]; | ||
} | ||
|
||
-(void)swipe{ | ||
[self.asyncSocket writeData:[@"swipe\r\n" dataUsingEncoding:NSUTF8StringEncoding] tag:2]; | ||
} | ||
|
||
-(void)stop{ | ||
[self.asyncSocket close]; | ||
} | ||
|
||
-(void)dealloc{ | ||
[self.socket close]; | ||
[self.asyncSocket close]; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// HKQueue.h | ||
// hkutil-static | ||
// | ||
// Created by akwei on 6/25/14. | ||
// Copyright (c) 2014 huoku. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HKQueue : NSObject | ||
|
||
/** | ||
向队列添加对象 | ||
@param obj 对象 | ||
*/ | ||
-(void)push:(id)obj; | ||
|
||
-(id)pop; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// HKQueue.m | ||
// hkutil-static | ||
// | ||
// Created by akwei on 6/25/14. | ||
// Copyright (c) 2014 huoku. All rights reserved. | ||
// | ||
|
||
#import "HKQueue.h" | ||
|
||
@interface HKQueue () | ||
@property(nonatomic,strong)NSMutableArray* objList; | ||
@end | ||
|
||
@implementation HKQueue{ | ||
dispatch_queue_t _serQueue; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
self.objList = [NSMutableArray array]; | ||
NSString* queueName = [NSString stringWithFormat:@"HKQueue_queueName_%@",[self description]]; | ||
_serQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL); | ||
} | ||
return self; | ||
} | ||
|
||
-(void)push:(id)obj{ | ||
__weak HKQueue* me = self; | ||
dispatch_sync(_serQueue, ^{ | ||
[me.objList addObject:obj]; | ||
}); | ||
} | ||
|
||
-(id)pop{ | ||
__weak HKQueue* me = self; | ||
__block id obj; | ||
dispatch_sync(_serQueue, ^{ | ||
if ([me.objList count] > 0) { | ||
obj =[me.objList lastObject]; | ||
[me.objList removeLastObject]; | ||
} | ||
}); | ||
return obj; | ||
} | ||
|
||
@end |