set_charging_action.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. checkLength("LocalWhiteCard0");
  14. checkLength("LocalWhiteCard1");
  15. checkLength("LocalWhiteCard2");
  16. checkLength("LocalWhiteCard3");
  17. checkLength("LocalWhiteCard4");
  18. checkLength("LocalWhiteCard5");
  19. checkLength("LocalWhiteCard6");
  20. checkLength("LocalWhiteCard7");
  21. checkLength("LocalWhiteCard8");
  22. checkLength("LocalWhiteCard9");
  23. checkMaxValue("MaxChargingEnergy");
  24. checkMaxValue("MaxChargingPower");
  25. checkMaxValue("MaxChargingCurrent");
  26. if(substr($_POST['ModelName'],-2)=="PS"){
  27. checkMaxValue("MaxChargingVoltage");
  28. }
  29. checkMaxValue("AcMaxChargingCurrent");
  30. checkMaxValue("MaxChargingDuration");
  31. checkMaxValue("MaxChargingSoc");
  32. checkValue("StopChargingByButton");
  33. checkValue("FanControlPolicy");
  34. if(substr($_POST['ModelName'],0,1)=="A" && $_POST['MaxChargingCurrent']<6 && $_POST['MaxChargingCurrent']!=0){
  35. $jsone['result'] = "Fail";
  36. $jsone['message'] = "MaxChargingCurrent must > 6";
  37. echo json_encode($jsone);
  38. exit;
  39. }
  40. if(isset($_POST['MaxChargingEnergy'])){
  41. $json['MaxChargingEnergy'] = (int)$_POST['MaxChargingEnergy'];
  42. }
  43. if(isset($_POST['MaxChargingPower'])){
  44. $json['MaxChargingPower'] = (int)$_POST['MaxChargingPower'];
  45. }
  46. if(isset($_POST['MaxChargingCurrent'])){
  47. $json['MaxChargingCurrent'] = (int)$_POST['MaxChargingCurrent'];
  48. }
  49. if(isset($_POST['MaxChargingVoltage'])){
  50. $json['MaxChargingVoltage'] = (int)$_POST['MaxChargingVoltage'];
  51. }
  52. if(isset($_POST['AcMaxChargingCurrent'])){
  53. $json['AcMaxChargingCurrent'] = (int)$_POST['AcMaxChargingCurrent'];
  54. }
  55. if(isset($_POST['MaxChargingDuration'])){
  56. $json['MaxChargingDuration'] = (int)$_POST['MaxChargingDuration'];
  57. }
  58. if(isset($_POST['MaxChargingSoc'])){
  59. $json['MaxChargingSoc'] = (int)$_POST['MaxChargingSoc'];
  60. }
  61. if(isset($_POST['StopChargingByButton'])){
  62. $json['StopChargingByButton'] = (int)$_POST['StopChargingByButton'];
  63. }
  64. if(isset($_POST['FanControlPolicy'])){
  65. $json['FanControlPolicy'] = (int)$_POST['FanControlPolicy'];
  66. }
  67. if(isset($_POST['LocalWhiteCard0'])){
  68. $json['LocalWhiteCard0'] = isset($_POST['LocalWhiteCard0'])?$_POST['LocalWhiteCard0']:"";
  69. }
  70. if(isset($_POST['LocalWhiteCard1'])){
  71. $json['LocalWhiteCard1'] = isset($_POST['LocalWhiteCard1'])?$_POST['LocalWhiteCard1']:"";
  72. }
  73. if(isset($_POST['LocalWhiteCard2'])){
  74. $json['LocalWhiteCard2'] = isset($_POST['LocalWhiteCard2'])?$_POST['LocalWhiteCard2']:"";
  75. }
  76. if(isset($_POST['LocalWhiteCard3'])){
  77. $json['LocalWhiteCard3'] = isset($_POST['LocalWhiteCard3'])?$_POST['LocalWhiteCard3']:"";
  78. }
  79. if(isset($_POST['LocalWhiteCard4'])){
  80. $json['LocalWhiteCard4'] = isset($_POST['LocalWhiteCard4'])?$_POST['LocalWhiteCard4']:"";
  81. }
  82. if(isset($_POST['LocalWhiteCard5'])){
  83. $json['LocalWhiteCard5'] = isset($_POST['LocalWhiteCard5'])?$_POST['LocalWhiteCard5']:"";
  84. }
  85. if(isset($_POST['LocalWhiteCard6'])){
  86. $json['LocalWhiteCard6'] = isset($_POST['LocalWhiteCard6'])?$_POST['LocalWhiteCard6']:"";
  87. }
  88. if(isset($_POST['LocalWhiteCard7'])){
  89. $json['LocalWhiteCard7'] = isset($_POST['LocalWhiteCard7'])?$_POST['LocalWhiteCard7']:"";
  90. }
  91. if(isset($_POST['LocalWhiteCard8'])){
  92. $json['LocalWhiteCard8'] = isset($_POST['LocalWhiteCard8'])?$_POST['LocalWhiteCard8']:"";
  93. }
  94. if(isset($_POST['LocalWhiteCard9'])){
  95. $json['LocalWhiteCard9'] = isset($_POST['LocalWhiteCard9'])?$_POST['LocalWhiteCard9']:"";
  96. }
  97. if(isset($_POST['isBilling'])){
  98. $json['isBilling'] = (int)$_POST['isBilling'];
  99. }
  100. if(isset($_POST['Currency'])){
  101. $json['Currency'] = (int)$_POST['Currency'];
  102. }
  103. if(isset($_POST['Fee0'])){
  104. $json['Fee0'] = isset($_POST['Fee0'])?round($_POST['Fee0'], 2):"";
  105. }
  106. if(isset($_POST['Fee1'])){
  107. $json['Fee1'] = isset($_POST['Fee1'])?round($_POST['Fee1'], 2):"";
  108. }
  109. if(isset($_POST['Fee2'])){
  110. $json['Fee2'] = isset($_POST['Fee2'])?round($_POST['Fee2'], 2):"";
  111. }
  112. if(isset($_POST['Fee3'])){
  113. $json['Fee3'] = isset($_POST['Fee3'])?round($_POST['Fee3'], 2):"";
  114. }
  115. if(isset($_POST['Fee4'])){
  116. $json['Fee4'] = isset($_POST['Fee4'])?round($_POST['Fee4'], 2):"";
  117. }
  118. if(isset($_POST['Fee5'])){
  119. $json['Fee5'] = isset($_POST['Fee5'])?round($_POST['Fee5'], 2):"";
  120. }
  121. if(isset($_POST['Fee6'])){
  122. $json['Fee6'] = isset($_POST['Fee6'])?round($_POST['Fee6'], 2):"";
  123. }
  124. if(isset($_POST['Fee7'])){
  125. $json['Fee7'] = isset($_POST['Fee7'])?round($_POST['Fee7'], 2):"";
  126. }
  127. if(isset($_POST['Fee8'])){
  128. $json['Fee8'] = isset($_POST['Fee8'])?round($_POST['Fee8'], 2):"";
  129. }
  130. if(isset($_POST['Fee9'])){
  131. $json['Fee9'] = isset($_POST['Fee9'])?round($_POST['Fee9'], 2):"";
  132. }
  133. if(isset($_POST['Fee10'])){
  134. $json['Fee10'] = isset($_POST['Fee10'])?round($_POST['Fee10'], 2):"";
  135. }
  136. if(isset($_POST['Fee11'])){
  137. $json['Fee11'] = isset($_POST['Fee11'])?round($_POST['Fee11'], 2):"";
  138. }
  139. if(isset($_POST['Fee12'])){
  140. $json['Fee12'] = isset($_POST['Fee12'])?round($_POST['Fee12'], 2):"";
  141. }
  142. if(isset($_POST['Fee13'])){
  143. $json['Fee13'] = isset($_POST['Fee13'])?round($_POST['Fee13'], 2):"";
  144. }
  145. if(isset($_POST['Fee14'])){
  146. $json['Fee14'] = isset($_POST['Fee14'])?round($_POST['Fee14'], 2):"";
  147. }
  148. if(isset($_POST['Fee15'])){
  149. $json['Fee15'] = isset($_POST['Fee15'])?round($_POST['Fee15'], 2):"";
  150. }
  151. if(isset($_POST['Fee16'])){
  152. $json['Fee16'] = isset($_POST['Fee16'])?round($_POST['Fee16'], 2):"";
  153. }
  154. if(isset($_POST['Fee17'])){
  155. $json['Fee17'] = isset($_POST['Fee17'])?round($_POST['Fee17'], 2):"";
  156. }
  157. if(isset($_POST['Fee18'])){
  158. $json['Fee18'] = isset($_POST['Fee18'])?round($_POST['Fee18'], 2):"";
  159. }
  160. if(isset($_POST['Fee19'])){
  161. $json['Fee19'] = isset($_POST['Fee19'])?round($_POST['Fee19'], 2):"";
  162. }
  163. if(isset($_POST['Fee20'])){
  164. $json['Fee20'] = isset($_POST['Fee20'])?round($_POST['Fee20'], 2):"";
  165. }
  166. if(isset($_POST['Fee21'])){
  167. $json['Fee21'] = isset($_POST['Fee21'])?round($_POST['Fee21'], 2):"";
  168. }
  169. if(isset($_POST['Fee22'])){
  170. $json['Fee22'] = isset($_POST['Fee22'])?round($_POST['Fee22'], 2):"";
  171. }
  172. if(isset($_POST['Fee23'])){
  173. $json['Fee23'] = isset($_POST['Fee23'])?round($_POST['Fee23'], 2):"";
  174. }
  175. $fCount = empty($_FILES['V2GPrivateKey']['tmp_name'][0]);
  176. if($fCount!=1){
  177. if ($_FILES["V2GPrivateKey"]["error"] > 0)
  178. {
  179. $jsone['result'] = "Error";
  180. $jsone['message'] = $_FILES["V2GPrivateKey"]["error"];
  181. echo json_encode($jsone);
  182. exit;
  183. }
  184. else if(strpos(exec('openssl rsa -noout -modulus -in '.$_FILES['V2GPrivateKey']['tmp_name']),"Modulus")===false){ //unable to load V2GPrivateKey
  185. $jsone['result'] = "Error";
  186. $jsone['message'] = "format of V2G Private Key is incorrect";
  187. echo json_encode($jsone);
  188. exit;
  189. }
  190. sleep(1);
  191. move_uploaded_file($_FILES["V2GPrivateKey"]["tmp_name"],"/Storage/certV2G.key");
  192. }
  193. $fCount = empty($_FILES['V2GRootCertificate']['tmp_name'][0]);
  194. if($fCount!=1){
  195. if ($_FILES["V2GRootCertificate"]["error"] > 0)
  196. {
  197. $jsone['result'] = "Error";
  198. $jsone['message'] = $_FILES["V2GRootCertificate"]["error"];
  199. echo json_encode($jsone);
  200. exit;
  201. }
  202. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['V2GRootCertificate']['tmp_name']),"Modulus")===false){ //unable to load V2GRootCertificate
  203. $jsone['result'] = "Error";
  204. $jsone['message'] = "format of V2G Root Certificate is incorrect";
  205. echo json_encode($jsone);
  206. exit;
  207. }
  208. sleep(1);
  209. move_uploaded_file($_FILES["V2GRootCertificate"]["tmp_name"],"/Storage/rootCaV2g.pem");
  210. }
  211. $fCount = empty($_FILES['V2GCertificateChain']['tmp_name'][0]);
  212. if($fCount!=1){
  213. if ($_FILES["V2GCertificateChain"]["error"] > 0)
  214. {
  215. $jsone['result'] = "Error";
  216. $jsone['message'] = $_FILES["V2GCertificateChain"]["error"];
  217. echo json_encode($jsone);
  218. exit;
  219. }
  220. else if(strpos(exec('openssl x509 -noout -modulus -in '.$_FILES['V2GCertificateChain']['tmp_name']),"Modulus")===false){ //unable to load V2GCertificateChain
  221. $jsone['result'] = "Error";
  222. $jsone['message'] = "format of V2G Certificate Chain is incorrect";
  223. echo json_encode($jsone);
  224. exit;
  225. }
  226. sleep(1);
  227. move_uploaded_file($_FILES["V2GCertificateChain"]["tmp_name"],"/Storage/certV2g.pem");
  228. }
  229. // ob_start();
  230. shell_exec('sync;sync;sync');
  231. chdir("/root");
  232. $str_json=json_encode($json);
  233. if(isset($_POST['RemoteApi'])){
  234. exec("'./WebService' '21' '".$str_json."'",$output,$return_var);
  235. }
  236. else{
  237. exec("'./WebService' '2' '".$str_json."'",$output,$return_var);
  238. }
  239. if(count($output)!=0){
  240. $jsone['result'] = "Success";
  241. $jsone['message'] = $json;
  242. echo json_encode($jsone);
  243. exit;
  244. }
  245. else{
  246. $jsone['result'] = "Error";
  247. $jsone['message'] = "Something went wrong on machine";
  248. echo json_encode($jsone);
  249. return false;
  250. exit;
  251. }
  252. // ob_end_clean();
  253. }
  254. function checkMaxValue($id){
  255. if($_POST[$id] != ""){
  256. if(!is_numeric($_POST[$id])){
  257. $jsone['result'] = "Fail";
  258. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  259. echo json_encode($jsone);
  260. exit;
  261. }
  262. if($_POST[$id]>65535){
  263. $jsone['result'] = "Fail";
  264. $jsone['message'] = "You have entered a over value on " . $id . ", it should be less than 65535";
  265. echo json_encode($jsone);
  266. exit;
  267. }
  268. if($_POST[$id]<0){
  269. $jsone['result'] = "Fail";
  270. $jsone['message'] = "You have entered a over value on " . $id . ", it should be more than 0";
  271. echo json_encode($jsone);
  272. exit;
  273. }
  274. }
  275. }
  276. function checkValue($id){
  277. if($_POST[$id] != ""){
  278. if(strlen($_POST[$id])!=1){
  279. $jsone['result'] = "Fail";
  280. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  281. echo json_encode($jsone);
  282. exit;
  283. }
  284. if(!is_numeric($_POST[$id])){
  285. $jsone['result'] = "Fail";
  286. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
  287. echo json_encode($jsone);
  288. exit;
  289. }
  290. }
  291. }
  292. function checkLength($id){
  293. if($_POST[$id] != ""){
  294. if(strlen($_POST[$id])>32){
  295. $jsone['result'] = "Fail";
  296. $jsone['message'] = "You have entered a wrong value on " . $id . ", it should be less than 32 bytes";
  297. echo json_encode($jsone);
  298. exit;
  299. }
  300. }
  301. }
  302. ?>