Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
寻找行李
//
// DZLuggageGuardViewController.swift
// DianZiCheng
//
// Created by jackyshan on 2018/3/10.
// Copyright © 2018年 jackyshan. All rights reserved.
//
import UIKit
class DZLuggageGuardViewController: DZBaseViewController {
// MARK: - 1、属性
@IBOutlet weak var sensitivitySwitchView: DZSwitchView!
@IBOutlet weak var alarmSwitchView: DZSwitchView!
@IBOutlet weak var searchSwitchView: DZBtnSwitchView!
@IBOutlet weak var btnAlarmSwitchView: DZBtnSwitchView!
@IBOutlet weak var distanceDescLb: UILabel!
@IBOutlet weak var distanceDescImgV: UIImageView!
var model: DZSettingModel? = DZSettingModel.query(DZSettingModel.self).last
private var timer: Timer?
private var sound: GciSound?
private var isSearch: Bool = false
// MARK: - 2、生命周期
init() {
super.init(nibName: "DZLuggageGuardViewController", bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
initUI()
initLinstener()
initData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
model = DZSettingModel.query(DZSettingModel.self).last
updateModel()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
timer?.invalidate()
timer = nil
}
// MARK: 初始化ui
func initUI() {
showWriteNavController()
showLeftButton()
self.view.backgroundColor = AppConfig.XXT_LightGray
self.localString = "Luggage Guard"
}
// MARK: 初始化linstener
func initLinstener() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
if sound == nil {
let path = Bundle.main.path(forResource: "tixing", ofType: "wav")
self.sound = GciSound(FilePath: path!)
}
sensitivitySwitchView.clickBlock = { [weak self] objStr in
let unit1 = DZAlertListModel.init(title: "LOW", idx: 0, isSelected: objStr == "LOW")
let unit2 = DZAlertListModel.init(title: "NORMAL", idx: 1, isSelected: objStr == "NORMAL")
let unit3 = DZAlertListModel.init(title: "HIGH", idx: 2, isSelected: objStr == "HIGH")
let alertListView = DZAlertListView.view()
alertListView.updateModels([unit1, unit2, unit3])
alertListView.show()
alertListView.resultBlock = { [weak self] obj in
guard let wself = self else {return}
DZSettingModel.update {
wself.model?.antiLostSensitivity = obj.idx
wself.updateModel()
}
}
}
alarmSwitchView.clickBlock = { [weak self] objStr in
let unit1 = DZAlertListModel.init(title: "Vibrate Only", idx: 0, isSelected: objStr == "Vibrate Only")
let unit2 = DZAlertListModel.init(title: "Alarm Only", idx: 1, isSelected: objStr == "Alarm Only")
let unit3 = DZAlertListModel.init(title: "Vibrate and Alarm", idx: 2, isSelected: objStr == "Vibrate and Alarm")
let alertListView = DZAlertListView.view()
alertListView.updateModels([unit1, unit2, unit3])
alertListView.show()
alertListView.resultBlock = { [weak self] obj in
guard let wself = self else {return}
DZSettingModel.update {
wself.model?.antiLostAlertType = obj.idx
wself.updateModel()
}
}
}
btnAlarmSwitchView.clickBlock = { [weak self] isOn in
guard let wself = self else {return}
if let obj = wself.model {
DZManageBLE.handleDeviceSend(obj)
}
DZSettingModel.update {
wself.model?.antiLostAlert = isOn
wself.updateModel()
}
}
searchSwitchView.clickBlock = { [weak self] isOn in
self?.isSearch = isOn
}
BLEScanner.sharedInstance.bleDidReadRSSI = { [weak self] rssi in
Log.i(rssi)
self?.rssiUpdate(rssi.int32Value)
}
}
// MARK: 初始化data
func initData() {
distanceDescLb.text = "..."
}
// MARK: 设置frame
override func didSystemAutoLayoutComplete() {
}
// MARK: - 3、代理
// MARK: - 4、业务
func updateModel() {
sensitivitySwitchView.updateTitle(model?.antiLostSensitivity == 0 ? "LOW" : model?.antiLostSensitivity == 1 ? "NORMAL" : "HIGH")
alarmSwitchView.updateTitle(model?.antiLostAlertType == 0 ? "Vibrate Only" : model?.antiLostAlertType == 1 ? "Alarm Only" : "Vibrate and Alarm")
btnAlarmSwitchView.updateOn(model?.antiLostAlert == true)
}
func timerAction() {
guard let peripheral = BLEScanner.sharedInstance.bleConnectedDevice?.peripheral else {return}
peripheral.readRSSI()
Log.i("读取rssi")
}
/**
计算公式:
d = 10^((abs(RSSI) - A) / (10 * n))
其中:
d - 计算所得距离
RSSI - 接收信号强度(负值)
A - 发射端和接收端相隔1米时的信号强度
n - 环境衰减因子
*/
//当前读取了多少次rssi
private var readRssiCount: Int32 = 0
//读取周期内的rssi总和
private var rssiTotal: Int32 = 0
//读取几次为一个周期
private var readPeriod: Int32 = 15
private var rssiA: Int32 = 0
private var newRssi: Int32 = 0
private var lastRssi: Int32 = 0
func calcDistByRSSI(rssi: Int32) -> Double? {
//累加
readRssiCount += 1
rssiTotal += rssi
if readRssiCount % readPeriod != 0 {
//没有到一个周期不处理
return nil
}
else {
//累计够了次数,则计算平均rssi,并清零rssiTotal
newRssi = rssiTotal / readPeriod
rssiTotal = 0
//记录第一次统计的rssi,设置为A值,用于计算距离
if readRssiCount < 19 {
rssiA = abs(newRssi)
Log.i("RSSI_A:" + String(rssiA))
}
}
let iRssi: Int32 = abs(newRssi)
let power: Double = Double(iRssi-rssiA)/(10*1.7)
return pow(10, power)
}
func rssiUpdate(_ rssi: Int32) {
if isSearch == false && model?.antiLostAlert == false {
readRssiCount = 0
rssiTotal = 0
}
guard let distance = calcDistByRSSI(rssi: rssi) else {return}
Log.i("rssi: " + String(rssi) + "; distance: " + String(distance) + "m")
//如果开启了距离搜索
if isSearch == true {
//两次变化比较的值大于5才会比较有意义
if abs(abs(newRssi) - abs(lastRssi)) > 5 {
distanceDescImgV.image = abs(newRssi) > abs(lastRssi) ? #imageLiteral(resourceName: "icon_yl") : #imageLiteral(resourceName: "icon_kj")
//记录新值用于下次比较
lastRssi = newRssi
}
}
//开启了防丢失
if model?.antiLostAlert == true {
//判断距离,做出警告
let lostAlertSensity = model?.antiLostSensitivity
let alertDistance: Double = lostAlertSensity == 0 ? 40 : lostAlertSensity == 1 ? 30 : 20
if distance > alertDistance {
doLostRemind()
}
}
}
var lostAlertLock = false
func doLostRemind() {
//正在弹出, 不显示第二次
guard lostAlertLock == false else {return}
guard let alertType = model?.antiLostAlertType else {return}
if alertType != 0 {
DZManageBLE.writeToDeviceAlertLong(true)//设备蜂鸣器触发
}
switch alertType {
case 0:
self.sound?.playVibrateAndCompletionAction()
break
case 1:
self.sound?.playAlertAndCompletionAction()
break
case 2:
self.sound?.playAlterVibrateAndCompletionAction()
break
default:
break
}
let alertView = DZAntiLostAlertView.view()
alertView.dismissBlock = {
self.lostAlertLock = false
self.sound?.stopPlay()
}
alertView.show()
lostAlertLock = true
}
override func backBtnAction() {
if let vcs = self.navigationController?.viewControllers {
for vc in vcs {
if vc is DZWeighingViewController {
self.navigationController?.popToViewController(vc, animated: true)
break
}
}
}
}
// MARK: - 5、网络
// MARK: - 6、其他
deinit {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.