<commit_before>//
//  BTDiscovery.swift
//  GarageOpener
//
//  Created by <NAME> on 10/01/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import CoreBluetooth

class BTDiscoveryManager: NSObject, CBCentralManagerDelegate {
    
    private let nc = NSNotificationCenter.defaultCenter()
    private let centralManager: CBCentralManager?
    
    let btConst = BTConstants()
    
    var activePeripheral : CBPeripheral? {
        didSet {
            println("activePeripheral got set")
            
        }
    }
    var activeService : BTService? {
        didSet {
            println("activeService got set")
            if let service = self.activeService {
                service.startDiscoveringServices()
            }
        }
    }
    
    override init() {
        super.init()
        
        let queue = dispatch_queue_create("no.malt", DISPATCH_QUEUE_SERIAL)
        centralManager = CBCentralManager(delegate: self, queue: queue)
    }
    
    func sayHello() -> String {
        println("Testing output.");
        return "Hello from BTDiscovery.";
    }
    
    
    func startScanning() {
        if let central = self.centralManager {
            central.scanForPeripheralsWithServices(nil, options: nil)
        }
    }
    
    func resetConnection() {
        self.activeService = nil
        self.activePeripheral = nil
    }
    
    //
    // Implementation of CBCentralManagerDelegate
    //
    
    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        println("Called didConnectPeripheral");
        
        if (peripheral == nil) {
            println("  peripheral argument was nil")
            return
        }
        
        let uuid = peripheral.identifier.UUIDString;
        
        println("  debug: " + peripheral.debugDescription)
        println("  identifier: \(uuid)")
        println("  name:       \(peripheral.name)")
        
        if (peripheral == self.activePeripheral) {
            println("  Got identical peripheral as in discovery. creating BTService object.")
            self.activeService = BTService(initWithPeripheral: peripheral)
        }
        
        println("Stopping scan!")
        central.stopScan()
    }
    
    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("Called did Disconnect Peripheral: \(peripheral)")
        
        if (peripheral == self.activePeripheral) {
            self.resetConnection()
            self.startScanning()
        }
    }
    
    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        
        let uuid = peripheral.identifier.UUIDString;
        
        println("Called did Discover Peripheral: \(uuid)")
        
        if ((self.activePeripheral != nil) && (self.activePeripheral?.state == CBPeripheralState.Connected)) {
            println("activePeripheral is alrady set and connected.")
            return
        }
        
        if (uuid == btConst.IDENTIFIER_UUID) {
            println("  Device match!")
            
            self.activePeripheral = peripheral
            
            println("  Connecting!")
            nc.postNotificationName("btFoundDeviceNotification", object: true)
            central.connectPeripheral(peripheral, options: nil)
        }
        
    }
    
    func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("Called didFailToConnectPeripheral")
    }
    
    func centralManager(central: CBCentralManager!, didRetrieveConnectedPeripherals peripherals: [AnyObject]!) {
        println("Called didRetrieveConnectedPeripherals")
    }
    
    func centralManager(central: CBCentralManager!, didRetrievePeripherals peripherals: [AnyObject]!) {
        println("called didRetrievePeripherals");
    }
    
    func centralManagerDidUpdateState(central: CBCentralManager!) {
        // println("Called centralManagerDidUpdateState: \(central.state)")
        
        switch (central.state) {
        case CBCentralManagerState.PoweredOn:
            println("BLE state Powered On")
            nc.postNotificationName(
                "btStateChangedNotification",
                object: "Scanning for peripherals..."
            )
            
            self.startScanning()
            
            break
        case CBCentralManagerState.Unknown:
            println("BLE state unknown")
            break
        case CBCentralManagerState.Resetting:
            println("BLE state resetting")
            break
        case CBCentralManagerState.Unsupported:
            println("BLE state unsupported")
            break
        case CBCentralManagerState.Unauthorized:
            println("BLE state unauthorized")
            break
        case CBCentralManagerState.PoweredOff:
            println("BLE state powered off")
            break
        default:
            break
        }
        
    }
    
    
}<commit_msg>Added NSTimer to fetch the rssi value every 2 seconds Implemented delay function with closure found on stack overflow to have an easier delay than nastier added notification to start scanning Implemented selector function for nastier Better notifications for disconnected state Added fix to only connect to peripheral when above certain rssi level when low signal when 1 second between scans<commit_after>//
//  BTDiscovery.swift
//  GarageOpener
//
//  Created by <NAME> on 10/01/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import CoreBluetooth

class BTDiscoveryManager: NSObject, CBCentralManagerDelegate {
    
    private let nc = NSNotificationCenter.defaultCenter()
    private let centralManager: CBCentralManager?
    
    let btConst = BTConstants()
    
    var activePeripheral : CBPeripheral? {
        didSet {
            println("activePeripheral got set")
        }
    }
    var activeService : BTService? {
        didSet {
            println("activeService got set")
            if let service = self.activeService {
                service.startDiscoveringServices()
            }
        }
    }
    
    override init() {
        super.init()
        
        let queue = dispatch_queue_create("no.malt", DISPATCH_QUEUE_SERIAL)
        centralManager = CBCentralManager(delegate: self, queue: queue)
        
        NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("doReadRSSI"), userInfo: nil, repeats: true)
    }
    
    func sayHello() -> String {
        println("Testing output.");
        return "Hello from BTDiscovery.";
    }
    
    
    
    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure
        )
    }
    

    func startScanning() {
        nc.postNotificationName("btStateChangedNotification", object: "Scanning")
        
        if let central = self.centralManager {
            central.scanForPeripheralsWithServices([CBUUID(string: btConst.SERVICE_UUID)], options: nil)
        }
    }
    
    func resetConnection() {
        self.activeService = nil
        self.activePeripheral = nil
    }
    
    
    func doReadRSSI() {
        
        if self.activePeripheral == nil { return }
        if self.activePeripheral?.state != CBPeripheralState.Connected { return }
        
        // println("got timer set right: and connected")
        
        self.activePeripheral?.readRSSI()
    }
    
    //
    // Implementation of CBCentralManagerDelegate
    //
    
    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        println("Called didConnectPeripheral");
        
        if (peripheral == nil) {
            println("  peripheral argument was nil")
            return
        }
        
        let uuid = peripheral.identifier.UUIDString;
        
        println("  debug: " + peripheral.debugDescription)
        println("  identifier: \(uuid)")
        println("  name:       \(peripheral.name)")
        
        if (peripheral == self.activePeripheral) {
            println("  Got identical peripheral as in discovery. creating BTService object.")
            self.activeService = BTService(initWithPeripheral: peripheral)
        }
        
        println("Stopping scan!")
        central.stopScan()
    }
    
    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("Called did Disconnect Peripheral: \(peripheral)")
        
        nc.postNotificationName("btStateChangedNotification", object: "Disconnected", userInfo: ["peripheral": peripheral])
        
        if (peripheral == self.activePeripheral) {
            self.resetConnection()
            self.startScanning()
        }
    }
    
    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        
        let uuid = peripheral.identifier.UUIDString;
        
        println("Called did Discover Peripheral: \(uuid)")
        
        if ((self.activePeripheral != nil) && (self.activePeripheral?.state == CBPeripheralState.Connected)) {
            println("activePeripheral is alrady set and connected.")
            return
        }
        
        if (RSSI.integerValue == 127 || RSSI.integerValue < -85) {
            self.handleLowSignal(RSSI.integerValue)
            return
        }
            
        self.activePeripheral = peripheral
            
        println("  Connecting!")
        nc.postNotificationName("btFoundDeviceNotification", object: true,
            userInfo: ["peripheral": peripheral, "RSSI": RSSI])
        
        central.connectPeripheral(peripheral, options: nil)
        
        
    }
    
    func handleLowSignal(signal: Int) {
        println("  Do not connect rssi: \(signal)")
        var msg = "Low Signal: \(signal)"
        nc.postNotificationName("btStateChangedNotification", object: msg)
        
        self.centralManager?.stopScan()
        self.resetConnection()
        delay(1.0) {
            // Wait one second to start scanning again.
            self.startScanning()
        }
    }
    
    func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("Called didFailToConnectPeripheral")
    }
    
    func centralManager(central: CBCentralManager!, didRetrieveConnectedPeripherals peripherals: [AnyObject]!) {
        println("Called didRetrieveConnectedPeripherals")
    }
    
    func centralManager(central: CBCentralManager!, didRetrievePeripherals peripherals: [AnyObject]!) {
        println("called didRetrievePeripherals");
    }
    
    func centralManagerDidUpdateState(central: CBCentralManager!) {
        var state : String?
        
        switch (central.state) {
        case CBCentralManagerState.PoweredOn:
            println("BLE state Powered On")
            state = nil
            self.startScanning()
            break
        case CBCentralManagerState.Unknown:
            println("BLE state unknown")
            state = "Unknown"
            break
        case CBCentralManagerState.Resetting:
            println("BLE state resetting")
            state = "Resetting"
            break
        case CBCentralManagerState.Unsupported:
            println("BLE state unsupported")
            state = "Unsupported Device"
            break
        case CBCentralManagerState.Unauthorized:
            println("BLE state unauthorized")
            state = "Unauthorized"
            break
        case CBCentralManagerState.PoweredOff:
            println("BLE state powered off")
            state = "Bluetooth Off"
            self.resetConnection()
            break
        default:
            break
        }
        
        if state == nil { return }
        
        nc.postNotificationName(
            "btStateChangedNotification",
            object: state
        )
    }
}
{'fraction_non_alphanumeric': 0.08655944395053745, 'fraction_numerical': 0.0026670977127616585, 'mean_word_length': 2.0850162054350534, 'pattern_counts': {'":': 3, '<': 8, '<?xml version=': 0, '>': 10, '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': '5612511', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3150, 'n_words': 729}