Basic network call implementation

This commit is contained in:
Madeline 2023-02-07 15:04:01 -05:00
parent 8876fc666a
commit dd4324e395
6 changed files with 133 additions and 4 deletions

View File

@ -16,6 +16,9 @@
E180B6002992CD9300425DB0 /* KlipperMonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B5FF2992CD9300425DB0 /* KlipperMonTests.swift */; };
E180B60A2992CD9300425DB0 /* KlipperMonUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B6092992CD9300425DB0 /* KlipperMonUITests.swift */; };
E180B60C2992CD9300425DB0 /* KlipperMonUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B60B2992CD9300425DB0 /* KlipperMonUITestsLaunchTests.swift */; };
E180B61B2992CF2200425DB0 /* KlipperWebsocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B61A2992CF2200425DB0 /* KlipperWebsocket.swift */; };
E180B61D2992D53700425DB0 /* PrinterObjectsQuery.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B61C2992D53700425DB0 /* PrinterObjectsQuery.swift */; };
E180B61F2992DBB000425DB0 /* KlipperMonMenuBarExtraView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B61E2992DBB000425DB0 /* KlipperMonMenuBarExtraView.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -49,6 +52,9 @@
E180B6052992CD9300425DB0 /* KlipperMonUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KlipperMonUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
E180B6092992CD9300425DB0 /* KlipperMonUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlipperMonUITests.swift; sourceTree = "<group>"; };
E180B60B2992CD9300425DB0 /* KlipperMonUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlipperMonUITestsLaunchTests.swift; sourceTree = "<group>"; };
E180B61A2992CF2200425DB0 /* KlipperWebsocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlipperWebsocket.swift; sourceTree = "<group>"; };
E180B61C2992D53700425DB0 /* PrinterObjectsQuery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterObjectsQuery.swift; sourceTree = "<group>"; };
E180B61E2992DBB000425DB0 /* KlipperMonMenuBarExtraView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlipperMonMenuBarExtraView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -106,6 +112,9 @@
E180B5F62992CD9200425DB0 /* KlipperMon.entitlements */,
E180B5F32992CD9200425DB0 /* KlipperMon.xcdatamodeld */,
E180B5EE2992CD9200425DB0 /* Preview Content */,
E180B61A2992CF2200425DB0 /* KlipperWebsocket.swift */,
E180B61C2992D53700425DB0 /* PrinterObjectsQuery.swift */,
E180B61E2992DBB000425DB0 /* KlipperMonMenuBarExtraView.swift */,
);
path = KlipperMon;
sourceTree = "<group>";
@ -265,10 +274,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E180B61D2992D53700425DB0 /* PrinterObjectsQuery.swift in Sources */,
E180B5F52992CD9200425DB0 /* KlipperMon.xcdatamodeld in Sources */,
E180B61B2992CF2200425DB0 /* KlipperWebsocket.swift in Sources */,
E180B5F22992CD9200425DB0 /* Persistence.swift in Sources */,
E180B5EB2992CD9100425DB0 /* ContentView.swift in Sources */,
E180B5E92992CD9100425DB0 /* KlipperMonApp.swift in Sources */,
E180B61F2992DBB000425DB0 /* KlipperMonMenuBarExtraView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -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>

View File

@ -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

View 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)
}
}

View File

@ -0,0 +1,9 @@
//
// KlipperWebsocket.swift
// KlipperMon
//
// Created by maddiefuzz on 2/7/23.
//
import Foundation

View 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
}