Skip to content

Commit

Permalink
NEW: Detection of StateText change TasmoAdmin#199, TasmoAdmin#154 [sk…
Browse files Browse the repository at this point in the history
…ip ci]
  • Loading branch information
reloxx13 committed Sep 4, 2018
1 parent 2fed609 commit 612ddab
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
- **CHANGE**: Startpage rework:
![startpage_v1.2.PNG](https://raw.githubusercontent.com/reloxx13/reloxx13.github.io/master/media/tasmoadmin/changelog/startpage_v1.2.PNG)
- **DEV**: Some more Error Catchings and debugs on error for lost configs [#207](https://github.com/reloxx13/TasmoAdmin/issues/207)
- **NEW**: Detection of StateText change [#199](https://github.com/reloxx13/TasmoAdmin/issues/199), [#154](https://github.com/reloxx13/TasmoAdmin/issues/154)

## v1.5.4
- **FIX** autoload case-sensitive [#182](https://github.com/reloxx13/TasmoAdmin/issues/182)
Expand Down
157 changes: 128 additions & 29 deletions tasmoadmin/includes/Sonoff.php
Expand Up @@ -528,6 +528,100 @@ public function compatibility( $status ) {
}


public function stateTextsDetection( $status ) {
/**
* v6.2.0.2 2018-09-04
* MQTT Changed Statetext is send in JSON, this is fail cuz it can be translated and not detected by other softwares.
*
* This function tries to detect the state by hardcoded keywords.
*/

$offArray = explode(
",",

strtolower(
""

/**
* EN
*/."off, down, offline, out,"

/**
* DE
*/."aus, unten, runter, schließen, schliessen, zu,"

/**
* PL
*/."z, poniżej, ponizej, blisko, do, zamknięte, zamkniete"
)
);
$onArray = explode(
",",

strtolower(
""

/**
* EN
*/."on, up, online, in,"

/**
* DE
*/."an, oben, hoch, öffnen, oeffnen, offen, "

/**
* PL
*/."do, powyżej, powyzej, wysoki, otwarte, "
)
);


$state = NULL;

//1 relay
if( isset( $status->StatusSTS->POWER ) ) {
$state = $status->StatusSTS->POWER;
//try to detect OFF
if( in_array( strtolower( $state ), $offArray ) ) {
$state = "OFF";
} elseif( in_array( $state, $onArray ) ) {
$state = "ON";
}

if( !empty( $state ) ) {
$status->StatusSTS->POWER = $state;
}
}

$i = 1;
$power = "POWER".$i;

while( isset( $status->StatusSTS->$power ) ) {
$state = NULL;


$state = $status->StatusSTS->$power;
//try to detect OFF
if( in_array( $state, $offArray ) ) {
$state = "OFF";
} elseif( in_array( $state, $onArray ) ) {
$state = "ON";
}

if( !empty( $state ) ) {
$status->StatusSTS->$power = $state;
}


$i++;
$power = "POWER".$i;
}


return $status;
}


/**
* @param $ip
* @param $cmnd
Expand All @@ -541,9 +635,9 @@ private function doRequest( $device, $cmnd, $try = 1 ) {
$result = NULL;


// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/BME680.json";
// }
// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/BME680.json";
// }


$ch = curl_init();
Expand Down Expand Up @@ -588,6 +682,7 @@ private function doRequest( $device, $cmnd, $try = 1 ) {
} else {
if( empty( $data->ERROR ) ) {
$data = $this->compatibility( $data );
$data = $this->stateTextsDetection( $data );
}
}

Expand All @@ -606,9 +701,9 @@ public function doAjax( $try = 1 ) {
urldecode( $_REQUEST[ "cmnd" ] )
);

// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/test.json";
// }
// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/test.json";
// }

$result = NULL;
$ch = curl_init();
Expand Down Expand Up @@ -654,6 +749,7 @@ public function doAjax( $try = 1 ) {
} else {
if( empty( $data->ERROR ) ) {
$data = $this->compatibility( $data );
$data = $this->stateTextsDetection( $data );
}
}
}
Expand All @@ -677,9 +773,9 @@ public function doAjaxAll( $try = 1 ) {
$cmnd
);

// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/BME680.json";
// }
// if( $device->id == 6 ) {
// $url = "http://tasmoAdmin/dev/BME680.json";
// }

$urls[ $url ] = $device;
$urlsClone[] = $url;
Expand All @@ -695,13 +791,13 @@ public function doAjaxAll( $try = 1 ) {
$options = [
CURLOPT_FOLLOWLOCATION => 0,
CURLOPT_RETURNTRANSFER => 1,
// CURLOPT_NOSIGNAL => 1,
// CURLOPT_HEADER => 0,
// CURLOPT_HTTPHEADER => [
// 'Content-Type: application/json',
// 'Accept: application/json',
// ],
// CURLOPT_CONNECTTIMEOUT => 5,
// CURLOPT_NOSIGNAL => 1,
// CURLOPT_HEADER => 0,
// CURLOPT_HTTPHEADER => [
// 'Content-Type: application/json',
// 'Accept: application/json',
// ],
// CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 8,
CURLOPT_ENCODING => '',
];
Expand Down Expand Up @@ -729,15 +825,15 @@ public function doAjaxAll( $try = 1 ) {
$output = curl_multi_getcontent( $done[ 'handle' ] );
$device = $urls[ $info[ 'url' ] ];

// if ( curl_errno( $done[ 'handle' ] ) !== 0
// || intval( $info[ 'http_code' ] ) !== 200 ) { //if server responded with http error
// var_dump( $info );
// var_dump( curl_errno( $done[ 'handle' ] ) );
// var_dump( curl_error( $done[ 'handle' ] ) );
// var_dump( $done[ 'handle' ] );
// if ( curl_errno( $done[ 'handle' ] ) !== 0
// || intval( $info[ 'http_code' ] ) !== 200 ) { //if server responded with http error
// var_dump( $info );
// var_dump( curl_errno( $done[ 'handle' ] ) );
// var_dump( curl_error( $done[ 'handle' ] ) );
// var_dump( $done[ 'handle' ] );
//
// die();
// }
// die();
// }

if( !$output || $output == "" ) {
$data = new stdClass();
Expand Down Expand Up @@ -776,6 +872,7 @@ public function doAjaxAll( $try = 1 ) {
}
if( empty( $data->ERROR ) ) {
$data = $this->compatibility( $data );
$data = $this->stateTextsDetection( $data );
}

$result[ $device->id ] = $data;
Expand Down Expand Up @@ -868,13 +965,15 @@ public function search( $urls = [] ) {

if( empty( $data->ERROR ) ) {
$data = $this->compatibility( $data );
$data = $this->stateTextsDetection( $data );
}
$result[] = $data;
}
} else {

if( empty( $data->ERROR ) ) {
$data = $this->compatibility( $data );
$data = $this->stateTextsDetection( $data );
}
$result[] = $data;
}
Expand Down Expand Up @@ -958,11 +1057,11 @@ public function decodeOptions( $options ) {
$decodedOptopns->$SetOPtion = new stdClass();
$decodedOptopns->$SetOPtion->desc = $a_setoption[ $i ];
$decodedOptopns->$SetOPtion->value = $optionV;
// $decodedOptopns[ $i ] = [
// "desc" => $a_setoption[ $i ],
// "value" => $optionV,
// ];
// debug( $a_setoption[ $i ]." => ".$optionV );
// $decodedOptopns[ $i ] = [
// "desc" => $a_setoption[ $i ],
// "value" => $optionV,
// ];
// debug( $a_setoption[ $i ]." => ".$optionV );
}


Expand Down

0 comments on commit 612ddab

Please sign in to comment.