-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from meshtastic/intents-newfeature
Intents newfeature
- Loading branch information
Showing
10 changed files
with
210 additions
and
6 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" | ||
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,39 @@ | ||
// | ||
// RestartNodeIntent.swift | ||
// Meshtastic | ||
// | ||
// Created by Benjamin Faershtein on 8/24/24. | ||
// | ||
|
||
import Foundation | ||
import AppIntents | ||
|
||
struct RestartNodeIntent: AppIntent { | ||
static var title: LocalizedStringResource = "Restart" | ||
|
||
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,36 @@ | ||
// | ||
// 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 \(.applicationName) node", | ||
"Shut down my \(.applicationName) node", | ||
"Turn off \(.applicationName) node", | ||
"Power down \(.applicationName) node", | ||
"Deactivate \(.applicationName) node"], | ||
shortTitle: "Shut Down", | ||
systemImageName: "power") | ||
|
||
AppShortcut(intent: RestartNodeIntent(), | ||
phrases: ["Restart \(.applicationName) node", | ||
"Restart my \(.applicationName) node", | ||
"Reboot \(.applicationName) node", | ||
"Reboot my \(.applicationName) node"], | ||
shortTitle: "Restart", | ||
systemImageName: "arrow.circlepath") | ||
|
||
AppShortcut(intent: MessageChannelIntent(), | ||
phrases: ["Message a \(.applicationName) channel", | ||
"Send a \(.applicationName) group message"], | ||
shortTitle: "Group Message", | ||
systemImageName: "message") | ||
} | ||
} |
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,39 @@ | ||
// | ||
// ShutDownNodeIntent.swift | ||
// Meshtastic | ||
// | ||
// Created by Benjamin Faershtein on 8/24/24. | ||
// | ||
|
||
import Foundation | ||
import AppIntents | ||
|
||
struct ShutDownNodeIntent: AppIntent { | ||
static var title: LocalizedStringResource = "Shut Down" | ||
|
||
static var description: IntentDescription = "Send a shutdown to the node you are connected to" | ||
|
||
func perform() async throws -> some IntentResult { | ||
try await requestConfirmation(result: .result(dialog: "Shut Down 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.sendShutdown(fromUser: fromUser, toUser: toUser, adminIndex: adminIndex) { | ||
throw AppIntentErrors.AppIntentError.message("Failed to shut down") | ||
} | ||
} else { | ||
throw AppIntentErrors.AppIntentError.message("Failed to retrieve connected node or required data") | ||
} | ||
return .result() | ||
} | ||
} |