set_charging_action.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. checkMaxValue("MaxChargingEnergy");
  14. checkMaxValue("MaxChargingPower");
  15. checkMaxValue("MaxChargingCurrent");
  16. checkMaxValue("MaxChargingDuration");
  17. checkValue("PhaseLossPolicy");
  18. // $json = json_decode(file_get_contents("charging.txt"), true);
  19. // $json = string[];
  20. $json['MaxChargingEnergy'] = (int)$_POST['MaxChargingEnergy'];
  21. $json['MaxChargingPower'] = (int)$_POST['MaxChargingPower'];
  22. $json['MaxChargingCurrent'] = (int)$_POST['MaxChargingCurrent'];
  23. $json['MaxChargingDuration'] = (int)$_POST['MaxChargingDuration'];
  24. $json['PhaseLossPolicy'] = (int)$_POST['PhaseLossPolicy'];
  25. $json['LocalWhiteCard0'] = isset($_POST['LocalWhiteCard0'])?$_POST['LocalWhiteCard0']:"";
  26. $json['LocalWhiteCard1'] = isset($_POST['LocalWhiteCard1'])?$_POST['LocalWhiteCard1']:"";
  27. $json['LocalWhiteCard2'] = isset($_POST['LocalWhiteCard2'])?$_POST['LocalWhiteCard2']:"";
  28. $json['LocalWhiteCard3'] = isset($_POST['LocalWhiteCard3'])?$_POST['LocalWhiteCard3']:"";
  29. $json['LocalWhiteCard4'] = isset($_POST['LocalWhiteCard4'])?$_POST['LocalWhiteCard4']:"";
  30. $json['LocalWhiteCard5'] = isset($_POST['LocalWhiteCard5'])?$_POST['LocalWhiteCard5']:"";
  31. $json['LocalWhiteCard6'] = isset($_POST['LocalWhiteCard6'])?$_POST['LocalWhiteCard6']:"";
  32. $json['LocalWhiteCard7'] = isset($_POST['LocalWhiteCard7'])?$_POST['LocalWhiteCard7']:"";
  33. $json['LocalWhiteCard8'] = isset($_POST['LocalWhiteCard8'])?$_POST['LocalWhiteCard8']:"";
  34. $json['LocalWhiteCard9'] = isset($_POST['LocalWhiteCard9'])?$_POST['LocalWhiteCard9']:"";
  35. // $json['CcsAuthentication'] = $_POST['CcsAuthentication'];
  36. // $json['AcCcsChargingModel'] = $_POST['AcCcsChargingModel'];
  37. // ob_start();
  38. shell_exec('sync;sync;sync');
  39. chdir("/root");
  40. $str_json=json_encode($json);
  41. exec("'./WebService' '2' '".$str_json."'",$output,$return_var);
  42. if(count($output)!=0){
  43. $jsone['result'] = "Success";
  44. $jsone['message'] = $json;
  45. echo json_encode($jsone);
  46. exit;
  47. }
  48. else{
  49. $jsone['result'] = "Error";
  50. $jsone['message'] = "Something went wrong on machine";
  51. echo json_encode($jsone);
  52. return false;
  53. exit;
  54. }
  55. // ob_end_clean();
  56. }
  57. function checkMaxValue($id){
  58. if($_POST[$id] != ""){
  59. if(!is_numeric($_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. if($_POST[$id]>65535){
  66. $jsone['result'] = "Fail";
  67. $jsone['message'] = "You have entered a over value on " . $id . ", it should be less than 65535";
  68. echo json_encode($jsone);
  69. exit;
  70. }
  71. if($_POST[$id]<0){
  72. $jsone['result'] = "Fail";
  73. $jsone['message'] = "You have entered a over value on " . $id . ", it should be more than 0";
  74. echo json_encode($jsone);
  75. exit;
  76. }
  77. }
  78. }
  79. function checkValue($id){
  80. if($_POST[$id] != ""){
  81. if(strlen($_POST[$id])!=1){
  82. $jsone['result'] = "Fail";
  83. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  84. echo json_encode($jsone);
  85. exit;
  86. }
  87. if(!ereg("[0-9]",$_POST[$id])){
  88. $jsone['result'] = "Fail";
  89. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  90. echo json_encode($jsone);
  91. exit;
  92. }
  93. }
  94. }
  95. ?>