<commit_before>//
//  PanoPeripheral.swift
//  PanoController
//
//  Created by <NAME> on 8/12/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import CoreBluetooth

class PanoPeripheral : NSObject, CBPeripheralDelegate {
    static let status = Status.status
    static let config = Config.config

    static let serviceUUID = CBUUID(string: "2017")
    static let configCharUUID = CBUUID(string: "0001")
    static let statusCharUUID = CBUUID(string: "0002")
    static let cmdCharUUID = CBUUID(string: "0003")

    var peripheral: CBPeripheral?

    init(_ peripheral: CBPeripheral) {
        super.init()
        self.peripheral = peripheral
        self.peripheral!.delegate = self
        peripheral.discoverServices(nil)
    }

    // Mark: - CBPeripheralDelegate

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services for \(peripheral.name!)")
        for service in peripheral.services! {
            let thisService = service as CBService
            print("    ", thisService)
            peripheral.discoverCharacteristics(nil, for: thisService)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("    Characteristics for \(String(describing: service))")
        for characteristic in service.characteristics! {
            let thisCharacteristic = characteristic as CBCharacteristic
            print("         ", thisCharacteristic)
            if thisCharacteristic.uuid == PanoPeripheral.statusCharUUID {
                peripheral.setNotifyValue(true, for: thisCharacteristic)
                peripheral.readValue(for: thisCharacteristic)
            }
            if thisCharacteristic.uuid == PanoPeripheral.configCharUUID {
                print("sending \(PanoPeripheral.config.pack())")
                peripheral.writeValue(PanoPeripheral.config.pack(), for: thisCharacteristic, type: .withoutResponse)
            }
            //peripheral.discoverDescriptors(for: thisCharacteristic)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
        print("          Descriptors for \(String(describing: characteristic))")
        for descriptor in characteristic.descriptors! {
            let thisDescriptor = descriptor as CBDescriptor
            print("             ", thisDescriptor)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print("didUpdateValueFor \(String(describing: characteristic))")
        switch characteristic.uuid {
        case PanoPeripheral.configCharUUID:
            print(characteristic.value!)

        case PanoPeripheral.statusCharUUID:
            print(characteristic.value!)
            PanoPeripheral.status.unpack(characteristic.value!)
            print(PanoPeripheral.status)

        default:
            print("Received update for unknown characteristic \(String(describing: characteristic))")
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
        print("didUpdateNotificationStateFor \(String(describing: characteristic))")
    }
}
<commit_msg>Add sendConfig() to send updates, readStatus() to read the latest status (kind of nop right now)<commit_after>//
//  PanoPeripheral.swift
//  PanoController
//
//  Created by <NAME> on 8/12/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import CoreBluetooth

class PanoPeripheral : NSObject, CBPeripheralDelegate {
    static let status = Status.status
    static let config = Config.config

    static let serviceUUID = CBUUID(string: "2017")
    static let configCharUUID = CBUUID(string: "0001")
    static let statusCharUUID = CBUUID(string: "0002")
    static let cmdCharUUID = CBUUID(string: "0003")
    var statusChar: CBCharacteristic?
    var configChar: CBCharacteristic?
    var cmdChar: CBCharacteristic?

    var peripheral: CBPeripheral?

    init(_ peripheral: CBPeripheral) {
        super.init()
        self.peripheral = peripheral
        self.peripheral!.delegate = self
        peripheral.discoverServices(nil)
    }

    func sendConfig(_ config: Config) {
        print("sending \(PanoPeripheral.config.pack())")
        if let characteristic = configChar {
            peripheral?.writeValue(config.pack(), for: characteristic, type: .withoutResponse)
        }
    }

    func readStatus() -> Status {
        return PanoPeripheral.status
    }

    // Mark: - CBPeripheralDelegate

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services for \(peripheral.name!)")
        for service in peripheral.services! {
            let thisService = service as CBService
            print("    ", thisService)
            peripheral.discoverCharacteristics(nil, for: thisService)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("    Characteristics for \(String(describing: service))")
        for characteristic in service.characteristics! {
            let thisCharacteristic = characteristic as CBCharacteristic
            print("         ", thisCharacteristic)
            if thisCharacteristic.uuid == PanoPeripheral.statusCharUUID {
                statusChar = thisCharacteristic
                peripheral.setNotifyValue(true, for: thisCharacteristic)
                peripheral.readValue(for: thisCharacteristic)
            }
            if thisCharacteristic.uuid == PanoPeripheral.configCharUUID {
                configChar = thisCharacteristic
                sendConfig(PanoPeripheral.config)
            }
            if thisCharacteristic.uuid == PanoPeripheral.cmdCharUUID {
                cmdChar = thisCharacteristic
            }
            //peripheral.discoverDescriptors(for: thisCharacteristic)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
        print("          Descriptors for \(String(describing: characteristic))")
        for descriptor in characteristic.descriptors! {
            let thisDescriptor = descriptor as CBDescriptor
            print("             ", thisDescriptor)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print("didUpdateValueFor \(String(describing: characteristic))")
        switch characteristic.uuid {
        case PanoPeripheral.configCharUUID:
            print(characteristic.value!)

        case PanoPeripheral.statusCharUUID:
            print(characteristic.value!)
            PanoPeripheral.status.unpack(characteristic.value!)
            print(PanoPeripheral.status)

        default:
            print("Received update for unknown characteristic \(String(describing: characteristic))")
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
        print("didUpdateNotificationStateFor \(String(describing: characteristic))")
    }
}

{'fraction_non_alphanumeric': 0.08700380641653073, 'fraction_numerical': 0.006797172376291463, 'mean_word_length': 2.768954918032787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '4502783', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1804, 'n_words': 449}