Tool.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace catchAdmin\api\controller;
  3. use catchAdmin\hydraulic\model\Hydraulic;
  4. use catchAdmin\hydraulic\model\maintenancemapper;
  5. use catchAdmin\hydraulic\model\Wrench as ModelWrench;
  6. use catchAdmin\worklocation\model\WorkRecord;
  7. use catcher\base\CatchRequest as Request;
  8. use catcher\CatchResponse;
  9. use catcher\base\CatchController;
  10. use PhpParser\Node\Expr\FuncCall;
  11. use think\facade\Db;
  12. use Workerman\Worker;
  13. use Wrench;
  14. error_reporting(0);
  15. class Tool extends CatchController
  16. {
  17. /**
  18. * @Descripttion: 下发设备 携带的参数 imei号 版本号 上传心跳包
  19. * @name: likang
  20. * @return {*}
  21. */
  22. public function Issued()
  23. {
  24. $token = $_GET['token'];
  25. if (!$token || $token != '22723927C') {
  26. json_fail('缺少token或者token错误');
  27. }
  28. $param = json_decode(optimize_json(file_get_contents("php://input")), true);
  29. if (json_last_error() != 0) {
  30. json_fail('解析异常', json_last_error_msg());
  31. }
  32. //校验imei是否存在
  33. if (!isset($param['IMEI']) || $param['IMEI'] == '') {
  34. json_fail('缺少设备IMEI号参数');
  35. }
  36. $Imei = $param['IMEI'];
  37. if (!$this->IsImei($Imei)) {
  38. json_fail('该Imei号不存在');
  39. }
  40. if (!($param['CntVersion']) || $param['CntVersion'] == '') {
  41. $param['CntVersion'] = 0;
  42. }
  43. $version = $param['CntVersion'];
  44. return $this->DataIssued($Imei, $version);
  45. }
  46. //收到的数据
  47. public function receive()
  48. {
  49. $token = $_GET['token'];
  50. if (!$token || $token != '22723927C') {
  51. json_fail('缺少token或者token不对');
  52. }
  53. $test = $_GET['test'];
  54. if ($test) {
  55. //用于测试
  56. $dayt = hex2bin(file_get_contents("php://input"));
  57. var_dump($dayt);
  58. var_dump(optimize_json(hex2bin(file_get_contents("php://input"))));
  59. $param = json_decode(optimize_json(hex2bin(file_get_contents("php://input"))), true);
  60. var_dump($param);
  61. echo $param['IMEI'];
  62. } else {
  63. $param = json_decode(optimize_json(file_get_contents("php://input")), true);
  64. }
  65. // $param = json_decode(optimize_json(file_get_contents("php://input")), true);
  66. if (json_last_error() != 0) {
  67. json_fail('解析异常', json_last_error_msg());
  68. }
  69. //校验imei是否存在
  70. $where = [];
  71. if (!isset($param['Imei']) || $param['Imei'] == '') {
  72. json_fail('缺少设备IMEI号参数');
  73. }
  74. $Imei = $param['Imei'];
  75. //校验Imei是否存在
  76. if (!$this->IsImei($Imei)) {
  77. json_fail('该Imei号不存在');
  78. }
  79. if (!isset($param['CntVersion']) || $param['CntVersion'] == '') {
  80. json_fail('版本号不存在');
  81. }
  82. $version = $param['CntVersion'];
  83. if (!isset($param['Succ']) || $param['Succ'] == '') {
  84. json_fail('缺少返回结果', '', $Imei);
  85. }
  86. $Succ = $param['Succ'];
  87. if (!isset($param['Extra'])) {
  88. json_fail('缺少回复内容参数', '', $Imei);
  89. }
  90. $Extra = json_encode($param['Extra']);
  91. $where[] = ['PublishVersion', '=', $version];
  92. $where[] = ['Imei', '=', $Imei];
  93. $ack = Db::name("publish_ack")->where($where)->find();
  94. if (!$ack) {
  95. json_fail('没有找到下发记录', '', $Imei);
  96. }
  97. $data = [
  98. 'Succ' => $Succ,
  99. 'AckTime' => msectime(),
  100. 'Extra' => $Extra
  101. ];
  102. Db::name("publish_ack")->where($where)->update($data);
  103. return $this->DataIssued($Imei, $version);
  104. }
  105. /**
  106. * @Descripttion: 数据处理
  107. * @name: likang
  108. * @return {*}
  109. */
  110. private function DataIssued($Imei, $version)
  111. {
  112. $where1 = [
  113. ['ContentType', '<>', 'WorkRecord'],
  114. ['Imei', '=', NULL],
  115. ['Version', '>', $version]
  116. ];
  117. $where2 = [
  118. ['Imei', '<>', $Imei],
  119. ['ContentType', '=', 'WorkRecord'],
  120. ['Version', '>', $version]
  121. ];
  122. $where4 = [
  123. ['Imei', '=', $Imei],
  124. ['Type', '=', 'directive'],
  125. ['Version', '>', $version]
  126. ];
  127. $where3 = [];
  128. $CntVersion = $version;
  129. $content = null;
  130. $list = Db::name('publish')
  131. ->whereOr([$where1, $where2, $where4])
  132. ->order('Version asc')->limit(5)->select();
  133. if ($list->isEmpty()) {
  134. return json_success('获取成功', $content, $Imei);
  135. }
  136. //目前不下发数据 截停
  137. //return json_success('获取成功', $content, $Imei);
  138. foreach ($list as $key => $value) {
  139. $da = json_decode($value['Content'], true);
  140. //如果是下发计划的话,或取当前长传记录 记录下数据
  141. if ($value['ContentType'] == 'WorkPlan') {
  142. $where3[] = ['wind_number', '=', $da['wnum']];
  143. $where3[] = ['fan_number', '=', $da['fnum']];
  144. $where3[] = ['parts', '=', $da['parts']];
  145. $where3[] = ['work_sign', '=', $da['work']];
  146. $DD = WorkRecord::where($where3)->order('fastening_time desc')->value('fastening_torque');
  147. $da['lt'] = $DD ? $DD : 0;
  148. }
  149. $da['id'] = strval($da['id']);
  150. $content[] = [
  151. 'OpType' => $value['Type'],
  152. 'CntType' => $value['ContentType'],
  153. 'CntVersion' => strval($value['Version']),
  154. 'id' => strval($value['ContentId']),
  155. 'Content' => $da
  156. ];
  157. $CntVersion = strval($value['Version']);
  158. }
  159. //保存下发的记录
  160. $ack = [
  161. 'PublishVersion' => $CntVersion,
  162. 'PublishContent' => json_encode($content),
  163. 'Imei' => $Imei,
  164. 'AddTime' => msectime()
  165. ];
  166. Db::name('publish_ack')->save($ack);
  167. return json_success('获取成功', $content, $Imei);
  168. }
  169. /**
  170. * @Descripttion: Imei 是否存在
  171. * @name: likang
  172. * @param {*} $imei
  173. * @return {*}
  174. */
  175. private function IsImei($imei)
  176. {
  177. $data = Hydraulic::where('imei', $imei)->find();
  178. if ($data) {
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. }