From 8a219146f3a1bc7dfc6b5f00d3b19f749cf99b4b Mon Sep 17 00:00:00 2001 From: Mantosh Kumar <> Date: Mon, 14 Oct 2024 12:31:58 +0530 Subject: [PATCH] BTAPP-2011 | Enable BLE provisioning for 917 sensor demo on IOS --- SiliconLabsApp.xcodeproj/project.pbxproj | 16 ++- SiliconLabsApp/SILAppDelegate.swift | 1 + .../SupportingFiles/BlueGecko/Info.plist | 11 +- .../SILAppSelectionViewController.swift | 47 ++++++-- .../WiFi_Sensor/APIService/APIRequest.swift | 33 +++++- .../Controller/SILNetworkCheck.swift | 68 +++++++++++ .../Controller/SILWiFiLEDViewController.swift | 4 +- .../SILWiFiMotionViewController.swift | 28 +++-- .../Controller/SILWifiSensorsHomeView.swift | 57 +++++++-- .../Model/SILAllWiFiSensorModel.swift | 1 + .../Model/SILWifiSensorsHomeViewModel.swift | 71 ++++++++++++ .../ViewModel/SILWiFiSensorsViewModel.swift | 108 +++++++++++++----- .../SILWifiCommissioningViewController.swift | 24 +++- .../ViewModels/SILDeviceSelectionViewModel.m | 2 + .../SILWifiCommissioningViewModel.swift | 3 + 15 files changed, 411 insertions(+), 63 deletions(-) create mode 100644 SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILNetworkCheck.swift create mode 100644 SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILWifiSensorsHomeViewModel.swift diff --git a/SiliconLabsApp.xcodeproj/project.pbxproj b/SiliconLabsApp.xcodeproj/project.pbxproj index a475b887..c89b9d4b 100644 --- a/SiliconLabsApp.xcodeproj/project.pbxproj +++ b/SiliconLabsApp.xcodeproj/project.pbxproj @@ -910,6 +910,8 @@ 4388B2D32C3047910068C950 /* SILWiFiMotionVcCh.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4388B2D22C3047910068C950 /* SILWiFiMotionVcCh.swift */; }; 4388B2D52C305B0C0068C950 /* WiFiMotionDemoConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4388B2D42C305B0C0068C950 /* WiFiMotionDemoConnection.swift */; }; 4388B2E22C3FC9370068C950 /* SILAllWiFiSensorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4388B2E12C3FC9370068C950 /* SILAllWiFiSensorModel.swift */; }; + 439B76122CA173D700B4E737 /* SILNetworkCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = 439B76112CA173D700B4E737 /* SILNetworkCheck.swift */; }; + 439B766A2CA4093F00B4E737 /* SILWifiSensorsHomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 439B76692CA4093F00B4E737 /* SILWifiSensorsHomeViewModel.swift */; }; 439F2C792C2C18BF00504A12 /* APIRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 439F2C782C2C18BF00504A12 /* APIRequest.swift */; }; 439F2C7F2C2C242E00504A12 /* SILWifiSensorsHomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 439F2C7D2C2C242E00504A12 /* SILWifiSensorsHomeView.swift */; }; 439F2C852C2C243600504A12 /* SILSensorCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 439F2C802C2C243600504A12 /* SILSensorCell.swift */; }; @@ -1939,6 +1941,8 @@ 4388B2D22C3047910068C950 /* SILWiFiMotionVcCh.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILWiFiMotionVcCh.swift; sourceTree = ""; }; 4388B2D42C305B0C0068C950 /* WiFiMotionDemoConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WiFiMotionDemoConnection.swift; sourceTree = ""; }; 4388B2E12C3FC9370068C950 /* SILAllWiFiSensorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILAllWiFiSensorModel.swift; sourceTree = ""; }; + 439B76112CA173D700B4E737 /* SILNetworkCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILNetworkCheck.swift; sourceTree = ""; }; + 439B76692CA4093F00B4E737 /* SILWifiSensorsHomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILWifiSensorsHomeViewModel.swift; sourceTree = ""; }; 439F2C782C2C18BF00504A12 /* APIRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIRequest.swift; sourceTree = ""; }; 439F2C7D2C2C242E00504A12 /* SILWifiSensorsHomeView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SILWifiSensorsHomeView.swift; sourceTree = ""; }; 439F2C802C2C243600504A12 /* SILSensorCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SILSensorCell.swift; sourceTree = ""; }; @@ -4489,6 +4493,7 @@ children = ( 4388B2CC2C2DD9FE0068C950 /* SILWiFiLEDModel.swift */, 4388B2E12C3FC9370068C950 /* SILAllWiFiSensorModel.swift */, + 439B76692CA4093F00B4E737 /* SILWifiSensorsHomeViewModel.swift */, ); path = Model; sourceTree = ""; @@ -4514,6 +4519,7 @@ 4388B2D02C30476E0068C950 /* SILWiFiMotionVC.swift */, 4388B2D22C3047910068C950 /* SILWiFiMotionVcCh.swift */, 4388B2D42C305B0C0068C950 /* WiFiMotionDemoConnection.swift */, + 439B76112CA173D700B4E737 /* SILNetworkCheck.swift */, ); path = Controller; sourceTree = ""; @@ -6284,6 +6290,7 @@ 20513222270365A800C27B92 /* RoundCell.swift in Sources */, 0C2FCB1A1F9A542300F4F259 /* SILDebugDeviceTableViewCell.m in Sources */, 2051323B270365A800C27B92 /* DeviceSelectionInteraction.swift in Sources */, + 439B76122CA173D700B4E737 /* SILNetworkCheck.swift in Sources */, 1EFC76D626AEE05C0035594E /* SILExitWarningViewModel.swift in Sources */, 1E4DB3B2266A720600405BD2 /* SILGattConfiguratorCharacteristicButtonCellView.swift in Sources */, FE7B8FDF292E4FBD0075D894 /* SILIOPTestDeviceSelectorController.swift in Sources */, @@ -6447,6 +6454,7 @@ 0C2FCB3D1F9A542300F4F259 /* SILApp+AttributedProfiles.m in Sources */, 0C2FCB3E1F9A542300F4F259 /* SILOTAFirmwareFile.m in Sources */, 1E4DB342266A711700405BD2 /* SILBluetoothServiceDescriptorModel.swift in Sources */, + 439B766A2CA4093F00B4E737 /* SILWifiSensorsHomeViewModel.swift in Sources */, 1E90F5E42473E73E0013AABD /* SILDebugServicesMenuViewController.swift in Sources */, 1EC1F1DB260CE95900508552 /* SILDiscoverFirmwareInfo.swift in Sources */, 72CA90002ABCBBD700FE70BA /* MatterHomeViewController.m in Sources */, @@ -7221,7 +7229,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_RESOURCE_RULES_PATH = ""; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 5; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = 52444FG85C; DISPLAY_NAME = "Si Connect"; @@ -7239,7 +7247,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 2.9.2; + MARKETING_VERSION = 2.9.3; PRODUCT_BUNDLE_IDENTIFIER = com.silabs.BlueGeckoDemoApp; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -7263,7 +7271,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_RESOURCE_RULES_PATH = ""; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 5; DEFINES_MODULE = YES; DEVELOPMENT_TEAM = ""; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = 52444FG85C; @@ -7281,7 +7289,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 2.9.2; + MARKETING_VERSION = 2.9.3; PRODUCT_BUNDLE_IDENTIFIER = com.silabs.BlueGeckoDemoApp; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = "BlueGeckoDemoApp Distribution"; diff --git a/SiliconLabsApp/SILAppDelegate.swift b/SiliconLabsApp/SILAppDelegate.swift index d784cb6c..7c382271 100644 --- a/SiliconLabsApp/SILAppDelegate.swift +++ b/SiliconLabsApp/SILAppDelegate.swift @@ -36,6 +36,7 @@ class SILAppDelegate : UIResponder, UIApplicationDelegate { window?.makeKeyAndVisible() setupLogs() + return true } private func setupLogs() { diff --git a/SiliconLabsApp/SupportingFiles/BlueGecko/Info.plist b/SiliconLabsApp/SupportingFiles/BlueGecko/Info.plist index 25db9dfe..6eb7b5b6 100644 --- a/SiliconLabsApp/SupportingFiles/BlueGecko/Info.plist +++ b/SiliconLabsApp/SupportingFiles/BlueGecko/Info.plist @@ -89,12 +89,21 @@ LSSupportsOpeningDocumentsInPlace + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + NSAllowsLocalNetworking + + NSBluetoothAlwaysUsageDescription This app uses Bluetooth and Bluetooth Low Energy (BLE) to connect to and control your Silicon Labs devices. NSBluetoothPeripheralUsageDescription This app uses Bluetooth and Bluetooth Low Energy (BLE) to connect to and control your Silicon Labs devices. NSBonjourServices + _lnp._tcp + _bonjour._tcp _matter._tcp _matterc._udp _matterd._udp @@ -110,7 +119,7 @@ NSLocationWhenInUseUsageDescription Location required for use of Retail Beacons. NSPhotoLibraryUsageDescription - Apple thinks the app accesses the user's photos but we don't know why. + Apple thinks the app accesses the user's photos but we don't know why. UIAppFonts Roboto-Black.ttf diff --git a/SiliconLabsApp/ViewControllers/AppSelection/SILAppSelectionViewController.swift b/SiliconLabsApp/ViewControllers/AppSelection/SILAppSelectionViewController.swift index 0582c637..644942c3 100644 --- a/SiliconLabsApp/ViewControllers/AppSelection/SILAppSelectionViewController.swift +++ b/SiliconLabsApp/ViewControllers/AppSelection/SILAppSelectionViewController.swift @@ -10,7 +10,8 @@ import Foundation import SVProgressHUD @objcMembers -class SILAppSelectionViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SILDeviceSelectionViewControllerDelegate, WYPopoverControllerDelegate, SILThunderboardDeviceSelectionViewControllerDelegate, SILWifiOTAConfigViewControllerDelegate { +class SILAppSelectionViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SILDeviceSelectionViewControllerDelegate, WYPopoverControllerDelegate, SILThunderboardDeviceSelectionViewControllerDelegate, SILWifiOTAConfigViewControllerDelegate, SILWifiCommissioningViewControllerDelegate { + var appsArray: [SILApp] = SILApp.demoApps() @@ -26,6 +27,11 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour private var disposeBag = SILObservableTokenBag() private var peripheralManager: SILThroughputPeripheralManager! + + + private var viewModel: SILWifiCommissioningViewModel! + + override func viewDidLoad() { super.viewDidLoad() self.setupAppCollectionView() @@ -93,8 +99,7 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour let selectionViewController = SILDeviceSelectionViewController(deviceSelectionViewModel: viewModel!, shouldConnect: shouldConnect) selectionViewController.centralManager = SILBrowserConnectionsViewModel.sharedInstance()!.centralManager! selectionViewController.delegate = self - self.devicePopoverController = WYPopoverController.sil_presentCenterPopover(withContentViewController: selectionViewController, presenting: self, - delegate: self, animated: true) + self.devicePopoverController = WYPopoverController.sil_presentCenterPopover(withContentViewController: selectionViewController, presenting: self, delegate: self, animated: true) } private func presentThunderboardDeviceSelection(app: SILApp!, animated: Bool, filter: ((Device) -> Bool)? = nil) { @@ -130,7 +135,7 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour private func moveToSILWifiSensorsDemoView() { let storyBoard : UIStoryboard = UIStoryboard(name: "SILWifiSensors", bundle:nil) let nextViewController = storyBoard.instantiateViewController(withIdentifier: "SILWifiSensorsHomeView") - self.navigationController?.pushViewController(nextViewController, animated: true) + self.navigationController?.pushViewController(nextViewController, animated: false) } private func showWifiDisabledAlert() { @@ -208,12 +213,18 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour showWiFiOTAScreen() } case .typeWifiSensor: - moveToSILWifiSensorsDemoView() + self.presentDeviceSelectionViewController(app: app, animated: true) { $0!.advertisedLocalName == "WIFI_SENSOR" } + //localNetworkCheck() default: return } } - +// func localNetworkCheck() { +// let networkOb = SILNetworkCheck() +// networkOb.requestAuthorization { val in +// print(val) +// } +// } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.appsArray.count } @@ -282,6 +293,7 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour if let wifiCommissioningController = UIStoryboard(name: "SILAppTypeWifiCommissioning", bundle: nil).instantiateInitialViewController() as? SILWifiCommissioningViewController { wifiCommissioningController.centralManager = viewController.centralManager wifiCommissioningController.connectedPeripheral = peripheral.peripheral + wifiCommissioningController.demoScreenName = "typeWifiCommissioning" self.navigationController?.pushViewController(wifiCommissioningController, animated: true) } @@ -293,13 +305,27 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour peripheralDelegate: eslPeripheralDelegate) self.navigationController?.pushViewController(eslDemoController, animated: true) } + case .typeWifiSensor: + if let wifiCommissioningController = UIStoryboard(name: "SILAppTypeWifiCommissioning", bundle: nil).instantiateInitialViewController() as? SILWifiCommissioningViewController { + wifiCommissioningController.centralManager = viewController.centralManager + wifiCommissioningController.connectedPeripheral = peripheral.peripheral + wifiCommissioningController.demoScreenName = "typeWifiSensor" + wifiCommissioningController.delegateWifiCommissioning = self + self.navigationController?.pushViewController(wifiCommissioningController, animated: true) + } default: break } } } - + + private func showBluetoothDisabledWarningTemp() { + let bluetoothDisabledAlert = SILBluetoothDisabledAlert.wifiCommissioning + self.alertWithOKButton(title: bluetoothDisabledAlert.title, message: bluetoothDisabledAlert.message) { _ in + self.navigationController?.popViewController(animated: true) + } + } func didDismissDeviceSelectionViewController() { if let peripheralManager = peripheralManager { peripheralManager.stopAdvertising() @@ -441,4 +467,11 @@ class SILAppSelectionViewController : UIViewController, UICollectionViewDataSour self.navigationController?.pushViewController(demoViewController, animated: true) } } + + // MARK: SILWifiCommissioningViewControllerDelegate + func onceDeviceIsConnect(isConnectedDevice: Bool) { + if isConnectedDevice { + moveToSILWifiSensorsDemoView() + } + } } diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/APIService/APIRequest.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/APIService/APIRequest.swift index ecf636a9..699253e6 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/APIService/APIRequest.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/APIService/APIRequest.swift @@ -15,17 +15,34 @@ struct ApiHTTPrequest { } +struct URLForRequest { + var uri: String + func getUrl() -> String { + let ipArress = UserDefaults.standard.string(forKey: "access_point_IPA") + let Url = String(format: "http://\(ipArress ?? "")/\(uri)") + return Url + } + +} + class APIRequest { static let sharedInstance = APIRequest() func postApiCall(parameterDictionary: String, url : String, completionBlock: @escaping (_ ReponsData: Data?, _ APIClientError:Error?) -> Void) { - let Url = String(format: "http://192.168.10.10/\(url)") +// let ipArress = UserDefaults.standard.string(forKey: "access_point_IPA") +// let Url = String(format: "http://\(ipArress ?? "")/\(url)") + + let UrlRe = URLForRequest(uri: url) + let Url = UrlRe.getUrl() + print(Url) + + //let Url = String(format: "http://192.168.10.10/\(url)") guard let serviceUrl = URL(string: Url) else { return } var request = URLRequest(url: serviceUrl) request.httpMethod = HttpMethods.POST.rawValue request.setValue("Application/json", forHTTPHeaderField: "Content-Type") - request.timeoutInterval = 10 + request.timeoutInterval = 30 let parameters = parameterDictionary let postData = parameters.data(using: .utf8) @@ -51,12 +68,20 @@ class APIRequest { } func getApiCall(url : String, completionBlock: @escaping (_ ReponsData: Data?, _ APIClientError:Error?) -> Void) { - let Url = String(format: "http://192.168.10.10/\(url)") + +// let ipArress = UserDefaults.standard.string(forKey: "access_point_IPA") +// let Url = String(format: "http://\(ipArress ?? "")/\(url)") +// print(Url) + + let UrlRe = URLForRequest(uri: url) + let Url = UrlRe.getUrl() + print(Url) + //let Url = String(format: "http://192.168.10.10/\(url)") guard let serviceUrl = URL(string: Url) else { return } var request = URLRequest(url: serviceUrl) request.httpMethod = HttpMethods.GET.rawValue request.setValue("Application/json", forHTTPHeaderField: "Content-Type") - request.timeoutInterval = 10 + request.timeoutInterval = 30 let session = URLSession.shared session.dataTask(with: request) { (data, response, error) in if let response = response { diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILNetworkCheck.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILNetworkCheck.swift new file mode 100644 index 00000000..66027dfd --- /dev/null +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILNetworkCheck.swift @@ -0,0 +1,68 @@ +// +// SILNetworkCheck.swift +// BlueGecko +// +// Created by SovanDas Maity on 23/09/24. +// Copyright © 2024 SiliconLabs. All rights reserved. +// + +import UIKit +import Foundation +import Network + + +class SILNetworkCheck: NSObject { + private var browser: NWBrowser? + private var netService: NetService? + private var completion: ((Bool) -> Void)? + + public func requestAuthorization(completion: @escaping (Bool) -> Void) { + self.completion = completion + + // Create parameters, and allow browsing over peer-to-peer link. + let parameters = NWParameters() + parameters.includePeerToPeer = true + + // Browse for a custom service type. + let browser = NWBrowser(for: .bonjour(type: "_bonjour._tcp", domain: nil), using: parameters) + self.browser = browser + browser.stateUpdateHandler = { newState in + switch newState { + case .failed(let error): + print(error.localizedDescription) + case .ready, .cancelled: + break + case let .waiting(error): + print("Local network permission has been denied: \(error)") + self.reset() + self.completion?(false) + default: + break + } + } + + self.netService = NetService(domain: "local.", type:"_lnp._tcp.", name: "LocalNetworkPrivacy", port: 1100) + self.netService?.delegate = self + + self.browser?.start(queue: .main) + self.netService?.publish() + } + + private func reset() { + self.browser?.cancel() + self.browser = nil + self.netService?.stop() + self.netService = nil + } +} + + + +@available(iOS 14.0, *) +extension SILNetworkCheck : NetServiceDelegate { + public func netServiceDidPublish(_ sender: NetService) { + self.reset() + print("Local network permission has been granted") + completion?(true) + } +} diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiLEDViewController.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiLEDViewController.swift index 9c91bfdb..9e2c40a1 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiLEDViewController.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiLEDViewController.swift @@ -33,7 +33,9 @@ class SILWiFiLEDViewController: UIViewController { //blubImg.image = LedImage.ledOnImage self.onBtn.backgroundColor = UIColor(named: "sil_boulderColor") self.offBtn.backgroundColor = UIColor(named: "sil_boulderColor") - ledStatus() + + //Comented: After BLE commissioning led status not to be check. + // ledStatus() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiMotionViewController.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiMotionViewController.swift index d67d323e..c7321bf7 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiMotionViewController.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWiFiMotionViewController.swift @@ -21,21 +21,32 @@ class SILWiFiMotionViewController: UIViewController, SILWiFiMotionSensorsViewMod var silWiFiMotionSensorsViewModel:SILWiFiMotionSensorsViewModel = SILWiFiMotionSensorsViewModel() var apiCallTimer: Timer? - var accelerationXStr: String = "" - var accelerationYStr: String = "" - var accelerationZStr: String = "" + private var accelerationXStr: String = "" + private var accelerationYStr: String = "" + private var accelerationZStr: String = "" - var orientationXStr: String = "" - var orientationYStr: String = "" - var orientationZStr: String = "" + private var orientationXStr: String = "" + private var orientationYStr: String = "" + private var orientationZStr: String = "" + var motionData: [String: Any]? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. silWiFiMotionSensorsViewModel.SILWiFiMotionSensorsViewModelDelegate = self - let motionData = ["gyroscope": ["x": orientationXStr, "y": orientationYStr, "z": orientationZStr], "accelerometer": ["x": accelerationXStr, "y": accelerationYStr, "z": accelerationZStr]] - uiUpdate(sensorsData: motionData) + if let motionDataTemp = motionData?["value"] as? [String : Any] { + orientationXStr = (motionDataTemp["gyroscope"] as! [String : Any])["x"] as! String + orientationYStr = (motionDataTemp["gyroscope"] as! [String : Any])["y"] as! String + orientationZStr = (motionDataTemp["gyroscope"] as! [String : Any])["z"] as! String + + accelerationXStr = (motionDataTemp["accelerometer"] as! [String : Any])["x"] as! String + accelerationYStr = (motionDataTemp["accelerometer"] as! [String : Any])["y"] as! String + accelerationZStr = (motionDataTemp["accelerometer"] as! [String : Any])["z"] as! String + + let motionData = ["gyroscope": ["x": orientationXStr, "y": orientationYStr, "z": orientationZStr], "accelerometer": ["x": accelerationXStr, "y": accelerationYStr, "z": accelerationZStr]] + uiUpdate(sensorsData: motionData) + } } @@ -79,6 +90,7 @@ class SILWiFiMotionViewController: UIViewController, SILWiFiMotionSensorsViewMod } } + //MARK: SILWiFiMotionSensorsViewModelProtocol func notifyMotionSensorsData(sensorsData: Dictionary?) { if let sensorsData = sensorsData { let aX = (sensorsData["accelerometer"] as? Dictionary)?["x"] diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWifiSensorsHomeView.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWifiSensorsHomeView.swift index afed3ca9..6d4f89bb 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWifiSensorsHomeView.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Controller/SILWifiSensorsHomeView.swift @@ -21,6 +21,7 @@ class SILWifiSensorsHomeView: UIViewController, SILWiFiSensorsViewModelProtocol //var timer = Timer() var sensorsData: [Any] = [] + //var sensorsData: SILAllWiFiSensorModel! var sensorTypeStr: String = "" var apiCallTimer: Timer? @@ -32,33 +33,65 @@ class SILWifiSensorsHomeView: UIViewController, SILWiFiSensorsViewModelProtocol //setupNavigationBar() silwifiSensorsViewModelObject.SILWiFiSensorsViewModelDelegate = self updateUI() - - } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) +// SVProgressHUD.show(withStatus: "Connecting") SVProgressHUD.show(withStatus: "Connecting") - sensorTypeStr = "" - silwifiSensorsViewModelObject.getTemperatureData { sensorsData, APIClientError in - if APIClientError == nil{ - DispatchQueue.main.async { - self.silwifiSensorsViewModelObject.getAllSensorData() - //self.silwifiSensorsViewModelObject.getAllSensor() - } + let networkOb = SILNetworkCheck() + networkOb.requestAuthorization { val in + print(val) + if val { + self.apiCall() }else{ DispatchQueue.main.async { SVProgressHUD.dismiss() + self.showWifiDisabledAlert() } } } } + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.tabBarController?.tabBar.isHidden = true } + //MARK: Private func + private func apiCall(){ + let seconds = 5.0 + DispatchQueue.main.asyncAfter(deadline: .now() + seconds) { + // Put your code which should be executed with a delay here + self.sensorTypeStr = "" + self.silwifiSensorsViewModelObject.getAllSensorValue() + +// self.silwifiSensorsViewModelObject.getTemperatureData { sensorsData, APIClientError in +// if APIClientError == nil{ +//// DispatchQueue.main.async { +//// self.silwifiSensorsViewModelObject.getAllSensorData() +//// //self.silwifiSensorsViewModelObject.getAllSensor() +//// } +// self.silwifiSensorsViewModelObject.getAllSensorValue() +// +// }else{ +// DispatchQueue.main.async { +// SVProgressHUD.dismiss() +// } +// } +// } + + } + } + private func showWifiDisabledAlert() { + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + let message = "Please enable local network access in Settings > Privacy & Security." + self.alertWithOKButton(title: "Local Network Disabled", message: message, completion: { _ in + self.navigationController?.popViewController(animated: true) + }) + } + } private func updateUI() { setLeftAlignedTitle("WiFi Sensors") let nib = UINib(nibName: "SILSensorCell", bundle: nil) @@ -68,6 +101,7 @@ class SILWifiSensorsHomeView: UIViewController, SILWiFiSensorsViewModelProtocol sensorePopupView.isHidden = true } + //MARK: @IBAction @IBAction func cancelBtn(_ sender: UIButton) { sensorePopupView.isHidden = true sensorTypeStr = "" @@ -231,8 +265,11 @@ extension SILWifiSensorsHomeView { sensorePopupView.isHidden = true //SILWiFiMotionVcCh //let SILWiFiMotionViewControllerObj = storyboard.instantiateViewController(withIdentifier: "SILWiFiMotionVcCh") - let SILWiFiMotionViewControllerObj = storyboard.instantiateViewController(withIdentifier: "SILWiFiMotionViewController") + + //let motionDic = ["gyroscope": gyroscope, "accelerometer": accelerometer] + let SILWiFiMotionViewControllerObj = storyboard.instantiateViewController(withIdentifier: "SILWiFiMotionViewController") as! SILWiFiMotionViewController SILWiFiMotionViewControllerObj.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext + SILWiFiMotionViewControllerObj.motionData = sensorsDataDic present(SILWiFiMotionViewControllerObj, animated: false) default: diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILAllWiFiSensorModel.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILAllWiFiSensorModel.swift index 38a9b902..e412ac51 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILAllWiFiSensorModel.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILAllWiFiSensorModel.swift @@ -38,3 +38,4 @@ enum SensorePopupViewName: String { case ledLightPopupViewTitle = "LED Control" case motionPopupViewTitle = "Motion Sensor" } + diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILWifiSensorsHomeViewModel.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILWifiSensorsHomeViewModel.swift new file mode 100644 index 00000000..82d3ea6e --- /dev/null +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/Model/SILWifiSensorsHomeViewModel.swift @@ -0,0 +1,71 @@ +// +// SILWifiSensorsHomeViewModel.swift +// BlueGecko +// +// Created by SovanDas Maity on 25/09/24. +// Copyright © 2024 SiliconLabs. All rights reserved. +// + +import Foundation + +// MARK: - SILWifiSensorsHomeViewModel +struct SILWifiSensorsHomeViewModel: Codable { + let led: LED + let light: Light + let temperature: TemperatureData + let accelerometer, gyroscope: Accelerometer + let humidity: HumidityData + let microphone: Microphone + enum CodingKeys: String, CodingKey { + case temperature = "temperature" + case humidity = "humidity" + case led, light, accelerometer, gyroscope, microphone + } +} + +// MARK: - Accelerometer +struct Accelerometer: Codable { + let x, y, z: String +} + +// MARK: - Humidity +struct HumidityData: Codable { + let humidityPercentage: String + + enum CodingKeys: String, CodingKey { + case humidityPercentage = "humidity_percentage" + } +} + +// MARK: - LED +struct LED: Codable { + let red, green, blue: String +} + +// MARK: - Light +struct Light: Codable { + let ambientLightLux, whiteLightLux: String + + enum CodingKeys: String, CodingKey { + case ambientLightLux = "ambient_light_lux" + case whiteLightLux = "white_light_lux" + } +} + +// MARK: - Microphone +struct Microphone: Codable { + let microphoneDecibel: String + + enum CodingKeys: String, CodingKey { + case microphoneDecibel = "microphone_decibel" + } +} + +// MARK: - Temperature +struct TemperatureData: Codable { + let temperatureCelcius: String + + enum CodingKeys: String, CodingKey { + case temperatureCelcius = "temperature_celcius" + } +} diff --git a/SiliconLabsApp/ViewControllers/WiFi_Sensor/ViewModel/SILWiFiSensorsViewModel.swift b/SiliconLabsApp/ViewControllers/WiFi_Sensor/ViewModel/SILWiFiSensorsViewModel.swift index e4508d32..21ff8317 100644 --- a/SiliconLabsApp/ViewControllers/WiFi_Sensor/ViewModel/SILWiFiSensorsViewModel.swift +++ b/SiliconLabsApp/ViewControllers/WiFi_Sensor/ViewModel/SILWiFiSensorsViewModel.swift @@ -30,32 +30,70 @@ class SILWiFiSensorsViewModel { typealias completionBlockSensors = (_ sensorsData: [Any]?, _ APIClientError:Error?) -> Void typealias completionBlock = (_ sensorsData: Dictionary?, _ APIClientError:Error?) -> Void - var sensorsData: [Any] = [] let APIRequestdispatchGroup = DispatchGroup() let sensorConcurrentQueue = DispatchQueue(label: "com.gcd.sensordispatchGroup", attributes: .concurrent) var countInt = 0 - func getSensors() { + + + + func getAllSensor(completionBlockSensor: @escaping (_ ReponsData: T?, _ APIClientError:Error?) -> Void) { + APIRequest.sharedInstance.getApiCall(url: "all_sensors") { ReponsData, APIClientError in + if APIClientError == nil { + do { +// let json = try JSONSerialization.jsonObject(with: ReponsData ?? Data(), options: []) +// print(json) +// let string = String(data: ReponsData ?? Data(), encoding: .utf8)! +// print(string) + let decodaData = try JSONDecoder().decode(T.self, from: ReponsData ?? Data()) + completionBlockSensor(decodaData, APIClientError) + + } catch { + print(APIClientError) + completionBlockSensor(nil, APIClientError) + } + }else{ + print(APIClientError) + completionBlockSensor(nil, APIClientError) + } + } + } + func getAllSensorValue() { + getAllSensor { (_ responseValue: SILWifiSensorsHomeViewModel?, APIClientError) in + //print(responseValue) + if let responseData = responseValue{ + self.refromArray(allData: responseData) + }else{ + self.SILWiFiSensorsViewModelDelegate?.notifySensorsData(sensorsData: []) + } + + } + } + + private func refromArray(allData: SILWifiSensorsHomeViewModel){ sensorsData = [] for val in SensorType.allSensors{ var valOfSensor = "" var tempDic = Dictionary() switch val { case SensorType.temp.rawValue: - tempDic = ["title": "\(val)", "value": temperature] + tempDic = ["title": "\(val)", "value": allData.temperature.temperatureCelcius] case SensorType.humudity.rawValue: - tempDic = ["title": "\(val)", "value": humidity] + tempDic = ["title": "\(val)", "value": allData.humidity.humidityPercentage] case SensorType.ambient.rawValue: //valOfSensor = "AL:\(ambientLightLux) WL:\(whiteLightLux)" //valOfSensor = "\(ambientLightLux) lx" - tempDic = ["title": "\(val)", "value": ["ambient_light_lux": ambientLightLux, "white_light_lux": whiteLightLux]] + tempDic = ["title": "\(val)", "value": ["ambient_light_lux": allData.light.ambientLightLux, "white_light_lux": allData.light.whiteLightLux]] case SensorType.motion.rawValue: - let motionDic = ["gyroscope": gyroscope, "accelerometer": accelerometer] + let gyroscopeDic = ["x": allData.gyroscope.x, "y": allData.gyroscope.y, "z": allData.gyroscope.z] + let accelerometerDic = ["x": allData.accelerometer.x, "y": allData.accelerometer.y, "z": allData.accelerometer.z] + let motionDic = ["gyroscope": gyroscopeDic, "accelerometer": accelerometerDic] tempDic = ["title": "\(val)", "value": motionDic] case SensorType.led.rawValue: - tempDic = ["title": "\(val)", "value": led] + let ledDic = ["red": allData.led.red, "green": allData.led.green, "blue": allData.led.blue] + tempDic = ["title": "\(val)", "value": ledDic] default: print("Have you done something new?") @@ -63,6 +101,7 @@ class SILWiFiSensorsViewModel { //let tempDic = ["title": "\(val)", "value": valOfSensor] sensorsData.append(tempDic) } + if sensorsData.count > 0 { print(sensorsData) SILWiFiSensorsViewModelDelegate?.notifySensorsData(sensorsData: sensorsData) @@ -71,25 +110,6 @@ class SILWiFiSensorsViewModel { } } - - func getAllSensor() { - APIRequest.sharedInstance.getApiCall(url: "all_sensors") { ReponsData, APIClientError in - if APIClientError == nil { - do { - let json = try JSONSerialization.jsonObject(with: ReponsData ?? Data(), options: []) - print(json) -// if let temperatureDic: Dictionary = json as? Dictionary{ -// self.temperature = "\(temperatureDic["temperature_celcius"] ?? "")" -// } - } catch { - print(APIClientError) - } - }else{ - print(APIClientError) - } - } - } - func getAllSensorData(){ if self.countInt == 0 { sensorConcurrentQueue.async(group: APIRequestdispatchGroup) { @@ -234,6 +254,42 @@ class SILWiFiSensorsViewModel { APIRequestdispatchGroup.wait() print("All functions completed wait") } + + private func getSensors() { + sensorsData = [] + for val in SensorType.allSensors{ + var valOfSensor = "" + var tempDic = Dictionary() + switch val { + case SensorType.temp.rawValue: + tempDic = ["title": "\(val)", "value": temperature] + case SensorType.humudity.rawValue: + tempDic = ["title": "\(val)", "value": humidity] + case SensorType.ambient.rawValue: + //valOfSensor = "AL:\(ambientLightLux) WL:\(whiteLightLux)" + //valOfSensor = "\(ambientLightLux) lx" + tempDic = ["title": "\(val)", "value": ["ambient_light_lux": ambientLightLux, "white_light_lux": whiteLightLux]] + case SensorType.motion.rawValue: + let motionDic = ["gyroscope": gyroscope, "accelerometer": accelerometer] + tempDic = ["title": "\(val)", "value": motionDic] + case SensorType.led.rawValue: + tempDic = ["title": "\(val)", "value": led] + + default: + print("Have you done something new?") + } + //let tempDic = ["title": "\(val)", "value": valOfSensor] + sensorsData.append(tempDic) + } + if sensorsData.count > 0 { + print(sensorsData) + SILWiFiSensorsViewModelDelegate?.notifySensorsData(sensorsData: sensorsData) + }else{ + SILWiFiSensorsViewModelDelegate?.notifySensorsData(sensorsData: sensorsData) + } + } + + func getTemperatureData(completionBlock: @escaping completionBlock) { APIRequest.sharedInstance.getApiCall(url: "temperature") { ReponsData, APIClientError in if APIClientError == nil { diff --git a/SiliconLabsApp/ViewControllers/WifiCommissioning/SILWifiCommissioningViewController.swift b/SiliconLabsApp/ViewControllers/WifiCommissioning/SILWifiCommissioningViewController.swift index 4fcd786b..6a5fd1e4 100644 --- a/SiliconLabsApp/ViewControllers/WifiCommissioning/SILWifiCommissioningViewController.swift +++ b/SiliconLabsApp/ViewControllers/WifiCommissioning/SILWifiCommissioningViewController.swift @@ -9,6 +9,10 @@ import Foundation import SVProgressHUD +protocol SILWifiCommissioningViewControllerDelegate { + func onceDeviceIsConnect(isConnectedDevice: Bool) +} + class SILWifiCommissioningViewController: UIViewController, SILWifiCommissioningPasswordPopupDelegate, SILWifiCommissioningViewModelDelegate, SILWifiCommissioningDisconnectPopupDelegate, WYPopoverControllerDelegate { @IBOutlet weak var onStartDisconnectView: UIView! @@ -25,6 +29,8 @@ class SILWifiCommissioningViewController: UIViewController, SILWifiCommissioning private var cellModels = [SILWifiCommissioningAPCellViewModel]() private var disposeBag = SILObservableTokenBag() + var demoScreenName: String = "" + var delegateWifiCommissioning:SILWifiCommissioningViewControllerDelegate? override func viewDidLoad() { super.viewDidLoad() @@ -84,9 +90,17 @@ class SILWifiCommissioningViewController: UIViewController, SILWifiCommissioning if !isConnected { self.viewModel.scan() } else { - self.showOnStartDisconnectView() + //self.showOnStartDisconnectView() + if self.demoScreenName == "typeWifiSensor" { + self.navigationController?.popViewController(animated: false) + self.delegateWifiCommissioning?.onceDeviceIsConnect(isConnectedDevice: true) + //self.connectedPeripheral.readCharacteristic(characteristic: self.readCharacteristic) + + }else { + self.showOnStartDisconnectView() + } + } - case .disconnectingStarted: SVProgressHUD.show(withStatus: "Disconnecting from access point...") @@ -111,6 +125,12 @@ class SILWifiCommissioningViewController: UIViewController, SILWifiCommissioning case .connected: SVProgressHUD.dismiss() self.showToast(message: "Access Point connected successfuly", toastType: .info, completion: {}) + if self.demoScreenName == "typeWifiSensor" { + //self.moveToSILWifiSensorsDemoView() + self.navigationController?.popViewController(animated: false) + self.delegateWifiCommissioning?.onceDeviceIsConnect(isConnectedDevice: true) + //self.connectedPeripheral.readCharacteristic(characteristic: self.readCharacteristic) + } case .connectionFailed: SVProgressHUD.dismiss() diff --git a/SiliconLabsApp/ViewModels/SILDeviceSelectionViewModel.m b/SiliconLabsApp/ViewModels/SILDeviceSelectionViewModel.m index a82bc103..9a7698fe 100644 --- a/SiliconLabsApp/ViewModels/SILDeviceSelectionViewModel.m +++ b/SiliconLabsApp/ViewModels/SILDeviceSelectionViewModel.m @@ -123,6 +123,8 @@ - (NSString *)selectDeviceInfoString { return @"A circuit board (SoC) must be connected and running firmware with a title containing \"Bluetooth - SoC Dev Kit\" or \"Bluetooth - SoC Thunderboard\"."; case SILAppTypeESLDemo: return @"A circuit board (SoC) must be connected and running firmware with a title containing \"Bluetooth - SoC NCP ESL Demo\"."; + case SILAppTypeWifiSensor: + return @"1. The SoC and evaluation board (EVK) must be connected and running the correct firmware.\n2. The Wi-Fi network on your mobile device should match the Wi-Fi network used for the sensor device's commissioning.\n3. Enable your phone's local network to access sensor data."; default: return @""; } diff --git a/SiliconLabsApp/ViewModels/WifiCommissioning/SILWifiCommissioningViewModel.swift b/SiliconLabsApp/ViewModels/WifiCommissioning/SILWifiCommissioningViewModel.swift index 4225c6a1..3e4e2d6d 100644 --- a/SiliconLabsApp/ViewModels/WifiCommissioning/SILWifiCommissioningViewModel.swift +++ b/SiliconLabsApp/ViewModels/WifiCommissioning/SILWifiCommissioningViewModel.swift @@ -314,6 +314,9 @@ class SILWifiCommissioningViewModel { accessPoint.connected = true accessPoints[index] = accessPoint print("ip", ipAddress, macAddress, value) + // Storing data + UserDefaults.standard.set("\(ipAddress)", forKey: "access_point_IPA") + self.selectedAccessPoint = accessPoint self.updateAccessPoints(accessPoint) self.wifiCommissioningState.value = .connected