Skip to content

Instantly share code, notes, and snippets.

@sneakyness
Created November 29, 2012 09:06
Show Gist options
  • Save sneakyness/4167722 to your computer and use it in GitHub Desktop.
Save sneakyness/4167722 to your computer and use it in GitHub Desktop.
I'm sure sending an HTTP request every time the slider moves will be just fine…
-(IBAction)sliderValueChanged:(id)sender {
UISlider *currentSlider = (UISlider*)sender;
NSString *initalJSONRequest = [NSString stringWithFormat:@"{\"on\":\"true\""];
NSString *jsonRequest;
if (currentSlider == self.brightnessSlider) {
self.brightnessValueLabel.text = [NSString stringWithFormat:@"%f", currentSlider.value];
jsonRequest = [initalJSONRequest stringByAppendingFormat:@",\"bri\":%.0f}", currentSlider.value];
}
else if (currentSlider == self.hueSlider) {
self.hueValueLabel.text = [NSString stringWithFormat:@"%f", currentSlider.value];
jsonRequest = [initalJSONRequest stringByAppendingFormat:@",\"hue\":%.0f}", currentSlider.value];
}
else if (currentSlider == self.saturationSlider) {
self.saturationValueLabel.text = [NSString stringWithFormat:@"%f", currentSlider.value];
jsonRequest = [initalJSONRequest stringByAppendingFormat:@",\"sat\":%.0f}", currentSlider.value];
}
else if (currentSlider == self.xSlider || currentSlider == self.ySlider) {
self.xValueLabel.text = [NSString stringWithFormat:@"%f", currentSlider.value];
jsonRequest = [initalJSONRequest stringByAppendingFormat:@",\"hue\":%.0f}", currentSlider.value];
}
else if (currentSlider == self.ctSlider) {
self.ctValueLabel.text = [NSString stringWithFormat:@"%f", currentSlider.value];
jsonRequest = [initalJSONRequest stringByAppendingFormat:@",\"ct\":%.0f}", currentSlider.value];
}
NSURL *url = [[self urlForLightbulb:self.lightbulbSelectionControl.selectedSegmentIndex+1] URLByAppendingPathComponent:@"state"];
NSLog(@"%@", url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSLog(@"%@", jsonRequest);
[request setHTTPMethod:@"PUT"];
[request setHTTPBody: requestData];
[NSURLConnection connectionWithRequest:request delegate:self];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment