set_pcabinet_action.php 3.3 KB

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