set_system_action.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. if($_POST['SystemDateTime'] != ""){
  14. if(!validateDate($_POST['SystemDateTime'])){
  15. $jsone['result'] = "Fail";
  16. $jsone['message'] = "SystemDateTime format or content error, fill it with 'yyyy-mm-dd hh:ii:ss";
  17. echo json_encode($jsone);
  18. return false;
  19. }
  20. }
  21. // checkValue("AcPhaseCount",1);
  22. // checkValue("FactoryConfiguration",1);
  23. checkValue("PhaseLossPolicy",2);
  24. checkValue("AuthorisationMode",2);
  25. checkValue("isAPP",2);
  26. checkValue("isQRCode",2);
  27. checkValue("isRFID",2);
  28. checkValue("QRCodeMadeMode",2);
  29. checkValue("Intensity",1);
  30. checkValue("RfidCardNumEndian",1);
  31. checkValue("PsuAcInputType",1);
  32. // $json = json_decode(file_get_contents("system.txt"), true);
  33. if($_POST['SystemId'] != ""){
  34. $json['SystemId'] = $_POST["SystemId"];
  35. }
  36. if($_POST['SystemDateTime'] != ""){
  37. $json['SystemDateTime'] = $_POST['SystemDateTime'];
  38. }
  39. if($_POST['PhaseLossPolicy'] != ""){
  40. $json['PhaseLossPolicy'] = (int)$_POST['PhaseLossPolicy'];
  41. }
  42. if($_POST['FactoryConfiguration'] != ""){
  43. $json['FactoryConfiguration'] = $_POST['FactoryConfiguration'];
  44. }
  45. if($_POST['AuthorisationMode'] != ""){
  46. $json['AuthorisationMode'] = (int)$_POST['AuthorisationMode'];
  47. }
  48. if($_POST['isAPP'] != ""){
  49. $json['isAPP'] = (int)$_POST['isAPP'];
  50. }
  51. if($_POST['isQRCode'] != ""){
  52. $json['isQRCode'] = (int)$_POST['isQRCode'];
  53. }
  54. if($_POST['isRFID'] != ""){
  55. $json['isRFID'] = (int)$_POST['isRFID'];
  56. }
  57. if($_POST['QRCodeMadeMode'] != ""){
  58. $json['QRCodeMadeMode'] = (int)$_POST['QRCodeMadeMode'];
  59. }
  60. if($_POST['QRCodeContent'] != ""){
  61. $json['QRCodeContent'] = $_POST['QRCodeContent'];
  62. }
  63. if($_POST['Intensity'] != ""){
  64. $json['Intensity'] = (int)$_POST['Intensity'];
  65. }
  66. if($_POST['RfidCardNumEndian'] != ""){
  67. $json['RfidCardNumEndian'] = $_POST['RfidCardNumEndian'];
  68. }
  69. if($_POST['PsuAcInputType'] != ""){
  70. $json['PsuAcInputType'] = $_POST['PsuAcInputType'];
  71. }
  72. // ob_start();
  73. shell_exec('sync;sync;sync');
  74. chdir("/root");
  75. $str_json=json_encode($json);//echo $str_json;exit;
  76. exec("'./WebService' '1' '".$str_json."'",$output,$return_var);
  77. if(count($output)!=0){
  78. $jsone['result'] = "Success";
  79. $jsone['message'] = $json;
  80. echo json_encode($jsone);
  81. exit;
  82. }
  83. else{
  84. $jsone['result'] = "Error";
  85. $jsone['message'] = "Something went wrong on machine";
  86. echo json_encode($jsone);
  87. return false;
  88. exit;
  89. }
  90. // ob_end_clean();
  91. }
  92. function validateDate($date, $format = 'Y-m-d H:i:s')
  93. {
  94. $d = DateTime::createFromFormat($format, $date);
  95. return $d && $d->format($format) == $date;
  96. }
  97. function checkValue($id,$idx){
  98. if($idx==1){
  99. if($_POST[$id]!=""){
  100. if(strlen($_POST[$id])!=1){
  101. $jsone['result'] = "Fail";
  102. $jsone['message'] = "You have entered a wrong length on " . $id . ", it should be 1 byte";
  103. echo json_encode($jsone);
  104. exit;
  105. }
  106. if(!ereg("[0-9]",$_POST[$id])){
  107. $jsone['result'] = "Fail";
  108. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  109. echo json_encode($jsone);
  110. exit;
  111. }
  112. }
  113. }
  114. if($idx==2){
  115. if($_POST[$id]!=""){
  116. if(strlen($_POST[$id])!=1){
  117. $jsone['result'] = "Fail";
  118. $jsone['message'] = "You have entered a wrong length on " . $id . ", it should be 1 byte";
  119. echo json_encode($jsone);
  120. exit;
  121. }
  122. if(!ereg("[0-1]",$_POST[$id])){
  123. $jsone['result'] = "Fail";
  124. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be 0 or 1";
  125. echo json_encode($jsone);
  126. exit;
  127. }
  128. }
  129. }
  130. // if($idx==2){
  131. // if($_POST[$id]!=""){
  132. // if(strlen($_POST[$id])>2){
  133. // $jsone['result'] = "Fail";
  134. // $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  135. // echo json_encode($jsone);
  136. // exit;
  137. // }
  138. // if(!ereg("[0-9]",$_POST[$id]) & !ereg("[0-9]{2}",$_POST[$id])){
  139. // $jsone['result'] = "Fail";
  140. // $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  141. // echo json_encode($jsone);
  142. // exit;
  143. // }
  144. // }
  145. // }
  146. }
  147. ?>