Skip to content

Commit

Permalink
Add USB device speed
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxudo committed Aug 15, 2023
1 parent 0fd8cea commit 5f93660
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 59 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ Table Schema
* type - varchar(255) - type of device, manually set via model
* manufacturer - varchar(255) - reported maker of device
* vendor_id - varchar(255) - device's vendor ID
* device_speed - varchar(255) - USB bus speed
* device_speed - varchar(255) - USB bus protocol
* internal - int - 0/1 for internal USB device
* media - int - 0/1 for removable media device
* bus_power - int - available bus power
* bus_power_used - int - bus power in use
* extra_current_used - int - extra current being provided
* usb_serial_number - varchar(255) - the serial number of the USB device
* printer_id - TEXT - information about the connected USB printer
* device_speed_bps - varchar(128) - USB device speed
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"camera": "Camera",
"clienttab": "USB Devices",
"device_speed": "USB Protocol",
"device_speed_bps": "Device Speed",
"extra_current_used": "Extra mAh",
"internal": "Internal",
"manufacturer": "Manufacturer",
Expand Down
27 changes: 27 additions & 0 deletions migrations/2023_08_15_000001_usb_add_device_speed_bps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Capsule\Manager as Capsule;

class UsbAddDeviceSpeedBps extends Migration
{
private $tableName = 'usb';

public function up()
{
$capsule = new Capsule();
$capsule::schema()->table($this->tableName, function (Blueprint $table) {
$table->string('device_speed_bps',128)->nullable();

$table->index('device_speed_bps');
});
}

public function down()
{
$capsule = new Capsule();
$capsule::schema()->table($this->tableName, function (Blueprint $table) {
$table->dropColumn('device_speed_bps');
});
}
}
29 changes: 26 additions & 3 deletions scripts/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import subprocess
import os
import plistlib
import sys

sys.path.insert(0, '/usr/local/munki')
sys.path.insert(0, '/usr/local/munkireport')

from munkilib import FoundationPlist

def get_usb_info():
'''Uses system profiler to get usb info for this machine.'''
Expand All @@ -30,14 +35,14 @@ def get_usb_info():
except Exception:
return {}

def flatten_usb_info(array):
def flatten_usb_info(array, localization):
'''Un-nest USB devices, return array with objects with relevant keys'''
out = []
for obj in array:
device = {'name': '', 'internal': 0, 'media': 0}
for item in obj:
if item == '_items':
out = out + flatten_usb_info(obj['_items'])
out = out + flatten_usb_info(obj['_items'], localization)
elif item == '_name':
device['name'] = obj[item]
elif item == 'vendor_id' or item == 'b_vendor_id':
Expand All @@ -46,6 +51,7 @@ def flatten_usb_info(array):
device['manufacturer'] = obj[item]
elif item == 'device_speed' or item == 'e_device_speed':
device['device_speed'] = obj[item]
device['device_speed_bps'] = localization[obj[item]].strip()
elif item == 'bus_power' or item == 'h_bus_power':
device['bus_power'] = obj[item]
elif item == 'bus_power_used' or item == 'j_bus_power_used':
Expand All @@ -70,7 +76,24 @@ def main():
# Get results
result = dict()
info = get_usb_info()
result = flatten_usb_info(info)

# Read in English localizations from SystemProfiler
if os.path.isfile('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/en.lproj/Localizable.strings'):
localization = FoundationPlist.readPlist('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/en.lproj/Localizable.strings')
elif os.path.isfile('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings'):
localization = FoundationPlist.readPlist('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings')
elif os.path.isfile('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/Localizable.loctable'):
localization_dict = FoundationPlist.readPlist('/System/Library/SystemProfiler/SPUSBReporter.spreporter/Contents/Resources/Localizable.loctable')
if "en" in localization_dict:
localization = localization_dict["en"]
elif "English" in localization_dict:
localization = localization_dict["English"]
else:
localization = {}
else:
localization = {}

result = flatten_usb_info(info, localization)

# Write usb results to cache
cachedir = '%s/cache' % os.path.dirname(os.path.realpath(__file__))
Expand Down
2 changes: 1 addition & 1 deletion usb_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get_data($serial_number = '')
// Remove non-serial number characters
$serial_number = preg_replace("/[^A-Za-z0-9_\-]]/", '', $serial_number);

$sql = "SELECT name, type, manufacturer, vendor_id, device_speed, internal, media, bus_power, bus_power_used, extra_current_used, usb_serial_number
$sql = "SELECT name, type, manufacturer, vendor_id, device_speed, device_speed_bps, internal, media, bus_power, bus_power_used, extra_current_used, usb_serial_number
FROM usb
WHERE serial_number = '$serial_number'";

Expand Down
3 changes: 2 additions & 1 deletion usb_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function __construct($serial='')
$this->rs['extra_current_used'] = 0;
$this->rs['usb_serial_number'] = ''; // USB device serial number
$this->rs['printer_id'] = ''; // 1284 Device ID information, only used by printers
$this->rs['device_speed_bps'] = null;

// Add local config
configAppendFile(__DIR__ . '/config.php');
Expand Down Expand Up @@ -80,7 +81,7 @@ function process($plist)

// Adjust USB speeds
if (array_key_exists("device_speed",$device)) {
$device['device_speed'] = str_replace(array('low_speed','full_speed','high_speed','super_speed'), array('USB 1.0','USB 1.1','USB 2.0','USB 3.x'), $device['device_speed']);
$device['device_speed'] = str_replace(array('low_speed','full_speed','high_speed','super_speed_plus_by_2','super_speed_plus','super_speed'), array('USB 1.0','USB 1.1','USB 2.0','USB 3.x','USB 3.x','USB 3.x'), $device['device_speed']);
} else {
$device['device_speed'] = 'USB 1.1';
}
Expand Down
106 changes: 53 additions & 53 deletions views/usb_listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@

<div class="container">
<div class="row">
<div class="col-lg-12">
<h3><span data-i18n="usb.reporttitle"></span> <span id="total-count" class='label label-primary'>…</span></h3>
<table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th data-i18n="listing.computername" data-colname='machine.computer_name'></th>
<th data-i18n="serial" data-colname='reportdata.serial_number'></th>
<th data-i18n="usb.name" data-colname='usb.name'></th>
<th data-i18n="usb.type" data-colname='usb.type'></th>
<th data-i18n="usb.manufacturer" data-colname='usb.manufacturer'></th>
<th data-i18n="usb.vendor_id" data-colname='usb.vendor_id'></th>
<th data-i18n="usb.device_speed" data-colname='usb.device_speed'></th>
<th data-i18n="usb.internal" data-colname='usb.internal'></th>
<th data-i18n="usb.media" data-colname='usb.media'></th>
</tr>
</thead>
<tbody>
<tr>
<td data-i18n="listing.loading" colspan="9" class="dataTables_empty"></td>
</tr>
</tbody>
</table>
</div> <!-- /span 12 -->
<div class="col-lg-12">
<h3><span data-i18n="usb.reporttitle"></span> <span id="total-count" class='label label-primary'>…</span></h3>
<table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th data-i18n="listing.computername" data-colname='machine.computer_name'></th>
<th data-i18n="serial" data-colname='reportdata.serial_number'></th>
<th data-i18n="usb.name" data-colname='usb.name'></th>
<th data-i18n="usb.type" data-colname='usb.type'></th>
<th data-i18n="usb.manufacturer" data-colname='usb.manufacturer'></th>
<th data-i18n="usb.vendor_id" data-colname='usb.vendor_id'></th>
<th data-i18n="usb.device_speed" data-colname='usb.device_speed'></th>
<th data-i18n="usb.device_speed_bps" data-colname='usb.device_speed_bps'></th>
<th data-i18n="usb.internal" data-colname='usb.internal'></th>
<th data-i18n="usb.media" data-colname='usb.media'></th>
</tr>
</thead>
<tbody>
<tr>
<td data-i18n="listing.loading" colspan="10" class="dataTables_empty"></td>
</tr>
</tbody>
</table>
</div> <!-- /span 12 -->
</div> <!-- /row -->
</div> <!-- /container -->

<script type="text/javascript">

$(document).on('appUpdate', function(e){
$(document).on('appUpdate', function(e){

var oTable = $('.table').DataTable();
oTable.ajax.reload();
return;
var oTable = $('.table').DataTable();
oTable.ajax.reload();
return;

});
});

$(document).on('appReady', function(e, lang) {
$(document).on('appReady', function(e, lang) {

// Get modifiers from data attribute
var mySort = [], // Initial sort
Expand All @@ -62,7 +63,7 @@
col++
});

oTable = $('.table').dataTable( {
oTable = $('.table').dataTable( {
ajax: {
url: appUrl + '/datatables/data',
type: "POST",
Expand All @@ -84,29 +85,28 @@
buttons: mr.dt.buttons,
order: mySort,
columnDefs: columnDefs,
createdRow: function( nRow, aData, iDataIndex ) {
// Update name in first column to link
var name=$('td:eq(0)', nRow).html();
if(name == ''){name = "No Name"};
var sn=$('td:eq(1)', nRow).html();
var link = mr.getClientDetailLink(name, sn, '#tab_usb-tab');
$('td:eq(0)', nRow).html(link);

// Internal/External Bus
var internal=$('td:eq(7)', nRow).html();
internal = internal == '1' ? i18n.t('internal') :
(internal === '0' ? i18n.t('external') : '')
$('td:eq(7)', nRow).html(internal)

// Media
var media=$('td:eq(8)', nRow).html();
media = media == '1' ? i18n.t('yes') :
(media === '0' ? i18n.t('no') : '')
$('td:eq(8)', nRow).html(media)
}
});

});
createdRow: function( nRow, aData, iDataIndex ) {
// Update name in first column to link
var name=$('td:eq(0)', nRow).html();
if(name == ''){name = "No Name"};
var sn=$('td:eq(1)', nRow).html();
var link = mr.getClientDetailLink(name, sn, '#tab_usb-tab');
$('td:eq(0)', nRow).html(link);

// Internal/External Bus
var internal=$('td:eq(8)', nRow).html();
internal = internal == '1' ? i18n.t('internal') :
(internal === '0' ? i18n.t('external') : '')
$('td:eq(8)', nRow).html(internal)

// Media
var media=$('td:eq(9)', nRow).html();
media = media == '1' ? i18n.t('yes') :
(media === '0' ? i18n.t('no') : '')
$('td:eq(9)', nRow).html(media)
}
});
});
</script>

<?php $this->view('partials/foot'); ?>

0 comments on commit 5f93660

Please sign in to comment.