-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Shortcuts provider and Shut Down, Reboot, and Factory Reset Int…
…ents. Also added logging for intent errors.
- Loading branch information
Showing
10 changed files
with
226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// FactoryResetNodeIntent.swift | ||
// Meshtastic | ||
// | ||
// Created by Benjamin Faershtein on 8/25/24. | ||
// | ||
|
||
import Foundation | ||
import AppIntents | ||
|
||
struct FactoryResetNodeIntent: AppIntent { | ||
static var title: LocalizedStringResource = "Factory Reset Node" | ||
static var description: IntentDescription = "Perform a factory reset on the node you are connected to" | ||
|
||
func perform() async throws -> some IntentResult { | ||
// Request user confirmation before performing the factory reset | ||
try await requestConfirmation(result: .result(dialog: "Are you sure you want to factory reset the node?"),confirmationActionName: ConfirmationActionName | ||
.custom(acceptLabel: "Factory Reset", acceptAlternatives: [], denyLabel: "Cancel", denyAlternatives: [], destructive: true)) | ||
|
||
// Ensure the node is connected | ||
if !BLEManager.shared.isConnected { | ||
throw AppIntentErrors.AppIntentError.notConnected | ||
} | ||
|
||
// Safely unwrap the connected node information | ||
if let connectedPeripheralNum = BLEManager.shared.connectedPeripheral?.num, | ||
let connectedNode = getNodeInfo(id: connectedPeripheralNum, context: PersistenceController.shared.container.viewContext), | ||
let fromUser = connectedNode.user, | ||
let toUser = connectedNode.user { | ||
|
||
// Attempt to send a factory reset command, throw an error if it fails | ||
if !BLEManager.shared.sendFactoryReset(fromUser: fromUser, toUser: toUser) { | ||
throw AppIntentErrors.AppIntentError.message("Failed to perform factory reset") | ||
} | ||
} else { | ||
throw AppIntentErrors.AppIntentError.message("Failed to retrieve connected node or required data") | ||
} | ||
// | ||
return .result() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// RestartNodeIntent.swift | ||
// Meshtastic | ||
// | ||
// Created by Benjamin Faershtein on 8/24/24. | ||
// | ||
|
||
import Foundation | ||
import AppIntents | ||
|
||
struct RestartNodeIntent: AppIntent { | ||
static var title: LocalizedStringResource = "Restart Node" | ||
|
||
static var description: IntentDescription = "Restart to the node you are connected to" | ||
|
||
|
||
func perform() async throws -> some IntentResult { | ||
|
||
try await requestConfirmation(result: .result(dialog: "Reboot Node?")) | ||
|
||
if !BLEManager.shared.isConnected { | ||
throw AppIntentErrors.AppIntentError.notConnected | ||
} | ||
// Safely unwrap the connectedNode using if let | ||
if let connectedPeripheralNum = BLEManager.shared.connectedPeripheral?.num, | ||
let connectedNode = getNodeInfo(id: connectedPeripheralNum, context: PersistenceController.shared.container.viewContext), | ||
let fromUser = connectedNode.user, | ||
let toUser = connectedNode.user, | ||
let adminIndex = connectedNode.myInfo?.adminIndex { | ||
|
||
// Attempt to send shutdown, throw an error if it fails | ||
if !BLEManager.shared.sendReboot(fromUser: fromUser, toUser: toUser, adminIndex: adminIndex) { | ||
throw AppIntentErrors.AppIntentError.message("Failed to restart") | ||
} | ||
} else { | ||
throw AppIntentErrors.AppIntentError.message("Failed to retrieve connected node or required data") | ||
} | ||
|
||
return .result() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// ShortcutsProvider.swift | ||
// Meshtastic | ||
// | ||
// Created by Benjamin Faershtein on 8/24/24. | ||
// | ||
|
||
import Foundation | ||
import AppIntents | ||
|
||
struct ShortcutsProvider: AppShortcutsProvider { | ||
static var appShortcuts: [AppShortcut] { | ||
AppShortcut(intent: ShutDownNodeIntent(), | ||
phrases: ["Shut down node in \(.applicationName)", | ||
"Turn off node in \(.applicationName)", | ||
"Power down node in \(.applicationName)", | ||
"Deactivate node in \(.applicationName)"], | ||
shortTitle: "Shut Down Node", | ||
systemImageName: "power") | ||
|
||
AppShortcut(intent: RestartNodeIntent(), | ||
phrases: ["Restart node in \(.applicationName)", | ||
"Reboot node in \(.applicationName)", | ||
"Reset node in \(.applicationName)", | ||
"Start node again in \(.applicationName)"], | ||
shortTitle: "Restart Node", | ||
systemImageName: "arrow.circlepath") | ||
|
||
AppShortcut(intent: MessageChannelIntent(), | ||
phrases: ["Message channel in \(.applicationName)",], | ||
shortTitle: "Message Channel", | ||
systemImageName: "message") | ||
} | ||
} | ||
|
||
|
||
|
Oops, something went wrong.