<commit_before>//
//  StatusViewController.swift
//  openXCenabler
//
//  Created by <NAME> on 2016-08-04.
//  Copyright (c) 2016 Ford Motor Company Licensed under the BSD license.
//

import UIKit
import openXCiOSFramework

// TODO: ToDo - Work on removing the warnings

class StatusViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  // UI Labels
  @IBOutlet weak var actConLab: UILabel!
  @IBOutlet weak var msgRvcdLab: UILabel!
  @IBOutlet weak var verLab: UILabel!
  @IBOutlet weak var devidLab: UILabel!
  
  // scan/connect button
  @IBOutlet weak var searchBtn: UIButton!
  
  // table for holding/showing discovered VIs
  @IBOutlet weak var peripheralTable: UITableView!
  
  // the VM
  var vm: VehicleManager!
  
  // timer for UI counter updates
  var timer: Timer!
  
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    // change tab bar text colors
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for:UIControlState())
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for:.selected)

  
    // instantiate the VM
    print("loading VehicleManager")
    vm = VehicleManager.sharedInstance
   
    
    // setup the status callback, and the command response callback
    vm.setManagerCallbackTarget(self, action: StatusViewController.manager_status_updates)
    vm.setCommandDefaultTarget(self, action: StatusViewController.handle_cmd_response)
    // turn on debug output
    vm.setManagerDebug(true)
    
    
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


  // this function is called when the scan button is hit
  @IBAction func searchHit(_ sender: UIButton) {
    
    // make sure we're not already connected first
    if (vm.connectionState==VehicleManagerConnectionState.notConnected) {
      
      // start a timer to update the UI with the total received messages
      timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(StatusViewController.msgRxdUpdate(_:)), userInfo: nil, repeats: true)
      
      // check to see if the config is set for autoconnect mode
      vm.setAutoconnect(false)
      if UserDefaults.standard.bool(forKey: "autoConnectOn") {
        vm.setAutoconnect(true)
      }
      
      // check to see if the config is set for protobuf mode
      vm.setProtobufMode(false)
      if UserDefaults.standard.bool(forKey: "protobufOn") {
        vm.setProtobufMode(true)
      }
      
      // check to see if a trace input file has been set up
      if UserDefaults.standard.bool(forKey: "traceInputOn") {
        if let name = UserDefaults.standard.value(forKey: "traceInputFilename") as? NSString {
          vm.enableTraceFileSource(name)
        }
      }

      // check to see if a trace output file has been configured
      if UserDefaults.standard.bool(forKey: "traceOutputOn") {
        if let name = UserDefaults.standard.value(forKey: "traceOutputFilename") as? NSString {
          vm.enableTraceFileSink(name)
        }
      }

      // start the VI scan
      vm.scan()

      // update the UI
      DispatchQueue.main.async {
        self.actConLab.text = "❓"
        self.searchBtn.setTitle("SCANNING",for:UIControlState())
      }
      

    }
    
  }
  
  
  // this function receives all status updates from the VM
  func manager_status_updates(_ rsp:NSDictionary) {
   
    // extract the status message
    let status = rsp.object(forKey: "status") as! Int
    let msg = VehicleManagerStatusMessage(rawValue: status)
    print("VM status : ",msg!)
    
    
    // show/reload the table showing detected VIs
    if msg==VehicleManagerStatusMessage.c5DETECTED {
      DispatchQueue.main.async {
        self.peripheralTable.isHidden = false
        self.peripheralTable.reloadData()
      }
    }
    
    // update the UI showing connected VI
    if msg==VehicleManagerStatusMessage.c5CONNECTED {
      DispatchQueue.main.async {
        self.peripheralTable.isHidden = true
        self.actConLab.text = "✅"
        self.searchBtn.setTitle("BTLE VI CONNECTED",for:UIControlState())
      }
    }
    
    // update the UI showing disconnected VI
    if msg==VehicleManagerStatusMessage.c5DISCONNECTED {
      DispatchQueue.main.async {
        self.actConLab.text = "---"
        self.msgRvcdLab.text = "---"
        self.verLab.text = "---"
        self.devidLab.text = "---"
        self.searchBtn.setTitle("SEARCH FOR BTLE VI",for:UIControlState())
      }
    }
    
    // when we see that notify is on, we can send 2 command requests
    // for version and device id, one after the other
    if msg==VehicleManagerStatusMessage.c5NOTIFYON {
     
      let delayTime = DispatchTime.now() + Double(Int64(0.25 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
      DispatchQueue.main.asyncAfter(deadline: delayTime) {
        print("sending version cmd")
        let cm = VehicleCommandRequest()
        cm.command = .version
        self.vm.sendCommand(cm)
      }
      
      let delayTime2 = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
      DispatchQueue.main.asyncAfter(deadline: delayTime2) {
        print("sending devid cmd")
        let cm = VehicleCommandRequest()
        cm.command = .device_id
        self.vm.sendCommand(cm)
      }
      
      
    }
    
  }
  
  // this function handles all command responses
  func handle_cmd_response(_ rsp:NSDictionary) {
    // extract the command response message
    let cr = rsp.object(forKey: "vehiclemessage") as! VehicleCommandResponse
    print("cmd response : \(cr.command_response)")
    
    // update the UI depending on the command type
    if cr.command_response.isEqual(to: ".version") {
      DispatchQueue.main.async {
        self.verLab.text = cr.message as String
      }
    }
    if cr.command_response.isEqual(to: ".deviceid") {
      DispatchQueue.main.async {
        self.devidLab.text = cr.message as String<commit_msg>changes to status view controller for json mode to print device id & version of the VI<commit_after>//
//  StatusViewController.swift
//  openXCenabler
//
//  Created by <NAME> on 2016-08-04.
//  Copyright (c) 2016 Ford Motor Company Licensed under the BSD license.
//

import UIKit
import openXCiOSFramework

// TODO: ToDo - Work on removing the warnings

class StatusViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  // UI Labels
  @IBOutlet weak var actConLab: UILabel!
  @IBOutlet weak var msgRvcdLab: UILabel!
  @IBOutlet weak var verLab: UILabel!
  @IBOutlet weak var devidLab: UILabel!
  
  // scan/connect button
  @IBOutlet weak var searchBtn: UIButton!
  
  // table for holding/showing discovered VIs
  @IBOutlet weak var peripheralTable: UITableView!
  
  // the VM
  var vm: VehicleManager!
  
  // timer for UI counter updates
  var timer: Timer!
  
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    // change tab bar text colors
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for:UIControlState())
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for:.selected)

  
    // instantiate the VM
    print("loading VehicleManager")
    vm = VehicleManager.sharedInstance
   
    
    // setup the status callback, and the command response callback
    vm.setManagerCallbackTarget(self, action: StatusViewController.manager_status_updates)
    vm.setCommandDefaultTarget(self, action: StatusViewController.handle_cmd_response)
    // turn on debug output
    vm.setManagerDebug(true)
    
    
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


  // this function is called when the scan button is hit
  @IBAction func searchHit(_ sender: UIButton) {
    
    // make sure we're not already connected first
    if (vm.connectionState==VehicleManagerConnectionState.notConnected) {
      
      // start a timer to update the UI with the total received messages
      timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(StatusViewController.msgRxdUpdate(_:)), userInfo: nil, repeats: true)
      
      // check to see if the config is set for autoconnect mode
      vm.setAutoconnect(false)
      if UserDefaults.standard.bool(forKey: "autoConnectOn") {
        vm.setAutoconnect(true)
      }
      
      // check to see if the config is set for protobuf mode
      vm.setProtobufMode(false)
      if UserDefaults.standard.bool(forKey: "protobufOn") {
        vm.setProtobufMode(true)
      }
      
      // check to see if a trace input file has been set up
      if UserDefaults.standard.bool(forKey: "traceInputOn") {
        if let name = UserDefaults.standard.value(forKey: "traceInputFilename") as? NSString {
          vm.enableTraceFileSource(name)
        }
      }

      // check to see if a trace output file has been configured
      if UserDefaults.standard.bool(forKey: "traceOutputOn") {
        if let name = UserDefaults.standard.value(forKey: "traceOutputFilename") as? NSString {
          vm.enableTraceFileSink(name)
        }
      }

      // start the VI scan
      vm.scan()

      // update the UI
      DispatchQueue.main.async {
        self.actConLab.text = "❓"
        self.searchBtn.setTitle("SCANNING",for:UIControlState())
      }
      

    }
    
  }
  
  
  // this function receives all status updates from the VM
  func manager_status_updates(_ rsp:NSDictionary) {
   
    // extract the status message
    let status = rsp.object(forKey: "status") as! Int
    let msg = VehicleManagerStatusMessage(rawValue: status)
    print("VM status : ",msg!)
    
    
    // show/reload the table showing detected VIs
    if msg==VehicleManagerStatusMessage.c5DETECTED {
      DispatchQueue.main.async {
        self.peripheralTable.isHidden = false
        self.peripheralTable.reloadData()
      }
    }
    
    // update the UI showing connected VI
    if msg==VehicleManagerStatusMessage.c5CONNECTED {
      DispatchQueue.main.async {
        self.peripheralTable.isHidden = true
        self.actConLab.text = "✅"
        self.searchBtn.setTitle("BTLE VI CONNECTED",for:UIControlState())
      }
    }
    
    // update the UI showing disconnected VI
    if msg==VehicleManagerStatusMessage.c5DISCONNECTED {
      DispatchQueue.main.async {
        self.actConLab.text = "---"
        self.msgRvcdLab.text = "---"
        self.verLab.text = "---"
        self.devidLab.text = "---"
        self.searchBtn.setTitle("SEARCH FOR BTLE VI",for:UIControlState())
      }
    }
    
    // when we see that notify is on, we can send 2 command requests
    // for version and device id, one after the other
    if msg==VehicleManagerStatusMessage.c5NOTIFYON {
     
      let delayTime = DispatchTime.now() + Double(Int64(0.25 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
      DispatchQueue.main.asyncAfter(deadline: delayTime) {
        print("sending version cmd")
        let cm = VehicleCommandRequest()
        cm.command = .version
        self.vm.sendCommand(cm)
      }
      
      let delayTime2 = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
      DispatchQueue.main.asyncAfter(deadline: delayTime2) {
        print("sending devid cmd")
        let cm = VehicleCommandRequest()
        cm.command = .device_id
        self.vm.sendCommand(cm)
      }
      
      
    }
    
  }
  
  // this function handles all command responses
  func handle_cmd_response(_ rsp:NSDictionary) {
    // extract the command response message
    let cr = rsp.object(forKey: "vehiclemessage") as! VehicleCommandResponse
    print("cmd response : \(cr.command_response)")
    
    // update the UI depending on the command type- version,device_id works for JSON mode, not in protobuf - TODO
    if cr.command_response.isEqual(to: "version") {
      DispatchQueue.main.async {
        self.verLab.text = cr.message as String
      }
    }
    if cr.command_response.isEqual(to: "device_id") {
      DispatchQueue.main.async {
        self.devidLab.text = cr.message as String
{'fraction_non_alphanumeric': 0.09279520220439258, 'fraction_numerical': 0.005024718372639598, 'mean_word_length': 2.722473604826546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6258123', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3634, 'n_words': 935}