From 6af754c1349b9d1329c94a85b1a09b59dc852d17 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Fri, 23 May 2014 14:45:48 +0000 Subject: [PATCH] --- DUREX Vendor Control/CommunicationProtocol.h | 24 ++++++++++++++++++++++++ DUREX Vendor Control/CommunicationProtocol.m | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.pbxproj | 28 ++++++++++++++++++++++------ DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.xcworkspace/xcuserdata/imanol.xcuserdatad/UserInterfaceState.xcuserstate | Bin 44968 -> 0 bytes DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist | 18 ++++++++++++++++++ DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcschemes/DUREX Vendor Control.xcscheme | 10 +++++----- DUREX Vendor Control/FirstAppExample/DUREXAppDelegate.h | 2 ++ DUREX Vendor Control/FirstAppExample/DUREXAppViewController.h | 4 +--- DUREX Vendor Control/FirstAppExample/DUREXAppViewController.m | 92 ++++++++++++++++++-------------------------------------------------------------------------- DUREX Vendor Control/FirstAppExample/MainStoryboard.storyboard | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ DUREX Vendor Control/InitialViewController.h | 17 +++++++++++++++++ DUREX Vendor Control/InitialViewController.m | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ DUREX Vendor Control/MenuTableViewController.h | 40 ++++++++++++++++++++++++++++++++++++++++ DUREX Vendor Control/MenuTableViewController.m | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ DUREX Vendor Control/bluetooth.png | Bin 0 -> 1052037 bytes 15 files changed, 711 insertions(+), 118 deletions(-) create mode 100644 DUREX Vendor Control/CommunicationProtocol.h create mode 100644 DUREX Vendor Control/CommunicationProtocol.m create mode 100644 DUREX Vendor Control/InitialViewController.h create mode 100644 DUREX Vendor Control/InitialViewController.m create mode 100644 DUREX Vendor Control/MenuTableViewController.h create mode 100644 DUREX Vendor Control/MenuTableViewController.m create mode 100644 DUREX Vendor Control/bluetooth.png diff --git a/DUREX Vendor Control/CommunicationProtocol.h b/DUREX Vendor Control/CommunicationProtocol.h new file mode 100644 index 0000000..78f31b8 --- /dev/null +++ b/DUREX Vendor Control/CommunicationProtocol.h @@ -0,0 +1,24 @@ +// +// CommunicationProtocol.h +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/23/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import +#import "EMFramework.h" + +#define MAX_STRING_LENGTH ((int)227) +#define MAX_RETRIES ((int)3) + +@interface CommunicationProtocol : NSObject + +-(Boolean) waitForMessageAvailable: (Boolean) status; +-(Boolean) writeMessage: (NSString*) message; +-(NSString*) readMessage; +-(Boolean) establishConnection; ++(id) sharedProtocol; +@property Boolean messageAvailable; + +@end diff --git a/DUREX Vendor Control/CommunicationProtocol.m b/DUREX Vendor Control/CommunicationProtocol.m new file mode 100644 index 0000000..517b656 --- /dev/null +++ b/DUREX Vendor Control/CommunicationProtocol.m @@ -0,0 +1,188 @@ +// +// CommunicationProtocol.m +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/23/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import "CommunicationProtocol.h" + +@interface CommunicationProtocol () + +@end + +@implementation CommunicationProtocol + ++ (id)sharedProtocol { + static CommunicationProtocol *shared = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + shared = [[self alloc] init]; + }); + return shared; +} + +-(Boolean) waitForMessageAvailable: (Boolean) status +{ + uint8_t retries = 0; + while([self messageAvailable] != status) + { + [NSThread sleepForTimeInterval:5]; + if([self messageAvailable] != status) + { + if(retries++ == MAX_RETRIES) + { + return FALSE; + } + } + } + return TRUE; +} + +-(NSString*) readMessage +{ + __block uint8_t numPackets, numBytes; + __block NSMutableString *message; + [message setString:@""]; + if([self waitForMessageAvailable:TRUE]) + { + [[EMConnectionManager sharedManager] readResource:@"numPackets" onSuccess:^(id readValue) + { + numPackets = (uint8_t) [readValue unsignedCharValue]; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + numPackets = 0; + }]; + if(numPackets) + { + for(int i = 0; i < numPackets; i++) + { + [[EMConnectionManager sharedManager] readResource:@"numBytes" onSuccess:^(id readValue) + { + numBytes = (uint8_t) [readValue unsignedCharValue]; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + numBytes = 0; + }]; + if(numBytes) + { + [[EMConnectionManager sharedManager] readResource:@"data" onSuccess:^(id readValue) + { + [message appendString:[readValue stringValue]]; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + }]; + } + } + } + } + [[EMConnectionManager sharedManager] writeValue:@"FALSE" toResource:@"messageAvailable" onSuccess:^ + { + + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + } + ]; + return message; +} + +-(Boolean) writeMessage: (NSString*) message +{ + unsigned long remainingBytes = [message length]; + uint8_t numBytes, current_index = 0; + __block Boolean status = FALSE; + [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithUnsignedChar:(unsigned char)([message length]/MAX_STRING_LENGTH)+1] toResource:@"numPackets" onSuccess:^ + { + status = TRUE; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + status = FALSE; + } + ]; + if(status != FALSE) + { + while(remainingBytes) + { + if(![self waitForMessageAvailable:FALSE]) + { + status = FALSE; + break; + } + if(remainingBytes > MAX_STRING_LENGTH) + { + numBytes = MAX_STRING_LENGTH; + remainingBytes -= MAX_STRING_LENGTH; + } + else + { + numBytes = remainingBytes; + remainingBytes = 0; + } + [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithUnsignedChar:(unsigned char)numBytes] toResource:@"data" onSuccess:^ + { + status = TRUE; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + status = FALSE; + } + ]; + if(status != FALSE) + { + NSString *data = [message substringWithRange:NSMakeRange(current_index, numBytes)]; + current_index += numBytes; + [[EMConnectionManager sharedManager] writeValue:data toResource:@"data" onSuccess:^ + { + status = TRUE; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + status = FALSE; + } + ]; + if(status != FALSE) + { + [[EMConnectionManager sharedManager] writeValue:@"TRUE" toResource:@"messageAvailable" onSuccess:^ + { + status = TRUE; + } + onFail:^(NSError *error) + { + NSLog(@"%@",error); + status = FALSE; + } + ]; + } + } + } + } + return status; +} + +-(Boolean) establishConnection +{ + if([self writeMessage:@"Hello"]) + { + NSString *answer = [self readMessage]; + if([answer isEqualToString:@"Hello"]) + { + return TRUE; + } + } + return FALSE; +} + +@end diff --git a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.pbxproj b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.pbxproj index 1b9684c..2415738 100644 --- a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.pbxproj +++ b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.pbxproj @@ -18,10 +18,13 @@ 34AAB883189804FF0019860D /* DUREXAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AAB877189804FF0019860D /* DUREXAppDelegate.m */; }; 34AAB884189804FF0019860D /* EMConnectingView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 34AAB878189804FF0019860D /* EMConnectingView.xib */; }; 34AAB885189804FF0019860D /* EMDevicePickerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AAB87A189804FF0019860D /* EMDevicePickerViewController.m */; }; - 34AAB886189804FF0019860D /* DUREXAppViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AAB87C189804FF0019860D /* DUREXAppViewController.m */; }; 34AAB889189804FF0019860D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 34AAB880189804FF0019860D /* main.m */; }; 34AAB88A189804FF0019860D /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 34AAB881189804FF0019860D /* MainStoryboard.storyboard */; }; 34AAB88D189805300019860D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 34AAB88C189805300019860D /* Images.xcassets */; }; + F98356D6192E835F00EA6821 /* InitialViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F98356D5192E835F00EA6821 /* InitialViewController.m */; }; + F98356D8192E906600EA6821 /* bluetooth.png in Resources */ = {isa = PBXBuildFile; fileRef = F98356D7192E906600EA6821 /* bluetooth.png */; }; + F98356DB192EAFD400EA6821 /* CommunicationProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F98356DA192EAFD400EA6821 /* CommunicationProtocol.m */; }; + F98356E1192EC84700EA6821 /* MenuTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F98356E0192EC84700EA6821 /* MenuTableViewController.m */; }; F9C77F4E192CDE18002DBE8A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F9C77F4C192CDE18002DBE8A /* InfoPlist.strings */; }; F9C77F50192CDE30002DBE8A /* durex.json in Resources */ = {isa = PBXBuildFile; fileRef = F9C77F4F192CDE30002DBE8A /* durex.json */; }; /* End PBXBuildFile section */ @@ -64,13 +67,18 @@ 34AAB878189804FF0019860D /* EMConnectingView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = EMConnectingView.xib; path = FirstAppExample/EMConnectingView.xib; sourceTree = SOURCE_ROOT; }; 34AAB879189804FF0019860D /* EMDevicePickerViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EMDevicePickerViewController.h; path = FirstAppExample/EMDevicePickerViewController.h; sourceTree = SOURCE_ROOT; }; 34AAB87A189804FF0019860D /* EMDevicePickerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = EMDevicePickerViewController.m; path = FirstAppExample/EMDevicePickerViewController.m; sourceTree = SOURCE_ROOT; }; - 34AAB87B189804FF0019860D /* DUREXAppViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DUREXAppViewController.h; path = FirstAppExample/DUREXAppViewController.h; sourceTree = SOURCE_ROOT; }; - 34AAB87C189804FF0019860D /* DUREXAppViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DUREXAppViewController.m; path = FirstAppExample/DUREXAppViewController.m; sourceTree = SOURCE_ROOT; }; 34AAB87E189804FF0019860D /* DUREX Vendor Control-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "DUREX Vendor Control-Info.plist"; path = "FirstAppExample/DUREX Vendor Control-Info.plist"; sourceTree = SOURCE_ROOT; }; 34AAB87F189804FF0019860D /* DUREX Vendor Control-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "DUREX Vendor Control-Prefix.pch"; path = "FirstAppExample/DUREX Vendor Control-Prefix.pch"; sourceTree = SOURCE_ROOT; }; 34AAB880189804FF0019860D /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = FirstAppExample/main.m; sourceTree = SOURCE_ROOT; }; 34AAB881189804FF0019860D /* MainStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = MainStoryboard.storyboard; path = FirstAppExample/MainStoryboard.storyboard; sourceTree = SOURCE_ROOT; }; 34AAB88C189805300019860D /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = FirstAppExample/Images.xcassets; sourceTree = SOURCE_ROOT; }; + F98356D4192E835F00EA6821 /* InitialViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InitialViewController.h; sourceTree = SOURCE_ROOT; }; + F98356D5192E835F00EA6821 /* InitialViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InitialViewController.m; sourceTree = SOURCE_ROOT; }; + F98356D7192E906600EA6821 /* bluetooth.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = bluetooth.png; sourceTree = SOURCE_ROOT; }; + F98356D9192EAFD400EA6821 /* CommunicationProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommunicationProtocol.h; sourceTree = SOURCE_ROOT; }; + F98356DA192EAFD400EA6821 /* CommunicationProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommunicationProtocol.m; sourceTree = SOURCE_ROOT; }; + F98356DF192EC84700EA6821 /* MenuTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MenuTableViewController.h; sourceTree = SOURCE_ROOT; }; + F98356E0192EC84700EA6821 /* MenuTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MenuTableViewController.m; sourceTree = SOURCE_ROOT; }; F9C77F4D192CDE18002DBE8A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = FirstAppExample/en.lproj/InfoPlist.strings; sourceTree = SOURCE_ROOT; }; F9C77F4F192CDE30002DBE8A /* durex.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = durex.json; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ @@ -137,8 +145,12 @@ 34AAB881189804FF0019860D /* MainStoryboard.storyboard */, 34AAB876189804FF0019860D /* DUREXAppDelegate.h */, 34AAB877189804FF0019860D /* DUREXAppDelegate.m */, - 34AAB87B189804FF0019860D /* DUREXAppViewController.h */, - 34AAB87C189804FF0019860D /* DUREXAppViewController.m */, + F98356D4192E835F00EA6821 /* InitialViewController.h */, + F98356D5192E835F00EA6821 /* InitialViewController.m */, + F98356DF192EC84700EA6821 /* MenuTableViewController.h */, + F98356E0192EC84700EA6821 /* MenuTableViewController.m */, + F98356D9192EAFD400EA6821 /* CommunicationProtocol.h */, + F98356DA192EAFD400EA6821 /* CommunicationProtocol.m */, 34AAB88C189805300019860D /* Images.xcassets */, 347186B91807290E00FA0FB2 /* Schemas */, 347186A718070F7F00FA0FB2 /* Device Picker */, @@ -152,6 +164,7 @@ 3471866718070D9300FA0FB2 /* Supporting Files */ = { isa = PBXGroup; children = ( + F98356D7192E906600EA6821 /* bluetooth.png */, 34AAB87E189804FF0019860D /* DUREX Vendor Control-Info.plist */, 34AAB87F189804FF0019860D /* DUREX Vendor Control-Prefix.pch */, 34AAB880189804FF0019860D /* main.m */, @@ -285,6 +298,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + F98356D8192E906600EA6821 /* bluetooth.png in Resources */, 34AAB884189804FF0019860D /* EMConnectingView.xib in Resources */, F9C77F50192CDE30002DBE8A /* durex.json in Resources */, F9C77F4E192CDE18002DBE8A /* InfoPlist.strings in Resources */, @@ -307,9 +321,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F98356D6192E835F00EA6821 /* InitialViewController.m in Sources */, 34AAB885189804FF0019860D /* EMDevicePickerViewController.m in Sources */, 34AAB883189804FF0019860D /* DUREXAppDelegate.m in Sources */, - 34AAB886189804FF0019860D /* DUREXAppViewController.m in Sources */, + F98356DB192EAFD400EA6821 /* CommunicationProtocol.m in Sources */, + F98356E1192EC84700EA6821 /* MenuTableViewController.m in Sources */, 34AAB889189804FF0019860D /* main.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.xcworkspace/xcuserdata/imanol.xcuserdatad/UserInterfaceState.xcuserstate b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.xcworkspace/xcuserdata/imanol.xcuserdatad/UserInterfaceState.xcuserstate index 345a2b6..5d646b0 100644 Binary files a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.xcworkspace/xcuserdata/imanol.xcuserdatad/UserInterfaceState.xcuserstate and b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/project.xcworkspace/xcuserdata/imanol.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index fe2b454..5d71db4 100644 --- a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -2,4 +2,22 @@ + + + + + + diff --git a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcschemes/DUREX Vendor Control.xcscheme b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcschemes/DUREX Vendor Control.xcscheme index 8fcb9b1..4fdaf38 100644 --- a/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcschemes/DUREX Vendor Control.xcscheme +++ b/DUREX Vendor Control/DUREX Vendor Control.xcodeproj/xcuserdata/imanol.xcuserdatad/xcschemes/DUREX Vendor Control.xcscheme @@ -15,7 +15,7 @@ @@ -33,7 +33,7 @@ @@ -43,7 +43,7 @@ @@ -62,7 +62,7 @@ @@ -80,7 +80,7 @@ diff --git a/DUREX Vendor Control/FirstAppExample/DUREXAppDelegate.h b/DUREX Vendor Control/FirstAppExample/DUREXAppDelegate.h index 03a75af..dfdd593 100644 --- a/DUREX Vendor Control/FirstAppExample/DUREXAppDelegate.h +++ b/DUREX Vendor Control/FirstAppExample/DUREXAppDelegate.h @@ -13,4 +13,6 @@ @property (strong, nonatomic) UIWindow *window; @property (nonatomic, strong) IBOutlet UINavigationController *navController; + + @end diff --git a/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.h b/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.h index 2d1c0f7..a729ea2 100644 --- a/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.h +++ b/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.h @@ -1,9 +1,7 @@ #import -@interface DUREXAppViewController : UIViewController -@property (nonatomic, strong) IBOutlet UILabel *valueLabel; -@property (nonatomic, strong) IBOutlet UISlider *valueSlider; +@interface DUREXAppViewController : UITableViewController diff --git a/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.m b/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.m index c8d1f7e..05c3af0 100644 --- a/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.m +++ b/DUREX Vendor Control/FirstAppExample/DUREXAppViewController.m @@ -1,11 +1,13 @@ #import "DUREXAppViewController.h" #import "EMFramework.h" +#import "CommunicationProtocol.h" @interface DUREXAppViewController () { IBOutlet UIActivityIndicatorView *_activityIndicator; NSTimer *_writeTimer; } +@property (nonatomic,strong) CommunicationProtocol* protocol; @end @@ -25,33 +27,35 @@ { [super viewDidLoad]; [_activityIndicator setHidden:YES]; - [self setTitle:[[[EMConnectionManager sharedManager] connectedDevice] name]]; - [[EMConnectionManager sharedManager] readResource:@"data" onSuccess:^(id readValue) - { - [[self valueLabel] setText:[NSString stringWithFormat:@"%d", [readValue intValue]]]; - [[self valueSlider] setValue:[readValue floatValue]]; - } - onFail:^(NSError *error) - { - - } - ]; - + [self setTitle:@"DUREX Vendor Control"]; + _protocol = [CommunicationProtocol sharedProtocol]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kEMConnectionDidReceiveIndicatorNotificationName object:nil]; - [[EMConnectionManager sharedManager] addObserver:self forKeyPath:@"connectionState" options:0 context:NULL]; + [_protocol setMessageAvailable:false]; } -(void)didReceiveNotification:(NSNotification*) notification { id notificationValue = [[notification userInfo] objectForKey:kEMIndicatorResourceValueKey]; NSString *resourceName = [[notification userInfo] objectForKey:kEMIndicatorNameKey]; - NSString *resourceValue = [notificationValue stringValue]; + if([resourceName isEqualToString:@"messageAvailable"]) + { + NSString *resourceValue = [notificationValue stringValue]; + if([resourceValue isEqualToString:@"TRUE"]) + { + [_protocol setMessageAvailable:TRUE]; + } + else + { + [_protocol setMessageAvailable:FALSE]; + } + } } -(void)dealloc { [[EMConnectionManager sharedManager] removeObserver:self forKeyPath:@"connectionState"]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context @@ -68,66 +72,6 @@ } } --(NSString*) readMessage -{ - -} - --(Boolean) writeMessage: (NSString*) message -{ - __block Boolean status = FALSE; - [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithFloat:[[self valueSlider] value]] toResource:@"data" onSuccess:^ - { - status = TRUE; - } - onFail:^(NSError *error) - { - NSLog(@"%@",error); - status = FALSE; - } - ]; - if(status != FALSE) - { - [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithFloat:[[self valueSlider] value]] toResource:@"data" onSuccess:^ - { - status = TRUE; - } - onFail:^(NSError *error) - { - NSLog(@"%@",error); - status = FALSE; - } - ]; - if(status != FALSE) - { - [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithFloat:[[self valueSlider] value]] toResource:@"data" onSuccess:^ - { - status = TRUE; - } - onFail:^(NSError *error) - { - NSLog(@"%@",error); - status = FALSE; - } - ]; - if(status != FALSE) - { - [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithFloat:[[self valueSlider] value]] toResource:@"data" onSuccess:^ - { - status = TRUE; - } - onFail:^(NSError *error) - { - NSLog(@"%@",error); - status = FALSE; - } - ]; - } - } - } - return status; -} - #pragma mark - Interface actions diff --git a/DUREX Vendor Control/FirstAppExample/MainStoryboard.storyboard b/DUREX Vendor Control/FirstAppExample/MainStoryboard.storyboard index 4117304..f1f4917 100644 --- a/DUREX Vendor Control/FirstAppExample/MainStoryboard.storyboard +++ b/DUREX Vendor Control/FirstAppExample/MainStoryboard.storyboard @@ -4,7 +4,7 @@ - + @@ -43,42 +43,53 @@ - + - + - + - - + + - - - - - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -88,14 +99,59 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/DUREX Vendor Control/InitialViewController.h b/DUREX Vendor Control/InitialViewController.h new file mode 100644 index 0000000..2078d07 --- /dev/null +++ b/DUREX Vendor Control/InitialViewController.h @@ -0,0 +1,17 @@ +// +// InitialViewController.h +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/22/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import + +@interface InitialViewController : UIViewController + +@property (nonatomic,strong) IBOutlet UIButton *connectButton; + +- (IBAction) connectPressed:(UIButton*)button; + +@end diff --git a/DUREX Vendor Control/InitialViewController.m b/DUREX Vendor Control/InitialViewController.m new file mode 100644 index 0000000..eb24e0a --- /dev/null +++ b/DUREX Vendor Control/InitialViewController.m @@ -0,0 +1,54 @@ +// +// InitialViewController.m +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/22/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import "InitialViewController.h" + +@interface InitialViewController () + +@end + +@implementation InitialViewController + +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +{ + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; + if (self) { + // Custom initialization + } + return self; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + // Do any additional setup after loading the view. +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +- (IBAction) connectPressed:(UIButton*)button +{ + [self performSegueWithIdentifier:@"ShowDevices" sender:self]; +} + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/DUREX Vendor Control/MenuTableViewController.h b/DUREX Vendor Control/MenuTableViewController.h new file mode 100644 index 0000000..462d419 --- /dev/null +++ b/DUREX Vendor Control/MenuTableViewController.h @@ -0,0 +1,40 @@ +// +// MenuTableViewController.h +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/23/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import +#import "EMFramework.h" +#import "CommunicationProtocol.h" + +#define num(x) [NSNumber numberWithUnsignedInt:x] + +#define MENU_ELEMENTS @"Maintenance",@"Basic Configuration",@"Sending a Report" +#define MENU_CELLS_PER_SECTION num(3) +#define MENU_SECTIONS 1 + +#define MAINTENANCE_ELEMENTS @"General Status",@"Sensor Report" +#define MAINTENANCE_CELLS_PER_SECTION num(2) +#define MAINTENANCE_SECTIONS 1 + +#define STATUS_ELEMENTS @"Time",@"20€ Notes",@"10€ Notes",@"5€ Notes",@"2€ Coins",@"1€ Coins",@"50c Coins",@"CPP_TABLE",@"1€ Change",@"50c Change" +#define STATUS_CELLS_PER_SECTION num(10) +#define STATUS_SECTIONS 1 + +#define CELL_IDENTIFIERS @"MenuCell",@"MaintenanceCell",@"ConfigCell",@"ReportCell" + +#define NUM_CHANNELS 8 + +enum { + MENU = 0, + MAINTENANCE, + BASIC_CONFIGURATION, + REPORT +} navigationLevel; + +@interface MenuTableViewController : UITableViewController + +@end diff --git a/DUREX Vendor Control/MenuTableViewController.m b/DUREX Vendor Control/MenuTableViewController.m new file mode 100644 index 0000000..b3d1271 --- /dev/null +++ b/DUREX Vendor Control/MenuTableViewController.m @@ -0,0 +1,236 @@ +// +// MenuTableViewController.m +// DUREX Vendor Control +// +// Created by Imanol Barba on 5/23/14. +// Copyright (c) 2014 Emmoco. All rights reserved. +// + +#import "MenuTableViewController.h" + +@interface MenuTableViewController () +{ + IBOutlet UIActivityIndicatorView *_activityIndicator; + NSTimer *_writeTimer; +} +@property (nonatomic,strong) const NSArray *menuElements; +@property (nonatomic,strong) const NSArray *menuStructure; + +@property (nonatomic,strong) const NSArray *maintenanceElements; +@property (nonatomic,strong) const NSArray *maintenanceStructure; + +@property (nonatomic,strong) const NSArray *cellIdentifiers; + +@property (nonatomic,strong) CommunicationProtocol* protocol; +@property uint8_t currentNavLevel; +@property NSString *currentCellIdentifier; +@property const NSArray *currentElements; +@property const NSArray *currentStructure; + +@end + +@implementation MenuTableViewController + +- (id)initWithStyle:(UITableViewStyle)style +{ + self = [super initWithStyle:style]; + if (self) + { + // Custom initialization + } + return self; +} + +- (void) initializeMenuEntries +{ + [self setMenuElements:[[NSArray alloc] initWithObjects:MENU_ELEMENTS, nil]]; + [self setMenuStructure:[[NSArray alloc] initWithObjects:MENU_CELLS_PER_SECTION, nil]]; + + [self setMaintenanceElements:[[NSArray alloc] initWithObjects:MAINTENANCE_ELEMENTS, nil]]; + [self setMaintenanceStructure:[[NSArray alloc] initWithObjects:MAINTENANCE_CELLS_PER_SECTION, nil]]; + + [self setCellIdentifiers:[[NSArray alloc] initWithObjects:CELL_IDENTIFIERS, nil]]; +} + +- (void) changeNavLevel: (uint8_t) level +{ + [self setCurrentNavLevel:level]; + [self setCurrentCellIdentifier:[[self cellIdentifiers] objectAtIndex:[self currentNavLevel]]]; + if([self currentNavLevel] == MAINTENANCE) + { + [self setCurrentElements:[self maintenanceElements]]; + [self setCurrentStructure:[self maintenanceStructure]]; + } + //AND SO ON... + else + { + [self setCurrentElements:[self menuElements]]; + [self setCurrentStructure:[self menuStructure]]; + } +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + [_activityIndicator setHidden:YES]; + + //Set title + NSMutableString *title; + [title setString:[[self navigationItem] title]] ; + [title appendString:[[[EMConnectionManager sharedManager] connectedDevice] name]]; + [[self navigationItem] setTitle:title]; + + //Emmoco protocol initialization + _protocol = [CommunicationProtocol sharedProtocol]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kEMConnectionDidReceiveIndicatorNotificationName object:nil]; + [[EMConnectionManager sharedManager] addObserver:self forKeyPath:@"connectionState" options:0 context:NULL]; + + //DUREX protocol initialization + [_protocol setMessageAvailable:false]; + if(![_protocol establishConnection]) + { + UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Communication error" message:@"Error while trying to connect to the device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; + [alert show]; + UIViewController *previous = [[[self navigationController] viewControllers] objectAtIndex:[[[self navigationController] viewControllers] count]-2]; + [[self navigationController] popToViewController:previous animated:YES]; + } + + //Set navigation level + [self initializeMenuEntries]; + [self changeNavLevel:MENU]; + + // Uncomment the following line to preserve selection between presentations. + // self.clearsSelectionOnViewWillAppear = NO; + + // Uncomment the following line to display an Edit button in the navigation bar for this view controller. + // self.navigationItem.rightBarButtonItem = self.editButtonItem; +} + +-(void)didReceiveNotification:(NSNotification*) notification +{ + id notificationValue = [[notification userInfo] objectForKey:kEMIndicatorResourceValueKey]; + NSString *resourceName = [[notification userInfo] objectForKey:kEMIndicatorNameKey]; + if([resourceName isEqualToString:@"messageAvailable"]) + { + NSString *resourceValue = [notificationValue stringValue]; + if([resourceValue isEqualToString:@"TRUE"]) + { + [_protocol setMessageAvailable:TRUE]; + } + else + { + [_protocol setMessageAvailable:FALSE]; + } + } +} + +-(void)dealloc +{ + [[EMConnectionManager sharedManager] removeObserver:self forKeyPath:@"connectionState"]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if (object == [EMConnectionManager sharedManager]) + { + if ([keyPath isEqualToString:@"connectionState"]) + { + if ([[EMConnectionManager sharedManager] connectionState] == EMConnectionStateDisrupted) + { + [[self navigationController] popToRootViewControllerAnimated:YES]; + } + } + } +} + +#pragma mark - Table view delegate + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + [tableView deselectRowAtIndexPath:indexPath animated:YES]; + NSLog(@"%d:%s",(int)[indexPath row],[[[[[self tableView] cellForRowAtIndexPath:indexPath]textLabel]text]cStringUsingEncoding:NSUTF8StringEncoding]); +} + +#pragma mark - Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return [[self currentStructure] count]; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + return [[[self currentStructure]objectAtIndex:section] integerValue]; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self currentCellIdentifier] forIndexPath:indexPath]; + + if (cell == nil) + { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self currentCellIdentifier]]; + } + + [[cell textLabel] setText:[[self currentElements] objectAtIndex:indexPath.row]]; + return cell; +} + + +/* +// Override to support conditional editing of the table view. +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + // Return NO if you do not want the specified item to be editable. + return YES; +} +*/ + +/* +// Override to support editing the table view. +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (editingStyle == UITableViewCellEditingStyleDelete) { + // Delete the row from the data source + [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; + } else if (editingStyle == UITableViewCellEditingStyleInsert) { + // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view + } +} +*/ + +/* +// Override to support rearranging the table view. +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath +{ +} +*/ + +/* +// Override to support conditional rearranging of the table view. +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath +{ + // Return NO if you do not want the item to be re-orderable. + return YES; +} +*/ + +/* +#pragma mark - Navigation + +// In a storyboard-based application, you will often want to do a little preparation before navigation +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. +} +*/ + +@end diff --git a/DUREX Vendor Control/bluetooth.png b/DUREX Vendor Control/bluetooth.png new file mode 100644 index 0000000..7befaba Binary files /dev/null and b/DUREX Vendor Control/bluetooth.png differ -- libgit2 0.22.2