Basic network call implementation
This commit is contained in:
		@@ -2,9 +2,11 @@
 | 
			
		||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 | 
			
		||||
<plist version="1.0">
 | 
			
		||||
<dict>
 | 
			
		||||
    <key>com.apple.security.app-sandbox</key>
 | 
			
		||||
    <true/>
 | 
			
		||||
    <key>com.apple.security.files.user-selected.read-only</key>
 | 
			
		||||
    <true/>
 | 
			
		||||
	<key>com.apple.security.app-sandbox</key>
 | 
			
		||||
	<true/>
 | 
			
		||||
	<key>com.apple.security.files.user-selected.read-only</key>
 | 
			
		||||
	<true/>
 | 
			
		||||
	<key>com.apple.security.network.client</key>
 | 
			
		||||
	<true/>
 | 
			
		||||
</dict>
 | 
			
		||||
</plist>
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,30 @@
 | 
			
		||||
 | 
			
		||||
import SwiftUI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@main
 | 
			
		||||
struct KlipperMonMenuBarApp: App {
 | 
			
		||||
    let persistenceController = PersistenceController.shared
 | 
			
		||||
    
 | 
			
		||||
    @State var currentIcon = "move.3d"
 | 
			
		||||
    
 | 
			
		||||
    var body: some Scene {
 | 
			
		||||
        WindowGroup {
 | 
			
		||||
            ContentView()
 | 
			
		||||
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        MenuBarExtra("Soyuz", systemImage: currentIcon) {
 | 
			
		||||
            KlipperMonMenuBarExtraView(currentMenuBarIcon: $currentIcon)
 | 
			
		||||
        }
 | 
			
		||||
        .menuBarExtraStyle(.window)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
protocol MenuBarExtraIconUpdater {
 | 
			
		||||
    func updateIcon(systemName: String)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct KlipperMonApp: App {
 | 
			
		||||
    let persistenceController = PersistenceController.shared
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										58
									
								
								KlipperMon/KlipperMonMenuBarExtraView.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								KlipperMon/KlipperMonMenuBarExtraView.swift
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
//
 | 
			
		||||
//  KlipperMonMenuBarExtraView.swift
 | 
			
		||||
//  KlipperMon
 | 
			
		||||
//
 | 
			
		||||
//  Created by maddiefuzz on 2/7/23.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import SwiftUI
 | 
			
		||||
 | 
			
		||||
struct KlipperMonMenuBarExtraView: View {
 | 
			
		||||
    @State var printPercentage: Double = 0
 | 
			
		||||
    @Binding var currentMenuBarIcon: String
 | 
			
		||||
    
 | 
			
		||||
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
 | 
			
		||||
    
 | 
			
		||||
    var body: some View {
 | 
			
		||||
        Label(String(printPercentage), systemImage: "thermometer.snowflake.circle")
 | 
			
		||||
            .onReceive(timer) { input in
 | 
			
		||||
                Task {
 | 
			
		||||
                    self.printPercentage = await self.getPrintPercentage()
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        
 | 
			
		||||
        Button("Check Printer") {
 | 
			
		||||
            currentMenuBarIcon = "flame"
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func getPrintPercentage() async -> Double {
 | 
			
		||||
        guard let url = URL(string: "http://10.0.21.39/printer/objects/query?extruder=temperature") else {
 | 
			
		||||
            fatalError("Missing URL")
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        let urlRequest = URLRequest(url: url)
 | 
			
		||||
        do {
 | 
			
		||||
            let (data, response) = try await URLSession.shared.data(for: urlRequest)
 | 
			
		||||
            guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
 | 
			
		||||
                print("Error!")
 | 
			
		||||
                return -1
 | 
			
		||||
            }
 | 
			
		||||
            print(String(data: data, encoding: .utf8))
 | 
			
		||||
            let decoder = JSONDecoder()
 | 
			
		||||
            let printerObjectsQuery = try decoder.decode(PrinterObjectsQuery.self, from: data)
 | 
			
		||||
            return printerObjectsQuery.result.status.extruder.temperature
 | 
			
		||||
            // handle data as JSON
 | 
			
		||||
        } catch {
 | 
			
		||||
            print("Error!")
 | 
			
		||||
            return -1
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct KlipperMonMenuBarExtraView_Previews: PreviewProvider {
 | 
			
		||||
    @State static var currentMenuBarIcon = "move.3d"
 | 
			
		||||
    static var previews: some View {
 | 
			
		||||
        KlipperMonMenuBarExtraView(currentMenuBarIcon: $currentMenuBarIcon)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								KlipperMon/KlipperWebsocket.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								KlipperMon/KlipperWebsocket.swift
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
//
 | 
			
		||||
//  KlipperWebsocket.swift
 | 
			
		||||
//  KlipperMon
 | 
			
		||||
//
 | 
			
		||||
//  Created by maddiefuzz on 2/7/23.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								KlipperMon/PrinterObjectsQuery.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								KlipperMon/PrinterObjectsQuery.swift
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
//
 | 
			
		||||
//  PrinterObjectsQuery.swift
 | 
			
		||||
//  KlipperMon
 | 
			
		||||
//
 | 
			
		||||
//  Created by maddiefuzz on 2/7/23.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
 | 
			
		||||
struct PrinterObjectsQuery: Decodable {
 | 
			
		||||
    let result: ResultsData
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct ResultsData: Decodable {
 | 
			
		||||
    let eventtime: Double
 | 
			
		||||
    let status: StatusData
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct StatusData: Decodable {
 | 
			
		||||
    let extruder: ExtruderData
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct ExtruderData: Decodable {
 | 
			
		||||
    let temperature: Double
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user