mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Finish wifi config
This commit is contained in:
parent
096930efd7
commit
045fb598d5
4 changed files with 202 additions and 30 deletions
|
|
@ -1112,6 +1112,35 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
|
|||
return 0
|
||||
}
|
||||
|
||||
public func saveWiFiConfig(config: Config.WiFiConfig, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 {
|
||||
|
||||
var adminPacket = AdminMessage()
|
||||
adminPacket.setConfig.wifi = config
|
||||
|
||||
var meshPacket: MeshPacket = MeshPacket()
|
||||
meshPacket.to = UInt32(connectedPeripheral.num)
|
||||
meshPacket.from = 0 //UInt32(connectedPeripheral.num)
|
||||
meshPacket.id = UInt32.random(in: UInt32(UInt8.max)..<UInt32.max)
|
||||
meshPacket.priority = MeshPacket.Priority.reliable
|
||||
meshPacket.wantAck = wantResponse
|
||||
meshPacket.hopLimit = 0
|
||||
|
||||
var dataMessage = DataMessage()
|
||||
dataMessage.payload = try! adminPacket.serializedData()
|
||||
dataMessage.portnum = PortNum.adminApp
|
||||
|
||||
meshPacket.decoded = dataMessage
|
||||
|
||||
let messageDescription = "Saved WiFi Config for \(toUser.longName ?? "Unknown")"
|
||||
|
||||
if sendAdminMessageToRadio(meshPacket: meshPacket, adminDescription: messageDescription, fromUser: fromUser, toUser: toUser) {
|
||||
|
||||
return Int64(meshPacket.id)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
public func saveCannedMessageModuleConfig(config: ModuleConfig.CannedMessageConfig, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 {
|
||||
|
||||
var adminPacket = AdminMessage()
|
||||
|
|
@ -1174,14 +1203,13 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
|
|||
var adminPacket = AdminMessage()
|
||||
adminPacket.getCannedMessageModulePart1Request = true
|
||||
|
||||
//adminPacket.getOwnerRequest = true
|
||||
|
||||
var meshPacket: MeshPacket = MeshPacket()
|
||||
meshPacket.to = UInt32(connectedPeripheral.num)
|
||||
meshPacket.from = 0 //UInt32(connectedPeripheral.num)
|
||||
meshPacket.id = UInt32.random(in: UInt32(UInt8.max)..<UInt32.max)
|
||||
meshPacket.priority = MeshPacket.Priority.reliable
|
||||
meshPacket.wantAck = wantResponse
|
||||
meshPacket.decoded.wantResponse = wantResponse
|
||||
|
||||
var dataMessage = DataMessage()
|
||||
dataMessage.payload = try! adminPacket.serializedData()
|
||||
|
|
|
|||
|
|
@ -257,6 +257,11 @@ func localConfig (config: Config, meshlogging: Bool, context:NSManagedObjectCont
|
|||
if (try! config.position.jsonString()) == "{}" {
|
||||
|
||||
isDefault = true
|
||||
if meshlogging { MeshLogger.log("🗺️ Default Position config received \(String(nodeNum))") }
|
||||
|
||||
} else {
|
||||
|
||||
if meshlogging { MeshLogger.log("🗺️ Custom Position config received \(String(nodeNum))") }
|
||||
}
|
||||
|
||||
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
|
||||
|
|
@ -334,6 +339,87 @@ func localConfig (config: Config, meshlogging: Bool, context:NSManagedObjectCont
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
if config.payloadVariant == Config.OneOf_PayloadVariant.wifi(config.wifi) {
|
||||
|
||||
var isDefault = false
|
||||
|
||||
if (try! config.wifi.jsonString()) == "{}" {
|
||||
|
||||
isDefault = true
|
||||
if meshlogging { MeshLogger.log("📶 Default WiFi config received \(String(nodeNum))") }
|
||||
|
||||
} else {
|
||||
|
||||
if meshlogging { MeshLogger.log("📶 Custom WiFi config received \(String(nodeNum))") }
|
||||
}
|
||||
|
||||
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
|
||||
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(nodeNum))
|
||||
|
||||
do {
|
||||
|
||||
let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity]
|
||||
// Found a node, save WiFi Config
|
||||
if !fetchedNode.isEmpty {
|
||||
|
||||
if fetchedNode[0].wiFiConfig == nil {
|
||||
|
||||
let newWiFiConfig = WiFiConfigEntity(context: context)
|
||||
|
||||
if isDefault {
|
||||
|
||||
newWiFiConfig.ssid = ""
|
||||
newWiFiConfig.password = ""
|
||||
newWiFiConfig.apMode = false
|
||||
newWiFiConfig.apHidden = false
|
||||
|
||||
} else {
|
||||
|
||||
newWiFiConfig.ssid = config.wifi.ssid
|
||||
newWiFiConfig.password = config.wifi.psk
|
||||
newWiFiConfig.apMode = config.wifi.apMode
|
||||
newWiFiConfig.apHidden = config.wifi.apHidden
|
||||
}
|
||||
newWiFiConfig.num = fetchedNode[0].num
|
||||
fetchedNode[0].wiFiConfig = newWiFiConfig
|
||||
|
||||
} else {
|
||||
|
||||
if isDefault {
|
||||
|
||||
fetchedNode[0].wiFiConfig?.ssid = ""
|
||||
fetchedNode[0].wiFiConfig?.password = ""
|
||||
fetchedNode[0].wiFiConfig?.apMode = false
|
||||
fetchedNode[0].wiFiConfig?.apHidden = false
|
||||
|
||||
} else {
|
||||
|
||||
fetchedNode[0].wiFiConfig?.ssid = config.wifi.ssid
|
||||
fetchedNode[0].wiFiConfig?.password = config.wifi.psk
|
||||
fetchedNode[0].wiFiConfig?.apMode = config.wifi.apMode
|
||||
fetchedNode[0].wiFiConfig?.apHidden = config.wifi.apHidden
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
try context.save()
|
||||
if meshlogging { MeshLogger.log("💾 Updated WiFi Config for node number: \(String(nodeNum))") }
|
||||
|
||||
} catch {
|
||||
|
||||
context.rollback()
|
||||
|
||||
let nsError = error as NSError
|
||||
print("💥 Error Updating Core Data WifionfigEntity: \(nsError)")
|
||||
}
|
||||
}
|
||||
|
||||
} catch {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func moduleConfig (config: ModuleConfig, meshlogging: Bool, context:NSManagedObjectContext, nodeNum: Int64, nodeLongName: String) {
|
||||
|
|
@ -425,7 +511,7 @@ func moduleConfig (config: ModuleConfig, meshlogging: Bool, context:NSManagedObj
|
|||
|
||||
try context.save()
|
||||
if meshlogging { MeshLogger.log("💾 Updated Canned Message Module Config for node number: \(String(nodeNum))") }
|
||||
print(try config.cannedMessage.jsonString())
|
||||
|
||||
|
||||
} catch {
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,55 @@ struct WiFiConfig: View {
|
|||
|
||||
Section(header: Text("SSID & Password")) {
|
||||
|
||||
HStack {
|
||||
Label("SSID", systemImage: "wifi")
|
||||
TextField("SSID", text: $ssid)
|
||||
.foregroundColor(.gray)
|
||||
.onChange(of: ssid, perform: { value in
|
||||
|
||||
let totalBytes = ssid.utf8.count
|
||||
|
||||
// Only mess with the value if it is too big
|
||||
if totalBytes > 30 {
|
||||
|
||||
let firstNBytes = Data(ssid.utf8.prefix(30))
|
||||
|
||||
if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) {
|
||||
|
||||
// Set the shortName back to the last place where it was the right size
|
||||
ssid = maxBytesString
|
||||
}
|
||||
}
|
||||
})
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.keyboardType(.default)
|
||||
.disableAutocorrection(true)
|
||||
|
||||
HStack {
|
||||
Label("Password", systemImage: "wallet.pass")
|
||||
TextField("Password", text: $password)
|
||||
.foregroundColor(.gray)
|
||||
.onChange(of: password, perform: { value in
|
||||
|
||||
let totalBytes = password.utf8.count
|
||||
|
||||
// Only mess with the value if it is too big
|
||||
if totalBytes > 60 {
|
||||
|
||||
let firstNBytes = Data(ssid.utf8.prefix(60))
|
||||
|
||||
if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) {
|
||||
|
||||
// Set the shortName back to the last place where it was the right size
|
||||
ssid = maxBytesString
|
||||
}
|
||||
}
|
||||
})
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.keyboardType(.default)
|
||||
.disableAutocorrection(true)
|
||||
|
||||
}
|
||||
Section(header: Text("AP Settings")) {
|
||||
|
|
@ -81,18 +130,18 @@ struct WiFiConfig: View {
|
|||
Button("Save WiFI Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
|
||||
|
||||
var wifi = Config.WiFiConfig()
|
||||
wifi.ssid = ssid
|
||||
wifi.psk = password
|
||||
wifi.apMode = apMode
|
||||
wifi.apHidden = apHidden
|
||||
wifi.ssid = self.ssid
|
||||
wifi.psk = self.password
|
||||
wifi.apMode = self.apMode
|
||||
wifi.apHidden = self.apHidden
|
||||
|
||||
let adminMessageId = bleManager.saveWiFiConfig(config: wifi, fromUser: node!.user!, toUser: node!.user!, wantResponse: true)
|
||||
|
||||
//let adminMessageId = bleManager.saveWiFiConfig(config: wiFi, fromUser: node!.user!, toUser: node!.user!, wantResponse: true)
|
||||
let adminMessageId = 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
|
||||
self.hasChanges = false
|
||||
|
||||
} else {
|
||||
|
||||
|
|
@ -124,23 +173,31 @@ struct WiFiConfig: View {
|
|||
}
|
||||
.onChange(of: ssid) { newSsid in
|
||||
|
||||
hasChanges = true
|
||||
//if node != nil && node!.wiFiConfig != nil { newSsid != node!.wiFiConfig.ssid { hasChanges = true }}
|
||||
if node != nil && node!.wiFiConfig != nil {
|
||||
|
||||
if newSsid != node!.wiFiConfig!.ssid { hasChanges = true }
|
||||
}
|
||||
}
|
||||
.onChange(of: password) { newPassword in
|
||||
|
||||
hasChanges = true
|
||||
//if node != nil && node!.wiFiConfig != nil { newPassword != node!.wiFiConfig!.password { hasChanges = true }}
|
||||
if node != nil && node!.wiFiConfig != nil {
|
||||
|
||||
if newPassword != node!.wiFiConfig!.password { hasChanges = true }
|
||||
}
|
||||
}
|
||||
.onChange(of: apMode) { newAPMode in
|
||||
.onChange(of: apMode) { newApMode in
|
||||
|
||||
hasChanges = true
|
||||
//if node != nil && node!.wiFiConfig != nil { newAPMode != node!.wiFiConfig!.apMode { self.hasChanges = true }}
|
||||
if node != nil && node!.wiFiConfig != nil {
|
||||
|
||||
if newApMode != node!.wiFiConfig!.apMode { hasChanges = true }
|
||||
}
|
||||
}
|
||||
.onChange(of: apHidden) { newAPHidden in
|
||||
.onChange(of: apHidden) { newApHidden in
|
||||
|
||||
hasChanges = true
|
||||
//if node != nil && node!.wiFiConfig != nil { newAPHidden != node!.wiFiConfig!.apHidden { hasChanges = true }}
|
||||
if node != nil && node!.wiFiConfig != nil {
|
||||
|
||||
if newApHidden != node!.wiFiConfig!.apHidden { hasChanges = true }
|
||||
}
|
||||
}
|
||||
.navigationViewStyle(StackNavigationViewStyle())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,6 +48,17 @@ struct UserConfig: View {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
.keyboardType(.default)
|
||||
.disableAutocorrection(true)
|
||||
Text("Long name can be up to 36 bytes long.")
|
||||
.font(.caption)
|
||||
|
||||
HStack {
|
||||
Label("Short Name", systemImage: "circlebadge.fill")
|
||||
TextField("Long Name", text: $shortName)
|
||||
.foregroundColor(.gray)
|
||||
.onChange(of: shortName, perform: { value in
|
||||
|
||||
let totalBytes = shortName.utf8.count
|
||||
|
|
@ -66,16 +77,6 @@ struct UserConfig: View {
|
|||
})
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.keyboardType(.default)
|
||||
.disableAutocorrection(true)
|
||||
Text("Long name can be up to 36 bytes long.")
|
||||
.font(.caption)
|
||||
|
||||
HStack {
|
||||
Label("Short Name", systemImage: "circlebadge.fill")
|
||||
TextField("Long Name", text: $shortName)
|
||||
.foregroundColor(.gray)
|
||||
}
|
||||
.keyboardType(.asciiCapable)
|
||||
.disableAutocorrection(true)
|
||||
Text("The short name is used in maps and messaging and will be appended to the last 4 of the device MAC address to set the device's BLE Name. It can be up to 4 bytes long.")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue