2015-11-19 21:29:13 +00:00
//
// ViewController . m
// BeerBox
//
// Created by Lukas on 24 / 07 / 14.
// Copyright ( c ) 2014 LBSFilm . All rights reserved .
//
# import "ViewController.h"
@ interface ViewController ( )
@ end
@ implementation ViewController
2016-07-29 19:34:39 +00:00
@ synthesize btControl , box , status , connected ;
2015-11-19 21:29:13 +00:00
- ( void ) viewDidLoad
{
[ super viewDidLoad ] ;
// Do any additional setup after loading the view , typically from a nib .
btControl = [ [ SerialGATT alloc ] init ] ;
[ btControl setup ] ;
btControl . delegate = self ;
2016-07-29 19:34:39 +00:00
[ _dataEntry setDelegate : self ] ;
[ _fileEntry setDelegate : self ] ;
2015-11-19 21:29:13 +00:00
}
- ( void ) didReceiveMemoryWarning
{
[ super didReceiveMemoryWarning ] ;
// Dispose of any resources that can be recreated .
}
2016-07-25 11:09:05 +00:00
// # # # # # # # # # # # # Connection Handling
2015-11-19 21:29:13 +00:00
- ( IBAction ) connect : ( id ) sender {
[ btControl findHMSoftPeripherals : 8 ] ;
[ NSTimer scheduledTimerWithTimeInterval : 8 target : self selector : @ selector ( scanTimer : ) userInfo : nil repeats : NO ] ;
}
2016-07-25 11:09:05 +00:00
- ( void ) scanTimer : ( NSTimer * ) timer {
2015-11-19 21:29:13 +00:00
if ( box = = nil ) [ [ [ UIAlertView alloc ] initWithTitle : @ "Error" message : @ "BeerBox not found!" delegate : self cancelButtonTitle : @ "Cancel" otherButtonTitles : nil ] show ] ;
}
2016-07-25 11:09:05 +00:00
- ( void ) peripheralFound : ( CBPeripheral * ) peripheral {
2015-11-19 21:29:13 +00:00
if ( [ peripheral . name isEqual : @ "LBsKeys" ] )
{
box = peripheral ;
[ btControl connect : box ] ;
}
}
2016-07-25 11:09:05 +00:00
- ( void ) serialGATTCharValueUpdated : ( NSString * ) UUID value : ( NSData * ) data {
2016-07-29 19:34:39 +00:00
NSString * retstring = [ [ NSString alloc ] initWithData : data encoding : NSASCIIStringEncoding ] ;
// [ [ [ UIAlertView alloc ] initWithTitle : @ "RCV!" message : retstring delegate : self cancelButtonTitle : @ "Cancel" otherButtonTitles : nil ] show ] ;
// parseValue here
_output . text = [ _output . text stringByAppendingString : retstring ] ;
/ *
NSData * retdata = [ retstring dataUsingEncoding : [ NSString defaultCStringEncoding ] ] ;
NSError * e = nil ;
NSArray * jsonArray = [ NSJSONSerialization JSONObjectWithData : retdata options : NSJSONReadingMutableContainers error : & e ] ;
if ( ! jsonArray ) {
NSLog ( @ "Error parsing JSON: %@" , e ) ;
} else {
for ( NSDictionary * item in jsonArray ) {
NSLog ( @ "Item: %@" , item ) ;
}
}
* /
}
2015-11-19 21:29:13 +00:00
2016-07-29 19:34:39 +00:00
- ( IBAction ) clearOutput : ( id ) sender {
_output . text = @ "" ;
2015-11-19 21:29:13 +00:00
}
2016-07-25 11:09:05 +00:00
- ( IBAction ) listDrinks : ( id ) sender {
2016-07-29 19:34:39 +00:00
if ( self . connected ) {
// NSString * sendString = [ NSString stringWithFormat : @ "!%@;" , _nameEntry . text ] ;
[ self sendString : @ "#1;" ] ;
} else {
[ [ [ UIAlertView alloc ] initWithTitle : @ "Error" message : @ "BeerBox not connected!" delegate : self cancelButtonTitle : @ "Cancel" otherButtonTitles : nil ] show ] ;
}
2016-07-25 11:09:05 +00:00
}
- ( IBAction ) listUsers : ( id ) sender {
2016-07-29 19:34:39 +00:00
if ( self . connected ) {
// NSString * sendString = [ NSString stringWithFormat : @ "!%@;" , _nameEntry . text ] ;
[ self sendString : @ "#2;" ] ;
} else {
[ [ [ UIAlertView alloc ] initWithTitle : @ "Error" message : @ "BeerBox not connected!" delegate : self cancelButtonTitle : @ "Cancel" otherButtonTitles : nil ] show ] ;
}
/ *
UITableViewController * tab = [ self . storyboard instantiateViewControllerWithIdentifier : @ "tableView" ] ;
// tab . anotherString = self . originalString ;
[ tab . tableView setDelegate : self ] ;
[ tab . tableView setDataSource : self ] ;
[ self presentViewController : tab animated : YES completion : nil ] ;
* /
}
- ( IBAction ) startScan : ( id ) sender {
if ( self . connected ) {
// NSString * sendString = [ NSString stringWithFormat : @ "!%@;" , _nameEntry . text ] ;
[ self sendString : @ "!3;" ] ;
} else {
[ [ [ UIAlertView alloc ] initWithTitle : @ "Error" message : @ "BeerBox not connected!" delegate : self cancelButtonTitle : @ "Cancel" otherButtonTitles : nil ] show ] ;
}
}
- ( IBAction ) getUser : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!2%@;" , _fileEntry . text ] ;
[ self sendString : sendString ] ;
2016-07-25 11:09:05 +00:00
}
- ( IBAction ) createUser : ( id ) sender {
2016-07-29 19:34:39 +00:00
NSString * sendString = [ NSString stringWithFormat : @ "!1%@;" , _dataEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( IBAction ) removeUser : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!6%@;" , _fileEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( IBAction ) setCredits : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!7%@;%@;" , _fileEntry . text , _dataEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( IBAction ) setName : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!8%@;%@;" , _fileEntry . text , _dataEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( IBAction ) revokeRFID : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!5%@;" , _fileEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( IBAction ) revokeFinger : ( id ) sender {
NSString * sendString = [ NSString stringWithFormat : @ "!4%@;" , _fileEntry . text ] ;
[ self sendString : sendString ] ;
}
- ( void ) sendString : ( NSString * ) sendString {
2015-11-19 21:29:13 +00:00
NSData * data = [ sendString dataUsingEncoding : [ NSString defaultCStringEncoding ] ] ;
if ( data . length > 20 )
{
int i = 0 ;
int nIndex = 0 ;
while ( ( i + 1 ) * 20 <= data . length ) {
nIndex = i * 20 ;
if ( nIndex > 0 )
nIndex - - ;
NSData * dataSend = [ data subdataWithRange : NSMakeRange ( nIndex , 20 ) ] ;
[ btControl write : box data : dataSend ] ;
i + + ;
}
i = data . length % 20 ;
if ( i > 0 )
{
NSData * dataSend = [ data subdataWithRange : NSMakeRange ( data . length - i , i ) ] ;
[ btControl write : box data : dataSend ] ;
}
} else
{
// NSData * data = [ MsgToArduino . text dataUsingEncoding : [ NSString defaultCStringEncoding ] ] ;
[ btControl write : box data : data ] ;
}
2016-07-29 19:34:39 +00:00
}
2015-11-19 21:29:13 +00:00
2016-07-29 19:34:39 +00:00
/ *
2015-11-19 21:29:13 +00:00
- ( IBAction ) revokeRFID : ( id ) sender {
UIAlertView * alert = [ [ UIAlertView alloc ] initWithTitle : @ "Revoke RFID" message : @ "Enter an ID" delegate : nil cancelButtonTitle : @ "Cancel" otherButtonTitles : @ "Add" , nil ] ;
alert . alertViewStyle = UIAlertViewStylePlainTextInput ;
[ alert show ] ;
[ alert setDelegate : self ] ;
[ [ alert textFieldAtIndex : 0 ] setDelegate : self ] ;
[ [ alert textFieldAtIndex : 0 ] resignFirstResponder ] ;
[ [ alert textFieldAtIndex : 0 ] setKeyboardType : UIKeyboardTypePhonePad ] ;
[ [ alert textFieldAtIndex : 0 ] becomeFirstResponder ] ;
}
2016-07-29 19:34:39 +00:00
* /
2015-11-19 21:29:13 +00:00
- ( void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex {
// The user created a new item , add it
NSLog ( @ "Recall!" ) ;
if ( buttonIndex = = 1 ) {
// Get the input text
NSString * newItem = [ [ alertView textFieldAtIndex : 0 ] text ] ;
UIAlertView * alert = [ [ UIAlertView alloc ] initWithTitle : @ "Number" message : newItem delegate : nil cancelButtonTitle : @ "ok" otherButtonTitles : @ "Add" , nil ] ;
[ alert show ] ;
}
}
- ( void ) setConnect
{
[ [ [ UIAlertView alloc ] initWithTitle : @ "Found!" message : @ "BeerBox found!" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil ] show ] ;
status . text = @ "Connected!" ;
2016-07-29 19:34:39 +00:00
self . connected = true ;
2015-11-19 21:29:13 +00:00
}
- ( void ) setDisconnect
{
[ [ [ UIAlertView alloc ] initWithTitle : @ "Lost!" message : @ "BeerBox lost :-(" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil ] show ] ;
status . text = @ "not Connected" ;
2016-07-29 19:34:39 +00:00
self . connected = false ;
2015-11-19 21:29:13 +00:00
}
- ( BOOL ) textFieldShouldReturn : ( UITextField * ) textField {
[ textField resignFirstResponder ] ;
return NO ;
}
2016-07-29 19:34:39 +00:00
// # # # # # # # Table View stuff
- ( NSInteger ) tableView : ( UITableView * ) tableView numberOfRowsInSection : ( NSInteger ) section {
return 10 ;
}
- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath {
NSString * protoID = @ "userCell" ;
UITableViewCell * cell = [ tableView dequeueReusableCellWithIdentifier : protoID ] ;
cell . textLabel . text = @ "Lol" ;
return cell ;
}
2015-11-19 21:29:13 +00:00
@ end