Last active
August 29, 2015 14:17
-
-
Save JRHeaton/1b29e79d4b5e761dc796 to your computer and use it in GitHub Desktop.
hue in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| typealias LightState = [String:NSObject] | |
| enum LightCommand { | |
| enum LightEffect: String { | |
| case None = "none" | |
| case ColorLoop = "colorloop" | |
| } | |
| enum LightAlert: String { | |
| case None = "none" | |
| case Single = "select" | |
| case Cycle30 = "lselect" | |
| } | |
| case Hue(UInt16) | |
| case Brightness(UInt8) | |
| case Saturation(UInt8) | |
| case Off | |
| case On | |
| case Effect(LightEffect) | |
| case Alert(LightAlert) | |
| case Custom(LightState) | |
| case TransitionTime(milliseconds: Int) | |
| case CIECoordinate(x: Float, y: Float) | |
| case Temperature(kelvin: UInt16) | |
| var state: LightState { | |
| switch self { | |
| case .Hue(let val): | |
| return ["hue":Int(val)] | |
| case .Brightness(let val): | |
| return ["bri":Int(val)] | |
| case .Saturation(let val): | |
| return ["sat":Int(val)] | |
| case .Off: | |
| return ["on":false] | |
| case .On: | |
| return ["on":true] | |
| case .Effect(let effect): | |
| return ["effect":effect.rawValue] | |
| case .Alert(let alert): | |
| return ["alert":alert.rawValue] | |
| case .Custom(let state): | |
| return state | |
| case .TransitionTime(let ms): | |
| return ["transitiontime": ms / 100] | |
| case .CIECoordinate(let x, let y): | |
| return ["xy":[x, y]] | |
| case .Temperature(let kelvin): | |
| return ["ct":Int(kelvin)] | |
| } | |
| } | |
| } | |
| struct Client { | |
| let bridgeIP: String | |
| let username: String | |
| static let Joan = Client(bridgeIP: "10.0.1.27", username: "joanlikeslights") | |
| func send(#method: String, path: String, body: LightState) { | |
| let request = NSMutableURLRequest(URL: NSURL(string: "http://\(bridgeIP)/api/\(username)/\(path)")!) | |
| request.HTTPMethod = method | |
| request.HTTPBody = NSJSONSerialization.dataWithJSONObject(body, options: nil, error: nil) | |
| NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { _ in () } | |
| } | |
| } | |
| struct Light { | |
| enum Location: Int { | |
| case TV = 1 | |
| case Window = 2 | |
| } | |
| let location: Location | |
| let client: Client = .Joan | |
| } | |
| func applyLightState(#client: Client)(identifier: Int)(_ state: LightState) { | |
| client.send(method: "PUT", path: "lights/\(identifier)/state", body: state) | |
| } | |
| infix operator •>> { associativity right } | |
| func •>> (left: Light, right: LightCommand) { | |
| applyLightState(client: left.client)(identifier: left.location.rawValue)(right.state) | |
| } | |
| func •>> (left: [Light], right: LightCommand) { | |
| map(left) { applyLightState(client: $0.client)(identifier: $0.location.rawValue)(right.state) } | |
| } | |
| func •>> (left: Light.Location, right: LightCommand) { | |
| applyLightState(client: .Joan)(identifier: left.rawValue)(right.state) | |
| } | |
| func •>> (left: [Light.Location], right: LightCommand) { | |
| map(left) { applyLightState(client: .Joan)(identifier: $0.rawValue)(right.state) } | |
| } | |
| func •>> (left: LightCommand, right: LightCommand) -> LightCommand { | |
| return .Custom(merge(left.state, right.state)) | |
| } | |
| func merge<K, V>(first: [K:V], second: [K:V]) -> [K:V] { | |
| var copy = second | |
| for (key, value) in first { | |
| copy[key] = value | |
| } | |
| return copy | |
| } | |
| // Now change the light | |
| [.TV, .Window] | |
| •>> .Hue(0) | |
| •>> .Saturation(0) | |
| •>> .Brightness(255) | |
| •>> .Effect(.None) | |
| CFRunLoopRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment