From b445697c8412fcff67f146b9271e25af56289845 Mon Sep 17 00:00:00 2001 From: TheMisfit68 Date: Fri, 5 Jan 2024 00:27:46 +0100 Subject: [PATCH] Migrated from JVCocoa to JVSwift --- .../CirtcuitEnabler.swift | 2 +- .../DimmableLight.swift | 11 +- .../DoorLock.swift | 2 +- .../FunctionKey.swift | 2 +- .../GarageDoor.swift | 2 +- .../ PLCClassAccessoryDelegates/Light.swift | 2 +- .../ PLCClassAccessoryDelegates/Outlet.swift | 2 +- .../SmartSprinkler.swift | 3 +- .../ToggleableOutlet.swift | 2 +- .../WindowCovering.swift | 2 +- Accessory Delegates/AccessoryDelegate.swift | 2 +- .../LeafAccessoryDelegate.swift | 2 +- .../MilightAccessoryDelegate.swift | 10 +- .../PLCAccessoryDelegate.swift | 2 +- .../TizenAccessoryDelegate.swift | 2 +- HAP Extensions/Accessory/Accessory.swift | 2 +- HAP Extensions/Bridge(Device]/Bridge.swift | 2 +- .../ElectricCarAccessory.swift | 3 +- HAPiNest.xcodeproj/project.pbxproj | 53 +++---- .../Bookmarks/bookmarks.plist | 25 ++-- .../xcshareddata/xcschemes/HAPiNest.xcscheme | 2 +- .../xcschemes/xcschememanagement.plist | 32 ++-- .../AmbientLightning/BrightnessTimer.swift | 78 ---------- HAPiNest/CyclicPoller.swift | 2 +- HAPiNest/HAPiNestApp.swift | 139 +++++++++--------- HAPiNest/HomekitServer/HomeKitServer.swift | 8 +- .../PreferencesWindow/PreferencesView.swift | 6 +- .../WeatherKitSettingsView.swift | 23 --- .../ExternalEventsServer/JVMQTTClient.swift | 104 +++++++++++++ .../ServerBasedAutomations/WakeupLights.swift | 73 +++++++++ HAPiNestTests/HAPTests.swift | 2 +- MainConfiguration.swift | 2 +- PLCConfiguration.swift | 2 + ToDo.md | 6 +- 34 files changed, 346 insertions(+), 266 deletions(-) delete mode 100644 HAPiNest/AmbientLightning/BrightnessTimer.swift delete mode 100644 HAPiNest/PreferencesWindow/WeatherKitSettingsView.swift create mode 100644 HAPiNest/ServerBasedAutomations/ExternalEventsServer/JVMQTTClient.swift create mode 100644 HAPiNest/ServerBasedAutomations/WakeupLights.swift diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/CirtcuitEnabler.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/CirtcuitEnabler.swift index 4cf1c6343..0b68fed42 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/CirtcuitEnabler.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/CirtcuitEnabler.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class CirtcuitEnabler:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/DimmableLight.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/DimmableLight.swift index cb8c0f89c..41e902ad7 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/DimmableLight.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/DimmableLight.swift @@ -11,21 +11,14 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class -class DimmableLight:PLCClassAccessoryDelegate, DimmedLight{ +class DimmableLight:PLCClassAccessoryDelegate{ // MARK: - Accessory binding typealias AccessorySubclass = Accessory.Lightbulb var characteristicChanged: Bool = false - - var brightnessTimer: BrightnessTimer! - - override init(){ - super.init() - self.brightnessTimer = BrightnessTimer(dimmer: self) - } // MARK: - State var powerState:Bool? = nil diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/DoorLock.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/DoorLock.swift index a04c976e3..9016d5c7d 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/DoorLock.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/DoorLock.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class Doorlock:PLCClassAccessoryDelegate, PulsOperatedCircuit{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/FunctionKey.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/FunctionKey.swift index 0f53780c9..adb6b6607 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/FunctionKey.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/FunctionKey.swift @@ -13,7 +13,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift class FunctionKey:PLCClassAccessoryDelegate{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/GarageDoor.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/GarageDoor.swift index 38c866a64..492c54719 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/GarageDoor.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/GarageDoor.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class GarageDoor:PLCClassAccessoryDelegate, PulsOperatedCircuit{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/Light.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/Light.swift index 1fd179fe1..ab1420413 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/Light.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/Light.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift import OSLog // MARK: - PLC level class diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/Outlet.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/Outlet.swift index f71b5ca67..dd4092ed0 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/Outlet.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/Outlet.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class Outlet:PLCClassAccessoryDelegate{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/SmartSprinkler.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/SmartSprinkler.swift index 59214e935..a5d3fed95 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/SmartSprinkler.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/SmartSprinkler.swift @@ -11,8 +11,9 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift import WeatherKit +import JVWeather import CoreLocation import OSLog diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/ToggleableOutlet.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/ToggleableOutlet.swift index 441a14521..f1b152693 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/ToggleableOutlet.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/ToggleableOutlet.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class ToggleableOutlet:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{ diff --git a/Accessory Delegates/ PLCClassAccessoryDelegates/WindowCovering.swift b/Accessory Delegates/ PLCClassAccessoryDelegates/WindowCovering.swift index 25f8afb20..d162fb9e5 100644 --- a/Accessory Delegates/ PLCClassAccessoryDelegates/WindowCovering.swift +++ b/Accessory Delegates/ PLCClassAccessoryDelegates/WindowCovering.swift @@ -11,7 +11,7 @@ import HAP import SoftPLC import ModbusDriver import IOTypes -import JVCocoa +import JVSwift // MARK: - PLC level class class WindowCovering:PLCClassAccessoryDelegate, PulsOperatedCircuit, Simulateable{ diff --git a/Accessory Delegates/AccessoryDelegate.swift b/Accessory Delegates/AccessoryDelegate.swift index df6ddcdf7..f71bdac71 100644 --- a/Accessory Delegates/AccessoryDelegate.swift +++ b/Accessory Delegates/AccessoryDelegate.swift @@ -8,7 +8,7 @@ import Foundation import HAP -import JVCocoa +import JVSwift import SoftPLC import OSLog diff --git a/Accessory Delegates/LeafAccessoryDelegate.swift b/Accessory Delegates/LeafAccessoryDelegate.swift index 98883cfe5..61a77a3f1 100644 --- a/Accessory Delegates/LeafAccessoryDelegate.swift +++ b/Accessory Delegates/LeafAccessoryDelegate.swift @@ -8,7 +8,7 @@ import Foundation import HAP -import JVCocoa +import JVSwift import LeafDriver import OSLog diff --git a/Accessory Delegates/MilightAccessoryDelegate.swift b/Accessory Delegates/MilightAccessoryDelegate.swift index 657e5b04b..172d054ae 100644 --- a/Accessory Delegates/MilightAccessoryDelegate.swift +++ b/Accessory Delegates/MilightAccessoryDelegate.swift @@ -8,16 +8,15 @@ import Foundation import HAP -import JVCocoa +import JVSwift import MilightDriver import OSLog /// Handles characteristic changes for a Homekit Accessory. /// It uses the MilightDriver to pass those changes to the hardware -class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate, DimmedLight { +class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate{ - var brightnessTimer: BrightnessTimer! let zone:MilightDriver.Zone var name: String{ @@ -27,7 +26,6 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate, DimmedLight { init(ipAddress: String, zone:MilightDriver.Zone){ self.zone = zone super.init(ipAddress: ipAddress) - self.brightnessTimer = BrightnessTimer(dimmer: self) } var characteristicChanged: Bool = false @@ -85,9 +83,7 @@ class MilightAccessoryDelegate:MilightDriverV6, AccessoryDelegate, DimmedLight { case CharacteristicType.brightness: -// brightness = characteristic.value as! Int - self.brightnessTimer.timeFor100percentChange = 1800 - self.brightnessTimer.targetBrightness = (characteristic.value as! Int) + brightness = characteristic.value as! Int case CharacteristicType.hue: diff --git a/Accessory Delegates/PLCAccessoryDelegate.swift b/Accessory Delegates/PLCAccessoryDelegate.swift index eade0f728..bc043b853 100644 --- a/Accessory Delegates/PLCAccessoryDelegate.swift +++ b/Accessory Delegates/PLCAccessoryDelegate.swift @@ -8,7 +8,7 @@ import Foundation import HAP -import JVCocoa +import JVSwift import SoftPLC import OSLog diff --git a/Accessory Delegates/TizenAccessoryDelegate.swift b/Accessory Delegates/TizenAccessoryDelegate.swift index e3344b914..6697fe2ff 100644 --- a/Accessory Delegates/TizenAccessoryDelegate.swift +++ b/Accessory Delegates/TizenAccessoryDelegate.swift @@ -8,7 +8,7 @@ import Foundation import HAP -import JVCocoa +import JVSwift import TizenDriver import OSLog diff --git a/HAP Extensions/Accessory/Accessory.swift b/HAP Extensions/Accessory/Accessory.swift index e4711777d..b2dffb6c5 100644 --- a/HAP Extensions/Accessory/Accessory.swift +++ b/HAP Extensions/Accessory/Accessory.swift @@ -9,7 +9,7 @@ import Foundation import SoftPLC import HAP -import JVCocoa +import JVSwift extension HAP.Accessory{ diff --git a/HAP Extensions/Bridge(Device]/Bridge.swift b/HAP Extensions/Bridge(Device]/Bridge.swift index 8f7c70155..21f9b72c1 100644 --- a/HAP Extensions/Bridge(Device]/Bridge.swift +++ b/HAP Extensions/Bridge(Device]/Bridge.swift @@ -7,7 +7,7 @@ // import Foundation -import JVCocoa +import JVSwift import OSLog import HAP import SoftPLC diff --git a/HAP Extensions/Custom Accessories/ElectricCarAccessory.swift b/HAP Extensions/Custom Accessories/ElectricCarAccessory.swift index 5f73e6a11..c627fbe07 100644 --- a/HAP Extensions/Custom Accessories/ElectricCarAccessory.swift +++ b/HAP Extensions/Custom Accessories/ElectricCarAccessory.swift @@ -11,8 +11,7 @@ import HAP extension Accessory { - class ElectricCar: Accessory{ - + class ElectricCar:Accessory{ let primaryService:Service.Battery = Service.Battery(characteristics: [.name(String(localized: "Battery status",table: "ServiceNames")), AnyCharacteristic(GenericCharacteristic( diff --git a/HAPiNest.xcodeproj/project.pbxproj b/HAPiNest.xcodeproj/project.pbxproj index 13b5af425..33cadf0a7 100644 --- a/HAPiNest.xcodeproj/project.pbxproj +++ b/HAPiNest.xcodeproj/project.pbxproj @@ -34,16 +34,14 @@ DC55156A296AE2A9003432CD /* ElectricCarAccessory.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC551569296AE2A9003432CD /* ElectricCarAccessory.swift */; }; DC6DEE212B2E5EE300BDAB31 /* FunctionKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6DEE202B2E5EE300BDAB31 /* FunctionKey.swift */; }; DC746D7325ACA97A00B33314 /* SmartSprinklerAccessory.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC746D7225ACA97A00B33314 /* SmartSprinklerAccessory.swift */; }; - DC846E3A26D996E9002CEDFB /* JVCocoa in Frameworks */ = {isa = PBXBuildFile; productRef = DC846E3926D996E9002CEDFB /* JVCocoa */; }; DC90DA642732817000068839 /* TizenSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA5E2732817000068839 /* TizenSettingsView.swift */; }; DC90DA652732817000068839 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA5F2732817000068839 /* PreferencesView.swift */; }; DC90DA672732817000068839 /* GeneralSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA612732817000068839 /* GeneralSettingsView.swift */; }; DC90DA682732817000068839 /* SunnyPortalSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA622732817000068839 /* SunnyPortalSettingsView.swift */; }; - DC90DA692732817000068839 /* WeatherKitSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA632732817000068839 /* WeatherKitSettingsView.swift */; }; DC90DA6C2732828200068839 /* DashboardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC90DA6B2732828200068839 /* DashboardView.swift */; }; DC9963892AD3073000915EAC /* CirtcuitEnabler.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC9963882AD3073000915EAC /* CirtcuitEnabler.swift */; }; DCA37CF926BB29EF003CC3BF /* HAPTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA37CF826BB29EF003CC3BF /* HAPTests.swift */; }; - DCA3DF312B28D97000554A8A /* BrightnessTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA3DF302B28D97000554A8A /* BrightnessTimer.swift */; }; + DCA3DF312B28D97000554A8A /* WakeupLights.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA3DF302B28D97000554A8A /* WakeupLights.swift */; }; DCA626B12B3083C900BBDD91 /* ServiceNames.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = DCA626B02B3083C900BBDD91 /* ServiceNames.xcstrings */; }; DCAB996A2A816734005FCC29 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = DCAB99692A816734005FCC29 /* README.md */; }; DCBBC7082B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCBBC7072B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift */; }; @@ -75,6 +73,10 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + DC0524D02B3B9C0D002156FC /* JVScripting */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVScripting; path = ../../JVSwift/JVScripting; sourceTree = ""; }; + DC0524D12B3B9F02002156FC /* JVSecurity */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVSecurity; path = ../../JVSwift/JVSecurity; sourceTree = ""; }; + DC0745772B388A6600C8C4CA /* JVNetworking */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVNetworking; path = ../../JVSwift/JVNetworking; sourceTree = ""; }; + DC0745792B388B5C00C8C4CA /* JVSwift */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVSwift; path = ../../JVSwift/JVSwift; sourceTree = ""; }; DC0761AE2A8A9DA3009FA191 /* HomeKitServerPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeKitServerPreview.swift; sourceTree = ""; }; DC0761AF2A8A9DA3009FA191 /* DashboardPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DashboardPreview.swift; sourceTree = ""; }; DC1888342961C0CF00D3363D /* EnableSwitchService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnableSwitchService.swift; sourceTree = ""; }; @@ -87,8 +89,6 @@ DC342E0B2AEE74EF00C578A2 /* PLCAccessoryDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PLCAccessoryDelegate.swift; sourceTree = ""; }; DC342E0D2AEE757E00C578A2 /* CyclicPoller.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CyclicPoller.swift; sourceTree = ""; }; DC3626EF256D9B0800F0AC35 /* HomeKitServerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeKitServerView.swift; sourceTree = ""; }; - DC4B0FFA29C5139300EA6E47 /* Awake */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Awake; path = "../../../../Third Party Code/Awake"; sourceTree = ""; }; - DC4B0FFB29C5175F00EA6E47 /* ClibModbus */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = ClibModbus; path = "../../../../Third Party Code/C-based Packages/System-libraries/ClibModbus"; sourceTree = ""; }; DC4D66792A956AC1004C9D30 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; DC4D667B2A9571BA004C9D30 /* AccessoryNames.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = AccessoryNames.xcstrings; sourceTree = ""; }; DC4D6DD325710B410087E51D /* MainConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainConfiguration.swift; sourceTree = ""; }; @@ -101,12 +101,14 @@ DC6DEE202B2E5EE300BDAB31 /* FunctionKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionKey.swift; sourceTree = ""; }; DC6E461523B75BF8006A3E61 /* ToDo.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = ToDo.md; sourceTree = ""; }; DC746D7225ACA97A00B33314 /* SmartSprinklerAccessory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmartSprinklerAccessory.swift; sourceTree = ""; }; - DC7FB14026C99142003CAFB0 /* JVCocoa */ = {isa = PBXFileReference; lastKnownFileType = folder; name = JVCocoa; path = ../../JVCocoa; sourceTree = ""; }; + DC880BAF2B3B816800CC3468 /* JVSwiftCore */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVSwiftCore; path = ../../JVSwift/JVSwiftCore; sourceTree = ""; }; + DC8EEE002B3F5AF200434343 /* JVData */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVData; path = ../../JVSwift/JVData; sourceTree = ""; }; + DC8EEE042B3F6F4A00434343 /* JVUI */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVUI; path = ../../JVSwift/JVUI; sourceTree = ""; }; + DC8EEE062B3F803D00434343 /* JVLocation */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVLocation; path = ../../JVSwift/JVLocation; sourceTree = ""; }; DC90DA5E2732817000068839 /* TizenSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TizenSettingsView.swift; sourceTree = ""; }; DC90DA5F2732817000068839 /* PreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = ""; }; DC90DA612732817000068839 /* GeneralSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralSettingsView.swift; sourceTree = ""; }; DC90DA622732817000068839 /* SunnyPortalSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SunnyPortalSettingsView.swift; sourceTree = ""; }; - DC90DA632732817000068839 /* WeatherKitSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeatherKitSettingsView.swift; sourceTree = ""; }; DC90DA6B2732828200068839 /* DashboardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DashboardView.swift; sourceTree = ""; }; DC980DF02ABBACC2006EAE8D /* HAP */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = HAP; path = "../../../../Third Party Code/HAP"; sourceTree = ""; }; DC9963882AD3073000915EAC /* CirtcuitEnabler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CirtcuitEnabler.swift; sourceTree = ""; }; @@ -115,9 +117,10 @@ DC9EFF2224F3131F001B70EE /* DoorLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoorLock.swift; sourceTree = ""; }; DCA37CF626BB29EF003CC3BF /* HAPiNestTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HAPiNestTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; DCA37CF826BB29EF003CC3BF /* HAPTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HAPTests.swift; sourceTree = ""; }; - DCA3DF302B28D97000554A8A /* BrightnessTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrightnessTimer.swift; sourceTree = ""; }; + DCA3DF302B28D97000554A8A /* WakeupLights.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WakeupLights.swift; sourceTree = ""; }; DCA626B02B3083C900BBDD91 /* ServiceNames.xcstrings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json.xcstrings; path = ServiceNames.xcstrings; sourceTree = ""; }; DCAB99692A816734005FCC29 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + DCB31F4D2B40160300585B13 /* JVWeather */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = JVWeather; path = ../../JVSwift/JVWeather; sourceTree = ""; }; DCBBC7072B2EE4D80005EE6E /* StatelessProgrammableSwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatelessProgrammableSwitch.swift; sourceTree = ""; }; DCBCFB5429678657000F0376 /* Enums.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enums.swift; sourceTree = ""; }; DCC01B5725B434BC00984BB5 /* SmartSprinkler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmartSprinkler.swift; sourceTree = ""; }; @@ -148,7 +151,6 @@ DC0719DB27CEBF0700AF0D68 /* HAP in Frameworks */, DC2C19BF26BB3BD400A3AB83 /* HAP in Frameworks */, DCF031E426868C8B005B5E88 /* LeafDriver in Frameworks */, - DC846E3A26D996E9002CEDFB /* JVCocoa in Frameworks */, DCF031EA2687064D005B5E88 /* TizenDriver in Frameworks */, DCF031E226868C87005B5E88 /* ModbusDriver in Frameworks */, DCF031E62687061B005B5E88 /* SoftPLC in Frameworks */, @@ -209,7 +211,7 @@ DC90DA6A2732828200068839 /* MainWindow */, DC90DA5D2732817000068839 /* PreferencesWindow */, DC2D4572256E486D00DF3EC5 /* HomekitServer */, - DCA3DF322B28D99000554A8A /* AmbientLightning */, + DCA3DF322B28D99000554A8A /* ServerBasedAutomations */, ); path = HAPiNest; sourceTree = ""; @@ -284,7 +286,6 @@ DC90DA5F2732817000068839 /* PreferencesView.swift */, DC90DA612732817000068839 /* GeneralSettingsView.swift */, DC90DA622732817000068839 /* SunnyPortalSettingsView.swift */, - DC90DA632732817000068839 /* WeatherKitSettingsView.swift */, ); path = PreferencesWindow; sourceTree = ""; @@ -306,12 +307,12 @@ path = HAPiNestTests; sourceTree = ""; }; - DCA3DF322B28D99000554A8A /* AmbientLightning */ = { + DCA3DF322B28D99000554A8A /* ServerBasedAutomations */ = { isa = PBXGroup; children = ( - DCA3DF302B28D97000554A8A /* BrightnessTimer.swift */, + DCA3DF302B28D97000554A8A /* WakeupLights.swift */, ); - path = AmbientLightning; + path = ServerBasedAutomations; sourceTree = ""; }; DCA8FA1B25B2FE0900532E4A /* Custom Accessories */ = { @@ -357,6 +358,15 @@ DCFB54682683F12300406E12 /* Packages */ = { isa = PBXGroup; children = ( + DCB31F4D2B40160300585B13 /* JVWeather */, + DC8EEE062B3F803D00434343 /* JVLocation */, + DC8EEE042B3F6F4A00434343 /* JVUI */, + DC8EEE002B3F5AF200434343 /* JVData */, + DC0524D12B3B9F02002156FC /* JVSecurity */, + DC0524D02B3B9C0D002156FC /* JVScripting */, + DC880BAF2B3B816800CC3468 /* JVSwiftCore */, + DC0745792B388B5C00C8C4CA /* JVSwift */, + DC0745772B388A6600C8C4CA /* JVNetworking */, DC980DF02ABBACC2006EAE8D /* HAP */, DCFB546B2683F1B500406E12 /* SoftPLC */, DCFB546D2683F20C00406E12 /* LeafDriver */, @@ -364,9 +374,6 @@ DCFB546F2683F25D00406E12 /* ModbusDriver */, DCFB54702683F28400406E12 /* SiriDriver */, DCFB54712683F2A800406E12 /* TizenDriver */, - DC7FB14026C99142003CAFB0 /* JVCocoa */, - DC4B0FFA29C5139300EA6E47 /* Awake */, - DC4B0FFB29C5175F00EA6E47 /* ClibModbus */, ); name = Packages; sourceTree = ""; @@ -394,7 +401,6 @@ DCF031E92687064D005B5E88 /* TizenDriver */, DC0CA92B268BBD0700E5AB06 /* MilightDriver */, DC2C19BE26BB3BD400A3AB83 /* HAP */, - DC846E3926D996E9002CEDFB /* JVCocoa */, DC0719DA27CEBF0700AF0D68 /* HAP */, ); productName = HAPiNest2; @@ -427,7 +433,7 @@ attributes = { BuildIndependentTargetsInParallel = YES; LastSwiftUpdateCheck = 1300; - LastUpgradeCheck = 1500; + LastUpgradeCheck = 1510; ORGANIZATIONNAME = "Jan Verrept"; TargetAttributes = { DC54B6BA2570586D00D9169F = { @@ -517,10 +523,9 @@ DC90DA642732817000068839 /* TizenSettingsView.swift in Sources */, DC54B6F42570628B00D9169F /* HomeKitServer.swift in Sources */, DC4D6DE225713CD00087E51D /* DimmableLight.swift in Sources */, - DCA3DF312B28D97000554A8A /* BrightnessTimer.swift in Sources */, + DCA3DF312B28D97000554A8A /* WakeupLights.swift in Sources */, DCD592F9270A6C2600882387 /* HAPiNestApp.swift in Sources */, DC90DA652732817000068839 /* PreferencesView.swift in Sources */, - DC90DA692732817000068839 /* WeatherKitSettingsView.swift in Sources */, DC1888352961C0CF00D3363D /* EnableSwitchService.swift in Sources */, DC4D6DEB25713CDA0087E51D /* GarageDoor.swift in Sources */, DC4D6DE525713CD40087E51D /* Light.swift in Sources */, @@ -559,6 +564,7 @@ ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = ""; ASSETCATALOG_COMPILER_OPTIMIZATION = ""; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; @@ -640,6 +646,7 @@ ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = ""; ASSETCATALOG_COMPILER_OPTIMIZATION = ""; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; @@ -891,10 +898,6 @@ isa = XCSwiftPackageProductDependency; productName = HAP; }; - DC846E3926D996E9002CEDFB /* JVCocoa */ = { - isa = XCSwiftPackageProductDependency; - productName = JVCocoa; - }; DCF031E126868C87005B5E88 /* ModbusDriver */ = { isa = XCSwiftPackageProductDependency; productName = ModbusDriver; diff --git a/HAPiNest.xcodeproj/project.xcworkspace/xcuserdata/janverrept.xcuserdatad/Bookmarks/bookmarks.plist b/HAPiNest.xcodeproj/project.xcworkspace/xcuserdata/janverrept.xcuserdatad/Bookmarks/bookmarks.plist index e608e5b0f..9ec1e53ac 100644 --- a/HAPiNest.xcodeproj/project.xcworkspace/xcuserdata/janverrept.xcuserdatad/Bookmarks/bookmarks.plist +++ b/HAPiNest.xcodeproj/project.xcworkspace/xcuserdata/janverrept.xcuserdatad/Bookmarks/bookmarks.plist @@ -15,13 +15,13 @@ EndingColumnNumber 0 EndingLineNumber - 44 + 43 StartingColumnNumber 0 StartingLineNumber - 43 + 42 Timestamp - 715212911.326378 + 725587463.3800499 rebasable-url @@ -104,15 +104,15 @@ location-parameters EndingColumnNumber - 32 + 0 EndingLineNumber - 55 + 57 StartingColumnNumber - 16 + 0 StartingLineNumber - 55 + 56 Timestamp - 721503133.0704319 + 725583443.55126 rebasable-url @@ -128,15 +128,16 @@ text-context focused - updateIsOutdated + private var updateIsOutdated:Bool{ + leading } - private var + trailing - :Bool{ - + let formatter = DateFormatter() + formatter.dateFormat = "yyyy/mm/dd HH:mm" type diff --git a/HAPiNest.xcodeproj/xcshareddata/xcschemes/HAPiNest.xcscheme b/HAPiNest.xcodeproj/xcshareddata/xcschemes/HAPiNest.xcscheme index ea788c78a..5ba54d0fb 100644 --- a/HAPiNest.xcodeproj/xcshareddata/xcschemes/HAPiNest.xcscheme +++ b/HAPiNest.xcodeproj/xcshareddata/xcschemes/HAPiNest.xcscheme @@ -1,6 +1,6 @@ isShown orderHint - 12 + 24 Demo (Playground) 10.xcscheme isShown orderHint - 29 + 33 Demo (Playground) 11.xcscheme isShown orderHint - 30 + 34 Demo (Playground) 12.xcscheme isShown orderHint - 31 + 35 Demo (Playground) 13.xcscheme isShown orderHint - 32 + 36 Demo (Playground) 14.xcscheme isShown orderHint - 33 + 37 Demo (Playground) 15.xcscheme @@ -202,63 +202,63 @@ isShown orderHint - 13 + 25 Demo (Playground) 3.xcscheme isShown orderHint - 22 + 26 Demo (Playground) 4.xcscheme isShown orderHint - 23 + 27 Demo (Playground) 5.xcscheme isShown orderHint - 24 + 28 Demo (Playground) 6.xcscheme isShown orderHint - 25 + 29 Demo (Playground) 7.xcscheme isShown orderHint - 26 + 30 Demo (Playground) 8.xcscheme isShown orderHint - 27 + 31 Demo (Playground) 9.xcscheme isShown orderHint - 28 + 32 Demo (Playground).xcscheme isShown orderHint - 11 + 23 DevelopmentTesting (Playground) 1.xcscheme @@ -317,7 +317,7 @@ HAPiNest.xcscheme_^#shared#^_ orderHint - 1 + 14 HAPiNestMP (iOS).xcscheme_^#shared#^_ diff --git a/HAPiNest/AmbientLightning/BrightnessTimer.swift b/HAPiNest/AmbientLightning/BrightnessTimer.swift deleted file mode 100644 index 879030de2..000000000 --- a/HAPiNest/AmbientLightning/BrightnessTimer.swift +++ /dev/null @@ -1,78 +0,0 @@ -// -// BrightnessTimer.swift -// HAPiNest -// -// Created by Jan Verrept on 12/12/2023. -// Copyright © 2023 Jan Verrept. All rights reserved. -// - -import Foundation -import SoftPLC - -protocol DimmedLight:AnyObject{ - - var brightness: Int? { get set } - var brightnessTimer:BrightnessTimer!{get set} - -} - -// Foundation.Timer is a class cluster and it's not meant to be subclassed directly, so -// use a helper class a.k.a. composition instead of inheritance. -class BrightnessTimer { - - weak var dimmer: DimmedLight? - - // Public property for time to make a 100% change - public var timeFor100percentChange: TimeInterval = 5.0 // Default to 5 seconds for a full range change - private var timer: Timer? - - var targetBrightness: Int? { - didSet { - if targetBrightness == nil { - stop() - } else if targetBrightness != oldValue { - adjustBrightness() - } - } - } - - var brightness: Int { - get { return dimmer?.brightness ?? 0} - set { dimmer?.brightness = newValue } - } - - init(dimmer: DimmedLight) { - self.dimmer = dimmer - } - - private func adjustBrightness() { - // Interval for 1% change - let intervalForOnePercentChange = timeFor100percentChange / 100 - - timer?.invalidate() // Invalidate any existing timer - timer = Timer.scheduledTimer(withTimeInterval: intervalForOnePercentChange, repeats: true) { [weak self] _ in - guard let self = self, let targetBrightness = self.targetBrightness else { return } - -#warning("DEBUGPRINT") // TODO: - remove temp print statement - print("🐞\tI'm dimming") - - // Apply a small 1% adjustment at the time in either direction - let onePercentAdjustment:Int = (targetBrightness >= self.brightness) ? 1 : -1 - - // Apply the adjustment only if it does not overshoot the target brightness - if (onePercentAdjustment == 1) && (self.brightness+onePercentAdjustment >= targetBrightness) || - (onePercentAdjustment == -1) && (self.brightness+onePercentAdjustment <= targetBrightness) { - self.brightness = targetBrightness - self.stop() - } else { - self.brightness += onePercentAdjustment - } - - } - } - - func stop() { - timer?.invalidate() - timer = nil - } -} diff --git a/HAPiNest/CyclicPoller.swift b/HAPiNest/CyclicPoller.swift index 10d7ce045..2f4bba5f8 100644 --- a/HAPiNest/CyclicPoller.swift +++ b/HAPiNest/CyclicPoller.swift @@ -7,7 +7,7 @@ // import Foundation -import JVCocoa +import JVSwift import SoftPLC // Any object that can be polled for changes in a timely matter diff --git a/HAPiNest/HAPiNestApp.swift b/HAPiNest/HAPiNestApp.swift index 01fcad03b..373b54c59 100644 --- a/HAPiNest/HAPiNestApp.swift +++ b/HAPiNest/HAPiNestApp.swift @@ -10,7 +10,9 @@ import Foundation import Cocoa import SwiftUI import OSLog -import JVCocoa +import JVSwift +import JVSwiftCore +import JVNetworking import HAP import SoftPLC import ModbusDriver @@ -22,77 +24,80 @@ import ModbusDriver /// The main accessory-delegate in this App is a SoftPLC that can interact with the outside world by means of a number of Input and Output-modules. @main struct HAPiNestApp: App { - @Environment(\.scenePhase) var scenePhase - - let appNapController: AppNapController = AppNapController.shared - - let homekitServer:HomeKitServer = HomeKitServer.shared - let plc:SoftPLC = SoftPLC(hardwareConfig:MainConfiguration.PLC.HardwareConfig, ioList: MainConfiguration.PLC.IOList, simulator:ModbusSimulator()) - let cyclicPoller:CyclicPoller = CyclicPoller(timeInterval: 1.0) - - init() { - - let bridgename = MainConfiguration.HomeKitServer.BridgeName - let setupCode = MainConfiguration.HomeKitServer.BridgeSetupCode - let configFile = MainConfiguration.HomeKitServer.BridgeConfigFile - - homekitServer.mainBridge = Bridge( - name:bridgename, - setupCode:setupCode, - accessories: MainConfiguration.Accessories.map{accessory, delegate in return accessory}, - configfileName: configFile - ) - - plc.plcObjects = MainConfiguration.PLC.PLCobjects + @SwiftUI.Environment(\.scenePhase) var scenePhase + + let appNapController: AppNapController = AppNapController.shared + + let homekitServer:HomeKitServer = HomeKitServer.shared + + let mqttCLient = MQTTClient() + + let plc:SoftPLC = SoftPLC(hardwareConfig:MainConfiguration.PLC.HardwareConfig, ioList: MainConfiguration.PLC.IOList, simulator:ModbusSimulator()) + let cyclicPoller:CyclicPoller = CyclicPoller(timeInterval: 1.0) + + init() { + + let bridgename = MainConfiguration.HomeKitServer.BridgeName + let setupCode = MainConfiguration.HomeKitServer.BridgeSetupCode + let configFile = MainConfiguration.HomeKitServer.BridgeConfigFile + + homekitServer.mainBridge = Bridge( + name:bridgename, + setupCode:setupCode, + accessories: MainConfiguration.Accessories.map{accessory, delegate in return accessory}, + configfileName: configFile + ) + mqttCLient.connect() + + plc.plcObjects = MainConfiguration.PLC.PLCobjects #if DEBUG - plc.toggleSimulator(true) - plc.toggleHardwareSimulation(true) + plc.toggleSimulator(true) + plc.toggleHardwareSimulation(true) #else - plc.toggleSimulator(false) + plc.toggleSimulator(false) #endif - // Only fire Up PLC after all components are initialized - plc.run() - - // Start an extra background cycle indepedent of the PLCs backroundCycle - // (to poll for harware changes on behalf of the other type acessoryDelegates) - cyclicPoller.run() - -} - - - var body: some Scene { - WindowGroup("HAPiNest dashboard 🛋") { - DashboardView( - serverView: homekitServer.dashboard!, - plcView: plc.controlPanel - ) - .padding() - .background(Color.Neumorphic.main) - .onAppear(perform: { - }) - } - .onChange(of: scenePhase) { - let logger = Logger(subsystem: "be.oneclick.HAPiNest", category:.lifeCycle) - switch scenePhase { - case .active: - logger.info("App became active") - case .inactive: - logger.info("App became inactive") - case .background: - logger.info("App sent to background") - @unknown default: - break - } - } - - // PreferenceWindow + // Only fire Up PLC after all components are initialized + plc.run() + + // Start an extra background cycle indepedent of the PLCs backroundCycle + // (to poll for harware changes on behalf of the other type acessoryDelegates) + cyclicPoller.run() + } + + + var body: some Scene { + WindowGroup("HAPiNest dashboard 🛋") { + DashboardView( + serverView: homekitServer.dashboard!, + plcView: plc.controlPanel + ) + .padding() + .background(Color.Neumorphic.main) + .onAppear(perform: { + }) + } + .onChange(of: scenePhase) { + let logger = Logger(subsystem: "be.oneclick.HAPiNest", category:.lifeCycle) + switch scenePhase { + case .active: + logger.info("App became active") + case .inactive: + logger.info("App became inactive") + case .background: + logger.info("App sent to background") + @unknown default: + break + } + } + + // PreferenceWindow #if os(macOS) - Settings{ - PreferencesView() - } + Settings{ + PreferencesView() + } #endif - } - + } + } diff --git a/HAPiNest/HomekitServer/HomeKitServer.swift b/HAPiNest/HomekitServer/HomeKitServer.swift index 20a3b021f..23501feda 100644 --- a/HAPiNest/HomekitServer/HomeKitServer.swift +++ b/HAPiNest/HomekitServer/HomeKitServer.swift @@ -9,7 +9,9 @@ import Foundation import HAP import Darwin -import JVCocoa +import JVSwift +import JVSwiftCore +import JVScripting import OSLog import SwiftUI import LeafDriver @@ -37,8 +39,8 @@ public class HomeKitServer:Singleton{ self.name = MainConfiguration.HomeKitServer.ServerName #if DEBUG - AppController(name: "Console", location: .systemUtilities).startIfInstalled() - AppController(name: "Home", location: .systemApps).startIfInstalled() + AppController(name: "Console", location: .systemUtilities, terminal: TerminalDriver()).startIfInstalled() + AppController(name: "Home", location: .systemApps, terminal: TerminalDriver()).startIfInstalled() #endif let logger = Logger(subsystem: "be.oneclick.HAPiNest", category: "HomeKitServer") logger.info("Initializing the server \(self.name, privacy: .public)...") diff --git a/HAPiNest/PreferencesWindow/PreferencesView.swift b/HAPiNest/PreferencesWindow/PreferencesView.swift index 97dabdba6..4cd8ef135 100644 --- a/HAPiNest/PreferencesWindow/PreferencesView.swift +++ b/HAPiNest/PreferencesWindow/PreferencesView.swift @@ -8,6 +8,8 @@ import SwiftUI import LeafDriver +import WeatherKit +import JVWeather struct PreferencesView: View { @@ -21,11 +23,11 @@ struct PreferencesView: View { .tabItem { Label("Samsung Tizen", systemImage: "tv") } - LeafSettingsView() + LeafDriver.LeafSettingsView() .tabItem { Label("Nissan Leaf", systemImage: "bolt.car") } - WeatherKitSettingsView() + WeatherService.CreditsView() .tabItem { Label("WeatherKit", systemImage: "cloud.sun.rain") } diff --git a/HAPiNest/PreferencesWindow/WeatherKitSettingsView.swift b/HAPiNest/PreferencesWindow/WeatherKitSettingsView.swift deleted file mode 100644 index ee9ad8f23..000000000 --- a/HAPiNest/PreferencesWindow/WeatherKitSettingsView.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// OpenWeatherSettingsView.swift -// HAPiNest -// -// Created by Jan Verrept on 02/10/2021. -// Copyright © 2021 Jan Verrept. All rights reserved. -// - -import SwiftUI -import OSLog -import WeatherKit - -struct WeatherKitSettingsView: View { - - var body: some View { - WeatherService.CreditsView() - } - -} - -#Preview { - WeatherKitSettingsView() -} diff --git a/HAPiNest/ServerBasedAutomations/ExternalEventsServer/JVMQTTClient.swift b/HAPiNest/ServerBasedAutomations/ExternalEventsServer/JVMQTTClient.swift new file mode 100644 index 000000000..57a95babb --- /dev/null +++ b/HAPiNest/ServerBasedAutomations/ExternalEventsServer/JVMQTTClient.swift @@ -0,0 +1,104 @@ +import CocoaMQTT + +// [MQTTClient].swift +// +// A blend of human creativity by TheMisfit68 and +// AI assistance from ChatGPT. +// Crafting the future, one line of Swift at a time. +// Copyright © 2023 Jan Verrept. All rights reserved. + +import Foundation +import CocoaMQTT +import OSLog + +class MQTTClient { + private var mqtt: CocoaMQTT! + private let logger = Logger(subsystem: "Test", category: "MQTT") // Create an OSLog object + + init() { + let clientID = "TheHAPINestServer" + mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883) + mqtt.delegate = self + mqtt.keepAlive = 60 + } + + public func connect() { + let isConnected = mqtt.connect() + + if isConnected { + logger.info("Connecting successful") // Log info level message + } else { + logger.error("Connecting failed") // Log error level message + } + } + + public func publish(topic: String, message: String) { + mqtt.publish(topic, withString: message) + logger.info("Published message to topic: \(topic)") // Log info level message + } + + public func subscribe(topic: String) { + logger.info("Subscribin to topic: \(topic)") // Log info level message + mqtt.subscribe(topic) + } +} + +extension MQTTClient: CocoaMQTTDelegate { + + func mqttDidPing(_ mqtt: CocoaMQTT) { + logger.info("MQTT did ping") + } + + func mqttDidReceivePong(_ mqtt: CocoaMQTT) { + logger.info("MQTT did receive pong") + } + + func mqtt(_ mqtt: CocoaMQTT, didConnect host: String, port: Int) { + logger.info("connected to \(host) on port \(port)") + } + + func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) { + self.subscribe(topic: "ToServer") + if ack == .accept { + } + logger.info("MQTT did receive connection acknowledgement: \(ack)") + } + + func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) { + logger.info("did publish message with id \(id)") + } + + func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) { + logger.info("did publish ack with id \(id)") + } + + func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) { + logger.info("did receive message with id \(id):\n\(message.string!)") + self.publish(topic: "FromServer", message: "I heard you loud and clear:\nmessage:\(message.string!)") + } + + func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String, messageID: UInt16) { + logger.info("did subscribe to topic \(topic)") + } + + func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) { + logger.info("did subscribe to topics") + } + + func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) { + logger.info("did unsubscribe from topic \(topic)") + } + + func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) { + logger.info("did unsubscribe from topics \(topics)") + } + + func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) { + logger.error("mqtt did disconnect \(err)") + + // Attempt to reconnect after 5 seconds + DispatchQueue.main.asyncAfter(deadline: .now() + 5) { + let _ = self.connect() + } + } +} diff --git a/HAPiNest/ServerBasedAutomations/WakeupLights.swift b/HAPiNest/ServerBasedAutomations/WakeupLights.swift new file mode 100644 index 000000000..bbdcca66f --- /dev/null +++ b/HAPiNest/ServerBasedAutomations/WakeupLights.swift @@ -0,0 +1,73 @@ +// +// BrightnessTimer.swift +// HAPiNest +// +// Created by Jan Verrept on 12/12/2023. +// Copyright © 2023 Jan Verrept. All rights reserved. +// + +import Foundation +import SoftPLC + +class WakeupLights:PLCClass{ + + override init() { + let bedRoomLightKey = String(localized:"Bedroom Light", table:"AccessoryNames") + let bedRoomLight = MainConfiguration.PLC.PLCobjects[bedRoomLightKey] as! DimmableLight + } + +} + +// Foundation.Timer is a class cluster and it's not meant to be subclassed directly, so +// use a helper class a.k.a. composition instead of inheritance. +class BrightnessTimer { + + // Public property for time to make a 100% change + private var timer: Timer? + + private func adjust(brightness:inout Int?, to targetBrightness:Int?, over totalTime:TimeInterval = 5.0){ + + if let previousBrightness = brightness, let targetBrightness = targetBrightness, brightness != targetBrightness { + + // Interval for 1% change + let intervalForOnePercentChange = totalTime/100 + + var newBrightness:Int = previousBrightness + + timer?.invalidate() // Invalidate any existing timer + timer = Timer.scheduledTimer(withTimeInterval: intervalForOnePercentChange, repeats: true) { [weak self] _ in + guard let self = self else { return } + + // Apply a small 1% adjustment at the time in either direction + // ensuring the result doesn't overshoot the targetBrighttness + if (targetBrightness > previousBrightness){ + if (previousBrightness+1 >= targetBrightness){ + newBrightness = targetBrightness + self.stop() + }else{ + newBrightness += 1 + } + }else if (targetBrightness < previousBrightness) { + if (previousBrightness-1 <= targetBrightness) { + newBrightness = targetBrightness + self.stop() + }else{ + newBrightness -= 1 + } + }else { + self.stop() + } + } + brightness = newBrightness + + }else{ + stop() + } + } + + func stop() { + timer?.invalidate() + timer = nil + } + +} diff --git a/HAPiNestTests/HAPTests.swift b/HAPiNestTests/HAPTests.swift index 54248a75d..6d3f5c5fc 100644 --- a/HAPiNestTests/HAPTests.swift +++ b/HAPiNestTests/HAPTests.swift @@ -8,7 +8,7 @@ import Foundation import HAP import XCTest -import JVCocoa +import JVSwift class HAPtests: XCTestCase { diff --git a/MainConfiguration.swift b/MainConfiguration.swift index 90177a666..3e82963af 100644 --- a/MainConfiguration.swift +++ b/MainConfiguration.swift @@ -7,7 +7,7 @@ // import Cocoa -import JVCocoa +import JVSwift import HAP import SoftPLC diff --git a/PLCConfiguration.swift b/PLCConfiguration.swift index a49fee13d..2ec5a54ba 100644 --- a/PLCConfiguration.swift +++ b/PLCConfiguration.swift @@ -228,6 +228,8 @@ extension MainConfiguration{ String(localized:"Front Door", table:"AccessoryNames") : Doorlock(), String(localized:"Garage Door", table:"AccessoryNames") : GarageDoor(), String(localized:"Smart Sprinklers", table:"AccessoryNames") : SmartSprinkler(), + + ] } diff --git a/ToDo.md b/ToDo.md index 75feaaebd..6bc272014 100644 --- a/ToDo.md +++ b/ToDo.md @@ -1,15 +1,15 @@ # ToDo for HAPiNest ## A personal Home Automation System - -- [ ] Complete the leafDriver1s ACController with temperature setpoint if Nissan developer account gets granted) +Remove archieved remote for JVCocoa on github - [ ] Create Preference view/container for each module, -use the leafsettingsView and to a lesser extend the WeatherService.CreditsView as an example +use the leafsettingsView and to a lesser extend the WeatherService.CreditsView as an example (the views should be embedded by extensions in the main driver type} use those views directly as the tabs from the apps prefs/settings - view - [ ] Create a Display/View/Container for each module where applicable +- [ ] Complete the leafDriver1s ACController with temperature setpoint if Nissan developer account gets granted) - [ ] Write DOCC files For Siridriver