Created
April 7, 2016 08:56
-
-
Save Qata/4ee8215805134a33893154843b879276 to your computer and use it in GitHub Desktop.
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
extension Device: Decodable { | |
static func decode(json: AnyObject) throws -> Device { | |
guard let resolvedTypes = try json => "devices" as? NSArray else { | |
throw NSArraySucks.ItReallySucks | |
} | |
let types = try (resolvedTypes as [AnyObject]).map { | |
try decodeAsOneOf($0, objectTypes: MSensorType.self, ColourType.self, LEDType.self, GenericType.self) | |
}.flatMap { | |
$0 as? DeviceType | |
} | |
return try Device(name: json => "name", | |
number: json => "number", | |
exists: (json => "exists") != 0, | |
ready: (json => "ready") != 0, | |
groups: json => "groups", | |
scenes: scenesToSensicalScenes(json => "scenes"), | |
maxLevel: json => "maxlevel", | |
minLevel: json => "minlevel", | |
physicalMinLevel: json => "physicalminlevel", | |
powerOnLevel: json => "poweronlevel", | |
systemFailureLevel: json => "systemfailurelevel", | |
level: json => "level", | |
lampfail: (json => "lampfail") != 0, | |
limitError: (json => "limiterror") != 0, | |
actuatorType: json => "actuatortype", | |
busErrors: json => "buserrors", | |
multiAddressedProbability: json => "doubleaddressedprobability", | |
types: types) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment