set_backend_action.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. checkLength("ChargeBoxId",25);
  24. checkLength("chargePointVendor",20);
  25. // $json = json_decode(file_get_contents("charging.txt"), true);
  26. // $json = string[];
  27. $json['BackendConnTimeout'] = (int)$_POST['BackendConnTimeout'];
  28. $json['OfflinePolicy'] = (int)$_POST['OfflinePolicy'];
  29. $json['OfflineMaxChargeEnergy'] = (int)$_POST['OfflineMaxChargeEnergy'];
  30. $json['OfflineMaxChargeDuration'] = (int)$_POST['OfflineMaxChargeDuration'];
  31. // $json['OcppConnStatus'] = $_POST['OcppConnStatus'];
  32. $json['OcppServerURL'] = $_POST['OcppServerURL'];
  33. $json['ChargeBoxId'] = $_POST['ChargeBoxId'];
  34. $json['chargePointVendor'] = $_POST['chargePointVendor'];
  35. // ob_start();
  36. shell_exec('sync;sync;sync');
  37. chdir("/root");
  38. $str_json=json_encode($json);//var_dump($str_json);
  39. exec("'./WebService' '4' '".$str_json."'",$output,$return_var);
  40. if(count($output)!=0){
  41. $jsone['result'] = "Success";
  42. $jsone['message'] = $json;
  43. echo json_encode($jsone);
  44. exit;
  45. }
  46. else{
  47. $jsone['result'] = "Error";
  48. $jsone['message'] = "Something went wrong on machine";
  49. echo json_encode($jsone);
  50. return false;
  51. exit;
  52. }
  53. // ob_end_clean();
  54. }
  55. function checkValue($id){
  56. if(strlen($_POST[$id])!=1){
  57. $jsone['result'] = "Fail";
  58. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  59. echo json_encode($jsone);
  60. exit;
  61. }
  62. if(!ereg("[0-9]",$_POST[$id])){
  63. $jsone['result'] = "Fail";
  64. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  65. echo json_encode($jsone);
  66. exit;
  67. }
  68. }
  69. function checkLength($id,$value){
  70. if(strlen($_POST[$id])>$value){
  71. $jsone['result'] = "Fail";
  72. $jsone['message'] = "Length of " . $id . " should be less than ".$value;
  73. echo json_encode($jsone);
  74. exit;
  75. }
  76. }
  77. ?>