Skip to content
Snippets Groups Projects
Commit c74d2b25 authored by Jan Cantaert's avatar Jan Cantaert
Browse files

Temper class + USBTemper class - unit tests : ok but empty variables in web interface

parent bfadb42b
No related branches found
No related tags found
2 merge requests!8Fix TEMPer,!7WIP: Multi CPU + Temper usb (not working yet)
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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]);
}
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment