/**
  * Switch the rig to the given miner.
  * 
  * @param array $data
  *   The orig data from phpminer. (Optional, default = array())
  * 
  * @return boolean
  *   True on success, else false.
  */
 public function switch_miner($data = array())
 {
     // Check if miner was provided and if it exists.
     if (empty($data['miner']) || !isset($this->config['miners'][$data['miner']])) {
         return false;
     }
     // Get current miner.
     $current_miner = $this->config['rpc_config']->current_miner;
     // Check if it is already on that miner.
     if ($data['miner'] === $current_miner) {
         log_console('Already on that miner');
         return true;
     }
     $this->kill_cgminer();
     $run_tries = 0;
     while ($this->is_cgminer_running()) {
         sleep(1);
         if ($run_tries++ >= 10) {
             log_console('Could not stop current miner');
             return false;
         }
     }
     $current_miner_config = $this->config['miners'][$data['miner']];
     $this->config['miner_api_ip'] = $current_miner_config['ip'];
     $this->config['miner_api_port'] = $current_miner_config['port'];
     $this->config['miner'] = $current_miner_config['miner'];
     $this->config['miner_binary'] = $current_miner_config['miner_binary'];
     $this->config['cgminer_config_path'] = $current_miner_config['cgminer_config_path'];
     $this->config['cgminer_path'] = $current_miner_config['cgminer_path'];
     $this->config['amd_sdk'] = $current_miner_config['amd_sdk'];
     $this->config['rpc_config']->current_miner = $data['miner'];
     $this->restart_cgminer();
     $client_api = new CGMinerAPI($this->config['miner_api_ip'], $this->config['miner_api_port']);
     $tries = 0;
     while ($tries++ <= 10) {
         try {
             $client_api->test_connection();
             return true;
         } catch (Exception $ex) {
         }
         sleep(1);
     }
     log_console('Could not start new miner');
     return false;
 }
}
function assertIsset($msg, $value, $key, $success = '', $error = 'ERROR')
{
    if (!isset($value[$key])) {
        log_console($msg . ': ' . $error);
        log_console('Please fix the error above');
        die;
    } else {
        if (empty($success)) {
            $success = "OK value = " . $value[$key];
        }
        log_console($msg . ': ' . $success);
    }
}
$rpc_api = new RPCClientApi($config);
$miner_api = new CGMinerAPI($config['miner_api_ip'], $config['miner_api_port']);
assertTrue('Check for provileged access', $miner_api->is_privileged());
$gpu_device_key_check = array('GPU', 'Enabled', 'Status', 'Temperature', 'Fan Speed', 'Fan Percent', 'GPU Clock', 'Memory Clock', 'GPU Voltage', 'GPU Activity', 'MHS av', 'MHS 5s', 'Accepted', 'Rejected', 'Hardware Errors', 'Utility', 'Intensity', 'Last Share Pool');
$pool_key_check = array('POOL', 'URL', 'Status', 'Priority', 'Quota');
$device_details_key_check = array('DEVDETAILS', 'Name', 'ID', 'Kernel', 'Model');
log_console('');
$devices = $miner_api->get_devices();
assertTrue("GPU's found", !empty($devices), count($devices) . ' found', 'ERROR: ' . count($devices) . ' found');
foreach ($devices as $index => $gpu_details) {
    log_console('');
    log_console('Verify GPU ' . ($index + 1));
    log_console('');
    log_console('    Verify response keys for command get_devices');
    foreach ($gpu_device_key_check as $key) {
        assertIsset("    Search for GPU " . $key, $gpu_details, $key);
    }