Skip to content

Commit

Permalink
add new nfc reader
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanwei committed Jun 26, 2014
1 parent ffe842d commit 81be9d1
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 1 deletion.
14 changes: 14 additions & 0 deletions hkutil-static.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
9B1F435417F99242009E7E4F /* HKCommonPrinter.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9B0F63D217F9581B0093797C /* HKCommonPrinter.h */; };
9B2C031E18EF97A4006E6C05 /* UIViewController+HKEx.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B2C031D18EF97A4006E6C05 /* UIViewController+HKEx.m */; };
9B2C031F18EF9982006E6C05 /* UIViewController+HKEx.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9B2C031C18EF97A4006E6C05 /* UIViewController+HKEx.h */; };
9B343976195AA671005DD310 /* HKQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B343975195AA671005DD310 /* HKQueue.m */; };
9B343977195AAAF0005DD310 /* HKQueue.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9B343974195AA671005DD310 /* HKQueue.h */; };
9B34397A195AAD08005DD310 /* HKNFCReader2.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B343979195AAD08005DD310 /* HKNFCReader2.m */; };
9B35EDD718E4062800CCB428 /* HKXCode93Data.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B35EDD618E4062800CCB428 /* HKXCode93Data.m */; };
9B35EDD818E4077F00CCB428 /* HKXCode93Data.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9B35EDD518E4062800CCB428 /* HKXCode93Data.h */; };
9B369BE218BA508F0079123C /* HKTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B369BE118BA508F0079123C /* HKTimer.m */; };
Expand Down Expand Up @@ -158,6 +161,7 @@
dstPath = "include/${PRODUCT_NAME}";
dstSubfolderSpec = 16;
files = (
9B343977195AAAF0005DD310 /* HKQueue.h in CopyFiles */,
9B02BE51193055B300F662B0 /* HKNFCReader.h in CopyFiles */,
9B5F5236190D4E0500D0CA9A /* HKCaller.h in CopyFiles */,
9B5F520F190B489B00D0CA9A /* CKCalendarView.h in CopyFiles */,
Expand Down Expand Up @@ -238,6 +242,10 @@
9B1F435117F9909F009E7E4F /* HKPrinterCfg.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKPrinterCfg.m; sourceTree = "<group>"; };
9B2C031C18EF97A4006E6C05 /* UIViewController+HKEx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+HKEx.h"; sourceTree = "<group>"; };
9B2C031D18EF97A4006E6C05 /* UIViewController+HKEx.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+HKEx.m"; sourceTree = "<group>"; };
9B343974195AA671005DD310 /* HKQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HKQueue.h; sourceTree = "<group>"; };
9B343975195AA671005DD310 /* HKQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKQueue.m; sourceTree = "<group>"; };
9B343978195AAD08005DD310 /* HKNFCReader2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HKNFCReader2.h; sourceTree = "<group>"; };
9B343979195AAD08005DD310 /* HKNFCReader2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKNFCReader2.m; sourceTree = "<group>"; };
9B35EDD518E4062800CCB428 /* HKXCode93Data.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HKXCode93Data.h; sourceTree = "<group>"; };
9B35EDD618E4062800CCB428 /* HKXCode93Data.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKXCode93Data.m; sourceTree = "<group>"; };
9B369BE018BA508F0079123C /* HKTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HKTimer.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -587,6 +595,8 @@
9B9E70381952FBC000BD14BF /* HKAsyncSocket.m */,
9B02BE4E192F5E9F00F662B0 /* HKNFCReader.h */,
9B02BE4F192F5E9F00F662B0 /* HKNFCReader.m */,
9B343978195AAD08005DD310 /* HKNFCReader2.h */,
9B343979195AAD08005DD310 /* HKNFCReader2.m */,
9B7E400B17F4932800258ACA /* CfgHeader.h */,
9BC8BD3817ED818100DF9423 /* HKGridTableView.h */,
9BC8BD3717ED818100DF9423 /* HKGridTableView.m */,
Expand Down Expand Up @@ -677,6 +687,8 @@
9B4C9ED118E95451003F08F4 /* HKClassUtil.m */,
9B2C031C18EF97A4006E6C05 /* UIViewController+HKEx.h */,
9B2C031D18EF97A4006E6C05 /* UIViewController+HKEx.m */,
9B343974195AA671005DD310 /* HKQueue.h */,
9B343975195AA671005DD310 /* HKQueue.m */,
);
path = tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -789,6 +801,7 @@
9BFFA9F317E48DE500F353A5 /* HKTimeUtil.m in Sources */,
9BFFA9F417E48DE500F353A5 /* HKURLImageView.m in Sources */,
9BFFA9F517E48DE500F353A5 /* HKViewUtil.m in Sources */,
9B343976195AA671005DD310 /* HKQueue.m in Sources */,
9BFFA9F717E48DE500F353A5 /* UIView+HKEx.m in Sources */,
9B5F5235190D42FB00D0CA9A /* HKCaller.m in Sources */,
9B603E8018D6C29E00B401CF /* HKAttrLabel.m in Sources */,
Expand All @@ -799,6 +812,7 @@
9BA56D4F1820AADD006A73FB /* UIImage+HKEx.m in Sources */,
9B1F435217F9909F009E7E4F /* HKPrinterCfg.m in Sources */,
9BBDED05184F7FC900994DAB /* NSData+HKEx.m in Sources */,
9B34397A195AAD08005DD310 /* HKNFCReader2.m in Sources */,
9B68FFBB17FBE1C1006F98A2 /* HKSocket.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 3 additions & 1 deletion util/tools/HKNFCReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "HKNFCReader.h"
#import "HKSocket.h"
#import "HKAsyncSocket.h"
#import "HKDataUtil.h"

@interface HKNFCReader ()
@property(nonatomic,strong)HKSocket* socket;
Expand Down Expand Up @@ -55,7 +56,7 @@ -(instancetype)initWithHost:(NSString*)host
NSString* uid = nil;
//nfcuid:
if ([str length] > 7) {
uid = [str substringFromIndex:7];
uid = [HKDataUtil trim:[str substringFromIndex:7]];
}
if (me.getNFCBlock) {
me.getNFCBlock(uid);
Expand Down Expand Up @@ -117,6 +118,7 @@ -(void)stop{
}

-(void)dealloc{
[self.socket close];
[self.asyncSocket close];
}

Expand Down
32 changes: 32 additions & 0 deletions util/tools/HKNFCReader2.h
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
124 changes: 124 additions & 0 deletions util/tools/HKNFCReader2.m
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
21 changes: 21 additions & 0 deletions util/tools/HKQueue.h
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
49 changes: 49 additions & 0 deletions util/tools/HKQueue.m
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

0 comments on commit 81be9d1

Please sign in to comment.