Skip to content

Commit

Permalink
Отслеживание json obj data unvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
fandaymon committed Oct 17, 2020
1 parent ceaae33 commit a353b53
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
60 changes: 57 additions & 3 deletions scripts/cycle_local_tuya.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//echo 'Запуск проверки статуса ' . date('H:i:s') . PHP_EOL;

foreach ($devices as $device) {
echo $device['TITLE'] . PHP_EOL;
//echo $device['TITLE'] . PHP_EOL;
$command = 'STATUS';

$local_key = $device['LOCAL_KEY'];
Expand All @@ -80,15 +80,15 @@
$buf='';

if (socket_connect($socket, $local_ip, 6668)) {
//echo 'Connect <BR>' ;
//echo 'Connect '. PHP_EOL ;
for ($i=0;$i<1;$i++) {
$send=socket_send($socket, $payload, strlen($payload), 0);
if ($send!=strlen($payload)) {
echo date('y-m-d h:i:s') . ' sended '.$send .' from ' .strlen($payload) . 'ip' . $local_ip . '<BR>';
}
$buf='';
$reciv=socket_recv ( $socket , $buf , 2048 ,0);
//echo date('y-m-d h:i:s') . ' recived '.strlen($buf) . '<BR>';
//echo date('y-m-d h:i:s') . ' recived '.strlen($buf) . PHP_EOL;
if ($buf!='') break;
sleep(1);
}
Expand All @@ -104,6 +104,60 @@
//echo $result . PHP_EOL;

$status=json_decode($result);

if ($status=='json obj data unvalid') {
$command = 'STATUS';

$local_key = $device['LOCAL_KEY'];
$dev_id = $device['DEV_ID'];
$local_ip = $device['DEV_IP'];

$hexByte="0d";
$dps= '{"1": null, "2": null}';

if ($device['ZIGBEE'] == 0) {
$json='{"gwId":"'.$dev_id.'","devId":"'.$dev_id.'", "t": "'.time().'", "dps": ' . $dps . '}';

} else {
$json = '{"dps":'.$dps.', "t": "'.time().'","cid":"'.$device['MAC'].'"}';
}

$payload =$tuya_module->TuyaLocalEncrypt($hexByte, $json, $local_key);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" => 0));

$buf='';

if (socket_connect($socket, $local_ip, 6668)) {
//echo 'Connect '. PHP_EOL ;
for ($i=0;$i<1;$i++) {
$send=socket_send($socket, $payload, strlen($payload), 0);
if ($send!=strlen($payload)) {
echo date('y-m-d h:i:s') . ' sended '.$send .' from ' .strlen($payload) . 'ip' . $local_ip . '<BR>';
}
$buf='';
$reciv=socket_recv ( $socket , $buf , 2048 ,0);
//echo date('y-m-d h:i:s') . ' recived '.strlen($buf) . '<BR>';
if ($buf!='') break;
sleep(1);
}

} else {
$err = socket_last_error($socket);
echo date('y-m-d h:i:s') .' ' .socket_strerror($err) . ' '. $local_ip ."\n";
}

socket_close($socket);
$result = substr($buf,20,-8);
$result = openssl_decrypt($result, 'AES-128-ECB', $local_key, OPENSSL_RAW_DATA);
echo $result . PHP_EOL;

$status=json_decode($result);


}

$dps=$status->dps;
foreach ($dps as $k=>$d){
if (is_bool($d)) {
Expand Down
17 changes: 12 additions & 5 deletions templates/tuya/tudevices_edit_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,23 @@
</td>
</tr>

<tr id="add[#ID#]" class="d-flex" style="display:none;" bgcolor="lightgray">
<tr id="add[#ID#]" style="display:none;" bgcolor="lightgray"><td colspan="8">
<table>
<tbody>
<tr>
<td><b>Alias:</b></td>
<td valign="top"><input type="text" name="alias[#ID#]" id="alias[#ID#]" value="[#ALIAS#]"></td>
<td valign="top"><input type="checkbox" name="dividedby10[#ID#]" id="dividedby10[#ID#]" value="1" [#if DIVIDEDBY10=='1'#]checked[#endif DIVIDEDBY10#]><label for="dividedby10[#ID#]"> /10</label></td>
<td valign="top" class="col-sm-2"><input type="checkbox" name="dividedby100[#ID#]" id="dividedby100[#ID#]" value="1" [#if DIVIDEDBY100=='1'#]checked[#endif DIVIDEDBY100#]><label for="dividedby100[#ID#]"> /100</label></td>
<td valign="top"><input type="checkbox" name="dividedby2[#ID#]" id="dividedby2[#ID#]" value="1" [#if DIVIDEDBY2=='1'#]checked[#endif DIVIDEDBY2#]><label for="dividedby2[#ID#]"> /2</label></td>
<td valign="top" width="100px"><input type="checkbox" name="dividedby10[#ID#]" id="dividedby10[#ID#]" value="1" [#if DIVIDEDBY10=='1'#]checked[#endif DIVIDEDBY10#]><label for="dividedby10[#ID#]"> /10</label></td>
<td valign="top" width="100px"><input type="checkbox" name="dividedby100[#ID#]" id="dividedby100[#ID#]" value="1" [#if DIVIDEDBY100=='1'#]checked[#endif DIVIDEDBY100#]><label for="dividedby100[#ID#]"> /100</label></td>
<td valign="top" width="100px"><input type="checkbox" name="dividedby2[#ID#]" id="dividedby2[#ID#]" value="1" [#if DIVIDEDBY2=='1'#]checked[#endif DIVIDEDBY2#]><label for="dividedby2[#ID#]"> /2</label></td>
<td><b>Value Scale:</b></td>
<td valign="top"><input type="text" name="value_scale[#ID#]" id="value_scale[#ID#]" value="[#VALUE_SCALE#]"></td>
<td valign="top"><b>VALUE_MIN:</b> [#VALUE_MIN#] <b>VALUE_MAX:</b> [#VALUE_MAX#] <b>UNIT:</b> [#VALUE_UNIT#] <b>Mode: </b>[#MODE#] </td>
</tr>
</tbody>
</table>
</td>
</tr>

[#end PROPERTIES#]
</table>
</div>
Expand Down

0 comments on commit a353b53

Please sign in to comment.