离线运行
离线使用 BLE/LAN 控制前,需要内置产品数据点配置。登录、云端绑定、云端确认配网和下载固件仍需要网络。将数据点 JSON 以 <productKey>.json 命名,加入 productConfig.bundle 并包含在 App 资源中。SDK 缓存目录为 Documents/productConfig/<openAPIInfo>/;使用自定义云域名时也要为该域名准备目录。
import Foundation
func copyProductConfigFiles() throws {
guard let source = Bundle.main.url(forResource: "productConfig", withExtension: "bundle") else {
return
}
let fm = FileManager.default
let documents = try fm.url(for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: true)
let files = try fm.contentsOfDirectory(at: source, includingPropertiesForKeys: nil)
.filter { $0.pathExtension == "json" }
for host in ["api.gizwits.com", "euapi.gizwits.com", "usapi.gizwits.com"] {
let directory = documents.appendingPathComponent("productConfig").appendingPathComponent(host)
try fm.createDirectory(at: directory, withIntermediateDirectories: true)
for file in files {
let destination = directory.appendingPathComponent(file.lastPathComponent)
if !fm.fileExists(atPath: destination.path) {
try fm.copyItem(at: file, to: destination)
}
}
}
}
// Run before SDK initialization; handle errors in the app.
try copyProductConfigFiles()数据点文件可在联网时获取:
curl 'https://api.gizwits.com/app/datapoint?product_key=<productKey>' -o '<productKey>.json'cleanCache() 会清理产品配置缓存,离线场景清理后需要重新准备文件。