Skip to content

Commit

Permalink
got the picture saving to Mobile Couchbase
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed May 14, 2011
1 parent 2102f2f commit 0decf38
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 54 deletions.
12 changes: 12 additions & 0 deletions OnTheSpot.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
79026EEB137EFD1D00939183 /* CJSONDeserializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 79026EEA137EFD1D00939183 /* CJSONDeserializer.h */; };
79026EEE137EFD5A00939183 /* CDataScanner.h in Headers */ = {isa = PBXBuildFile; fileRef = 79026EEC137EFD5A00939183 /* CDataScanner.h */; };
79026EEF137EFD5A00939183 /* CJSONDeserializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 79026EED137EFD5A00939183 /* CJSONDeserializer.h */; };
7929D9CB137B55A300D3185B /* Couchbase.h in Headers */ = {isa = PBXBuildFile; fileRef = 7929D9C9137B558400D3185B /* Couchbase.h */; };
7929D9D2137B572400D3185B /* libCouchbase-iphoneos.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7929D9D0137B572400D3185B /* libCouchbase-iphoneos.a */; };
7929D9D3137B572400D3185B /* libCouchbase-iphonesimulator.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7929D9D1137B572400D3185B /* libCouchbase-iphonesimulator.a */; };
Expand Down Expand Up @@ -48,6 +51,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
79026EEA137EFD1D00939183 /* CJSONDeserializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CJSONDeserializer.h; path = ../Vendor/TouchJSON/Source/JSON/CJSONDeserializer.h; sourceTree = "<group>"; };
79026EEC137EFD5A00939183 /* CDataScanner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDataScanner.h; path = ../Vendor/TouchJSON/Source/CDataScanner.h; sourceTree = "<group>"; };
79026EED137EFD5A00939183 /* CJSONDeserializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CJSONDeserializer.h; path = ../Vendor/TouchJSON/Source/JSON/CJSONDeserializer.h; sourceTree = "<group>"; };
7929D9C9137B558400D3185B /* Couchbase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Couchbase.h; sourceTree = "<group>"; };
7929D9D0137B572400D3185B /* libCouchbase-iphoneos.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = "libCouchbase-iphoneos.a"; sourceTree = "<group>"; };
7929D9D1137B572400D3185B /* libCouchbase-iphonesimulator.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = "libCouchbase-iphonesimulator.a"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -139,6 +145,9 @@
isa = PBXGroup;
children = (
BCC6E87E136034BA00BBA835 /* CJSONSerializer.h */,
79026EEC137EFD5A00939183 /* CDataScanner.h */,
79026EED137EFD5A00939183 /* CJSONDeserializer.h */,
79026EEA137EFD1D00939183 /* CJSONDeserializer.h */,
7929D9C9137B558400D3185B /* Couchbase.h */,
BCB6429D135D7F5000DA6256 /* OnTheSpotAppDelegate.h */,
BCB6429E135D7F5000DA6256 /* OnTheSpotAppDelegate.m */,
Expand Down Expand Up @@ -190,6 +199,9 @@
buildActionMask = 2147483647;
files = (
7929D9CB137B55A300D3185B /* Couchbase.h in Headers */,
79026EEB137EFD1D00939183 /* CJSONDeserializer.h in Headers */,
79026EEE137EFD5A00939183 /* CDataScanner.h in Headers */,
79026EEF137EFD5A00939183 /* CJSONDeserializer.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 0 additions & 2 deletions OnTheSpot/OnTheSpotAppDelegate.h
Expand Up @@ -7,7 +7,6 @@
//

#import <UIKit/UIKit.h>
#import "Couchbase.h"

@interface OnTheSpotAppDelegate : NSObject <UIApplicationDelegate> {

Expand All @@ -17,7 +16,6 @@

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) NSURL *couchURL;


@end
9 changes: 1 addition & 8 deletions OnTheSpot/OnTheSpotAppDelegate.m
Expand Up @@ -16,21 +16,15 @@ @implementation OnTheSpotAppDelegate

@synthesize navigationController=_navigationController;

@synthesize couchURL=_couchURL;

- (void)couchbaseDidStart:(NSURL *)serverURL {
self.couchURL = serverURL;
// self.navigationController.couchURL = serverURL;
NSLog(@"Couch is ready!");
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
[Couchbase startCouchbase:self.window.rootViewController];
return YES;
}

Expand Down Expand Up @@ -77,7 +71,6 @@ - (void)dealloc
{
[_window release];
[_navigationController release];
[_couchURL release];
[super dealloc];
}

Expand Down
7 changes: 5 additions & 2 deletions OnTheSpot/RootViewController.h
Expand Up @@ -9,11 +9,14 @@
#import <UIKit/UIKit.h>
#import <CoreMotion/CMMotionManager.h>
#import <CoreLocation/CoreLocation.h>
#import "Couchbase.h"


@interface RootViewController : UITableViewController
< UINavigationControllerDelegate
, UIImagePickerControllerDelegate
, CLLocationManagerDelegate
, CouchbaseDelegate
> {

@private
Expand All @@ -24,9 +27,9 @@
NSMutableArray* _locationSamples;
NSMutableArray* _headingSamples;
NSTimer* _sampleTimer;
NSURL *_couchURL;
NSURL *_dbURL;
}
@property (nonatomic, retain) NSURL *couchURL;
@property (nonatomic, retain) NSURL *dbURL;

- (IBAction)takePicture;

Expand Down
104 changes: 62 additions & 42 deletions OnTheSpot/RootViewController.m
Expand Up @@ -3,12 +3,14 @@
// OnTheSpot
//
// Created by afh on 19/04/11.
// Couchbase and JSON work 14/05/11 Chris Anderson
// Copyright 2011 Alexis Hildebrandt. All rights reserved.
//

#import "RootViewController.h"
#import "DetailViewController.h"
#import "CJSONSerializer.h"
#import "CJSONDeserializer.h"

@interface RootViewController()
@property (nonatomic, retain) NSMutableArray* images;
Expand All @@ -24,7 +26,7 @@ @implementation RootViewController

@synthesize sampleTimer = _sampleTimer;
@synthesize images = _images;
@synthesize couchURL=_couchURL;
@synthesize dbURL=_dbURL;

- (id)initWithCoder:(NSCoder *)aDecoder
{
Expand All @@ -35,14 +37,14 @@ - (id)initWithCoder:(NSCoder *)aDecoder
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.purpose = NSLocalizedString(@"Location services are used to attach the location of where a photo was taken.", nil);
[Couchbase startCouchbase:self];
}
return self;
}

- (void)couchbaseDidStart:(NSURL *)serverURL {
self.couchURL = serverURL;
// self.navigationController.couchURL = serverURL;
NSLog(@"Couch is woo ha!");
self.dbURL = [serverURL URLByAppendingPathComponent:@"spot"];
NSLog(@"Couch is ready!");
}

- (void)viewDidLoad
Expand Down Expand Up @@ -190,6 +192,7 @@ - (void)dealloc
{
[_motionManager release];
[_locationManager release];
[_dbURL release];
[super dealloc];
}

Expand Down Expand Up @@ -242,7 +245,7 @@ - (void)locationManager:(CLLocationManager *)manager
, [NSNumber numberWithDouble:newLocation.course], @"course"
, [NSNumber numberWithDouble:newLocation.speed], @"speed"
, [NSNumber numberWithDouble:newLocation.horizontalAccuracy], @"horizontalAccuracy"
, [NSNumber numberWithDouble:newLocation.verticalAccuracy], @"verticalAccuracy`"
, [NSNumber numberWithDouble:newLocation.verticalAccuracy], @"verticalAccuracy"
, nil];
[_locationSamples addObject:sample];

Expand Down Expand Up @@ -307,24 +310,20 @@ - (void)storeImage:(UIImage*)anImage withMetaData:(NSDictionary*)metaData
break;
}
}
// NSString* dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZ";
// NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter ]

NSMutableDictionary* jsonData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
// anImage, @"imageData" // will be stored as an attachment to the document in CouchDB
metaData, @"mediaMetaData"
, motionData, @"motionData"
, locationData, @"locationData"
, headingData, @"headingData"
// , [NSDate date], @"timestamp"
, [[NSDate date] description], @"timestamp"
, nil];
[_images addObject:jsonData];
[self.tableView reloadData];

[dateFormatter release];

// store image in CouchDB
// Test the various JSON transforms
NSError* error = NULL;
NSData* theJSON = [[CJSONSerializer serializer] serializeDictionary:jsonData error:&error];
if (error != NULL) {
Expand All @@ -333,31 +332,46 @@ - (void)storeImage:(UIImage*)anImage withMetaData:(NSDictionary*)metaData
else {
NSString *jsonString = [[NSString alloc] initWithData:theJSON encoding:NSUTF8StringEncoding];
NSLog(@"Saving JSON %@", jsonString);
NSLog(@"Couch URL %@", self.dbURL);
NSNumber *contentLength = [NSNumber numberWithUnsignedInt: [theJSON length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dbURL];
assert(request != nil);

// create the Database, just in case
[request setHTTPMethod: @"PUT"];
NSData *createDB = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSLog(@"createDB %@", [[NSString alloc] initWithData:createDB encoding:NSUTF8StringEncoding]);

// create the Document
[request setHTTPMethod: @"POST"];
[request setHTTPBody: theJSON];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[contentLength description] forHTTPHeaderField:@"Content-Length"];

NSData *createDoc = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSLog(@"createDoc %@", [[NSString alloc] initWithData:createDoc encoding:NSUTF8StringEncoding]);

NSData* parsedJSON = [[CJSONDeserializer deserializer] deserialize:createDoc error:&error];

// requestWithURL
// upload the photo
NSData *photo = UIImageJPEGRepresentation(anImage, 0.75);
contentLength = [NSNumber numberWithUnsignedInt: [photo length]];

NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@?rev=%@",
[self.dbURL absoluteString],
[parsedJSON valueForKey:@"id"],
@"photo.jpg",
[parsedJSON valueForKey:@"rev"]];

//
// CouchDBSuccessHandler inSuccessHandler = ^(id inParameter) {
// NSLog(@"Wooohooo! %@", inParameter);
//// [delegate performSelector:@selector(newItemAdded)];
// };
//
// CouchDBFailureHandler inFailureHandler = ^(NSError *error) {
// NSLog(@"D'OH! %@", error);
// };
// CFUUIDRef uuid = CFUUIDCreate(nil);
// NSString *guid = (NSString*)CFUUIDCreateString(nil, uuid);
// CFRelease(uuid);
// NSString *docId = [NSString stringWithFormat:@"%f-%@", CFAbsoluteTimeGetCurrent(), guid];
//
// DatabaseManager *sharedManager = [DatabaseManager sharedManager:[delegate getCouchbaseURL]];
// CURLOperation *op = [sharedManager.database operationToCreateDocument:inDocument
// identifier:docId
// successHandler:inSuccessHandler
// failureHandler:inFailureHandler];
// [op start];
// }
NSURL *imagePUTURL = [NSURL URLWithString:urlString];
request = [NSMutableURLRequest requestWithURL:imagePUTURL];
[request setHTTPMethod: @"PUT"];
[request setHTTPBody: photo];
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[request setValue:[contentLength description] forHTTPHeaderField:@"Content-Length"];
NSData *putPhoto = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSLog(@"putPhoto %@", [[NSString alloc] initWithData:putPhoto encoding:NSUTF8StringEncoding]);
}
}

Expand Down Expand Up @@ -434,15 +448,21 @@ - (void)takeMotionSample:(NSTimer*)aTimer
NSMutableDictionary* sample =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSDate date], @"timestamp"
, [NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.x], @"gyro-x"
, [NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.y], @"gyro-y"
, [NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.z], @"gyro-z"
, [NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.roll], @"attitude-roll"
, [NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.pitch], @"attitude-pitch"
, [NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.yaw], @"attitude-yaw"
, [NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.x], @"accel-x"
, [NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.y], @"accel-y"
, [NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.z], @"accel-z"
, [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.x], @"x"
, [NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.y], @"y"
, [NSNumber numberWithDouble:_motionManager.gyroData.rotationRate.z], @"z"
,nil ], @"gyro"
, [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.roll], @"roll"
, [NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.pitch], @"pitch"
, [NSNumber numberWithDouble:_motionManager.deviceMotion.attitude.yaw], @"yaw"
, nil], @"attitude"
, [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.x], @"x"
, [NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.y], @"y"
, [NSNumber numberWithDouble:_motionManager.accelerometerData.acceleration.z], @"z"
, nil], @"accel"
, nil];
[_motionSamples addObject:sample];
}
Expand Down

0 comments on commit 0decf38

Please sign in to comment.