Tool.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Base
  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(file_get_contents("php://input"), true);
  29. //校验imei是否存在
  30. if (!isset($param['IMEI']) || $param['IMEI'] == '') {
  31. json_fail('缺少设备IMEI号参数');
  32. }
  33. $Imei = $param['IMEI'];
  34. if (!$this->IsImei($Imei)) {
  35. json_fail('该Imei号不存在');
  36. }
  37. if (!($param['CntVersion']) || $param['CntVersion'] == '') {
  38. $param['CntVersion'] = 0;
  39. }
  40. $version = $param['CntVersion'];
  41. return $this->DataIssued($Imei, $version);
  42. }
  43. //收到的数据
  44. public function receive()
  45. {
  46. $token = $_GET['token'];
  47. if (!$token || $token != '22723927C') {
  48. json_fail('缺少token或者token不对');
  49. }
  50. $param = json_decode(file_get_contents("php://input"), true);
  51. print('===========================');
  52. var_dump(json_last_error());
  53. var_dump(file_get_contents("php://input"));
  54. //校验imei是否存在
  55. $where = [];
  56. if (!isset($param['Imei']) || $param['Imei'] == '') {
  57. json_fail('缺少设备IMEI号参数');
  58. }
  59. $Imei = $param['Imei'];
  60. //校验Imei是否存在
  61. if (!$this->IsImei($Imei)) {
  62. json_fail('该Imei号不存在');
  63. }
  64. if (!isset($param['CntVersion']) || $param['CntVersion'] == '') {
  65. json_fail('版本号不存在');
  66. }
  67. $version = $param['CntVersion'];
  68. if (!isset($param['Succ']) || $param['Succ'] == '') {
  69. json_fail('缺少返回结果', '', $Imei);
  70. }
  71. $Succ = $param['Succ'];
  72. if (!isset($param['Extra'])) {
  73. json_fail('缺少回复内容参数', '', $Imei);
  74. }
  75. $Extra = json_encode($param['Extra']);
  76. $where[] = ['PublishVersion', '=', $version];
  77. $where[] = ['Imei', '=', $Imei];
  78. $ack = Db::name("publish_ack")->where($where)->find();
  79. if (!$ack) {
  80. json_fail('没有找到下发记录', '', $Imei);
  81. }
  82. $data = [
  83. 'Succ' => $Succ,
  84. 'AckTime' => msectime(),
  85. 'Extra' => $Extra
  86. ];
  87. Db::name("publish_ack")->where($where)->update($data);
  88. return $this->DataIssued($Imei, $version);
  89. }
  90. /**
  91. * @Descripttion: 数据处理
  92. * @name: likang
  93. * @return {*}
  94. */
  95. private function DataIssued($Imei, $version)
  96. {
  97. $where1 = [
  98. ['ContentType', '<>', 'WorkRecord'],
  99. ['Imei', '=', NULL],
  100. ['Version', '>', $version]
  101. ];
  102. $where2 = [
  103. ['Imei', '<>', $Imei],
  104. ['ContentType', '=', 'WorkRecord'],
  105. ['Version', '>', $version]
  106. ];
  107. $where4 = [
  108. ['Imei', '=', $Imei],
  109. ['ContentType', '=', 'File'],
  110. ['Version', '>', $version]
  111. ];
  112. $where3 = [];
  113. $CntVersion = $version;
  114. $content = null;
  115. $list = Db::name('publish')
  116. ->whereOr([$where1, $where2, $where4])
  117. ->order('Version asc')->limit(5)->select();
  118. if ($list->isEmpty()) {
  119. return json_success('获取成功', $content, $Imei);
  120. }
  121. //目前不下发数据 截停
  122. return json_success('获取成功', $content, $Imei);
  123. foreach ($list as $key => $value) {
  124. $da = json_decode($value['Content'], true);
  125. //如果是下发计划的话,或取当前长传记录 记录下数据
  126. if ($value['ContentType'] == 'WorkPlan') {
  127. $where3[] = ['wind_number', '=', $da['wnum']];
  128. $where3[] = ['fan_number', '=', $da['fnum']];
  129. $where3[] = ['parts', '=', $da['parts']];
  130. $where3[] = ['work_sign', '=', $da['work']];
  131. $DD = WorkRecord::where($where3)->order('fastening_time desc')->value('fastening_torque');
  132. $da['lt'] = $DD ? $DD : 0;
  133. }
  134. $da['id'] = strval($da['id']);
  135. $content[] = [
  136. 'OpType' => $value['Type'],
  137. 'CntType' => $value['ContentType'],
  138. 'CntVersion' => strval($value['Version']),
  139. 'id' => strval($value['ContentId']),
  140. 'Content' => $da
  141. ];
  142. $CntVersion = strval($value['Version']);
  143. }
  144. //保存下发的记录
  145. $ack = [
  146. 'PublishVersion' => $CntVersion,
  147. 'PublishContent' => json_encode($content),
  148. 'Imei' => $Imei,
  149. 'AddTime' => msectime()
  150. ];
  151. Db::name('publish_ack')->save($ack);
  152. return json_success('获取成功', $content, $Imei);
  153. }
  154. /**
  155. * @Descripttion: Imei 是否存在
  156. * @name: likang
  157. * @param {*} $imei
  158. * @return {*}
  159. */
  160. private function IsImei($imei)
  161. {
  162. $data = Hydraulic::where('imei', $imei)->find();
  163. if ($data) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. }
  169. }