set_backend_action.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // 設置資料類型 json,編碼格式 utf-8
  3. header('Content-Type: application/json; charset=UTF-8');
  4. // 判斷如果是 GET 請求,則進行搜尋;如果是 POST 請求,則進行新建
  5. // $_SERVER['REQUEST_METHOD'] 返回訪問頁面使用的請求方法
  6. if ($_SERVER['REQUEST_METHOD'] == "GET") {
  7. create();
  8. } else if ($_SERVER['REQUEST_METHOD'] == "POST") {
  9. create();
  10. }
  11. // 新建員工
  12. function create() {
  13. $pattern="/^(ws|wss):\/\/((([0-9]{1,3}\.){3}[0-9]{1,3})|(([a-zA-Z0-9]+(([\-]?[a-zA-Z0-9]+)*\.)+)*[a-zA-Z]{2,}))+/";
  14. if($_POST['OcppServerURL']!=""){
  15. if(!preg_match($pattern, $_POST['OcppServerURL'])){
  16. $jsone['result'] = "Fail";
  17. $jsone['message'] = "OcppServerURL format error, fill it with 'ws://' or 'wss://'";
  18. echo json_encode($jsone);
  19. return false;
  20. }
  21. }
  22. checkValue("OfflinePolicy");
  23. // $json = json_decode(file_get_contents("charging.txt"), true);
  24. // $json = string[];
  25. $json['BackendConnTimeout'] = (int)$_POST['BackendConnTimeout'];
  26. $json['OfflinePolicy'] = (int)$_POST['OfflinePolicy'];
  27. $json['OfflineMaxChargeEnergy'] = (int)$_POST['OfflineMaxChargeEnergy'];
  28. $json['OfflineMaxChargeDuration'] = (int)$_POST['OfflineMaxChargeDuration'];
  29. // $json['OcppConnStatus'] = $_POST['OcppConnStatus'];
  30. $json['OcppServerURL'] = $_POST['OcppServerURL'];
  31. $json['ChargeBoxId'] = $_POST['ChargeBoxId'];
  32. // ob_start();
  33. shell_exec('sync;sync;sync');
  34. chdir("/root");
  35. $str_json=json_encode($json);//var_dump($str_json);
  36. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  37. if(count($output)!=0){
  38. $jsone['result'] = "Success";
  39. $jsone['message'] = $json;
  40. echo json_encode($jsone);
  41. exit;
  42. }
  43. else{
  44. $jsone['result'] = "Error";
  45. $jsone['message'] = "Something went wrong on machine";
  46. echo json_encode($jsone);
  47. return false;
  48. exit;
  49. }
  50. // ob_end_clean();
  51. }
  52. function checkValue($id){
  53. if(strlen($_POST[$id])!=1){
  54. $jsone['result'] = "Fail";
  55. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  56. echo json_encode($jsone);
  57. exit;
  58. }
  59. if(!ereg("[0-9]",$_POST[$id])){
  60. $jsone['result'] = "Fail";
  61. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  62. echo json_encode($jsone);
  63. exit;
  64. }
  65. }
  66. ?>