DeviceCommonService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace App\Service\Device;
  3. use App\Jobs\PushDeviceJob;
  4. use App\Service\Service;
  5. class DeviceCommonService extends Service
  6. {
  7. public $deviceList = [
  8. //腹带抛光机
  9. ///HC/STWDL/WriteRealtimeData/WDL2
  10. ['type' => 'FDPGJ', 'id' => 'FDPGJ', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  11. ['type' => 'WXM', 'id' => 'WXM1', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  12. ['type' => 'STWDL', 'id' => 'WDL2', 'cmd' => '00 00 00 00 00 06 01 02 00 00 00 0A'],
  13. ['type' => 'STWDL', 'id' => 'WDL3', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 32'],
  14. ['type' => 'GTPGJ', 'id' => 'GTPG1', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  15. ['type' => 'GTPGJ', 'id' => 'GTPG2', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  16. ['type' => 'GTPGJ', 'id' => 'GTPG3', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  17. ['type' => 'GTPGJ', 'id' => 'GTPG4', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  18. ['type' => 'GTPGJ', 'id' => 'GTPG5', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  19. ['type' => 'GTPGJ', 'id' => 'GTPG6', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  20. ['type' => 'GTPGJ', 'id' => 'GTPG7', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  21. ['type' => 'GTPGJ', 'id' => 'GTPG8', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 07'],
  22. ['type' => 'BLJ', 'id' => 'BL1', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  23. ['type' => 'QZJ', 'id' => 'QZ1', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  24. ['type' => 'QZJ', 'id' => 'QZ2', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  25. ['type' => 'QZJ', 'id' => 'QZ3', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  26. ['type' => 'QZJ', 'id' => 'QZ4', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  27. ['type' => 'QZJ', 'id' => 'QZ5', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  28. ['type' => 'QZJ', 'id' => 'QZ6', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  29. ['type' => 'QZJ', 'id' => 'QZ7', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  30. ['type' => 'QZJ', 'id' => 'QZ8', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  31. ['type' => 'QZJ', 'id' => 'QZ9', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  32. ['type' => 'QZJ', 'id' => 'QZ10', 'cmd' => '00 00 00 00 00 06 01 03 00 00 00 09'],
  33. ['type' => 'CGJ', 'id' => 'CG1', 'cmd' => '00 03 00 00 00 06 01 03 A4 04 00 01'],
  34. ['type' => 'CGJ', 'id' => 'CG1', 'cmd' => '00 09 00 00 00 06 01 02 60 05 00 01'],
  35. ['type' => 'CGJ', 'id' => 'CG1', 'cmd' => '00 0A 00 00 00 06 01 02 60 06 00 01'],
  36. // 以后增加更多设备...
  37. ];
  38. public function dealDevice($type, $message, callable $logger = null)
  39. {
  40. $message = str_replace(" ", "", $message);
  41. $return = '';
  42. switch ($type) {
  43. case "FDPGJ":
  44. $return = $this->FDPGJ($message, $type);
  45. break;
  46. case "WXM1":
  47. $return = $this->WXM1($message, $type);
  48. break;
  49. case "GTPG1":
  50. case "GTPG2":
  51. case "GTPG3":
  52. case "GTPG4":
  53. case "GTPG5":
  54. case "GTPG6":
  55. case "GTPG7":
  56. case "GTPG8":
  57. $return = $this->GTPG($message, $type);
  58. break;
  59. case "BL1":
  60. $return = $this->BLJ($message, $type);
  61. break;
  62. case "QZ1":
  63. case "QZ2":
  64. case "QZ3":
  65. case "QZ4":
  66. case "QZ5":
  67. case "QZ6":
  68. case "QZ7":
  69. case "QZ8":
  70. case "QZ9":
  71. case "QZ10":
  72. $return = $this->QZ($message, $type);
  73. break;
  74. case "CG1":
  75. $return = $this->CG($message, $type);
  76. break;
  77. case "WDL3":
  78. $return = $this->WDL($message, $type);
  79. break;
  80. default:
  81. break;
  82. }
  83. if (!empty($return)) {
  84. $data = [
  85. "data" => [
  86. "deviceId" => "",
  87. "dataPoints" => [
  88. ],
  89. ],
  90. "type" => "dataPoint"
  91. ];
  92. foreach ($return as $v) {
  93. if(empty($data['data']['deviceId'])) $data['data']['deviceId'] = $v['code'];
  94. $data['data']['dataPoints'][] = [
  95. "dataPointId" => $v['type'],
  96. "value" => $v['value'],
  97. ];
  98. }
  99. dispatch((new PushDeviceJob($data)))->onQueue('device_hc');
  100. //拼装结构
  101. if (isset($data)) {
  102. $logger("设备 {$type} 处理完成,数据:" . json_encode($data));
  103. }
  104. }
  105. }
  106. public function WDL($message, $type)
  107. {
  108. //0000 0000 0067 0103 64
  109. //0255 0288 02a2 02a2 028f 0247 0022 0000 0000 0079 0087 008e 0086 0022 0000 0000 0000 0000 0002 0002 0002 0002 0002 0002 0000 0002 0002 0002 0002 0001 0000 0000
  110. //0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
  111. //运行状态、运行时间(分)、停止时间(分)、下压次数、下压频率(速度),次/分
  112. if (strlen($message) > 100) {
  113. $list = $this->dealMessage($message, [0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]);
  114. $dispatch = [
  115. [
  116. 'code' => $type,
  117. "type" => '_0',
  118. "value" => $list[0],
  119. ], [
  120. 'code' => $type,
  121. "type" => '_1',
  122. "value" => $list[1],
  123. ], [
  124. 'code' => $type,
  125. "type" => '_2',
  126. "value" => $list[2],
  127. ], [
  128. 'code' => $type,
  129. "type" => '_3',
  130. "value" => $list[3],
  131. ], [
  132. 'code' => $type,
  133. "type" => '_4',
  134. "value" => $list[4],
  135. ], [
  136. 'code' => $type,
  137. "type" => '_5',
  138. "value" => $list[5],
  139. ], [
  140. 'code' => $type,
  141. "type" => '_6',
  142. "value" => $list[6],
  143. ], [
  144. 'code' => $type,
  145. "type" => '_9',
  146. "value" => $list[9],
  147. ], [
  148. 'code' => $type,
  149. "type" => '_10',
  150. "value" => $list[10],
  151. ], [
  152. 'code' => $type,
  153. "type" => '_11',
  154. "value" => $list[11],
  155. ], [
  156. 'code' => $type,
  157. "type" => '_12',
  158. "value" => $list[12],
  159. ], [
  160. 'code' => $type,
  161. "type" => '_20',
  162. "value" => $list[20],
  163. ]
  164. ];
  165. return $dispatch;
  166. }
  167. }
  168. //车沟机
  169. public function CG($message, $type)
  170. {
  171. $code = substr($message, 3, 1);
  172. //0004 0000 0005 0103 02 3a98
  173. if (strlen($message) > 10) {
  174. //进刀长度\进刀速度\退刀速度\产量计数\产量设定\急停\主机热继电器\料斗热继电器\气压报警\车沟马达启动(运行信号)\报警
  175. $dispatch = [];
  176. switch ($code) {
  177. case "3":
  178. $list = $this->dealMessage($message, [4]);
  179. $value = hexdec($list[0]);
  180. $num = 3;
  181. break;
  182. case "9":
  183. $list = $this->dealMessage($message, [2]);
  184. $value = hexdec($list[0]);
  185. $num = 9;
  186. break;
  187. case "a":
  188. $list = $this->dealMessage($message, [2]);
  189. $value = hexdec($list[0]);
  190. $num = 10;
  191. break;
  192. default:
  193. break;
  194. }
  195. if (isset($value)) {
  196. $dispatch[] = [
  197. 'code' => $type,
  198. "type" => '_' . $num,
  199. "value" => $value,
  200. ];
  201. }
  202. return $dispatch;
  203. }
  204. }
  205. //切轴机
  206. public function QZ($message, $type)
  207. {
  208. //0000 0000 0015 01 03 12 0001 00000370 00000326 00020b3d 000001e0
  209. //运行状态、运行时间(分)、停止时间(分)、下压次数、下压频率(速度),次/分
  210. if (strlen($message) > 50) {
  211. $list = $this->dealMessage($message, [4, 8, 8, 8, 8]);
  212. $run_state = hexdec($list[0]);
  213. $run_time = hexdec($list[1]);
  214. $stop_time = hexdec($list[2]);
  215. $run_num = hexdec($list[3]);
  216. $run_frequency = hexdec($list[4]);
  217. $dispatch = [
  218. [
  219. 'code' => $type,
  220. "type" => '_0',
  221. "value" => $run_state,
  222. ], [
  223. 'code' => $type,
  224. "type" => '_1',
  225. "value" => $run_time,
  226. ], [
  227. 'code' => $type,
  228. "type" => '_2',
  229. "value" => $stop_time,
  230. ], [
  231. 'code' => $type,
  232. "type" => '_3',
  233. "value" => $run_num,
  234. ], [
  235. 'code' => $type,
  236. "type" => '_4',
  237. "value" => $run_frequency,
  238. ],
  239. ];
  240. return $dispatch;
  241. }
  242. }
  243. //编链机
  244. public function BLJ($message, $type)
  245. {
  246. //0000 0000 0015 0103 12 0000 00000000 00000036 00000011 00000000
  247. //运行状态、运行时间(分)、停止时间(分)、下压次数、下压频率(速度),次/分
  248. if (strlen($message) > 50) {
  249. $list = $this->dealMessage($message, [4, 8, 8, 8, 8]);
  250. $run_state = hexdec($list[0]);
  251. $run_time = hexdec($list[1]);
  252. $stop_time = hexdec($list[2]);
  253. $run_num = hexdec($list[3]);
  254. $run_frequency = hexdec($list[4]);
  255. $dispatch = [
  256. [
  257. 'code' => $type,
  258. "type" => '_0',
  259. "value" => $run_state,
  260. ], [
  261. 'code' => $type,
  262. "type" => '_1',
  263. "value" => $run_time,
  264. ], [
  265. 'code' => $type,
  266. "type" => '_2',
  267. "value" => $stop_time,
  268. ], [
  269. 'code' => $type,
  270. "type" => '_3',
  271. "value" => $run_num,
  272. ], [
  273. 'code' => $type,
  274. "type" => '_4',
  275. "value" => $run_frequency,
  276. ],
  277. ];
  278. return $dispatch;
  279. }
  280. }
  281. //滚筒抛光机
  282. public function GTPG($message, $type)
  283. {
  284. //0000 0000 0011 01 03 0e 0001 0000004b 00000355 00000007
  285. //运行状态、运行时间(分)、停止时间(分)、运行次数
  286. if (strlen($message) > 40) {
  287. $list = $this->dealMessage($message, [4, 8, 8, 8]);
  288. $run_state = hexdec($list[0]);
  289. $run_time = hexdec($list[1]);
  290. $stop_time = hexdec($list[2]);
  291. $run_num = hexdec($list[3]);
  292. $dispatch = [
  293. [
  294. 'code' => $type,
  295. "type" => '_0',
  296. "value" => $run_state,
  297. ], [
  298. 'code' => $type,
  299. "type" => '_1',
  300. "value" => $run_time,
  301. ], [
  302. 'code' => $type,
  303. "type" => '_2',
  304. "value" => $stop_time,
  305. ], [
  306. 'code' => $type,
  307. "type" => '_3',
  308. "value" => $run_num,
  309. ],
  310. ];
  311. return $dispatch;
  312. }
  313. }
  314. //无芯磨机
  315. public function WXM1($message, $type)
  316. {
  317. //0000 0000 0015 01 03 12 0001 00000153 00000244 00000003 00000000
  318. //00000000001101030e0001000006e7000003e20000000a
  319. //运行状态、运行时间(分)、停止时间(分)、运行次数
  320. if (strlen($message) > 40) {
  321. $list = $this->dealMessage($message, [4, 8, 8, 8]);
  322. $run_state = hexdec($list[0]);
  323. $run_time = hexdec($list[1]);
  324. $stop_time = hexdec($list[2]);
  325. $run_num = hexdec($list[3]);
  326. $dispatch = [
  327. [
  328. 'code' => $type,
  329. "type" => '_0',
  330. "value" => $run_state,
  331. ], [
  332. 'code' => $type,
  333. "type" => '_1',
  334. "value" => $run_time,
  335. ], [
  336. 'code' => $type,
  337. "type" => '_2',
  338. "value" => $stop_time,
  339. ], [
  340. 'code' => $type,
  341. "type" => '_3',
  342. "value" => $run_num,
  343. ],
  344. ];
  345. return $dispatch;
  346. }
  347. }
  348. //腹带抛光机
  349. public function FDPGJ($message, $type)
  350. {
  351. //0000 0000 0015 01 03 12 0000 00000000 000003d7 00000000 00000000
  352. //运行状态、运行时间(分)、停止时间(分)、运行次数
  353. if (strlen($message) > 40) {
  354. $list = $this->dealMessage($message, [4, 8, 8, 8]);
  355. $run_state = hexdec($list[0]);
  356. $run_time = hexdec($list[1]);
  357. $stop_time = hexdec($list[2]);
  358. $run_num = hexdec($list[3]);
  359. $dispatch = [
  360. [
  361. "code" => $type,
  362. "type" => '_0',
  363. "value" => $run_state,
  364. ], [
  365. "type" => $type . '_1',
  366. "value" => $run_time,
  367. ], [
  368. "type" => $type . '_2',
  369. "value" => $stop_time,
  370. ], [
  371. "type" => $type . '_3',
  372. "value" => $run_num,
  373. ],
  374. ];
  375. return $dispatch;
  376. }
  377. }
  378. public function dealMessage($message, $list)
  379. {
  380. $message = substr($message, 18);
  381. $return = [];
  382. foreach ($list as $v) {
  383. $return[] = substr($message, 0, $v);
  384. $message = substr($message, $v);
  385. }
  386. return $return;
  387. }
  388. public function disPatch($data){
  389. }
  390. }