Commit 9905a3cf authored by Roddi Deecke's avatar Roddi Deecke Committed by Thomas Mellenthin

fix control statements

mostly parentheses
parent 54b8b5e9
......@@ -18,12 +18,10 @@ disabled_rules:
- force_cast
- syntactic_sugar
- implicit_getter
- control_statement
- todo
- file_length
- function_body_length
- cyclomatic_complexity
- unneeded_break_in_switch
- type_body_length
- vertical_parameter_alignment
\ No newline at end of file
......@@ -385,7 +385,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
print("found service: \(service.uuid)")
if (service.uuid.isEqual(card10ServiceUUID)) {
if service.uuid.isEqual(card10ServiceUUID) {
peripheral.discoverCharacteristics([card10TimeCharacteristic,
card10VibrateCharacteristic,
card10RocketsCharacteristic,
......@@ -399,7 +399,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
card10SingleRgbLedCharacteristic,
card10LightSensorCharacteristic], for: service)
}
if (service.uuid.isEqual(card10RxTxServiceUUID)) {
if service.uuid.isEqual(card10RxTxServiceUUID) {
peripheral.discoverCharacteristics([card10TXWriteCharacteristicUUID,
card10RXReadCharacteristicUUID], for: service)
}
......
......@@ -62,23 +62,24 @@ class DataTransferManager: NSObject {
switch packageType {
case .START?:
break
case .START_ACK?:
self.transferState = .READY_TO_SEND
let chunkToSend = self.chunkedReader?.nextChunk()
guard chunkToSend!.count > 0 else { self.sendPackage(type: .ERROR, payload: nil, offset: 0); return }
self.currentChunkToSend = chunkToSend
sendPackage(type: .CHUNK, payload: chunkToSend, offset: 0)
break
case .CHUNK?:
break
case .CHUNK_ACK?:
self.transferState = .CHUNK_SENT
// calculate CRC and send new chunk
let range: Range<Data.Index> = 0..<1
let crc = package.subdata(in: range)
updateProgress()
if (checkChunkWithResponse(crc)) {
if (currentChunkToSend!.count < chunkedReader!.packageSize) {
if checkChunkWithResponse(crc) {
if currentChunkToSend!.count < chunkedReader!.packageSize {
// The last package arrived, we are done here
sendPackage(type: .FINISH, payload: nil, offset: 0)
break
......@@ -88,34 +89,36 @@ class DataTransferManager: NSObject {
self.currentChunkToSend = chunkToSend
sendPackage(type: .CHUNK, payload: chunkToSend, offset: self.chunkedReader!.currentOffset-20)
}
} else if (retryCount < 9) {
} else if retryCount < 9 {
sendPackage(type: .CHUNK, payload: self.currentChunkToSend, offset: self.chunkedReader!.currentOffset-20)
retryCount += 1
} else {
self.sendPackage(type: .ERROR, payload: nil,offset: 0)
}
break
case .FINISH?:
break
case .FINISH_ACK?:
// finished, cleanup
self.transferState = .FINISH_SENT
self.chunkedReader = nil
self.dataToSend = nil
self.transferState = .IDLE
break
case .ERROR?:
// fucked up, send error ack
print("Error from card10, message ",String(data:package,encoding: .ascii)!)
self.sendPackage(type: .ERROR_ACK, payload: nil, offset: 0)
delegate?.didFailToSendFile()
break
case .ERROR_ACK?:
// device knows we aborted
self.chunkedReader = nil
self.dataToSend = nil
self.transferState = .IDLE
break
default:
print("Broken Package ", firstChar)
}
......@@ -141,31 +144,33 @@ class DataTransferManager: NSObject {
payloadToSend = PackageType.START.rawValue.data(using: .ascii)
payloadToSend?.append(payload!)
lastPackageSentAt = NSDate.init().timeIntervalSinceNow
break
case .START_ACK:
break
case .CHUNK:
payloadToSend = PackageType.CHUNK.rawValue.data(using: .ascii)
var int = UInt32(bigEndian: UInt32(offset))
payloadToSend?.append(Data(bytes: &int, count: MemoryLayout.size(ofValue: UInt32())))
payloadToSend?.append(payload!)
lastPackageSentAt = NSDate.init().timeIntervalSinceNow
break
case .CHUNK_ACK:
break
case .FINISH:
payloadToSend = PackageType.FINISH.rawValue.data(using: .ascii)
break
case .FINISH_ACK:
break
case .ERROR:
payloadToSend = PackageType.ERROR.rawValue.data(using: .ascii)
delegate?.didFailToSendFile()
break
case .ERROR_ACK:
payloadToSend = PackageType.ERROR_ACK.rawValue.data(using: .ascii)
delegate?.didFailToSendFile()
break
}
if payloadToSend != nil {
delegate?.wantsToSendPackage(payloadToSend!)
......@@ -180,7 +185,7 @@ class DataTransferManager: NSObject {
let responseInUInt32 = response.withUnsafeBytes{ $0.load(as: UInt8.self) }
let chunkInUInt8 = [UInt8](self.currentChunkToSend!)
let checksum = CRC32.checksum(bytes: chunkInUInt8)
if (responseInUInt32 == checksum) {
if responseInUInt32 == checksum {
return true
}
return false
......
......@@ -126,7 +126,7 @@ extension ConnectedViewController {
extension ConnectedViewController {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if (controller.documentPickerMode == .import) {
if controller.documentPickerMode == .import {
for url in urls {
do {
let data = try Data(contentsOf: url as URL)
......@@ -202,7 +202,7 @@ extension ConnectedViewController {
var g: UInt8 = 0
var b: UInt8 = 0
while (UInt64(r) + UInt64(g) + UInt64(b) == UInt64(0)) {
while UInt64(r) + UInt64(g) + UInt64(b) == UInt64(0) {
r = UInt8.random(in: 0..<3) * 127 // 128 would overflow UInt 😬
g = UInt8.random(in: 0..<3) * 127
b = UInt8.random(in: 0..<3) * 127
......
......@@ -140,7 +140,7 @@ class HatcheryClient: NSObject, URLSessionDelegate {
let documentsUrl = FileManager.default.temporaryDirectory
let destinationUrl = documentsUrl.appendingPathComponent(fromURL.lastPathComponent)
if (FileManager.default.fileExists(atPath: destinationUrl.path)) {
if FileManager.default.fileExists(atPath: destinationUrl.path) {
do {
try FileManager.default.removeItem(at: destinationUrl)
} catch {
......@@ -176,7 +176,7 @@ class HatcheryClient: NSObject, URLSessionDelegate {
folderNameForEgg = fileName.deletingLastPathComponent().path // Kind of betting on that there are no subfolders
}
try FileManager.default.createDirectory(at: fileName.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
if (FileManager.default.createFile(atPath: fileName.path, contents: data, attributes: nil)) {
if FileManager.default.createFile(atPath: fileName.path, contents: data, attributes: nil) {
createdFileCount += 1 //Increment so we know if we wrote all the files
} else {
print("Failed to write file to path ", fileName)
......
......@@ -43,7 +43,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
}
override func viewDidAppear(_ animated: Bool) {
if (client?.loadedEggs.count == 0) {
if client?.loadedEggs.count == 0 {
client?.updateListOfHatcheryEggs()
}
super.viewDidAppear(animated)
......@@ -74,7 +74,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
let cell = tableView.dequeueReusableCell(withIdentifier: "HatcheryRootTableViewCell", for: indexPath) as! HatcheryRootTableViewCell
let eggForCell: HatcheryEgg
if (isFiltering()) {
if isFiltering() {
eggForCell = self.filteredEggs[indexPath.row]
} else {
eggForCell = (self.client?.loadedEggs[indexPath.section][indexPath.row])!
......@@ -87,7 +87,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (isFiltering()) {
if isFiltering() {
return nil
}
......@@ -105,7 +105,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (isFiltering()) {
if isFiltering() {
return 0
}
return 44
......@@ -115,7 +115,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
let indexPath = self.tableView.indexPathForSelectedRow!
let eggForCell: HatcheryEgg
if (isFiltering()) {
if isFiltering() {
eggForCell = self.filteredEggs[indexPath.row]
} else {
eggForCell = (self.client?.loadedEggs[indexPath.section][indexPath.row])!
......@@ -128,7 +128,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
func didRefreshListOfEggs() {
self.refreshControl?.endRefreshing()
if (self.tableView.numberOfSections > 0) {
if self.tableView.numberOfSections > 0 {
let rangeToReload = IndexSet.init(integersIn: 0..<(client?.loadedEggs.count)!)
self.tableView.reloadSections(rangeToReload, with: .automatic)
} else {
......@@ -139,7 +139,7 @@ class HatcheryRootTableViewController: UITableViewController, HatcheryClientDele
func didFailToRefreshListOfEggs(_ error: Error?) {
self.refreshControl?.endRefreshing()
var errorToShow: String = "Failed to download eggs"
if (error != nil) {
if error != nil {
errorToShow.append(contentsOf: "\n")
errorToShow.append(contentsOf: error!.localizedDescription)
}
......
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