diff --git a/Soyuz.xcodeproj/project.pbxproj b/Soyuz.xcodeproj/project.pbxproj index 8a7d829..a94af93 100644 --- a/Soyuz.xcodeproj/project.pbxproj +++ b/Soyuz.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ E124B9D929941A4D00C0D2D2 /* PrinterConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */; }; E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */; }; - E16378B429A491E6002F05E9 /* PrinterRequestsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */; }; + E16378B429A491E6002F05E9 /* PrinterRequestManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */; }; E180B5E92992CD9100425DB0 /* SoyuzApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B5E82992CD9100425DB0 /* SoyuzApp.swift */; }; E180B5ED2992CD9200425DB0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EC2992CD9200425DB0 /* Assets.xcassets */; }; E180B5F02992CD9200425DB0 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EF2992CD9200425DB0 /* Preview Assets.xcassets */; }; @@ -46,7 +46,7 @@ E124B9D72993FE5500C0D2D2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = ""; }; E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterConfigView.swift; sourceTree = ""; }; E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzScratchTests.swift; sourceTree = ""; }; - E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterRequestsManagerTests.swift; sourceTree = ""; }; + E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterRequestManagerTests.swift; sourceTree = ""; }; E180B5E52992CD9100425DB0 /* Soyuz.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Soyuz.app; sourceTree = BUILT_PRODUCTS_DIR; }; E180B5E82992CD9100425DB0 /* SoyuzApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzApp.swift; sourceTree = ""; }; E180B5EC2992CD9200425DB0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -143,7 +143,7 @@ children = ( E180B5FF2992CD9300425DB0 /* SoyuzTests.swift */, E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */, - E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */, + E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */, ); path = SoyuzTests; sourceTree = ""; @@ -309,7 +309,7 @@ buildActionMask = 2147483647; files = ( E180B6002992CD9300425DB0 /* SoyuzTests.swift in Sources */, - E16378B429A491E6002F05E9 /* PrinterRequestsManagerTests.swift in Sources */, + E16378B429A491E6002F05E9 /* PrinterRequestManagerTests.swift in Sources */, E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Soyuz.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Soyuz.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 3128429..919434a 100644 --- a/Soyuz.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/Soyuz.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/Soyuz.xcodeproj/xcuserdata/madelinecr.xcuserdatad/xcschemes/xcschememanagement.plist b/Soyuz.xcodeproj/xcuserdata/madelinecr.xcuserdatad/xcschemes/xcschememanagement.plist index bf4945b..c865a91 100644 --- a/Soyuz.xcodeproj/xcuserdata/madelinecr.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Soyuz.xcodeproj/xcuserdata/madelinecr.xcuserdatad/xcschemes/xcschememanagement.plist @@ -9,6 +9,11 @@ orderHint 0 + Soyuz.xcscheme_^#shared#^_ + + orderHint + 0 + diff --git a/Soyuz/PrinterConfigView.swift b/Soyuz/PrinterConfigView.swift index a374f0b..6396ecf 100644 --- a/Soyuz/PrinterConfigView.swift +++ b/Soyuz/PrinterConfigView.swift @@ -9,7 +9,7 @@ import SwiftUI import Network struct PrinterConfigView: View { - @ObservedObject var printerManager = PrinterRequestManager.shared + @ObservedObject var printerManager: PrinterRequestManager var body: some View { VStack { @@ -52,8 +52,10 @@ struct PrinterConfigView: View { } struct PrinterConfigView_Previews: PreviewProvider { + @State static var printerManager = PrinterRequestManager() + static var previews: some View { - PrinterConfigView() + PrinterConfigView(printerManager: printerManager) } } diff --git a/Soyuz/PrinterRequestManager.swift b/Soyuz/PrinterRequestManager.swift index d37ea9a..fb01a0a 100644 --- a/Soyuz/PrinterRequestManager.swift +++ b/Soyuz/PrinterRequestManager.swift @@ -7,6 +7,7 @@ import Foundation import Network +import AppKit import Starscream // MARK: Bonjour Protocol @@ -17,13 +18,22 @@ protocol NetworkDiscoveryEngine { func setStateUpdateHandler(_ handler: @escaping ((NWBrowser.State) -> Void)) } +//protocol NDEResult { +// var endpoint: NWEndpoint { get } +// var interface: [NWInterface] { get } +// var metadata: NWBrowser.Result.Metadata { get } +//} + extension NWBrowser: NetworkDiscoveryEngine { +//class NDEngine: NetworkDiscoveryEngine, func startScan(queue: DispatchQueue) { start(queue: queue) } + func setBrowseResultsChangedHandler(_ handler: @escaping ((Set, Set) -> Void)) { self.browseResultsChangedHandler = handler } + func setStateUpdateHandler(_ handler: @escaping ((State) -> Void)) { self.stateUpdateHandler = handler } @@ -50,8 +60,6 @@ struct JsonRpcRequest: Codable { class PrinterRequestManager: ObservableObject, WebSocketDelegate { let WEBSOCKET_TIMEOUT_INTERVAL: TimeInterval = 60.0 - static let shared = PrinterRequestManager() - // Debug stuff let startDate = Date() let startDateString: String @@ -96,8 +104,6 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate { var socket: WebSocket? var lastPingDate = Date() - // TODO: Set this up to actually reconnect - // Parse a JSON-RPC query-response message func parse_response(_ response: jsonRpcResponse) { state = response.result.status.print_stats?.state ?? "" @@ -124,7 +130,7 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate { } } - private init(browser: NetworkDiscoveryEngine = NWBrowser(for: .bonjourWithTXTRecord(type: "_moonraker._tcp", domain: "local."), using: .tcp)) { + init(browser: NetworkDiscoveryEngine = NWBrowser(for: .bonjourWithTXTRecord(type: "_moonraker._tcp", domain: "local."), using: .tcp)) { state = "" progress = 0.0 extruderTemperature = 0.0 @@ -167,6 +173,38 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate { }) // Start up the bonjour browser, get results and process them in the update handler nwBrowser.startScan(queue: DispatchQueue.main) + + // Screen sleep functionality +// NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(screenDidSleep(_:)), name: NSWorkspace.screensDidSleepNotification, object: nil) +// NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(screenDidWake(_:)), name: NSWorkspace.screensDidWakeNotification, object: nil) + let center = NSWorkspace.shared.notificationCenter; + let mainQueue = OperationQueue.main + + center.addObserver(forName: NSWorkspace.screensDidWakeNotification, object: nil, queue: mainQueue) { notification in + self.screenChangedSleepState(notification) + } + + center.addObserver(forName: NSWorkspace.screensDidSleepNotification, object: nil, queue: mainQueue) { notification in + self.screenChangedSleepState(notification) + } + } + + func screenChangedSleepState(_ notification: Notification) { + switch(notification.name) { + case NSWorkspace.screensDidSleepNotification: + socket?.disconnect() + case NSWorkspace.screensDidWakeNotification: + self.openWebsocket() + default: + return + } + } + + func screenDidWake(_ notification: Notification) { + print("Screen woke: \(notification.name)") + if socket != nil { + self.openWebsocket() + } } // Called from the UI with an endpoint. diff --git a/Soyuz/SoyuzApp.swift b/Soyuz/SoyuzApp.swift index 22d85b9..31b9fab 100644 --- a/Soyuz/SoyuzApp.swift +++ b/Soyuz/SoyuzApp.swift @@ -13,6 +13,8 @@ struct SoyuzApp: App { @State var currentIcon = "move.3d" + @ObservedObject var printerManager = PrinterRequestManager() + var body: some Scene { // WindowGroup(id: "floating-stats") { // KlipperMonMenuBarExtraView(currentMenuBarIcon: $currentIcon) @@ -20,13 +22,13 @@ struct SoyuzApp: App { // } WindowGroup("Configuration", id: "soyuz_cfg", content: { - PrinterConfigView() + PrinterConfigView(printerManager: printerManager) //.frame(minWidth: 300, maxWidth: 600, minHeight: 60, maxHeight: 100) }) .windowResizability(.contentSize) MenuBarExtra("Soyuz", systemImage: currentIcon) { - SoyuzMenuBarExtraView(currentMenuBarIcon: $currentIcon) + SoyuzMenuBarExtraView(printerManager: printerManager, currentMenuBarIcon: $currentIcon) .padding([.top, .leading, .trailing], 8) .padding([.bottom], 6) } diff --git a/Soyuz/SoyuzMenuBarExtraView.swift b/Soyuz/SoyuzMenuBarExtraView.swift index cb9392f..ba7cbec 100644 --- a/Soyuz/SoyuzMenuBarExtraView.swift +++ b/Soyuz/SoyuzMenuBarExtraView.swift @@ -15,7 +15,7 @@ struct SoyuzMenuBarExtraView: View { @Environment(\.openWindow) var openWindow - @ObservedObject var printerManager = PrinterRequestManager.shared + @ObservedObject var printerManager: PrinterRequestManager @State var printPercentage: Double = 0 @@ -100,8 +100,10 @@ struct SoyuzMenuBarExtraView: View { struct KlipperMonMenuBarExtraView_Previews: PreviewProvider { @State static var currentMenuBarIcon = "move.3d" + @State static var printerManager = PrinterRequestManager() + static var previews: some View { - SoyuzMenuBarExtraView(currentMenuBarIcon: $currentMenuBarIcon) + SoyuzMenuBarExtraView(printerManager: printerManager, currentMenuBarIcon: $currentMenuBarIcon) } } diff --git a/SoyuzTests/PrinterRequestManagerTests.swift b/SoyuzTests/PrinterRequestManagerTests.swift new file mode 100644 index 0000000..e394e73 --- /dev/null +++ b/SoyuzTests/PrinterRequestManagerTests.swift @@ -0,0 +1,33 @@ +// +// PrinterRequestsManagerTests.swift +// SoyuzTests +// +// Created by maddiefuzz on 2/21/23. +// + +import XCTest +import Network +@testable import Soyuz + +class PrinterRequestManagerTests: XCTestCase { + var printerRequestManager: PrinterRequestManager? + + var testBonjourListener: NWListener? + + override func setUp() { + printerRequestManager = PrinterRequestManager(browser: NWBrowser(for: .bonjour(type: "_http._tcp", domain: "local."), using: .tcp)) + + // Set up test bonjour server + //let parameters = NWParameters(tls: .none, tcp: NWListener.) + do { + testBonjourListener = try NWListener(using: .tcp, on: .http) + testBonjourListener!.start(queue: DispatchQueue.main) + } catch { + print("Error: \(error)") + } + } + + func testBonjourDiscoveredItemsNotNil() { + XCTAssertNotNil(printerRequestManager?.nwBrowserDiscoveredItems) + } +} diff --git a/SoyuzTests/PrinterRequestsManagerTests.swift b/SoyuzTests/PrinterRequestsManagerTests.swift deleted file mode 100644 index 5d73f97..0000000 --- a/SoyuzTests/PrinterRequestsManagerTests.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// PrinterRequestsManagerTests.swift -// SoyuzTests -// -// Created by maddiefuzz on 2/21/23. -// - -import XCTest -@testable import Soyuz - -class FileHandleMock: FileHandle { - override func write(_ data: Data) { - return - } -} - -class PrinterRequestManagerTests: XCTestCase { - var printerRequestsManager: PrinterRequestManager? - - override func setUp() { - printerRequestsManager = PrinterRequestManager.shared - } - -}