SHARE
TWEET

Untitled

a guest Sep 18th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  PinxinTakeMeasurementScenario.swift
  3. //  Harmonize
  4. //
  5. //  Created by Valerii Hvozdiev on 9/18/19.
  6. //  Copyright © 2019 Harmonize. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import SwiftyTimer
  11.  
  12. class PinxinTakeMeasurementScenario : ScenarioProtocol, SensorCommunicatorRouterSimpleDelegate {
  13.    
  14.     class Constants {
  15.         static let weightLowerBound : Float = 20.0
  16.         static let valuesInRowNeededForAccept = 4
  17.         static let acceptTimeout = 5.0
  18.     }
  19.    
  20.     var scenarioDelegate: ScenarioDelegate?
  21.     var scenarioIsCompletedSuccessfully: Bool = false
  22.     var measurementDelegate: WeightTakeMeasurementScenarioDelegate?
  23.    
  24.     private var peripheralController : PeripheralControllerProtocol!
  25.     private var valueAcceptTimer : Timer?
  26.     private var sameWeightInRowCount = 0
  27.     private var lastWeightValue : Float = 0
  28.    
  29.     func start() {
  30.         sameWeightInRowCount = 0
  31.         valueAcceptTimer = nil
  32.         lastWeightValue = 0
  33.        
  34.         measurementDelegate?.measurementBegan()
  35.         peripheralController.setCharacteristicNotification(Devices.PinxinWeight.weightNotifyCharacteristic.identifier)
  36.         peripheralController.writeCharacteristic(Devices.PinxinWeight.weightSettingCharacteristic.identifier, value: Data([1,1]))
  37.     }
  38.    
  39.     func stop() {
  40.         peripheralController.requestDeviceDisconnect()
  41.     }
  42.    
  43.     func restart() {
  44.         start()
  45.     }
  46.    
  47.     func setController(_ controller: PeripheralControllerProtocol) {
  48.         self.peripheralController = controller
  49.     }
  50.  
  51.     func onReadCharacteristic(_ characteristic: SensorCharacteristicIdentifier) {
  52.         guard let data = characteristic.value?.map({Int($0)}), data.count >= 13 else { return }
  53.         let weightValue = Float(data[12] << 8 + data[11]) / 10.0
  54.         receiveValue(weightLbs: weightValue)
  55.     }
  56.    
  57.     private func receiveValue(weightLbs: Float) {
  58.         // reject values lower then 20lbs
  59.         if weightLbs < Constants.weightLowerBound {
  60.             sameWeightInRowCount = 0
  61.             return
  62.         }
  63.        
  64.         print("same in row count \(sameWeightInRowCount) \(lastWeightValue)")
  65.         // count how many equal weight values were received in row
  66.         if weightLbs == lastWeightValue {
  67.             sameWeightInRowCount += 1
  68.         } else {
  69.             sameWeightInRowCount = 0
  70.             valueAcceptTimer?.invalidate()
  71.             lastWeightValue = weightLbs
  72.         }
  73.        
  74.         // start finisher timer if we have 5 measurements and new measurements
  75.         // will not be received in next 5 seconds
  76.         guard sameWeightInRowCount == Constants.valuesInRowNeededForAccept else { return }
  77.        
  78.         print("timer reset")
  79.         valueAcceptTimer?.invalidate()
  80.         valueAcceptTimer = Timer.after(Constants.acceptTimeout.seconds, { [weak self] in
  81.             print("weight timer fired")
  82.             self?.finishMeasurement()
  83.         })
  84.     }
  85.    
  86.     private func finishMeasurement() {
  87.         scenarioIsCompletedSuccessfully = true
  88.         peripheralController.requestDeviceDisconnect()
  89.         measurementDelegate?.measurementFinished(weightLbs: lastWeightValue)
  90.         scenarioDelegate?.scenarioExecutionFinished(scenario: self)
  91.     }
  92.    
  93.     deinit {
  94.         valueAcceptTimer?.invalidate()
  95.         valueAcceptTimer = nil
  96.     }
  97.    
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top