From 3e2e09213b7a8a029e41b41755208769de658a67 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 11:08:53 +0200 Subject: [PATCH 01/20] added a total of 6 methods for controlling LEDs, using #1 documentation --- Sources/YeeLampa/YeeLampa.swift | 86 +++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeeLampa/YeeLampa.swift index bbc7130..d2dc037 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeeLampa/YeeLampa.swift @@ -70,10 +70,88 @@ public class YeeLampa { } } - public func setPower(to on: Bool, for device: Device) async throws { - try await self.sendDeviceRequest( - url: URL(string: "https://\(self.region.urlFormat).openapp.io.mi.com/openapp/device/rpc/\(device.deviceId)")!, - arguments: ["method": "set_power", "params": [on ? "on" : "off"]] + /// Set a color temperature for a device that supports it. + /// - Parameters: + /// - temp: A target temperature. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setColorTemperature(to temp: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_ct_abx", params: [temp, effect.rawValue, duration]) + } + + /// Sets power state for a device. + /// - Parameters: + /// - on: The power state you want your device to change to. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setPower(to on: Bool, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration]) + } + + /// Toggles an LED. + /// - Parameter device: A target device. + public func togglePower(ofDevice device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "toggle", params: []) + } + + /// Sets an RGB color. + /// - Parameters: + /// - color: A RGB color you want to set. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setRgbColor( + of color: ( + red: Int, + green: Int, + blue: Int + ), + withEffect effect: ChangeEffect = .smooth, + withDuration duration: Int = 500, + forDevice device: Device + ) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_rgb", params: [ + ((color.red << 16) + (color.green << 8) + color.blue), + effect.rawValue, + duration] + ) + } + + /// Sets an HSV color. + /// - Parameters: + /// - color: A color you want to set. `hue`'s bounds are 0 to 359(yea, it's strange, but this is how the API works), `saturation` is from 0 to 100, and `brightness` is from 0 to 100. Note that if a `brightness` value is provided, then there will be 2 requests, one to set the `hue` and `saturation`, and one for `brightness`. Don't blame me on this design, this is how the API works! :D + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setHsvColor( + of color: ( + hue: Int, + saturation: Int + ), + withEffect effect: ChangeEffect = .smooth, + withDuration duration: Int = 500, + forDevice device: Device + ) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_hsv", params: [color.hue, color.saturation, effect.rawValue, duration]) + } + + + /// Sets the brightness of a device. + /// - Parameters: + /// - value: A brightness value you want to set. Allowed values are from 0 to 100. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setBrightness(to value: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_bright", params: [value, effect.rawValue, duration]) + } + + private func modifyDeviceState(deviceId: String, method: String, params: Array) async throws { + let _ = try await self.sendDeviceRequest( + url: URL(string: "https://\(self.region.urlFormat).openapp.io.mi.com/openapp/device/rpc/\(deviceId)")!, + arguments: ["method": method, "params": params] ) } From 985d3c228f64b6f5f6f5e3605e4f7f973af3b374 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 11:30:10 +0200 Subject: [PATCH 02/20] smol API update --- Sources/YeeLampa/YeeLampa.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeeLampa/YeeLampa.swift index d2dc037..c225daa 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeeLampa/YeeLampa.swift @@ -76,7 +76,7 @@ public class YeeLampa { /// - effect: An effect that wiil be used. /// - duration: Time that it will take. /// - device: A target device. - public func setColorTemperature(to temp: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + public func setColorTemperature(to temp: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_ct_abx", params: [temp, effect.rawValue, duration]) } @@ -86,7 +86,7 @@ public class YeeLampa { /// - effect: An effect that wiil be used. /// - duration: Time that it will take. /// - device: A target device. - public func setPower(to on: Bool, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + public func setPower(to on: Bool, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration]) } @@ -110,7 +110,7 @@ public class YeeLampa { ), withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, - forDevice device: Device + for device: Device ) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_rgb", params: [ ((color.red << 16) + (color.green << 8) + color.blue), @@ -132,7 +132,7 @@ public class YeeLampa { ), withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, - forDevice device: Device + for device: Device ) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_hsv", params: [color.hue, color.saturation, effect.rawValue, duration]) } @@ -144,7 +144,7 @@ public class YeeLampa { /// - effect: An effect that wiil be used. /// - duration: Time that it will take. /// - device: A target device. - public func setBrightness(to value: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, forDevice device: Device) async throws { + public func setBrightness(to value: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_bright", params: [value, effect.rawValue, duration]) } From 877db521f4305453dc48e7b75af290c67f2a6f58 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 12:44:49 +0200 Subject: [PATCH 03/20] Moved ChangeEffect enum under Enums directory, removed boilerplate --- Sources/YeeLampa/ChangeEffect.swift | 20 -------------------- Sources/YeeLampa/Enums/ChangeEffect.swift | 11 +++++++++++ 2 files changed, 11 insertions(+), 20 deletions(-) delete mode 100644 Sources/YeeLampa/ChangeEffect.swift create mode 100644 Sources/YeeLampa/Enums/ChangeEffect.swift diff --git a/Sources/YeeLampa/ChangeEffect.swift b/Sources/YeeLampa/ChangeEffect.swift deleted file mode 100644 index a167e52..0000000 --- a/Sources/YeeLampa/ChangeEffect.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// File.swift -// -// -// Created by Егор Яковенко on 18.12.2021. -// - -public enum ChangeEffect { - case sudden - case smooth - - var rawValue: String { - switch self { - case .sudden: - return "sudden" - case .smooth: - return "smooth" - } - } -} diff --git a/Sources/YeeLampa/Enums/ChangeEffect.swift b/Sources/YeeLampa/Enums/ChangeEffect.swift new file mode 100644 index 0000000..5e9f3c5 --- /dev/null +++ b/Sources/YeeLampa/Enums/ChangeEffect.swift @@ -0,0 +1,11 @@ +// +// File.swift +// +// +// Created by Егор Яковенко on 18.12.2021. +// + +public enum ChangeEffect: String { + case sudden = "sudden" + case smooth = "smooth" +} From ad12571de7a9055bf6b56fda580fb8f5f4680e56 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 12:45:12 +0200 Subject: [PATCH 04/20] added PowerState enum to represent power state (cap) --- Sources/YeeLampa/Enums/PowerState.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Sources/YeeLampa/Enums/PowerState.swift diff --git a/Sources/YeeLampa/Enums/PowerState.swift b/Sources/YeeLampa/Enums/PowerState.swift new file mode 100644 index 0000000..78b15fb --- /dev/null +++ b/Sources/YeeLampa/Enums/PowerState.swift @@ -0,0 +1,11 @@ +// +// PowerState.swift +// +// +// Created by Егор Яковенко on 19.12.2021. +// + +public enum PowerState: String { + case on = "on" + case off = "off" +} From e31add6e09268f2227ec57c937bb52e4de9e2ec6 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 12:45:55 +0200 Subject: [PATCH 05/20] Added TurnOnMode enum for setPower method --- Sources/YeeLampa/Enums/TurnOnMode.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Sources/YeeLampa/Enums/TurnOnMode.swift diff --git a/Sources/YeeLampa/Enums/TurnOnMode.swift b/Sources/YeeLampa/Enums/TurnOnMode.swift new file mode 100644 index 0000000..1c62f50 --- /dev/null +++ b/Sources/YeeLampa/Enums/TurnOnMode.swift @@ -0,0 +1,22 @@ +// +// TurnOnMode.swift +// +// +// Created by Егор Яковенко on 19.12.2021. +// + +/// Modes available when turning on a device. +public enum TurnOnMode: Int { + /// Normal turn on operation (default value). + case normal = 0 + /// Turn on and switch to CT mode. + case colorTemperature = 1 + /// Turn on and switch to RGB mode. + case rgb = 2 + /// Turn on and switch to HSV mode. + case hsv = 3 + /// Turn on and switch to color flow mode. + case colorFlow = 4 + /// Turn on and switch to Night light mode. (Ceiling light only). + case nightLight = 5 +} From 74cbf53da20161c767315cea2807db08a2700d79 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 13:23:36 +0200 Subject: [PATCH 06/20] reorganised functions, added saveCurrentStateAsDefault, which represents set_default method, started working on color flow representation --- Sources/YeeLampa/YeeLampa.swift | 73 +++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeeLampa/YeeLampa.swift index c225daa..46c5c2b 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeeLampa/YeeLampa.swift @@ -70,7 +70,7 @@ public class YeeLampa { } } - /// Set a color temperature for a device that supports it. + /// Set a color temperature for a device that supports it. Only accepted when a device is in `on` state. /// - Parameters: /// - temp: A target temperature. /// - effect: An effect that wiil be used. @@ -80,23 +80,7 @@ public class YeeLampa { try await modifyDeviceState(deviceId: device.deviceId, method: "set_ct_abx", params: [temp, effect.rawValue, duration]) } - /// Sets power state for a device. - /// - Parameters: - /// - on: The power state you want your device to change to. - /// - effect: An effect that wiil be used. - /// - duration: Time that it will take. - /// - device: A target device. - public func setPower(to on: Bool, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration]) - } - - /// Toggles an LED. - /// - Parameter device: A target device. - public func togglePower(ofDevice device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "toggle", params: []) - } - - /// Sets an RGB color. + /// Sets an RGB color. Only accepted when a device is in `on` state. /// - Parameters: /// - color: A RGB color you want to set. /// - effect: An effect that wiil be used. @@ -119,7 +103,7 @@ public class YeeLampa { ) } - /// Sets an HSV color. + /// Sets an HSV color. Only accepted when a device is in `on` state. /// - Parameters: /// - color: A color you want to set. `hue`'s bounds are 0 to 359(yea, it's strange, but this is how the API works), `saturation` is from 0 to 100, and `brightness` is from 0 to 100. Note that if a `brightness` value is provided, then there will be 2 requests, one to set the `hue` and `saturation`, and one for `brightness`. Don't blame me on this design, this is how the API works! :D /// - effect: An effect that wiil be used. @@ -137,8 +121,7 @@ public class YeeLampa { try await modifyDeviceState(deviceId: device.deviceId, method: "set_hsv", params: [color.hue, color.saturation, effect.rawValue, duration]) } - - /// Sets the brightness of a device. + /// Sets the brightness of a device. Only accepted when a device is in `on` state. /// - Parameters: /// - value: A brightness value you want to set. Allowed values are from 0 to 100. /// - effect: An effect that wiil be used. @@ -148,6 +131,54 @@ public class YeeLampa { try await modifyDeviceState(deviceId: device.deviceId, method: "set_bright", params: [value, effect.rawValue, duration]) } + /// Sets power state for a device. + /// - Parameters: + /// - on: The power state you want your device to change to. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setPower( + to on: Bool, + withEffect effect: ChangeEffect = .smooth, + withDuration duration: Int = 500, + withMode mode: TurnOnMode = .normal, + for device: Device + ) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration, mode.rawValue]) + } + + /// Sets power state for a device. + /// - Parameters: + /// - state: The power state you want your device to change to. + /// - effect: An effect that wiil be used. + /// - duration: Time that it will take. + /// - device: A target device. + public func setPower( + to state: PowerState, + withEffect effect: ChangeEffect = .smooth, + withDuration duration: Int = 500, + withMode mode: TurnOnMode = .normal, + for device: Device + ) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [state.rawValue, effect.rawValue, duration, mode.rawValue]) + } + + /// Toggles an LED. + /// - Parameter device: A target device. + public func togglePower(of device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "toggle", params: []) + } + + /// This method is used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state. + /// - Parameter device: Target device. + public func saveCurrentStateAsDefault(for device: Device) async throws { + try await modifyDeviceState(deviceId: device.deviceId, method: "set_default", params: []) + } + + public func startColorFlow(of flow: ColorFlow, for device: Device) async throws { + + } + private func modifyDeviceState(deviceId: String, method: String, params: Array) async throws { let _ = try await self.sendDeviceRequest( url: URL(string: "https://\(self.region.urlFormat).openapp.io.mi.com/openapp/device/rpc/\(deviceId)")!, From b815220b283cf60a9947eda95c365605207dfa6d Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 13:23:59 +0200 Subject: [PATCH 07/20] removed unused parts of code --- Sources/YeeLampa/Enums/ChangeEffect.swift | 4 ++-- Sources/YeeLampa/Enums/PowerState.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/YeeLampa/Enums/ChangeEffect.swift b/Sources/YeeLampa/Enums/ChangeEffect.swift index 5e9f3c5..0a75f79 100644 --- a/Sources/YeeLampa/Enums/ChangeEffect.swift +++ b/Sources/YeeLampa/Enums/ChangeEffect.swift @@ -6,6 +6,6 @@ // public enum ChangeEffect: String { - case sudden = "sudden" - case smooth = "smooth" + case sudden + case smooth } diff --git a/Sources/YeeLampa/Enums/PowerState.swift b/Sources/YeeLampa/Enums/PowerState.swift index 78b15fb..ab5f928 100644 --- a/Sources/YeeLampa/Enums/PowerState.swift +++ b/Sources/YeeLampa/Enums/PowerState.swift @@ -6,6 +6,6 @@ // public enum PowerState: String { - case on = "on" - case off = "off" + case on + case off } From 864e67c613a9f5afc9684a986f510cfd0d1cb3b8 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 13:24:23 +0200 Subject: [PATCH 08/20] added a struct(WIP) that will be used to represent color flow --- Sources/YeeLampa/ColorFlow.swift | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Sources/YeeLampa/ColorFlow.swift diff --git a/Sources/YeeLampa/ColorFlow.swift b/Sources/YeeLampa/ColorFlow.swift new file mode 100644 index 0000000..a316d54 --- /dev/null +++ b/Sources/YeeLampa/ColorFlow.swift @@ -0,0 +1,30 @@ +// +// File.swift +// +// +// Created by Егор Яковенко on 19.12.2021. +// + +public struct ColorFlow { + public enum Action { + case recoverToPreviousState + case stayTheSame + case shutDown + } + public struct FlowExpression { + enum Mode: Int { + case color = 1 + case colorTemperature = 2 + case sleep = 7 + } + let duration: Int64 + let mode: Mode + let value: (red: Int, green: Int, blue: Int) + let brightness: Int? + } + /// Total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing. + let count: Int + let action: Action + let flow: [FlowExpression] + +} From b64d297e9fe8139c3492966e64fe299b6a47163b Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 13:34:28 +0200 Subject: [PATCH 09/20] bumped minimum macOS version to 12 --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 952edc3..7f6cf6a 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ let package = Package( name: "YeeLampa", platforms: [ .iOS(.v13), - .macOS(.v10_15) + .macOS(.v12) ], products: [ .library( From 66b4f74577ee5a28641ff2740450a2b8fb8037ba Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 13:43:45 +0200 Subject: [PATCH 10/20] fixed availability issues for Device class --- Sources/YeeLampa/Models/Device.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/YeeLampa/Models/Device.swift b/Sources/YeeLampa/Models/Device.swift index 8bfbe3a..0693bb8 100644 --- a/Sources/YeeLampa/Models/Device.swift +++ b/Sources/YeeLampa/Models/Device.swift @@ -12,14 +12,14 @@ open class Device: Identifiable { return lhs.deviceId == rhs.deviceId } - let deviceId: String - let name: String - let model: DeviceModel - let macAddress: String - let longitude: Double - let latitude: Double - let isOnline: Bool - let isOn: Bool + public let deviceId: String + public let name: String + public let model: DeviceModel + public let macAddress: String + public let longitude: Double + public let latitude: Double + public let isOnline: Bool + public let isOn: Bool init(deviceId: String, name: String, model: DeviceModel, macAddress: String, longitude: Double, latitude: Double, isOnline: Bool, isOn: Bool) { self.deviceId = deviceId From 418ac93d0eedb5b84d2bb38370460ccef3802295 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 14:21:25 +0200 Subject: [PATCH 11/20] removed QueryString because it was unused --- Sources/YeeLampa/QueryString.swift | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Sources/YeeLampa/QueryString.swift diff --git a/Sources/YeeLampa/QueryString.swift b/Sources/YeeLampa/QueryString.swift deleted file mode 100644 index 6dddddd..0000000 --- a/Sources/YeeLampa/QueryString.swift +++ /dev/null @@ -1,17 +0,0 @@ -// -// QueryString.swift -// -// -// Created by Егор Яковенко on 01.12.2021. -// - -extension Dictionary { - var queryString: String { - var output: String = "" - for (key,value) in self { - output += "\(key)=\(value)&" - } - output = String(output.dropLast()) - return output - } -} From 615fca24c4c157162d1a45c22e4a179906a2414a Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 14:21:37 +0200 Subject: [PATCH 12/20] compacted Region enum --- Sources/YeeLampa/Region.swift | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Sources/YeeLampa/Region.swift b/Sources/YeeLampa/Region.swift index 3e8482d..774faba 100644 --- a/Sources/YeeLampa/Region.swift +++ b/Sources/YeeLampa/Region.swift @@ -5,14 +5,7 @@ // Created by Егор Яковенко on 28.11.2021. // -public enum Region { - case Germany - case Russia - - var urlFormat: String { - switch self { - case .Germany: return "de" - case .Russia: return "ru" - } - } +public enum Region: String { + case Germany = "de" + case Russia = "ru" } From 005ec6b5bc97c385e7b7ca5a365f47232095555c Mon Sep 17 00:00:00 2001 From: ggoraa Date: Sun, 19 Dec 2021 14:48:57 +0200 Subject: [PATCH 13/20] changed how startColorFlow works, also made it working --- Sources/YeeLampa/ColorFlow.swift | 30 ----------- .../YeeLampa/Enums/ColorFlowExpression.swift | 12 +++++ Sources/YeeLampa/YeeLampa.swift | 53 +++++++++++++++++-- 3 files changed, 60 insertions(+), 35 deletions(-) delete mode 100644 Sources/YeeLampa/ColorFlow.swift create mode 100644 Sources/YeeLampa/Enums/ColorFlowExpression.swift diff --git a/Sources/YeeLampa/ColorFlow.swift b/Sources/YeeLampa/ColorFlow.swift deleted file mode 100644 index a316d54..0000000 --- a/Sources/YeeLampa/ColorFlow.swift +++ /dev/null @@ -1,30 +0,0 @@ -// -// File.swift -// -// -// Created by Егор Яковенко on 19.12.2021. -// - -public struct ColorFlow { - public enum Action { - case recoverToPreviousState - case stayTheSame - case shutDown - } - public struct FlowExpression { - enum Mode: Int { - case color = 1 - case colorTemperature = 2 - case sleep = 7 - } - let duration: Int64 - let mode: Mode - let value: (red: Int, green: Int, blue: Int) - let brightness: Int? - } - /// Total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing. - let count: Int - let action: Action - let flow: [FlowExpression] - -} diff --git a/Sources/YeeLampa/Enums/ColorFlowExpression.swift b/Sources/YeeLampa/Enums/ColorFlowExpression.swift new file mode 100644 index 0000000..7c1ec79 --- /dev/null +++ b/Sources/YeeLampa/Enums/ColorFlowExpression.swift @@ -0,0 +1,12 @@ +// +// ColorFlowExpression.swift +// +// +// Created by Егор Яковенко on 19.12.2021. +// + +public enum ColorFlowExpression { + case color(duration: Int, value: (red: Int, green: Int, blue: Int), brightness: Int?) + case colorTemperature(duration: Int, value: (red: Int, green: Int, blue: Int), brightness: Int?) + case sleep(duration: Int, value: (red: Int, green: Int, blue: Int)) +} diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeeLampa/YeeLampa.swift index 46c5c2b..1a6343b 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeeLampa/YeeLampa.swift @@ -16,7 +16,7 @@ public class YeeLampa { public init(accessToken: String, region: Region) { self.region = region self.accessToken = accessToken - self.deviceListUrl = URL(string: "https://\(region.urlFormat).openapp.io.mi.com/openapp/user/device_list")! + self.deviceListUrl = URL(string: "https://\(region.rawValue).openapp.io.mi.com/openapp/user/device_list")! } private struct AuthResponse: Codable { @@ -169,19 +169,62 @@ public class YeeLampa { try await modifyDeviceState(deviceId: device.deviceId, method: "toggle", params: []) } + public enum Action: Int { + case recoverToPreviousState = 0 + case stayTheSame + case shutDown + } + /// This method is used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state. /// - Parameter device: Target device. public func saveCurrentStateAsDefault(for device: Device) async throws { try await modifyDeviceState(deviceId: device.deviceId, method: "set_default", params: []) } - public func startColorFlow(of flow: ColorFlow, for device: Device) async throws { - + public func startColorFlow(of flow: [ColorFlowExpression], withCount count: Int, withAction action: Action, for device: Device) async throws { + var flowString: String = "" + for (index, element) in flow.enumerated() { + switch element { + case .color(let duration, let value, let brightness): + var computedBrightness: Int { + if brightness == nil { + return -1 + } else { + return brightness! + } + } + + flowString.append("\(duration), 1, \(((value.red << 16) + (value.green << 8) + value.blue)), \(computedBrightness)") + if index != flow.count { + flowString.append(",") + } + case .colorTemperature(let duration, let value, let brightness): + var computedBrightness: Int { + if brightness == nil { + return -1 + } else { + return brightness! + } + } + + flowString.append("\(duration), 2, \(((value.red << 16) + (value.green << 8) + value.blue)), \(computedBrightness)") + if index != flow.count { + flowString.append(",") + } + case .sleep(let duration, let value): + flowString.append("\(duration), 7, \(((value.red << 16) + (value.green << 8) + value.blue)), 0") + if index != flow.count { + flowString.append(",") + } + } + } + print(flowString) + try await modifyDeviceState(deviceId: device.deviceId, method: "start_cf", params: [count, action.rawValue, flowString]) } - + private func modifyDeviceState(deviceId: String, method: String, params: Array) async throws { let _ = try await self.sendDeviceRequest( - url: URL(string: "https://\(self.region.urlFormat).openapp.io.mi.com/openapp/device/rpc/\(deviceId)")!, + url: URL(string: "https://\(self.region.rawValue).openapp.io.mi.com/openapp/device/rpc/\(deviceId)")!, arguments: ["method": method, "params": params] ) } From 18d65ed4c18fd05c9deb253b73b96f77ea957c3f Mon Sep 17 00:00:00 2001 From: ggoraa Date: Tue, 21 Dec 2021 19:03:02 +0200 Subject: [PATCH 14/20] added Package.resolved to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bb460e7..59e2947 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ xcuserdata/ DerivedData/ .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +Package.resolved From 9e55c5a19097dae8eff0b85bf5f8880e4b85fd8e Mon Sep 17 00:00:00 2001 From: ggoraa Date: Wed, 22 Dec 2021 21:44:47 +0200 Subject: [PATCH 15/20] upd readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 74e6dcc..0ef5705 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # YeeLampa -A Swift package to interact with Yeelight devices. +A Swift package for type-safe interactions with Yeelight API. From 357e0a46966203e32063e97cda1c56fdd264ab2c Mon Sep 17 00:00:00 2001 From: ggoraa Date: Wed, 22 Dec 2021 21:45:12 +0200 Subject: [PATCH 16/20] added an enum that represents available device properties --- Sources/YeeLampa/DeviceProperty.swift | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Sources/YeeLampa/DeviceProperty.swift diff --git a/Sources/YeeLampa/DeviceProperty.swift b/Sources/YeeLampa/DeviceProperty.swift new file mode 100644 index 0000000..01a39fc --- /dev/null +++ b/Sources/YeeLampa/DeviceProperty.swift @@ -0,0 +1,33 @@ +// +// DeviceProperty.swift +// +// +// Created by Егор Яковенко on 22.12.2021. +// + +/// An enum for representing device properties. Used in ``YeeLampa/YeeLampa/get(properties:of:)`` function, to get individual properties of a device. +public enum DeviceProperty: String { + case isOn = "power" + case brightness = "bright" + case colorTemperature = "ct" + case rgbColor = "rgb" + case hue + case saturation = "sat" + case colorMode = "color_mode" + /// Is the flow mode running. + case isFlowing = "flowing" + case isMusicModeOn = "music_on" + case name + case isBackgroundOn = "bg_power" + /// Is the flow mode running on background light. + case isBackgroundFlowing = "bg_flowing" + case backgroundColorTemperature = "bg_ct" + case backgroundColorMode = "bg_lmode" + case backgroundBrightness = "bg_bright" + case backgroundRgbColor = "bg_rgb" + case backgroundHue = "bg_hue" + case backgroundSaturation = "bg_sat" + case nightLightBrightness = "nl_br" + /// Only applicable for the ceiling light. + case activeMode = "active_mode" +} From 96b82e77c3ceac6c56efdae5fda8952713786d64 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Wed, 22 Dec 2021 21:46:01 +0200 Subject: [PATCH 17/20] added get(properties:of:) and get(property:of:) functions --- Sources/YeeLampa/YeeLampa.swift | 40 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeeLampa/YeeLampa.swift index 1a6343b..73e8570 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeeLampa/YeeLampa.swift @@ -77,7 +77,7 @@ public class YeeLampa { /// - duration: Time that it will take. /// - device: A target device. public func setColorTemperature(to temp: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_ct_abx", params: [temp, effect.rawValue, duration]) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_ct_abx", params: [temp, effect.rawValue, duration]) } /// Sets an RGB color. Only accepted when a device is in `on` state. @@ -96,7 +96,7 @@ public class YeeLampa { withDuration duration: Int = 500, for device: Device ) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_rgb", params: [ + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_rgb", params: [ ((color.red << 16) + (color.green << 8) + color.blue), effect.rawValue, duration] @@ -118,7 +118,7 @@ public class YeeLampa { withDuration duration: Int = 500, for device: Device ) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_hsv", params: [color.hue, color.saturation, effect.rawValue, duration]) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_hsv", params: [color.hue, color.saturation, effect.rawValue, duration]) } /// Sets the brightness of a device. Only accepted when a device is in `on` state. @@ -128,7 +128,7 @@ public class YeeLampa { /// - duration: Time that it will take. /// - device: A target device. public func setBrightness(to value: Int, withEffect effect: ChangeEffect = .smooth, withDuration duration: Int = 500, for device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_bright", params: [value, effect.rawValue, duration]) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_bright", params: [value, effect.rawValue, duration]) } /// Sets power state for a device. @@ -144,7 +144,7 @@ public class YeeLampa { withMode mode: TurnOnMode = .normal, for device: Device ) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration, mode.rawValue]) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_power", params: [(on ? "on" : "off"), effect.rawValue, duration, mode.rawValue]) } /// Sets power state for a device. @@ -160,13 +160,13 @@ public class YeeLampa { withMode mode: TurnOnMode = .normal, for device: Device ) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_power", params: [state.rawValue, effect.rawValue, duration, mode.rawValue]) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_power", params: [state.rawValue, effect.rawValue, duration, mode.rawValue]) } /// Toggles an LED. /// - Parameter device: A target device. public func togglePower(of device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "toggle", params: []) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "toggle", params: []) } public enum Action: Int { @@ -178,7 +178,7 @@ public class YeeLampa { /// This method is used to save current state of smart LED in persistent memory. So if user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state. /// - Parameter device: Target device. public func saveCurrentStateAsDefault(for device: Device) async throws { - try await modifyDeviceState(deviceId: device.deviceId, method: "set_default", params: []) + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "set_default", params: []) } public func startColorFlow(of flow: [ColorFlowExpression], withCount count: Int, withAction action: Action, for device: Device) async throws { @@ -218,17 +218,33 @@ public class YeeLampa { } } } - print(flowString) - try await modifyDeviceState(deviceId: device.deviceId, method: "start_cf", params: [count, action.rawValue, flowString]) + + let _ = try await runDeviceMethod(deviceId: device.deviceId, method: "start_cf", params: [count, action.rawValue, flowString]) } - private func modifyDeviceState(deviceId: String, method: String, params: Array) async throws { - let _ = try await self.sendDeviceRequest( + private func runDeviceMethod(deviceId: String, method: String, params: Array) async throws -> Data { + return try await self.sendDeviceRequest( url: URL(string: "https://\(self.region.rawValue).openapp.io.mi.com/openapp/device/rpc/\(deviceId)")!, arguments: ["method": method, "params": params] ) } + public func get(properties: [DeviceProperty], of device: Device) async throws -> [String] { + var requestProps: [String] = [] + + for prop in properties { + requestProps.append(prop.rawValue) + } + + let response = try await runDeviceMethod(deviceId: device.deviceId, method: "get_prop", params: requestProps) + return try JSONDecoder().decode(BaseJsonModel<[String]>.self, from: response).result + } + + public func get(property: DeviceProperty, of device: Device) async throws -> String { + let response = try await runDeviceMethod(deviceId: device.deviceId, method: "get_prop", params: [property.rawValue]) + return try JSONDecoder().decode(BaseJsonModel<[String]>.self, from: response).result[0] + } + /// An internal function to pass requests to devices. /// - Parameters: /// - url: A url to call when making a request. From 33ab33f4a6e7a14e084a1b4a11d3fec6a5984b36 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Wed, 22 Dec 2021 21:46:20 +0200 Subject: [PATCH 18/20] started implementing docs --- Sources/Documentation.docc/Documentation.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Sources/Documentation.docc/Documentation.md diff --git a/Sources/Documentation.docc/Documentation.md b/Sources/Documentation.docc/Documentation.md new file mode 100644 index 0000000..604df90 --- /dev/null +++ b/Sources/Documentation.docc/Documentation.md @@ -0,0 +1,8 @@ +# ``YeeLampa`` + +A Swift package for type-safe interactions with Yeelight API. + +## Overview + +yee + From c86bddca2a51538f4696bc68a130fb7f244dd77e Mon Sep 17 00:00:00 2001 From: ggoraa Date: Thu, 23 Dec 2021 11:02:23 +0200 Subject: [PATCH 19/20] moved enums to their respective folder --- Sources/YeeLampa/{ => Enums}/DeviceModel.swift | 0 Sources/YeeLampa/{ => Enums}/DeviceProperty.swift | 0 Sources/YeeLampa/{ => Enums}/Region.swift | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename Sources/YeeLampa/{ => Enums}/DeviceModel.swift (100%) rename Sources/YeeLampa/{ => Enums}/DeviceProperty.swift (100%) rename Sources/YeeLampa/{ => Enums}/Region.swift (100%) diff --git a/Sources/YeeLampa/DeviceModel.swift b/Sources/YeeLampa/Enums/DeviceModel.swift similarity index 100% rename from Sources/YeeLampa/DeviceModel.swift rename to Sources/YeeLampa/Enums/DeviceModel.swift diff --git a/Sources/YeeLampa/DeviceProperty.swift b/Sources/YeeLampa/Enums/DeviceProperty.swift similarity index 100% rename from Sources/YeeLampa/DeviceProperty.swift rename to Sources/YeeLampa/Enums/DeviceProperty.swift diff --git a/Sources/YeeLampa/Region.swift b/Sources/YeeLampa/Enums/Region.swift similarity index 100% rename from Sources/YeeLampa/Region.swift rename to Sources/YeeLampa/Enums/Region.swift From f930ec63dc8fea9ebff5eabb9b10d5f90e5ce2e4 Mon Sep 17 00:00:00 2001 From: ggoraa Date: Tue, 25 Jan 2022 14:00:16 +0200 Subject: [PATCH 20/20] feat: full project rebranding from YeeLampa to YeelightKit --- .../xcshareddata/xcschemes/YeeLampa.xcscheme | 91 ------------------- Package.swift | 8 +- README.md | 2 +- Sources/Documentation.docc/Documentation.md | 2 +- .../Enums/ChangeEffect.swift | 0 .../Enums/ColorFlowExpression.swift | 0 .../Enums/DeviceModel.swift | 0 .../Enums/DeviceProperty.swift | 2 +- .../Enums/PowerState.swift | 0 .../Enums/Region.swift | 0 .../Enums/TurnOnMode.swift | 0 .../Errors/LoginError.swift | 0 .../Errors/RequestError.swift | 0 .../Models/BedsideLampDevice.swift | 0 .../Models/DeskLampDevice.swift | 0 .../Models/Device.swift | 0 .../Models/Json/BaseJsonModel.swift | 0 .../Models/Json/BatchRpcJsonModel.swift | 0 .../Models/Json/DeviceListJsonModel.swift | 0 .../Models/LightStripDevice.swift | 0 .../Resources/DeviceProps.plist | 0 .../{YeeLampa => YeelightKit}/YeeLampa.swift | 16 ++-- 22 files changed, 15 insertions(+), 106 deletions(-) delete mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/YeeLampa.xcscheme rename Sources/{YeeLampa => YeelightKit}/Enums/ChangeEffect.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Enums/ColorFlowExpression.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Enums/DeviceModel.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Enums/DeviceProperty.swift (84%) rename Sources/{YeeLampa => YeelightKit}/Enums/PowerState.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Enums/Region.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Enums/TurnOnMode.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Errors/LoginError.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Errors/RequestError.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/BedsideLampDevice.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/DeskLampDevice.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/Device.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/Json/BaseJsonModel.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/Json/BatchRpcJsonModel.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/Json/DeviceListJsonModel.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Models/LightStripDevice.swift (100%) rename Sources/{YeeLampa => YeelightKit}/Resources/DeviceProps.plist (100%) rename Sources/{YeeLampa => YeelightKit}/YeeLampa.swift (96%) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/YeeLampa.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/YeeLampa.xcscheme deleted file mode 100644 index cc6fb7a..0000000 --- a/.swiftpm/xcode/xcshareddata/xcschemes/YeeLampa.xcscheme +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Package.swift b/Package.swift index 7f6cf6a..84ef7ce 100644 --- a/Package.swift +++ b/Package.swift @@ -3,22 +3,22 @@ import PackageDescription let package = Package( - name: "YeeLampa", + name: "YeelightKit", platforms: [ .iOS(.v13), .macOS(.v12) ], products: [ .library( - name: "YeeLampa", - targets: ["YeeLampa"]), + name: "YeelightKit", + targets: ["YeelightKit"]), ], dependencies: [ .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0")) ], targets: [ .target( - name: "YeeLampa", + name: "YeelightKit", dependencies: ["Alamofire"], resources: [ .copy("Resources/DeviceProps.plist") diff --git a/README.md b/README.md index 0ef5705..709453e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# YeeLampa +# YeelightKit A Swift package for type-safe interactions with Yeelight API. diff --git a/Sources/Documentation.docc/Documentation.md b/Sources/Documentation.docc/Documentation.md index 604df90..c972c91 100644 --- a/Sources/Documentation.docc/Documentation.md +++ b/Sources/Documentation.docc/Documentation.md @@ -1,4 +1,4 @@ -# ``YeeLampa`` +# ``YeelightKit`` A Swift package for type-safe interactions with Yeelight API. diff --git a/Sources/YeeLampa/Enums/ChangeEffect.swift b/Sources/YeelightKit/Enums/ChangeEffect.swift similarity index 100% rename from Sources/YeeLampa/Enums/ChangeEffect.swift rename to Sources/YeelightKit/Enums/ChangeEffect.swift diff --git a/Sources/YeeLampa/Enums/ColorFlowExpression.swift b/Sources/YeelightKit/Enums/ColorFlowExpression.swift similarity index 100% rename from Sources/YeeLampa/Enums/ColorFlowExpression.swift rename to Sources/YeelightKit/Enums/ColorFlowExpression.swift diff --git a/Sources/YeeLampa/Enums/DeviceModel.swift b/Sources/YeelightKit/Enums/DeviceModel.swift similarity index 100% rename from Sources/YeeLampa/Enums/DeviceModel.swift rename to Sources/YeelightKit/Enums/DeviceModel.swift diff --git a/Sources/YeeLampa/Enums/DeviceProperty.swift b/Sources/YeelightKit/Enums/DeviceProperty.swift similarity index 84% rename from Sources/YeeLampa/Enums/DeviceProperty.swift rename to Sources/YeelightKit/Enums/DeviceProperty.swift index 01a39fc..b125d23 100644 --- a/Sources/YeeLampa/Enums/DeviceProperty.swift +++ b/Sources/YeelightKit/Enums/DeviceProperty.swift @@ -5,7 +5,7 @@ // Created by Егор Яковенко on 22.12.2021. // -/// An enum for representing device properties. Used in ``YeeLampa/YeeLampa/get(properties:of:)`` function, to get individual properties of a device. +/// An enum for representing device properties. Used in ``YeelightKit/YeelightKit/get(properties:of:)`` function, to get individual properties of a device. public enum DeviceProperty: String { case isOn = "power" case brightness = "bright" diff --git a/Sources/YeeLampa/Enums/PowerState.swift b/Sources/YeelightKit/Enums/PowerState.swift similarity index 100% rename from Sources/YeeLampa/Enums/PowerState.swift rename to Sources/YeelightKit/Enums/PowerState.swift diff --git a/Sources/YeeLampa/Enums/Region.swift b/Sources/YeelightKit/Enums/Region.swift similarity index 100% rename from Sources/YeeLampa/Enums/Region.swift rename to Sources/YeelightKit/Enums/Region.swift diff --git a/Sources/YeeLampa/Enums/TurnOnMode.swift b/Sources/YeelightKit/Enums/TurnOnMode.swift similarity index 100% rename from Sources/YeeLampa/Enums/TurnOnMode.swift rename to Sources/YeelightKit/Enums/TurnOnMode.swift diff --git a/Sources/YeeLampa/Errors/LoginError.swift b/Sources/YeelightKit/Errors/LoginError.swift similarity index 100% rename from Sources/YeeLampa/Errors/LoginError.swift rename to Sources/YeelightKit/Errors/LoginError.swift diff --git a/Sources/YeeLampa/Errors/RequestError.swift b/Sources/YeelightKit/Errors/RequestError.swift similarity index 100% rename from Sources/YeeLampa/Errors/RequestError.swift rename to Sources/YeelightKit/Errors/RequestError.swift diff --git a/Sources/YeeLampa/Models/BedsideLampDevice.swift b/Sources/YeelightKit/Models/BedsideLampDevice.swift similarity index 100% rename from Sources/YeeLampa/Models/BedsideLampDevice.swift rename to Sources/YeelightKit/Models/BedsideLampDevice.swift diff --git a/Sources/YeeLampa/Models/DeskLampDevice.swift b/Sources/YeelightKit/Models/DeskLampDevice.swift similarity index 100% rename from Sources/YeeLampa/Models/DeskLampDevice.swift rename to Sources/YeelightKit/Models/DeskLampDevice.swift diff --git a/Sources/YeeLampa/Models/Device.swift b/Sources/YeelightKit/Models/Device.swift similarity index 100% rename from Sources/YeeLampa/Models/Device.swift rename to Sources/YeelightKit/Models/Device.swift diff --git a/Sources/YeeLampa/Models/Json/BaseJsonModel.swift b/Sources/YeelightKit/Models/Json/BaseJsonModel.swift similarity index 100% rename from Sources/YeeLampa/Models/Json/BaseJsonModel.swift rename to Sources/YeelightKit/Models/Json/BaseJsonModel.swift diff --git a/Sources/YeeLampa/Models/Json/BatchRpcJsonModel.swift b/Sources/YeelightKit/Models/Json/BatchRpcJsonModel.swift similarity index 100% rename from Sources/YeeLampa/Models/Json/BatchRpcJsonModel.swift rename to Sources/YeelightKit/Models/Json/BatchRpcJsonModel.swift diff --git a/Sources/YeeLampa/Models/Json/DeviceListJsonModel.swift b/Sources/YeelightKit/Models/Json/DeviceListJsonModel.swift similarity index 100% rename from Sources/YeeLampa/Models/Json/DeviceListJsonModel.swift rename to Sources/YeelightKit/Models/Json/DeviceListJsonModel.swift diff --git a/Sources/YeeLampa/Models/LightStripDevice.swift b/Sources/YeelightKit/Models/LightStripDevice.swift similarity index 100% rename from Sources/YeeLampa/Models/LightStripDevice.swift rename to Sources/YeelightKit/Models/LightStripDevice.swift diff --git a/Sources/YeeLampa/Resources/DeviceProps.plist b/Sources/YeelightKit/Resources/DeviceProps.plist similarity index 100% rename from Sources/YeeLampa/Resources/DeviceProps.plist rename to Sources/YeelightKit/Resources/DeviceProps.plist diff --git a/Sources/YeeLampa/YeeLampa.swift b/Sources/YeelightKit/YeeLampa.swift similarity index 96% rename from Sources/YeeLampa/YeeLampa.swift rename to Sources/YeelightKit/YeeLampa.swift index 73e8570..afb0774 100644 --- a/Sources/YeeLampa/YeeLampa.swift +++ b/Sources/YeelightKit/YeeLampa.swift @@ -1,15 +1,15 @@ import Foundation import Alamofire -/// Main class of YeeLampa. -public class YeeLampa { +/// Main class of YeelightKit. +public class YeelightKit { public static let clientId = 2882303761517308695 public static let clientSecret = "OrwZHJ/drEXakH1LsfwwqQ==" private var region: Region private var accessToken: String private let deviceListUrl: URL - /// Creates a YeeLampa instance with provided credentials. + /// Creates a YeelightKit instance with provided credentials. /// - Parameters: /// - accessToken: The token you acquired when logging in. /// - region: The region we will connect to. @@ -32,8 +32,8 @@ public class YeeLampa { url, method: .get, parameters: [ - "client_id": YeeLampa.clientId, - "client_secret": YeeLampa.clientSecret, + "client_id": YeelightKit.clientId, + "client_secret": YeelightKit.clientSecret, "grant_type": "authorization_code", "redirect_uri": "http://www.mi.com", "code": grant @@ -57,10 +57,10 @@ public class YeeLampa { var request = URLRequest(url: self.deviceListUrl) request.httpMethod = "POST" request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") - request.httpBody = Data("clientId=\(YeeLampa.clientId)&accessToken=\(self.accessToken)".utf8) + request.httpBody = Data("clientId=\(YeelightKit.clientId)&accessToken=\(self.accessToken)".utf8) let task = AF.request(self.deviceListUrl, method: .post, parameters: [ - "clientId": YeeLampa.clientId, + "clientId": YeelightKit.clientId, "accessToken": self.accessToken ], headers: [ "Content-Type": "application/x-www-form-urlencoded" @@ -257,7 +257,7 @@ public class YeeLampa { url, method: .post, parameters: [ - "clientId": YeeLampa.clientId, + "clientId": YeelightKit.clientId, "accessToken": self.accessToken, "data": String(data: payload, encoding: .utf8)!.replacingOccurrences(of: "\n", with: "") ],