2022-01-27 18:41:16 +01:00
< ? php
// server init: No Timeout
set_time_limit ( 0 );
ob_implicit_flush ();
$error_message = null ;
$error_code = null ;
$listenHost = " 0.0.0.0 " ;
$listenPort = 1025 ;
$socket = stream_socket_server ( " tcp:// " . $listenHost . " : " . $listenPort , $error_code , $error_message ) or logger ( ERROR , _ ( " Could not create socket " ) . EOL );
stream_set_blocking ( $socket , false );
$read = array ( $socket );
function askWebServer ( $read )
{
$array = array ();
$argList = array ();
if ( stream_select ( $read , $array , $array , 0 ))
{
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " socket ready to read " ), __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
$spawn = stream_socket_accept ( $read [ 0 ]);
if ( $spawn !== false )
{
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " socket accepted " ), __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
$input = fgets ( $spawn , 4096 );
2022-01-28 23:05:58 +01:00
logger ( DEBUG , $input , __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
if ( ! empty ( $input ))
{
$input = substr ( $input , 5 );
$input = explode ( " " , $input ); // suppress text
$argTmp = explode ( " & " , $input [ 0 ]);
foreach ( $argTmp as $tmp )
{
2022-02-02 21:18:44 +01:00
//logger(DEBUG, $tmp, __FILE__ . ":" . __LINE__);
$argList [ strchr ( $tmp , " = " , true )] = substr ( strchr ( $tmp , " = " ), 1 );
//logger(DEBUG, $argList[0] . " ==========> " . $argList[1]);
2022-01-28 23:05:58 +01:00
//print_r($array);
2022-02-02 21:18:44 +01:00
//if (isset($array[1])) $argList[$array[0]] = $array[1];
2022-01-27 18:41:16 +01:00
}
2022-02-02 21:18:44 +01:00
logger ( DEBUG , print_r ( $argList , true ));
2022-01-27 18:41:16 +01:00
if ( array_key_exists ( " cmd " , $argList ))
{
2022-01-28 23:05:58 +01:00
$command = strtolower ( $argList [ " cmd " ]);
2022-02-02 21:18:44 +01:00
logger ( DEBUG , _ ( " command is " ) . $command );
2022-01-28 23:05:58 +01:00
switch ( $command )
2022-01-27 18:41:16 +01:00
{
case " get " :
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " GET reached " ), __FILE__ . " : " . __LINE__ );
2022-02-02 21:18:44 +01:00
if ( ! array_key_exists ( " topic " , $argList ) or ! array_key_exists ( " fn " , $argList ) or ! array_key_exists ( " property " , $argList ))
2022-01-27 18:41:16 +01:00
{
2022-02-02 21:18:44 +01:00
$response = " <html><header></header><body>GET: " . _ ( " no parameters passed, need topic, fn and property " ) . " </body></html> " ;
} else
{
$device = getDevice ( $argList [ " topic " ], $argList [ " fn " ]);
$property = $argList [ " property " ];
$response = " <html><header></header><body>GET: " . bool2string ( $device -> $property [ " value " ]) . " </body></html> " ;
2022-01-27 18:41:16 +01:00
}
fwrite ( $spawn , $response );
break ;
case " set " :
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " SET reached " ), __FILE__ . " : " . __LINE__ );
2022-02-02 21:18:44 +01:00
if ( ! array_key_exists ( " topic " , $argList ) or ! array_key_exists ( " fn " , $argList ) or ! array_key_exists ( " property " , $argList ) or ! array_key_exists ( " value " , $argList ))
{
$response = " <html><header></header><body>SET: " . _ ( " no parameters passed, need topic, fn, property and value " ) . " passed</body></html> " ;
fwrite ( $spawn , $response );
} else
{
$response = " <html><header></header><body>setting property " . $argList [ " property " ] . " of " . $argList [ " fn " ] . " to value: " . $argList [ " value " ] . " </body></html> " ;
fwrite ( $spawn , $response );
$payload = array ( $argList [ " property " ] => $argList [ " value " ]);
publish ( Z2M . " / " . $argList [ " fn " ], $payload );
}
2022-01-27 18:41:16 +01:00
break ;
2022-01-28 23:05:58 +01:00
case " dump " :
2022-01-27 18:41:16 +01:00
case " print " :
2022-01-28 23:05:58 +01:00
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
$var = $GLOBALS [ $argList [ " object " ]];
if ( isset ( $argList [ " topic " ]))
{
$topic = $argList [ " topic " ];
}
if ( isset ( $argList [ " address " ]))
{
$var = $var [ $argList [ " address " ]];
} elseif ( isset ( $argList [ " fn " ]))
{
if ( ! empty ( $topic ))
{
2022-01-30 00:21:50 +01:00
$var = getDevice ( $topic , $argList [ " fn " ]);
2022-01-27 18:41:16 +01:00
} else
{
2022-01-28 23:05:58 +01:00
$str = _ ( " topic is not defining: add &topic= \n These topics are availables: " );
foreach ( $topics as $key => $value )
{
$str .= $key . EOL ;
}
logger ( ERROR , $str , __FILE__ . " : " . __LINE__ );
2022-01-27 18:41:16 +01:00
}
}
$error = error_get_last ();
if ( $error !== null )
{
2022-02-02 21:18:44 +01:00
$response = " <html><header></header><body> " . $error [ " message " ] . " file: " . $error [ " file " ] . " line: " . $error [ " line " ] . " </body></html> " ;
2022-01-27 18:41:16 +01:00
}
2022-01-28 23:05:58 +01:00
if ( $command === " print " )
{
$response = print_r ( $var , true );
} elseif ( $command === " dump " )
{
$response = " Dump " . EOL ;
$response .= var_export ( $var , true );
}
2022-01-27 18:41:16 +01:00
fwrite ( $spawn , $response );
break ;
2022-01-30 00:21:50 +01:00
case " notify " :
2022-02-02 21:18:44 +01:00
logger ( DEBUG , $command . _ ( " reached " ), __FILE__ . " : " . __LINE__ );
2022-01-30 00:21:50 +01:00
if ( ! array_key_exists ( " topic " , $argList ) or ! array_key_exists ( " fn " , $argList ) or ! array_key_exists ( " property " , $argList ) or ! array_key_exists ( " condition " , $argList ) or ! array_key_exists ( " value " , $argList ))
{
2022-02-02 21:18:44 +01:00
$response = " <html><header></header><body> " . _ ( " Error: With 'notify' command, you need 4 parameters: topic, fn, property, condition, value " ) . " </body></html> " ;
fwrite ( $spawn , $response );
2022-01-30 00:21:50 +01:00
} else
{
2022-02-02 21:18:44 +01:00
$response = " <html><header></header><body> " . _ ( " notify command have been set " ) . " </body></html> " ;
2022-01-30 00:21:50 +01:00
$monitored [] = new watch ( $argList [ " topic " ], $argList [ " fn " ], $argList [ " property " ], $argList [ " condition " ], $argList [ " value " ]);
2022-02-02 21:18:44 +01:00
fwrite ( $spawn , $response );
2022-01-30 00:21:50 +01:00
}
2022-02-02 21:18:44 +01:00
logger ( DEBUG , print_r ( $monitored , true ));
2022-01-30 00:21:50 +01:00
break ;
2022-01-27 18:41:16 +01:00
default :
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " unknown command " ), __FILE__ . " : " . __LINE__ );
2022-02-02 21:18:44 +01:00
$response = " <html><header></header><body> " . _ ( " unknown command " ) . " </body></html> " ;
fwrite ( $spawn , $response );
2022-01-27 18:41:16 +01:00
}
}
}
}
}
}
?>