set_backend_action.php 3.0 KB

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