<commit_before>//
//  RileyLinkDeviceManager.swift
//  RileyLink
//
//  Created by <NAME> on 4/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import RileyLinkBLEKit


public class RileyLinkDeviceManager {
    
    public static let DidDiscoverDeviceNotification = "com.rileylink.RileyLinkKit.DidDiscoverDeviceNotification"
    
    public static let ConnectionStateDidChangeNotification = "com.rileylink.RileyLinkKit.ConnectionStateDidChangeNotification"

    public static let RSSIDidChangeNotification = "com.rileylink.RileyLinkKit.RSSIDidChangeNotification"

    public static let RileyLinkDeviceKey = "com.rileylink.RileyLinkKit.RileyLinkDevice"
    public static let RileyLinkRSSIKey = "com.rileylink.RileyLinkKit.RileyLinkRSSI"
    
    public var pumpState: PumpState? {
        didSet {
            for device in _devices {
                device.pumpState = pumpState
            }
        }
    }
    
    public init(pumpState: PumpState?, autoConnectIDs: Set<String>) {
        self.pumpState = pumpState
        
        BLEManager.autoConnectIds = autoConnectIDs
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(discoveredBLEDevice(_:)), name: RILEYLINK_EVENT_LIST_UPDATED, object: BLEManager)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(connectionStateDidChange(_:)), name: RILEYLINK_EVENT_DEVICE_CONNECTED, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(connectionStateDidChange(_:)), name: RILEYLINK_EVENT_DEVICE_DISCONNECTED, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rssiDidChange(_:)), name: RILEYLINK_EVENT_RSSI_CHANGED, object: nil)
    }
    
    public var deviceScanningEnabled: Bool {
        get {
            return BLEManager.scanningEnabled
        }
        set {
            BLEManager.scanningEnabled = newValue
        }
    }
    
    public var timerTickEnabled = true {
        didSet {
            for device in _devices {
                device.device.timerTickEnabled = timerTickEnabled
            }
        }
    }
    
    private var _devices: [RileyLinkDevice] = []
    
    public var devices: [RileyLinkDevice] {
        return _devices
    }
    
    public var firstConnectedDevice: RileyLinkDevice? {
        if let index = _devices.indexOf({ $0.peripheral.state == .Connected }) {
            return _devices[index]
        } else {
            return nil
        }
    }
    
    public func connectDevice(device: RileyLinkDevice) {
        BLEManager.connectPeripheral(device.peripheral)
    }
    
    public func disconnectDevice(device: RileyLinkDevice) {
        BLEManager.disconnectPeripheral(device.peripheral)
    }
    
    private let BLEManager = RileyLinkBLEManager()
    
    // MARK: - RileyLinkBLEManager
    
    @objc private func discoveredBLEDevice(note: NSNotification) {
        if let BLEDevice = note.userInfo?["device"] as? RileyLinkBLEDevice {
            BLEDevice.timerTickEnabled = timerTickEnabled
            
            let device = RileyLinkDevice(BLEDevice: BLEDevice, pumpState: pumpState)
            
            _devices.append(device)
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.DidDiscoverDeviceNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device])
            
        }
    }
    
    @objc private func connectionStateDidChange(note: NSNotification) {
        if let BLEDevice = note.object as? RileyLinkBLEDevice,
            index = _devices.indexOf({ $0.peripheral == BLEDevice.peripheral }) {
            let device = _devices[index]
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.ConnectionStateDidChangeNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device])
            
            if device.peripheral.state == CBPeripheralState.Connected {
                
            }
        }
    }
    
    @objc private func rssiDidChange(note: NSNotification) {
        if let BLEDevice = note.object as? RileyLinkBLEDevice,
            index = _devices.indexOf({ $0.peripheral == BLEDevice.peripheral }) {
            let device = _devices[index]
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.RSSIDidChangeNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device, self.dynamicType.RileyLinkRSSIKey: note.userInfo!["RSSI"]!])
        }
    }
    
}<commit_msg>unused code<commit_after>//
//  RileyLinkDeviceManager.swift
//  RileyLink
//
//  Created by <NAME> on 4/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import RileyLinkBLEKit


public class RileyLinkDeviceManager {
    
    public static let DidDiscoverDeviceNotification = "com.rileylink.RileyLinkKit.DidDiscoverDeviceNotification"
    
    public static let ConnectionStateDidChangeNotification = "com.rileylink.RileyLinkKit.ConnectionStateDidChangeNotification"

    public static let RSSIDidChangeNotification = "com.rileylink.RileyLinkKit.RSSIDidChangeNotification"

    public static let RileyLinkDeviceKey = "com.rileylink.RileyLinkKit.RileyLinkDevice"
    public static let RileyLinkRSSIKey = "com.rileylink.RileyLinkKit.RileyLinkRSSI"
    
    public var pumpState: PumpState? {
        didSet {
            for device in _devices {
                device.pumpState = pumpState
            }
        }
    }
    
    public init(pumpState: PumpState?, autoConnectIDs: Set<String>) {
        self.pumpState = pumpState
        
        BLEManager.autoConnectIds = autoConnectIDs
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(discoveredBLEDevice(_:)), name: RILEYLINK_EVENT_LIST_UPDATED, object: BLEManager)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(connectionStateDidChange(_:)), name: RILEYLINK_EVENT_DEVICE_CONNECTED, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(connectionStateDidChange(_:)), name: RILEYLINK_EVENT_DEVICE_DISCONNECTED, object: nil)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(rssiDidChange(_:)), name: RILEYLINK_EVENT_RSSI_CHANGED, object: nil)
    }
    
    public var deviceScanningEnabled: Bool {
        get {
            return BLEManager.scanningEnabled
        }
        set {
            BLEManager.scanningEnabled = newValue
        }
    }
    
    public var timerTickEnabled = true {
        didSet {
            for device in _devices {
                device.device.timerTickEnabled = timerTickEnabled
            }
        }
    }
    
    private var _devices: [RileyLinkDevice] = []
    
    public var devices: [RileyLinkDevice] {
        return _devices
    }
    
    public var firstConnectedDevice: RileyLinkDevice? {
        if let index = _devices.indexOf({ $0.peripheral.state == .Connected }) {
            return _devices[index]
        } else {
            return nil
        }
    }
    
    public func connectDevice(device: RileyLinkDevice) {
        BLEManager.connectPeripheral(device.peripheral)
    }
    
    public func disconnectDevice(device: RileyLinkDevice) {
        BLEManager.disconnectPeripheral(device.peripheral)
    }
    
    private let BLEManager = RileyLinkBLEManager()
    
    // MARK: - RileyLinkBLEManager
    
    @objc private func discoveredBLEDevice(note: NSNotification) {
        if let BLEDevice = note.userInfo?["device"] as? RileyLinkBLEDevice {
            BLEDevice.timerTickEnabled = timerTickEnabled
            
            let device = RileyLinkDevice(BLEDevice: BLEDevice, pumpState: pumpState)
            
            _devices.append(device)
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.DidDiscoverDeviceNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device])
            
        }
    }
    
    @objc private func connectionStateDidChange(note: NSNotification) {
        if let BLEDevice = note.object as? RileyLinkBLEDevice,
            index = _devices.indexOf({ $0.peripheral == BLEDevice.peripheral }) {
            let device = _devices[index]
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.ConnectionStateDidChangeNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device])            
        }
    }
    
    @objc private func rssiDidChange(note: NSNotification) {
        if let BLEDevice = note.object as? RileyLinkBLEDevice,
            index = _devices.indexOf({ $0.peripheral == BLEDevice.peripheral }) {
            let device = _devices[index]
            
            NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.RSSIDidChangeNotification, object: self, userInfo: [self.dynamicType.RileyLinkDeviceKey: device, self.dynamicType.RileyLinkRSSIKey: note.userInfo!["RSSI"]!])
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08233630628670872, 'fraction_numerical': 0.0026103980857080703, 'mean_word_length': 2.706166868198307, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7545402', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2523, 'n_words': 470}