Skip to content

Commit

Permalink
Add model lookup to module
Browse files Browse the repository at this point in the history
  • Loading branch information
bochoven committed Jan 19, 2020
1 parent fef70eb commit 7c256bf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
37 changes: 37 additions & 0 deletions helpers/model_lookup_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use munkireport\lib\Request;

function model_description_lookup($serial)
{
if (strpos($serial, 'VMWV') === 0) {
return 'VMware virtual machine';
}

$options = [
'query' => [
'page' => 'categorydata',
'serialnumber' => $serial
]
];

$client = new Request();
$result = $client->get('http://km.support.apple.com/kb/index', $options);

if ( ! $result) {
return 'model_lookup_failed';
}

try {
$categorydata = json_decode($result);
if(isset($categorydata->name)){
return $categorydata->name;
}
else{
return 'unknown_model';
}
} catch (Exception $e) {
return 'model_lookup_failed';
}

}
26 changes: 26 additions & 0 deletions machine_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,30 @@ private function _trait_stats($what = 'os_version'){
}
return $out;
}

/**
* Run machine lookup at Apple
*
**/
public function model_lookup($serial_number)
{
require_once(__DIR__ . '/helpers/model_lookup_helper.php');
$out = ['error' => '', 'model' => ''];
try {
$machine = Machine_model::select()
->where('serial_number', $serial_number)
->firstOrFail();
$machine->machine_desc = model_description_lookup($serial_number);
$machine->save();
$out['model'] = $machine->machine_desc;
} catch (\Throwable $th) {
// Record does not exist
$out['error'] = 'lookup_failed';
}
$obj = new View();
$obj->view('json', [
'msg' => $out
]);

}
} // END class Machine_controller
25 changes: 22 additions & 3 deletions machine_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,28 @@ public function run($plist)
$mylist['buildversion'] = preg_replace('/[^A-Za-z0-9]/', '', $mylist['buildversion']);
}

Machine_model::updateOrCreate(
['serial_number' => $this->serial_number],
$mylist
// Try to retrieve machine and add model
try {
$machine = Machine_model::select()
->where('serial_number', $this->serial_number)
->firstOrFail();
if ($this->should_run_model_description_lookup($machine)){
require_once(__DIR__ . '/helpers/model_lookup_helper.php');
$mylist['machine_desc'] = model_description_lookup($this->serial_number);
$machine->fill($mylist);
}
} catch (\Throwable $th) {
require_once(__DIR__ . '/helpers/model_lookup_helper.php');
$mylist['machine_desc'] = model_description_lookup($this->serial_number);
Machine_model::create($mylist);
}
}

function should_run_model_description_lookup($machine)
{
return (
! $machine['machine_desc'] or
in_array($machine['machine_desc'], ['model_lookup_failed', 'unknown_model'])
);
}
}
22 changes: 21 additions & 1 deletion views/machine_detail_widget1.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@
</div>
</div>
<span class="mr-machine_desc"></span> <a class="mr-refresh-desc" href=""><i class="fa fa-refresh"></i></a>
</div>
</div>

<script>
// ------------------------------------ Refresh machine description

$('.mr-refresh-desc')
.attr('href', appUrl + '/module/machine/model_lookup/' + serialNumber)
.click(function(e){
e.preventDefault();
// show that we're doing a lookup
$('.mr-machine_desc').text(i18n.t('loading'));
$.getJSON( appUrl + '/module/machine/model_lookup/' + serialNumber, function( data ) {
if(data['error'] == ''){
$('.mr-machine_desc').text(data['model']);
}
else{
$('.mr-machine_desc').text(data['error']);
}
});
});
</script>

0 comments on commit 7c256bf

Please sign in to comment.