set_pcabinet_action.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //Cmd 1: force charging 2: remote stop
  14. //1: force charging
  15. //EnableForceCharging
  16. //StartForceCharging
  17. //FTargetVoltage
  18. //FTargetCurrent
  19. //2: remote stop
  20. checkValue('Cmd');
  21. if(isset($_REQUEST['Gun'])){
  22. isNumeric("Gun",4);
  23. $json['Gun'] = (int)$_REQUEST['Gun'];
  24. }
  25. else{
  26. $jsone['result'] = "Fail";
  27. $jsone['message'] = "You should enter a value of Gun(0~3)";
  28. echo json_encode($jsone);
  29. exit;
  30. }
  31. if($_REQUEST['Cmd']==1){
  32. if(isset($_REQUEST['FTargetVoltage'])){
  33. isNumeric("FTargetVoltage",65535);
  34. $json['FTargetVoltage'] = (int)$_REQUEST['FTargetVoltage'];
  35. }
  36. else{
  37. $jsone['result'] = "Fail";
  38. $jsone['message'] = "You should enter a value of FTargetVoltage";
  39. echo json_encode($jsone);
  40. exit;
  41. }
  42. if(isset($_REQUEST['FTargetCurrent'])){
  43. isNumeric("FTargetCurrent",65535);
  44. $json['FTargetCurrent'] = (int)$_REQUEST['FTargetCurrent'];
  45. }
  46. else{
  47. $jsone['result'] = "Fail";
  48. $jsone['message'] = "You should enter a value of FTargetCurrent";
  49. echo json_encode($jsone);
  50. exit;
  51. }
  52. }
  53. // ob_start();
  54. shell_exec('sync;sync;sync');
  55. chdir("/root");
  56. $str_json=json_encode($json);//var_dump($str_json);
  57. if($_REQUEST['Cmd']=="1"){
  58. exec("'./WebService' 'PowerCabinet' '1' '".$str_json."'",$output,$return_var);
  59. }
  60. if($_REQUEST['Cmd']=="2"){
  61. exec("'./WebService' 'PowerCabinet' '2' '".$str_json."'",$output,$return_var);
  62. }
  63. if(count($output)!=0){
  64. $jsone['result'] = "Success";
  65. $jsone['message'] = $json;
  66. echo json_encode($jsone);
  67. exit;
  68. }
  69. else{
  70. $jsone['result'] = "Error";
  71. $jsone['message'] = "Something went wrong on machine";
  72. echo json_encode($jsone);
  73. return false;
  74. exit;
  75. }
  76. // ob_end_clean();
  77. }
  78. function checkValue($id){
  79. if($_REQUEST[$id] != ""){
  80. if($_REQUEST[$id]!=1 && $_REQUEST[$id]!=2){
  81. $jsone['result'] = "Fail";
  82. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be 1 or 2";
  83. echo json_encode($jsone);
  84. exit;
  85. }
  86. /* if(strlen($_REQUEST[$id])!=1){
  87. $jsone['result'] = "Fail";
  88. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  89. echo json_encode($jsone);
  90. exit;
  91. }
  92. if(!is_numeric($_REQUEST[$id])){
  93. $jsone['result'] = "Fail";
  94. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  95. echo json_encode($jsone);
  96. exit;
  97. }*/
  98. }
  99. }
  100. function checkLength($id,$value){
  101. if(strlen($_REQUEST[$id])>$value){
  102. $jsone['result'] = "Fail";
  103. $jsone['message'] = "Length of " . $id . " should be less than ".$value+"(Chinese 5 words)";
  104. echo json_encode($jsone);
  105. exit;
  106. }
  107. }
  108. function isNumeric($id,$value){
  109. if($_REQUEST[$id] != ""){
  110. if(!is_numeric($_REQUEST[$id])){
  111. $jsone['result'] = "Fail";
  112. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  113. echo json_encode($jsone);
  114. exit;
  115. }
  116. if($_REQUEST[$id]>=$value){
  117. $jsone['result'] = "Fail";
  118. $jsone['message'] = "Value of " . $id . " should be less than ".$value;
  119. echo json_encode($jsone);
  120. exit;
  121. }
  122. }
  123. else{
  124. $jsone['result'] = "Fail";
  125. $jsone['message'] = "You should enter a value of " . $id;
  126. echo json_encode($jsone);
  127. exit;
  128. }
  129. }
  130. ?>