OpcDataController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use Illuminate\Contracts\Support\Renderable;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Response;
  6. use Illuminate\Routing\Controller;
  7. use Illuminate\Support\Facades\DB;
  8. use Modules\OpcData\Entities\OpcDataDict;
  9. class OpcDataController extends Controller
  10. {
  11. protected $opcDB;
  12. // private $opcDictArr = array();
  13. protected $opcConnectName = 'mysql_opc_jinjiaqu';
  14. // private $dictTableName = 'tb_dev_dict_list';
  15. // private $dataTableName = 'equipment';
  16. public function __construct() {
  17. $this->opcDB = DB::connection($this->opcConnectName);
  18. }
  19. /**
  20. * Display a listing of the resource.
  21. * @return Renderable
  22. */
  23. public function index()
  24. {
  25. return view('opcdata::index');
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. * @return Renderable
  30. */
  31. public function create()
  32. {
  33. return view('opcdata::create');
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. * @param Request $request
  38. * @return Renderable
  39. */
  40. public function store(Request $request)
  41. {
  42. //
  43. }
  44. /**
  45. * Show the specified resource.
  46. * @param int $id
  47. * @return Renderable
  48. */
  49. public function show($id)
  50. {
  51. return view('opcdata::show');
  52. }
  53. /**
  54. * Show the form for editing the specified resource.
  55. * @param int $id
  56. * @return Renderable
  57. */
  58. public function edit($id)
  59. {
  60. return view('opcdata::edit');
  61. }
  62. /**
  63. * Update the specified resource in storage.
  64. * @param Request $request
  65. * @param int $id
  66. * @return Renderable
  67. */
  68. public function update(Request $request, $id)
  69. {
  70. //
  71. }
  72. /**
  73. * Remove the specified resource from storage.
  74. * @param int $id
  75. * @return Renderable
  76. */
  77. public function destroy($id)
  78. {
  79. //
  80. }
  81. public function getData(Request $request)
  82. {
  83. $getDataType = $request->system_type; // 获取需要的数据
  84. $dataArr = array(); // 返回数据数组
  85. $result;
  86. if ($getDataType == 'ventilation_zb'){
  87. // 中部风机
  88. $groupId = 1;
  89. $result = $this->getFanData($groupId);
  90. } else if ($getDataType == 'ventilation_bb') {
  91. // 北部风机
  92. $groupId = 2;
  93. $result = $this->getFanData($groupId);
  94. } else if ($getDataType == 'pump_zy') {
  95. // 中央水泵
  96. $groupId = 3;
  97. $result = $this->getPumpData($groupId);
  98. } else if ($getDataType == 'pump_690') {
  99. // 609水泵
  100. $groupId = 4;
  101. $result = $this->getPumpData($groupId);
  102. } else if ($getDataType == 'compressed_air') {
  103. // 压风
  104. $groupId = 5;
  105. $result = $this->getCompressedAirData($groupId);
  106. } else if ($getDataType == 'belt_13') {
  107. // 13采区胶带运输机系统
  108. $groupId = 6;
  109. $result = $this->getBeltData($groupId);
  110. } else if ($getDataType == 'hoist') {
  111. // 提升机
  112. $groupId = 7;
  113. $result = $this->getHoistData($groupId);
  114. } else {
  115. return;
  116. }
  117. return response()->json($result);
  118. }
  119. // 水泵
  120. public function getFanData($groupId) {
  121. $sqlStr = "
  122. select t1.dev_type_id,
  123. t1.dev_type_name,
  124. t1.device_num,
  125. t1.device_name,
  126. case t.val
  127. when 'True' then 1
  128. when 'False' then 2
  129. else t.val
  130. end val,
  131. t1.json_key,
  132. t1.row_number,
  133. t1.unit
  134. from equipment t
  135. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  136. where t1.group_id = ".$groupId."
  137. and t1.is_show = 1
  138. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  139. ";
  140. $dbResult = $this->opcDB->select($sqlStr);
  141. // dd($dbResult);
  142. foreach ($dbResult as $key => $val) {
  143. $fanNum = $val->dev_type_id - 1; // 风机号
  144. $label = $val->device_name; // 设备名
  145. $devVal = $val->val; // 数值
  146. $jsonKey = $val->json_key; // json键值
  147. $unit = $val->unit; // 单位
  148. $dataArr[$fanNum]['title'] = $val->dev_type_name; // 风机名
  149. $valArr = [
  150. 'label' => $label, // 标题
  151. 'unit' => $unit // 单位
  152. ];
  153. // 状态与value
  154. if($jsonKey == 'state_list') {
  155. $valArr['state'] = $devVal;
  156. } else {
  157. $valArr['value'] = $devVal;
  158. }
  159. // 根据风量判断风机开关
  160. if($label == '风量') {
  161. if ($devVal <= 0 ) {
  162. $dataArr[$fanNum]['state'] = 2;
  163. } else {
  164. $dataArr[$fanNum]['state'] = 1;
  165. }
  166. }
  167. $dataArr[$fanNum][$jsonKey][] = $valArr;
  168. }
  169. return $dataArr;
  170. }
  171. public function getPumpData($groupId) {
  172. $sqlStr = "
  173. select t1.dev_type_id,
  174. t1.dev_type_name,
  175. t1.device_num,
  176. t1.device_name,
  177. case t.val
  178. when 'True' then 1
  179. when 'False' then 2
  180. else t.val
  181. end val,
  182. t1.json_key,
  183. t1.row_number,
  184. t1.unit
  185. from equipment t
  186. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  187. where t1.group_id = ".$groupId."
  188. and t1.is_show = 1
  189. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  190. ";
  191. $dbResult = $this->opcDB->select($sqlStr);
  192. $timeArr = array(); // 用于拼接时分秒
  193. $timeJsonKey; // 记录time字段key名
  194. // dd($dbResult);
  195. foreach ($dbResult as $key => $val) {
  196. $devName = $val->dev_type_name; // 泵名
  197. $rowNumber = $val->row_number; // 排序
  198. $jsonKey = $val->json_key; // json键值
  199. $label = $val->device_name; // 设备名
  200. $devNum = $val->dev_type_id - 1; // 设备号
  201. $unit = $val->unit; // 单位
  202. $devVal = $val->val; // 数值
  203. if ($rowNumber <= 4) {
  204. $valArr = [
  205. 'label' => $label, // 标题
  206. 'value' => $devVal, // 值
  207. 'unit' => $unit // 单位
  208. ];
  209. $dataArr[$jsonKey][] = $valArr;
  210. } else if ($rowNumber == 5) {
  211. if ($groupId == 4) {
  212. // 609水泵房单独处理状态
  213. $dataArr['water_pump'][$devNum]['name'] = $devName;
  214. if (array_key_exists($jsonKey, $dataArr['water_pump'][$devNum])) {
  215. $state = $dataArr['water_pump'][$devNum][$jsonKey];
  216. $dataArr['water_pump'][$devNum][$jsonKey] = max($devVal, $state);
  217. } else {
  218. $dataArr['water_pump'][$devNum][$jsonKey] = $devVal;
  219. }
  220. } else {
  221. // 中央水泵房电机开启状态处理
  222. $valArr = [
  223. 'label' => $devName, // 标题
  224. 'state' => $devVal // 值
  225. ];
  226. $dataArr[$jsonKey][] = $valArr;
  227. }
  228. } else if ($rowNumber == 10 or $rowNumber == 11) {
  229. $timeArr[$label][$devNum][$unit] = $devVal;
  230. $timeJsonKey = $jsonKey;
  231. } else if ($rowNumber > 5) {
  232. $valArr = [
  233. 'label' => $label, // 标题
  234. 'value' => $devVal, // 值
  235. 'unit' => $unit // 单位
  236. ];
  237. $dataArr['water_pump'][$devNum][$jsonKey][] = $valArr;
  238. }
  239. }
  240. foreach ($timeArr as $key => $value) {
  241. for ($i=0; $i < count($value); $i++) {
  242. $timeFormatStr = $value[$i]['HH'].'时'.$value[$i]['MM'].'分'.$value[$i]['SS'].'秒';
  243. $valArr = [
  244. 'label' => $key, // 标题
  245. 'value' => $timeFormatStr, // 值
  246. 'unit' => '' // 单位
  247. ];
  248. $dataArr['water_pump'][$i][$timeJsonKey][] = $valArr;
  249. }
  250. }
  251. return $dataArr;
  252. }
  253. // 压风
  254. public function getCompressedAirData($groupId) {
  255. $sqlStr = "
  256. select t1.dev_type_id,
  257. t1.dev_type_name,
  258. t1.device_num,
  259. t1.device_name,
  260. case t.val
  261. when 'True' then 1
  262. when 'False' then 2
  263. else t.val
  264. end val,
  265. t1.json_key,
  266. t1.row_number,
  267. t1.unit
  268. from equipment t
  269. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  270. where t1.group_id = ".$groupId."
  271. and t1.is_show = 1
  272. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  273. ";
  274. $dbResult = $this->opcDB->select($sqlStr);
  275. $timeArr = array(); // 用于拼接时分秒
  276. $timeJsonKey; // 记录time字段key名
  277. // dd($dbResult);
  278. foreach ($dbResult as $key => $val) {
  279. $devName = $val->dev_type_name; // 泵名
  280. $rowNumber = $val->row_number; // 排序
  281. $jsonKey = $val->json_key; // json键值
  282. $label = $val->device_name; // 设备名
  283. $devNum = $val->dev_type_id - 1; // 设备号
  284. $unit = $val->unit; // 单位
  285. $devVal = $val->val; // 数值
  286. // $valArr[$jsonKey] = $devVal;
  287. if ($jsonKey == 'state') {
  288. $dataArr['compressed_air'][$devNum]['name'] = $devName;
  289. $dataArr['compressed_air'][$devNum]['state'] = $devVal;
  290. } else if ($jsonKey == 'run_parameter') {
  291. $valArr = [
  292. 'label' => $label, // 标题
  293. 'state' => $devVal, // 值
  294. 'unit' => $unit // 单位
  295. // ,'json' => $jsonKey
  296. ];
  297. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  298. } else if ($jsonKey == 'common') {
  299. $valArr = [
  300. 'label' => $label, // 标题
  301. 'value' => $devVal, // 值
  302. 'unit' => $unit // 单位
  303. ];
  304. $dataArr[$jsonKey][] = $valArr;
  305. } else if ($jsonKey == 'drain_value_state') {
  306. // 开关状态单独处理
  307. $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
  308. if (array_key_exists($jsonKey, $dataArr['compressed_air'][$devNum])) {
  309. $state = $dataArr['compressed_air'][$devNum][$jsonKey];
  310. $dataArr['compressed_air'][$devNum][$jsonKey] = max($devVal, $state);
  311. } else {
  312. $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
  313. }
  314. } else if ($jsonKey == 'compressed_air') {
  315. $valArr = [
  316. 'label' => $label, // 标题
  317. 'value' => $devVal, // 值
  318. 'unit' => $unit // 单位
  319. ];
  320. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  321. } else if ($jsonKey == 'warn_state') {
  322. $valArr = [
  323. 'label' => $label, // 标题
  324. 'state' => $devVal, // 值
  325. 'unit' => $unit // 单位
  326. ];
  327. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  328. }
  329. }
  330. return $dataArr;
  331. }
  332. public function getBeltData($groupId) {
  333. $sqlStr = "
  334. select t1.dev_type_id,
  335. t1.dev_type_name,
  336. t1.device_num,
  337. t1.device_name,
  338. case t.val
  339. when 'True' then 1
  340. when 'False' then 2
  341. else t.val
  342. end val,
  343. t1.json_key,
  344. t1.row_number,
  345. t1.unit
  346. from equipment t
  347. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  348. where t1.group_id = ".$groupId."
  349. and t1.is_show = 1
  350. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  351. ";
  352. $dbResult = $this->opcDB->select($sqlStr);
  353. $valDict = array();
  354. foreach ($dbResult as $key => $val) {
  355. $devName = $val->dev_type_name; // 泵名
  356. $rowNumber = $val->row_number; // 排序
  357. $jsonKey = $val->json_key; // json键值
  358. $label = $val->device_name; // 设备名
  359. $devTypeNum = $val->dev_type_id - 1; // 设备号
  360. $devNum = $val->device_num - 1; // 设备号
  361. $unit = $val->unit; // 单位
  362. $devVal = $val->val; // 数值
  363. // $valArr[$jsonKey] = $devVal;
  364. if ($jsonKey == 'common') {
  365. $valArr = [
  366. 'label' => $label, // 标题
  367. 'value' => $devVal, // 值
  368. 'unit' => $unit // 单位
  369. ];
  370. $dataArr[$jsonKey][] = $valArr;
  371. } else if ($jsonKey == 'state') {
  372. $dataArr['state'] = $devVal;
  373. $dataArr['name'] = $label;
  374. } else if ($jsonKey == 'electric_parameter' or $jsonKey == 'converter_parameter') {
  375. $valArr = [
  376. // 'devNum' => $devNum, // 标题
  377. 'value' => $devVal, // 值
  378. 'unit' => $unit // 单位
  379. // ,'label' => $label // 单位
  380. ];
  381. $valDict[$jsonKey][$label][] = $valArr;
  382. } else {
  383. $valArr = [
  384. 'label' => $label, // 标题
  385. 'state' => $devVal, // 值
  386. // 'unit' => $unit // 单位
  387. ];
  388. $dataArr[$jsonKey][] = $valArr;
  389. }
  390. }
  391. foreach ($valDict as $key => $val) {
  392. $i = 0;
  393. foreach ($val as $valKey => $valVal) {
  394. $dataArr[$key][$i]['label'] = $valKey;
  395. $dataArr[$key][$i]['value_list'] = $valVal;
  396. $i++;
  397. }
  398. }
  399. // dd($dataArr);
  400. return $dataArr;
  401. }
  402. public function getHoistData($groupId) {
  403. $sqlStr = "
  404. select t1.dev_type_id,
  405. t1.dev_type_name,
  406. t1.device_num,
  407. t1.device_name,
  408. case t.val
  409. when 'True' then 1
  410. when 'False' then 2
  411. else t.val
  412. end val,
  413. t1.json_key,
  414. t1.row_number,
  415. t1.unit
  416. from equipment t
  417. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  418. where t1.group_id = ".$groupId."
  419. and t1.is_show = 1
  420. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.dev_type_name, t1.device_name
  421. ";
  422. $dbResult = $this->opcDB->select($sqlStr);
  423. $valDict = array();
  424. $state = 2;
  425. // dd($dbResult);
  426. foreach ($dbResult as $key => $val) {
  427. $devName = $val->dev_type_name; // 泵名
  428. $rowNumber = $val->row_number; // 排序
  429. $jsonKey = $val->json_key; // json键值
  430. $label = $val->device_name; // 设备名
  431. $devTypeNum = $val->dev_type_id; // 设备号
  432. $devNum = $val->device_num - 1; // 设备号
  433. $unit = $val->unit; // 单位
  434. $devVal = $val->val; // 数值
  435. if ($jsonKey == 'position') {
  436. $valArr = [
  437. 'label' => $label, // 标题
  438. 'value' => $devVal, // 值
  439. 'unit' => $unit // 单位
  440. ];
  441. $dataArr['common'][$jsonKey][] = $valArr;
  442. } else if ($jsonKey == 'direction') {
  443. $valArr = [
  444. 'label' => $label, // 标题
  445. 'state' => $devVal, // 值
  446. ];
  447. $dataArr['common'][$jsonKey][] = $valArr;
  448. if ($devVal == 1) {
  449. $state = 1;
  450. }
  451. } else if ($jsonKey == 'run_parameter') {
  452. $valArr = [
  453. 'label' => $label, // 标题
  454. 'value' => $devVal, // 值
  455. 'unit' => $unit // 单位
  456. ];
  457. $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
  458. $dataArr['hoist'][$devTypeNum]['name'] = '中部副立井';
  459. // $dataArr['common']['direction']
  460. } else {
  461. $valArr = [
  462. 'label' => $label, // 标题
  463. 'state' => $devVal, // 值
  464. ];
  465. $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
  466. }
  467. }
  468. $dataArr['hoist'][0]['state'] = $state;
  469. return $dataArr;
  470. }
  471. }