72 lines
1.3 KiB
Swift
72 lines
1.3 KiB
Swift
|
|
//
|
||
|
|
// DeviceProtocol.swift
|
||
|
|
// XIONControlPanel
|
||
|
|
//
|
||
|
|
// Created by James Magahern on 3/13/20.
|
||
|
|
// Copyright © 2020 XION. All rights reserved.
|
||
|
|
//
|
||
|
|
|
||
|
|
import Foundation
|
||
|
|
|
||
|
|
enum DeviceState
|
||
|
|
{
|
||
|
|
case off
|
||
|
|
case on
|
||
|
|
}
|
||
|
|
|
||
|
|
enum DeviceType
|
||
|
|
{
|
||
|
|
case `switch`
|
||
|
|
}
|
||
|
|
|
||
|
|
protocol Device
|
||
|
|
{
|
||
|
|
// Read-only properties
|
||
|
|
var name: String { get }
|
||
|
|
var serial: String { get }
|
||
|
|
var type: DeviceType { get }
|
||
|
|
|
||
|
|
// Writable properties
|
||
|
|
var state: DeviceState { get set }
|
||
|
|
}
|
||
|
|
|
||
|
|
extension Device where Self: Hashable
|
||
|
|
{
|
||
|
|
func hash(into hasher: inout Hasher)
|
||
|
|
{
|
||
|
|
hasher.combine(self.name)
|
||
|
|
hasher.combine(self.type)
|
||
|
|
hasher.combine(self.serial)
|
||
|
|
}
|
||
|
|
|
||
|
|
static func == (lhs: Self, rhs: Self) -> Bool
|
||
|
|
{
|
||
|
|
return lhs.name == rhs.name && lhs.serial == rhs.serial
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class AnyDevice : Device
|
||
|
|
{
|
||
|
|
private var device: Device
|
||
|
|
|
||
|
|
public var name: String
|
||
|
|
{ get { return device.name } }
|
||
|
|
|
||
|
|
public var serial: String
|
||
|
|
{ get { return device.serial } }
|
||
|
|
|
||
|
|
public var type: DeviceType
|
||
|
|
{ get { return device.type } }
|
||
|
|
|
||
|
|
public var state: DeviceState {
|
||
|
|
get { return device.state }
|
||
|
|
set { device.state = newValue }
|
||
|
|
}
|
||
|
|
|
||
|
|
init(_ device: Device) {
|
||
|
|
self.device = device
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
extension AnyDevice : Hashable {}
|