2023-11-28 20:03:08 -08:00
|
|
|
//
|
|
|
|
|
// AmbientLightingConfig.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 11/26/23
|
|
|
|
|
//
|
2024-06-07 22:09:20 -05:00
|
|
|
import MeshtasticProtobufs
|
2023-11-28 20:03:08 -08:00
|
|
|
import SwiftUI
|
2024-09-05 19:31:29 -07:00
|
|
|
import OSLog
|
2024-06-07 22:09:20 -05:00
|
|
|
|
2023-11-28 20:03:08 -08:00
|
|
|
@available(iOS 17.0, macOS 14.0, *)
|
|
|
|
|
struct AmbientLightingConfig: View {
|
|
|
|
|
@Environment(\.self) var environment
|
|
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
|
|
|
|
@Environment(\.dismiss) private var goBack
|
|
|
|
|
|
|
|
|
|
var node: NodeInfoEntity?
|
|
|
|
|
|
|
|
|
|
@State private var isPresentingSaveConfirm: Bool = false
|
|
|
|
|
@State var hasChanges = false
|
|
|
|
|
@State var ledState: Bool = false
|
|
|
|
|
@State var current = 10
|
|
|
|
|
@State var red = 0
|
|
|
|
|
@State var green = 0
|
|
|
|
|
@State var blue = 0
|
|
|
|
|
@State private var color = Color(red: 51, green: 199, blue: 88) // Color(.sRGB, red: 0.98, green: 0.9, blue: 0.2)
|
|
|
|
|
@State private var components: Color.Resolved?
|
|
|
|
|
var body: some View {
|
|
|
|
|
VStack {
|
|
|
|
|
Form {
|
2024-02-17 22:39:22 -07:00
|
|
|
ConfigHeader(title: "Ambient Lighting", config: \.ambientLightingConfig, node: node, onAppear: setAmbientLightingConfigValue)
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-11-28 20:03:08 -08:00
|
|
|
Section(header: Text("options")) {
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-12-01 16:33:17 -08:00
|
|
|
Toggle(isOn: $ledState) {
|
|
|
|
|
Label("LED State", systemImage: ledState ? "lightbulb.led.fill" : "lightbulb.led")
|
2024-02-21 23:35:28 -08:00
|
|
|
Text("The state of the LED (on/off)")
|
2023-12-01 16:33:17 -08:00
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-12-01 16:33:17 -08:00
|
|
|
HStack {
|
|
|
|
|
Image(systemName: "eyedropper")
|
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
|
ColorPicker("Color", selection: $color, supportsOpacity: false)
|
|
|
|
|
.padding(5)
|
|
|
|
|
}
|
|
|
|
|
HStack {
|
|
|
|
|
Image(systemName: "directcurrent")
|
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
|
Stepper("Current: \(current)", value: $current, in: 0...31, step: 1)
|
|
|
|
|
.padding(5)
|
2023-11-28 20:03:08 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-01 16:33:17 -08:00
|
|
|
.disabled(self.bleManager.connectedPeripheral == nil || node?.ambientLightingConfig == nil)
|
2023-11-28 20:03:08 -08:00
|
|
|
|
2024-02-18 00:03:34 -07:00
|
|
|
SaveConfigButton(node: node, hasChanges: $hasChanges) {
|
2023-11-28 20:03:08 -08:00
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
2024-02-18 00:03:34 -07:00
|
|
|
if connectedNode != nil {
|
|
|
|
|
var al = ModuleConfig.AmbientLightingConfig()
|
|
|
|
|
al.ledState = ledState
|
|
|
|
|
al.current = UInt32(current)
|
|
|
|
|
if let components {
|
|
|
|
|
al.red = UInt32(components.red * 255)
|
|
|
|
|
al.green = UInt32(components.green * 255)
|
|
|
|
|
al.blue = UInt32(components.blue * 255)
|
|
|
|
|
}
|
2023-11-28 20:03:08 -08:00
|
|
|
|
2024-02-18 00:03:34 -07:00
|
|
|
let adminMessageId = bleManager.saveAmbientLightingModuleConfig(config: al, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
|
|
|
|
|
if adminMessageId > 0 {
|
|
|
|
|
// Should show a saved successfully alert once I know that to be true
|
|
|
|
|
// for now just disable the button after a successful save
|
|
|
|
|
hasChanges = false
|
|
|
|
|
goBack()
|
2023-11-28 20:03:08 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.navigationTitle("ambient.lighting.config")
|
2024-08-11 09:07:22 -07:00
|
|
|
.navigationBarItems(
|
|
|
|
|
trailing: ZStack {
|
|
|
|
|
ConnectedDevice(
|
|
|
|
|
bluetoothOn: bleManager.isSwitchedOn,
|
|
|
|
|
deviceConnected: bleManager.connectedPeripheral != nil,
|
|
|
|
|
name: bleManager.connectedPeripheral?.shortName ?? "?"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-09-05 19:31:29 -07:00
|
|
|
.onFirstAppear {
|
2023-11-28 20:03:08 -08:00
|
|
|
// Need to request a Ambient Lighting Config from the remote node before allowing changes
|
2024-09-05 19:31:29 -07:00
|
|
|
if let connectedPeripheral = bleManager.connectedPeripheral, let node {
|
|
|
|
|
Logger.mesh.info("empty ambient lighting config")
|
|
|
|
|
let connectedNode = getNodeInfo(id: connectedPeripheral.num, context: context)
|
|
|
|
|
if let connectedNode {
|
|
|
|
|
if node.num != connectedNode.num {
|
|
|
|
|
if UserDefaults.enableAdministration {
|
|
|
|
|
/// 2.5 Administration with session passkey
|
|
|
|
|
let expiration = node.sessionExpiration ?? Date()
|
|
|
|
|
if expiration < Date() || node.ambientLightingConfig == nil {
|
|
|
|
|
_ = bleManager.requestAmbientLightingConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/// Legacy Administration
|
|
|
|
|
_ = bleManager.requestAmbientLightingConfig(fromUser: connectedNode.user!, toUser: node.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-28 20:03:08 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-11 17:31:27 -07:00
|
|
|
.onChange(of: ledState) {
|
|
|
|
|
if let val = node?.ambientLightingConfig?.ledState {
|
|
|
|
|
hasChanges = $0 != val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: current) {
|
|
|
|
|
if let val = node?.ambientLightingConfig?.current {
|
|
|
|
|
hasChanges = $0 != val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: color) { c in
|
|
|
|
|
if color != c {
|
|
|
|
|
hasChanges = true
|
2023-11-28 20:03:08 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func setAmbientLightingConfigValue() {
|
|
|
|
|
self.ledState = node?.ambientLightingConfig?.ledState ?? false
|
|
|
|
|
self.current = Int(node?.ambientLightingConfig?.current ?? 10)
|
2023-11-28 21:18:59 -08:00
|
|
|
let red = Double(node?.ambientLightingConfig?.red ?? 255)
|
|
|
|
|
let green = Double(node?.ambientLightingConfig?.green ?? 255)
|
|
|
|
|
let blue = Double(node?.ambientLightingConfig?.blue ?? 255)
|
|
|
|
|
color = Color(red: red / 255.0, green: green / 255.0, blue: blue / 255.0)
|
2023-11-28 20:03:08 -08:00
|
|
|
self.hasChanges = false
|
|
|
|
|
}
|
|
|
|
|
}
|