Commit a3a3b9ff authored by Anthony Carno's avatar Anthony Carno
Browse files

updated CaptuvoSDKDemo based on testing (handles pressing keys), also added...

updated CaptuvoSDKDemo based on testing (handles pressing keys), also added CaptuvoSDK to repository
parent c7b93073
#import <Cocoa/Cocoa.h>
#define USER_DEFAULT_BARCODE_S @"user.default.barcode.store"
#define USER_DEFAULT_MSR_S @"user.default.msr.store"
@class AsyncSocket;
@interface AppController : NSObject
{
AsyncSocket *listenSocket;
AsyncSocket *acceptSocket;
NSMutableArray *connectedSockets;
BOOL isRunning;
IBOutlet NSTabView *tabView ;
IBOutlet NSTabViewItem *scanItem ;
IBOutlet NSTabViewItem *barcodeItem ;
IBOutlet NSTabViewItem *msrItem ;
IBOutlet NSTabViewItem *mfgItem ;
IBOutlet NSButton *settingbutton ;
IBOutlet NSButton *brConfigBtn ;
IBOutlet NSButton *msrConfigBtn ;
IBOutlet NSTextView * logView;
IBOutlet NSTextField *portField;
IBOutlet NSButton *startStopButton;
IBOutlet NSButton * ConnectButton;
IBOutlet NSButton *DisConnectButton;
IBOutlet NSButton * scanButton ;
IBOutlet NSButton *swipButton ;
//Barcode setting and storage.
IBOutlet NSArrayController *mybarcodeContentArray;
IBOutlet NSTableView *barcodetableView ;
IBOutlet NSTextField *barcodeField;
//MSR setting and storage.
IBOutlet NSArrayController *myMSRContentArray ;
IBOutlet NSTableView *msrTableView ;
IBOutlet NSTextField *msrField ;
//MFG setting and storage.
IBOutlet NSTextField *accessoryField ;
IBOutlet NSTextField *ManufacturerField ;
IBOutlet NSTextField *ModelNumberField ;
IBOutlet NSTextField *SerialNumberField ;
IBOutlet NSTextField *FirmwareRevField ;
IBOutlet NSTextField *HardwareRevField ;
IBOutlet NSTextField *SDKRevField ;
}
//@property(nonatomic,retain) IBOutlet NSTextField * portField;
//
//@property(nonatomic,retain) IBOutlet NSButton *DisConnectButton;
//
//@property(nonatomic,retain) NSButton *ConnectButton ;
- (IBAction)startStop:(id)sender;
- (IBAction)scanAction:(id)sender ;
- (IBAction)SwipeCardAction:(id)sender ;
- (IBAction)NotificationConnectAction:(id)sender ;
- (IBAction)NotificationDisConnectAction:(id)sender ;
- (IBAction)setBarcodeData:(id)sender ;
- (IBAction)setMsrData:(id)sender ;
- (IBAction)settingAction:(id)sender ;
//Barcode setting.
- (void)updateClientBarcodelist:(NSString*)barcodelist ;
- (IBAction)AddnewBarContentcode:(id)sender ;
- (IBAction)RemoveBarContentcode:(id)sender ;
- (IBAction)barcodeConfigAction:(id)sender ;
//msr setting
- (void)updateClientMSRList:(NSString*)msrList;
- (IBAction)AddnewMSRContent:(id)sender ;
- (IBAction)RemoveMSRContent:(id)sender ;
- (IBAction)msrConfigAction:(id)sender ;
//MFG configure.
- (IBAction)setAccessoryname:(id)sender;
- (IBAction)setManufacture:(id)sender ;
- (IBAction)setModelNumber:(id)sender ;
- (IBAction)setSerialNumber:(id)sender ;
- (IBAction)setFirmwareRev:(id)sender ;
-(IBAction)setHardwareRev:(id)sender ;
- (IBAction)setSDKRev:(id)sender ;
- (IBAction)configureMFG:(id)sender ;
@end
#import "AppController.h"
#import "AsyncSocket.h"
#define WELCOME_MSG 0
#define ECHO_MSG 1
#define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]
@interface AppController (PrivateAPI)
- (void)logError:(NSString *)msg;
- (void)logInfo:(NSString *)msg;
- (void)logMessage:(NSString *)msg;
@end
@implementation AppController
//@synthesize DisConnectButton = DisconectButton ;
- (id)init
{
if(self = [super init])
{
listenSocket = [[AsyncSocket alloc] initWithDelegate:self];
acceptSocket = nil;//[[AsyncSocket alloc] init] ;
connectedSockets = [[NSMutableArray alloc] initWithCapacity:1];
// [portField intValue] = 6000;
//it is running or not.
isRunning = NO;
}
return self;
}
- (void)awakeFromNib
{
[logView setString:@""];
[ConnectButton setEnabled:YES] ;
[DisConnectButton setEnabled:NO] ;
portField.stringValue = @"6000";
[scanButton setEnabled:NO] ;
[swipButton setEnabled:NO] ;
// NSTableColumn *firstNameColumn = [barcodetableView tableColumnWithIdentifier:@"barcode"];
// [firstNameColumn bind:@"value" toObject:mybarcodeContentArray withKeyPath:@"arrangedObjects.barcode" options:nil];
//for barcode setting listview.
[self startStop:nil] ;
[settingbutton setEnabled:NO] ;
NSArray *barcodearray = [[NSUserDefaults standardUserDefaults] objectForKey:USER_DEFAULT_BARCODE_S];
[mybarcodeContentArray removeObjects:barcodearray] ;
for (NSDictionary *dict in barcodearray) {
[mybarcodeContentArray addObject: dict];
}
NSArray *msrarray = [[NSUserDefaults standardUserDefaults] objectForKey:USER_DEFAULT_MSR_S] ;
[myMSRContentArray removeObjects:msrarray] ;
for (NSDictionary *dict in msrarray) {
[myMSRContentArray addObject:dict] ;
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"Ready");
// Advanced options - enable the socket to contine operations even during modal dialogs, and menu browsing
[listenSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
- (void)scrollToBottom
{
NSScrollView *scrollView = [logView enclosingScrollView];
NSPoint newScrollOrigin;
if ([[scrollView documentView] isFlipped])
newScrollOrigin = NSMakePoint(0.0, NSMaxY([[scrollView documentView] frame]));
else
newScrollOrigin = NSMakePoint(0.0, 0.0);
[[scrollView documentView] scrollPoint:newScrollOrigin];
}
- (void)logError:(NSString *)msg
{
NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
[attributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
[as autorelease];
[[logView textStorage] appendAttributedString:as];
[self scrollToBottom];
}
- (void)logInfo:(NSString *)msg
{
NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
[attributes setObject:[NSColor purpleColor] forKey:NSForegroundColorAttributeName];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
[as autorelease];
[[logView textStorage] appendAttributedString:as];
[self scrollToBottom];
}
- (void)logMessage:(NSString *)msg
{
NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
[attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
[as autorelease];
[[logView textStorage] appendAttributedString:as];
[self scrollToBottom];
}
- (IBAction)startStop:(id)sender
{
if(!isRunning)
{
int port = [portField intValue];
if(port < 0 || port > 65535)
{
port = 0;
}
[listenSocket disconnect] ;
for(int i = 0; i < [connectedSockets count]; i++)
{
// Call disconnect on the socket,
// which will invoke the onSocketDidDisconnect: method,
// which will remove the socket from the list.
[[connectedSockets objectAtIndex:i] disconnect];
}
NSError *error = nil;
if(![listenSocket acceptOnPort:port error:&error])
{
[self logError:FORMAT(@"Error starting server: %@", error)];
return;
}
[self logInfo:FORMAT(@"Captuvo Emulator Controller server started on port %hu", [listenSocket localPort])];
isRunning = YES;
[portField setEnabled:NO];
[startStopButton setTitle:@"Stop Server"];
}
else
{
// Stop accepting connections
[listenSocket disconnect];
// Stop any client connections
int i;
for(i = 0; i < [connectedSockets count]; i++)
{
// Call disconnect on the socket,
// which will invoke the onSocketDidDisconnect: method,
// which will remove the socket from the list.
[[connectedSockets objectAtIndex:i] disconnect];
}
[self logInfo:@"Stopped Captuvo Emulator Controller Server"];
isRunning = false;
[portField setEnabled:YES];
[startStopButton setTitle:@"Start Server"];
}
}
- (IBAction)scanAction:(id)sender
{
// NSString *sendstring = @"HoneywellCommand10:123456789" ;
// NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
//
// if (acceptSocket)
// {
// [acceptSocket writeData:data withTimeout:-1 tag:1] ;
// }
NSString *sendstring = @"HoneywellCommand00:" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)SwipeCardAction:(id)sender
{
// NSString *sendstring = @"HoneywellCommand00:" ;
// NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
//
// if (acceptSocket)
// {
// [acceptSocket writeData:data withTimeout:-1 tag:1] ;
// }
NSString *sendstring = @"HoneywellCommand01:" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)NotificationConnectAction:(id)sender
{
[DisConnectButton setEnabled:YES];
[ConnectButton setEnabled:NO];
[settingbutton setEnabled:YES] ;
NSString *sendstring = @"HoneywellCommand02:" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)NotificationDisConnectAction:(id)sender
{
[ConnectButton setEnabled:YES];
[DisConnectButton setEnabled:NO] ;
[settingbutton setEnabled:NO] ;
NSString *sendstring = @"HoneywellCommand03:" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setAccessoryname:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand04:%@",sender] ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setManufacture:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand05:%@",sender] ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setModelNumber:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand06:%@",sender];
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setSerialNumber:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand07:%@",sender];
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setFirmwareRev:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand08:%@",sender];
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
-(IBAction)setHardwareRev:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand09:%@",sender];
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
-(IBAction)setSDKRev:(id)sender
{
NSString *sendstring =[NSString stringWithFormat:@"HoneywellCommand10:%@",sender];
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setBarcodeData:(id)sender
{
NSString *sendstring = @"HoneywellCommand11:123456789" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)setMsrData:(id)sender
{
NSString *sendstring = @"HoneywellCommand12:0000123456789" ;
NSData *data = [sendstring dataUsingEncoding:NSUTF8StringEncoding] ;
if (acceptSocket)
{
[acceptSocket writeData:data withTimeout:-1 tag:1] ;
}
}
- (IBAction)settingAction:(id)sender
{
NSMutableArray*existbarArray = (NSMutableArray*)[mybarcodeContentArray arrangedObjects] ;
NSMutableArray *existMsrobjsArray = (NSMutableArray*)[myMSRContentArray arrangedObjects] ;
if(([existbarArray count] <=0) && ([existMsrobjsArray count] <=0))
{
NSAlert *alert=[NSAlert alertWithMessageText:@"warning" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"barcode list or msr list is empty currently, you should input some data."];
[alert beginSheetModalForWindow:nil modalDelegate:self didEndSelector:@selector(alertEnded:code:context:) contextInfo:nil];
return;
}
[settingbutton setEnabled:NO] ;
[self barcodeConfigAction:nil] ;
[self msrConfigAction:nil] ;
}
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket
{
// if (!acceptSocket) {
acceptSocket = [newSocket retain] ;
NSLog(@"did accept new socket") ;
// }
[connectedSockets addObject:newSocket];
}
- (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket:(AsyncSocket *)newSocket{
NSLog(@"wants runloop for new socket.");
return [NSRunLoop currentRunLoop];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
[self logInfo:FORMAT(@"Accepted client %@:%hu", host, port)];
// NSString *welcomeMsg = @"Welcome to the AsyncSocket Captuvo Simulator Controller\r\n";
// NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];
// [sock writeData:welcomeData withTimeout:-1 tag:WELCOME_MSG];
[self NotificationConnectAction:nil] ;
NSLog(@"did connect to host");
if (acceptSocket)
{
[acceptSocket readDataWithTimeout:-1 tag:1] ;
}
[self configureMFG:nil] ;
// We could call readDataToData:withTimeout:tag: here - that would be perfectly fine.
// If we did this, we'd want to add a check in onSocket:didWriteDataWithTag: and only
// queue another read if tag != WELCOME_MSG.
}
//- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
//{
// [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
//}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
// NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
// NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
// if(msg)
// {
// [self logMessage:msg];
// }
// else
// {
// [self logError:@"Error converting received data into UTF-8 String"];
// }
//
// // Even if we were unable to write the incoming data to the log,
// // we're still going to echo it back to the client.
// [sock writeData:data withTimeout:-1 tag:ECHO_MSG];
NSLog(@"did read data");
NSString</