Skip to content

Instantly share code, notes, and snippets.

@Qata
Created April 7, 2016 08:56
Show Gist options
  • Save Qata/4ee8215805134a33893154843b879276 to your computer and use it in GitHub Desktop.
Save Qata/4ee8215805134a33893154843b879276 to your computer and use it in GitHub Desktop.
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