Commit 3fb7eb48 authored by Thomas Mellenthin's avatar Thomas Mellenthin

cleanup, refactoring

parent 841d70a8
......@@ -23,13 +23,20 @@ struct AboveLEDs {
return result
}
public static func randomRGB() -> AboveLEDs {
var LEDs: [RGBLED] = []
private static func combineValues(_ rgbValue: () -> RGBLED) -> AboveLEDs {
var LEDs: [RGBLED] = []
for _ in 0 ..< 11 {
LEDs.append(RGBLED.randomRGB())
LEDs.append(rgbValue())
}
return AboveLEDs(LEDs: LEDs)
}
public static func randomRGB() -> AboveLEDs {
return combineValues({ RGBLED.randomRGB() })
}
public static func off() -> AboveLEDs {
return combineValues({ RGBLED(red: 0, green: 0, blue: 0) })
}
}
......@@ -158,12 +158,12 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
}
public func sendFileData(data: Data, fileName: String) {
guard self.connectedPeripheral != nil else {
subscribers.forEach { $0.value?.didFailToSendFile() }
return
}
self.dataTransferManager = DataTransferManager(self)
self.subscribeToRxChannel()
......@@ -249,8 +249,8 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
guard let aboveCharacteristic = self.findCharacteristic(on: peripheral,
forServiceUUID: card10ServiceUUID,
forCharacteristicUUID: card10LedsAboveCharacteristic) else {
print("setLEDsAbove could not find card10RocketsCharacteristic!")
return
print("setLEDsAbove could not find card10RocketsCharacteristic!")
return
}
self.connectedPeripheral?.writeValue(aboveLEDs.data,
......
......@@ -25,7 +25,7 @@ class ChunkedDataReader: NSObject {
guard dataToReturn!.count > 0 else { return nil }
currentOffset += packageSize
return dataToReturn
}
......
......@@ -96,7 +96,7 @@ class DataTransferManager: NSObject {
sendPackage(type: .CHUNK, payload: self.currentChunkToSend, offset: self.chunkedReader!.currentOffset-20)
retryCount += 1
} else {
self.sendPackage(type: .ERROR, payload: nil,offset: 0)
self.sendPackage(type: .ERROR, payload: nil, offset: 0)
}
case .FINISH?:
......@@ -111,7 +111,7 @@ class DataTransferManager: NSObject {
case .ERROR?:
// fucked up, send error ack
print("Error from card10, message ",String(data:package,encoding: .ascii)!)
print("Error from card10, message ", String(data: package, encoding: .ascii)!)
self.sendPackage(type: .ERROR_ACK, payload: nil, offset: 0)
delegate?.didFailToSendFile()
break
......@@ -181,11 +181,11 @@ class DataTransferManager: NSObject {
}
func checkChunkWithResponse(_ response: Data) -> Bool {
return true
//FIXME: This CRC32 implementation doesn't work. Disabled for now
let responseInUInt32 = response.withUnsafeBytes{ $0.load(as: UInt8.self) }
let responseInUInt32 = response.withUnsafeBytes { $0.load(as: UInt8.self) }
let chunkInUInt8 = [UInt8](self.currentChunkToSend!)
let checksum = CRC32.checksum(bytes: chunkInUInt8)
if responseInUInt32 == checksum {
......@@ -198,7 +198,7 @@ class DataTransferManager: NSObject {
guard chunkedReader != nil && dataToSend != nil else { return }
let progress = Double(chunkedReader!.currentOffset) / Double(self.dataToSend!.count)
print("Progress sending file: ",progress)
print("Progress sending file: ", progress)
delegate?.didUpdateProgress(progress)
}
}
......@@ -113,10 +113,11 @@ extension ConnectedViewController {
@IBAction func disconnectButtonAction(sender: UIButton) {
pollingTimer?.cancel()
let rgbOff = RGBLED.init(r: 0, g: 0, b: 0)
let rgbOff = RGBLED.init(red: 0, green: 0, blue: 0)
BluetoothManager.sharedInstance().setBackgroundLEDs(topLeft: rgbOff, topRight: rgbOff, bottomRight: rgbOff, bottomLeft: rgbOff)
BluetoothManager.sharedInstance().setLEDsAbove(a: rgbOff, b: rgbOff, c: rgbOff, d: rgbOff, e: rgbOff, f: rgbOff, g: rgbOff, h: rgbOff, i: rgbOff, j: rgbOff, k: rgbOff)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { BluetoothManager.sharedInstance().disconnectFromCard10Badge()
BluetoothManager.sharedInstance().setLEDsAbove(AboveLEDs.off())
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
BluetoothManager.sharedInstance().disconnectFromCard10Badge()
}
}
}
......
......@@ -8,7 +8,7 @@
import UIKit
fileprivate struct DataToTransfer: Hashable {
private struct DataToTransfer: Hashable {
let fileName: String
let data: Data
}
......@@ -62,20 +62,20 @@ class HatcheryDetailViewController: UIViewController, HatcheryClientDelegate, Bl
let fileContent = FileManager.default.contents(atPath: fileURL.path)
let fileName = FileManager.default.displayName(atPath: fileURL.path)
guard fileContent != nil else { return }
self.filesToTransfer.append(DataToTransfer(fileName: String.init(format: "apps/%@/%@",folderName,fileName), data: fileContent!))
self.filesToTransfer.append(DataToTransfer(fileName: String.init(format: "apps/%@/%@", folderName, fileName), data: fileContent!))
}
BluetoothManager.sharedInstance().subscribe(self)
uploadAlert = UIAlertController(title: "Uploading", message: "Please wait", preferredStyle: .alert)
progressBar = UIProgressView(progressViewStyle: .default)
progressBar?.setProgress(0.0, animated: true)
progressBar?.frame = CGRect(x: 10, y: 70, width: 250, height: 0)
uploadAlert?.view.addSubview(progressBar!)
self.present(uploadAlert!, animated: true, completion: nil)
self.transferNextFile()
} catch {
print("Error while enumerating files \(path.path): \(error.localizedDescription)")
}
......@@ -83,7 +83,7 @@ class HatcheryDetailViewController: UIViewController, HatcheryClientDelegate, Bl
func didFailToDownloadEgg() {
let failedAlert = UIAlertController(title: "Error", message: "Failed to download egg", preferredStyle: .alert)
failedAlert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: nil))
self.present(failedAlert, animated: true)
}
......@@ -100,9 +100,9 @@ class HatcheryDetailViewController: UIViewController, HatcheryClientDelegate, Bl
})
}
}
//MARK: - BluetoothManagerDelegate
// MARK: - BluetoothManagerDelegate
func didFinishToSendFile() {
guard self.filesToTransfer.count > 0 else {
return
......@@ -110,7 +110,7 @@ class HatcheryDetailViewController: UIViewController, HatcheryClientDelegate, Bl
self.filesToTransfer.removeFirst()
self.transferNextFile()
}
func didFailToSendFile() {
self.filesToTransfer.removeAll()
BluetoothManager.sharedInstance().unsubscribe(self)
......@@ -120,7 +120,7 @@ class HatcheryDetailViewController: UIViewController, HatcheryClientDelegate, Bl
self.present(finishedController, animated: true, completion: nil)
})
}
func didUpdateProgressOnFile(_ progress: Double) {
self.progressBar?.setProgress(Float(progress), animated: true)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment