diff --git a/app/Sensor/Temper.php b/app/Sensor/Temper.php index 75d8af66862245e565e232a0d63ea4d0adc37ecb..bbff617f7c95cc0c3cd4db09abc18039ac6bf14b 100644 --- a/app/Sensor/Temper.php +++ b/app/Sensor/Temper.php @@ -9,16 +9,16 @@ namespace App\Sensor; */ class Temper { - public $part1= ""; //eg : 0a - public $part2= ""; //eg : 6c - public $temp=array();//eg : [26,28] + public $part1= "0a"; //eg : 0a + public $part2= "6c"; //eg : 6c + public $temp=array();//eg : [26,68] public function conversion() { - $hexatemp=$this->part1 . $this->part2; //eg : 0a6c - $decitemp=hexdec($hexatemp); //eg : 2628 + $hexatemp=$this->part1.$this->part2; //eg : 0a6c + $decitemp=hexdec($hexatemp); //eg : 2668 $this->temp[1]=substr($decitemp,0,-2); //eg : 26 - $this->temp[2]=substr($decitemp,-2); //eg : 28 - return $this->temp; //T° is 26.28°C + $this->temp[2]=substr($decitemp,-2); //eg : 68 + return $this->temp; } } diff --git a/app/Sensor/USBtemperature.php b/app/Sensor/USBtemperature.php index 0276383829dff8cd4ee1947119fc1b727fb64a14..b38e87e1368b8956e2977d0622d9f3faaef9e2b8 100644 --- a/app/Sensor/USBtemperature.php +++ b/app/Sensor/USBtemperature.php @@ -10,7 +10,7 @@ namespace App\Sensor; class USBtemperature extends \App\AbstractSensor { //get device responce (8 bytes) : - const REGEXP = "^(80 80)\s*([A-z\/0-9]+) \s*([A-z\/0-9]+)/m"; + const REGEXP = "/^(80 80)\s*([A-z\/0-9]+) \s*([A-z\/0-9]+)/m"; public function report() { @@ -20,10 +20,9 @@ class USBtemperature extends \App\AbstractSensor . "<p>Maybe <code>TEMPer</code> is not installed.</p>" . "<p>You can install it following the tutorial on the Gitlab repository</p>"; } - $temper = self::parse($record['TEMPer']); - $return= "<p>Ambient temperature (USB TEMPer) : " - . $temper[1] . "." . $temper[2] . " °C " . "</p>"; - return $return; + $temper = self::parse($record["TEMPer"]); + $return= "<p>Ambient temperature (USB TEMPer) : ".$temper->temp[1].".".$temper->temp[2]." °C "."</p>"; + return $return;//$record['TEMPer']; } public function status() @@ -32,29 +31,29 @@ class USBtemperature extends \App\AbstractSensor if ($record == null) { return self::STATUS_UNKNOWN; } - - $all_status = []; - foreach (self::parse($record['TEMPer']) as $USBTemp) { - /* @var $USBTemp Temper */ - $status = self::STATUS_OK; - if ($USBTemp->temperature[1] > 75) { - $status = self::STATUS_WARNING; - } - $all_status[] = $status; + //foreach (self::parse($record['TEMPer']) as $USBTemp) { + // /* @var $USBTemp Temper */ + + $status = self::STATUS_OK; + /* + $USBTemp = self::parse($record['TEMPer']); + if ((int)($USBTemp->temp[1]) > 75) { + $status = self::STATUS_WARNING; } - - return max($all_status); + */ + return $status; + } public static function parse(string $string) { $values = array(); preg_match_all(self::REGEXP, $string, $values); //get 8 bytes response from TEMPerUSB device - $temperatures = array(); $USBTemp = new Temper(); - $USBTemp->part1 = $values[2]; - $USBTemp->part2 = $values[3]; - $temper[] = $USBTemp->conversion(); //1st element = integer part, 2th = decimal part + $USBTemp->part1 = implode($values[2]); + $USBTemp->part2 = implode($values[3]); + $USBTemp->conversion(); //1st element = integer part, 2th = decimal part + $temper=$USBTemp; return $temper; } } diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php index 4512394ea19341ebbf9f9e5688ad2095fb0e4fde..d51c63846b71c85e44d76c76fcce4f660c4470f3 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/ExampleTest.php @@ -8,6 +8,7 @@ use App\Organization; use App\Server; use App\Sensor\Disks; use App\Sensor\CPUtemperature; +use App\Sensor\USBtemperature; use App\Sensor\Ifconfig; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -283,4 +284,17 @@ class ExampleTest extends TestCase $this->assertEquals("Core 3", $CPUTEMPS[3]->name); $this->assertEquals("34.0", $CPUTEMPS[3]->value); } + /** + * @group USBtemp + */ + public function testTEMPer() + { + $string = file_get_contents(__DIR__ . "/TEMPer"); + $TEMPer = new USBtemperature(new Server()); + $USBTemp = $TEMPer->parse($string); + $this->assertEquals("0a", $USBTemp->part1); + $this->assertEquals("6c", $USBTemp->part2); + $this->assertEquals("26", $USBTemp->temp[1]); + $this->assertEquals("68", $USBTemp->temp[2]); + } } diff --git a/tests/Unit/TEMPer b/tests/Unit/TEMPer new file mode 100644 index 0000000000000000000000000000000000000000..6f24190a6a96db9708b6b886853f483ea99e04de --- /dev/null +++ b/tests/Unit/TEMPer @@ -0,0 +1,7 @@ +Device /dev/hidraw4 : 413d:2107 interface 1 : (null) (null) + +Writing data (9 bytes): +00 01 80 33 01 00 00 00 00 + +Response from device (8 bytes): +80 80 0a 6c 4e 20 00 00 \ No newline at end of file