Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PinxinTakeMeasurementScenario.swift
- // Harmonize
- //
- // Created by Valerii Hvozdiev on 9/18/19.
- // Copyright © 2019 Harmonize. All rights reserved.
- //
- import Foundation
- import SwiftyTimer
- class PinxinTakeMeasurementScenario : ScenarioProtocol, SensorCommunicatorRouterSimpleDelegate {
- class Constants {
- static let weightLowerBound : Float = 20.0
- static let valuesInRowNeededForAccept = 4
- static let acceptTimeout = 5.0
- }
- var scenarioDelegate: ScenarioDelegate?
- var scenarioIsCompletedSuccessfully: Bool = false
- var measurementDelegate: WeightTakeMeasurementScenarioDelegate?
- private var peripheralController : PeripheralControllerProtocol!
- private var valueAcceptTimer : Timer?
- private var sameWeightInRowCount = 0
- private var lastWeightValue : Float = 0
- func start() {
- sameWeightInRowCount = 0
- valueAcceptTimer = nil
- lastWeightValue = 0
- measurementDelegate?.measurementBegan()
- peripheralController.setCharacteristicNotification(Devices.PinxinWeight.weightNotifyCharacteristic.identifier)
- peripheralController.writeCharacteristic(Devices.PinxinWeight.weightSettingCharacteristic.identifier, value: Data([1,1]))
- }
- func stop() {
- peripheralController.requestDeviceDisconnect()
- }
- func restart() {
- start()
- }
- func setController(_ controller: PeripheralControllerProtocol) {
- self.peripheralController = controller
- }
- func onReadCharacteristic(_ characteristic: SensorCharacteristicIdentifier) {
- guard let data = characteristic.value?.map({Int($0)}), data.count >= 13 else { return }
- let weightValue = Float(data[12] << 8 + data[11]) / 10.0
- receiveValue(weightLbs: weightValue)
- }
- private func receiveValue(weightLbs: Float) {
- // reject values lower then 20lbs
- if weightLbs < Constants.weightLowerBound {
- sameWeightInRowCount = 0
- return
- }
- print("same in row count \(sameWeightInRowCount) \(lastWeightValue)")
- // count how many equal weight values were received in row
- if weightLbs == lastWeightValue {
- sameWeightInRowCount += 1
- } else {
- sameWeightInRowCount = 0
- valueAcceptTimer?.invalidate()
- lastWeightValue = weightLbs
- }
- // start finisher timer if we have 5 measurements and new measurements
- // will not be received in next 5 seconds
- guard sameWeightInRowCount == Constants.valuesInRowNeededForAccept else { return }
- print("timer reset")
- valueAcceptTimer?.invalidate()
- valueAcceptTimer = Timer.after(Constants.acceptTimeout.seconds, { [weak self] in
- print("weight timer fired")
- self?.finishMeasurement()
- })
- }
- private func finishMeasurement() {
- scenarioIsCompletedSuccessfully = true
- peripheralController.requestDeviceDisconnect()
- measurementDelegate?.measurementFinished(weightLbs: lastWeightValue)
- scenarioDelegate?.scenarioExecutionFinished(scenario: self)
- }
- deinit {
- valueAcceptTimer?.invalidate()
- valueAcceptTimer = nil
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement