OpcDataController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. public function getCompressedAirData($groupId) {
  254. $sqlStr = "
  255. select t1.dev_type_id,
  256. t1.dev_type_name,
  257. t1.device_num,
  258. t1.device_name,
  259. case t.val
  260. when 'True' then 1
  261. when 'False' then 2
  262. else t.val
  263. end val,
  264. t1.json_key,
  265. t1.row_number,
  266. t1.unit
  267. from equipment t
  268. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  269. where t1.group_id = ".$groupId."
  270. and t1.is_show = 1
  271. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  272. ";
  273. $dbResult = $this->opcDB->select($sqlStr);
  274. $timeArr = array(); // 用于拼接时分秒
  275. $timeJsonKey; // 记录time字段key名
  276. // dd($dbResult);
  277. foreach ($dbResult as $key => $val) {
  278. $devName = $val->dev_type_name; // 泵名
  279. $rowNumber = $val->row_number; // 排序
  280. $jsonKey = $val->json_key; // json键值
  281. $label = $val->device_name; // 设备名
  282. $devNum = $val->dev_type_id - 1; // 设备号
  283. $unit = $val->unit; // 单位
  284. $devVal = $val->val; // 数值
  285. // $valArr[$jsonKey] = $devVal;
  286. if ($jsonKey == 'run_parameter') {
  287. $dataArr['compressed_air'][$devNum]['name'] = $devName;
  288. $dataArr['compressed_air'][$devNum]['state'] = $devVal;
  289. $valArr = [
  290. 'label' => $label, // 标题
  291. 'state' => $devVal, // 值
  292. 'unit' => $unit // 单位
  293. // ,'json' => $jsonKey
  294. ];
  295. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  296. } else if ($jsonKey == 'common') {
  297. $valArr = [
  298. 'label' => $label, // 标题
  299. 'value' => $devVal, // 值
  300. 'unit' => $unit // 单位
  301. ];
  302. $dataArr[$jsonKey][] = $valArr;
  303. } else if ($jsonKey == 'drain_value_state') {
  304. // 开关状态单独处理
  305. $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
  306. if (array_key_exists($jsonKey, $dataArr['compressed_air'][$devNum])) {
  307. $state = $dataArr['compressed_air'][$devNum][$jsonKey];
  308. $dataArr['compressed_air'][$devNum][$jsonKey] = max($devVal, $state);
  309. } else {
  310. $dataArr['compressed_air'][$devNum][$jsonKey] = $devVal;
  311. }
  312. } else if ($jsonKey == 'compressed_air') {
  313. $valArr = [
  314. 'label' => $label, // 标题
  315. 'value' => $devVal, // 值
  316. 'unit' => $unit // 单位
  317. ];
  318. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  319. } else if ($jsonKey == 'warn_state') {
  320. $valArr = [
  321. 'label' => $label, // 标题
  322. 'state' => $devVal, // 值
  323. 'unit' => $unit // 单位
  324. ];
  325. $dataArr['compressed_air'][$devNum][$jsonKey][] = $valArr;
  326. }
  327. }
  328. return $dataArr;
  329. }
  330. public function getBeltData($groupId) {
  331. $sqlStr = "
  332. select t1.dev_type_id,
  333. t1.dev_type_name,
  334. t1.device_num,
  335. t1.device_name,
  336. case t.val
  337. when 'True' then 1
  338. when 'False' then 2
  339. else t.val
  340. end val,
  341. t1.json_key,
  342. t1.row_number,
  343. t1.unit
  344. from equipment t
  345. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  346. where t1.group_id = ".$groupId."
  347. and t1.is_show = 1
  348. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.device_name
  349. ";
  350. $dbResult = $this->opcDB->select($sqlStr);
  351. $valDict = array();
  352. foreach ($dbResult as $key => $val) {
  353. $devName = $val->dev_type_name; // 泵名
  354. $rowNumber = $val->row_number; // 排序
  355. $jsonKey = $val->json_key; // json键值
  356. $label = $val->device_name; // 设备名
  357. $devTypeNum = $val->dev_type_id - 1; // 设备号
  358. $devNum = $val->device_num - 1; // 设备号
  359. $unit = $val->unit; // 单位
  360. $devVal = $val->val; // 数值
  361. // $valArr[$jsonKey] = $devVal;
  362. if ($jsonKey == 'common') {
  363. $valArr = [
  364. 'label' => $label, // 标题
  365. 'value' => $devVal, // 值
  366. 'unit' => $unit // 单位
  367. ];
  368. $dataArr[$jsonKey][] = $valArr;
  369. } else if ($jsonKey == 'state') {
  370. $dataArr['state'] = $devVal;
  371. $dataArr['name'] = $label;
  372. } else if ($jsonKey == 'electric_parameter' or $jsonKey == 'converter_parameter') {
  373. $valArr = [
  374. 'devNum' => $devNum, // 标题
  375. 'state' => $devVal, // 值
  376. 'unit' => $unit // 单位
  377. // ,'label' => $label // 单位
  378. ];
  379. $valDict[$jsonKey][$label][] = $valArr;
  380. } else {
  381. $valArr = [
  382. 'label' => $label, // 标题
  383. 'state' => $devVal, // 值
  384. // 'unit' => $unit // 单位
  385. ];
  386. $dataArr[$jsonKey][] = $valArr;
  387. }
  388. }
  389. foreach ($valDict as $key => $val) {
  390. $i = 0;
  391. foreach ($val as $valKey => $valVal) {
  392. $dataArr[$key][$i]['label'] = $valKey;
  393. $dataArr[$key][$i]['value_list'] = $valVal;
  394. $i++;
  395. }
  396. }
  397. // dd($dataArr);
  398. return $dataArr;
  399. }
  400. public function getHoistData($groupId) {
  401. $sqlStr = "
  402. select t1.dev_type_id,
  403. t1.dev_type_name,
  404. t1.device_num,
  405. t1.device_name,
  406. case t.val
  407. when 'True' then 1
  408. when 'False' then 2
  409. else t.val
  410. end val,
  411. t1.json_key,
  412. t1.row_number,
  413. t1.unit
  414. from equipment t
  415. join tb_dev_dict_list t1 on t.selItem = t1.selItem
  416. where t1.group_id = ".$groupId."
  417. and t1.is_show = 1
  418. order by t1.dev_type_id, t1.row_number, t1.device_num, t1.dev_type_name, t1.device_name
  419. ";
  420. $dbResult = $this->opcDB->select($sqlStr);
  421. $valDict = array();
  422. $state = 2;
  423. // dd($dbResult);
  424. foreach ($dbResult as $key => $val) {
  425. $devName = $val->dev_type_name; // 泵名
  426. $rowNumber = $val->row_number; // 排序
  427. $jsonKey = $val->json_key; // json键值
  428. $label = $val->device_name; // 设备名
  429. $devTypeNum = $val->dev_type_id; // 设备号
  430. $devNum = $val->device_num - 1; // 设备号
  431. $unit = $val->unit; // 单位
  432. $devVal = $val->val; // 数值
  433. if ($jsonKey == 'position') {
  434. $valArr = [
  435. 'label' => $label, // 标题
  436. 'value' => $devVal, // 值
  437. 'unit' => $unit // 单位
  438. ];
  439. $dataArr['common'][$jsonKey][] = $valArr;
  440. } else if ($jsonKey == 'direction') {
  441. $valArr = [
  442. 'label' => $label, // 标题
  443. 'state' => $devVal, // 值
  444. ];
  445. $dataArr['common'][$jsonKey][] = $valArr;
  446. if ($devVal == 1) {
  447. $state = 1;
  448. }
  449. } else if ($jsonKey == 'run_parameter') {
  450. $valArr = [
  451. 'label' => $label, // 标题
  452. 'value' => $devVal, // 值
  453. 'unit' => $unit // 单位
  454. ];
  455. $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
  456. $dataArr['hoist'][$devTypeNum]['name'] = '中部副立井';
  457. // $dataArr['common']['direction']
  458. } else {
  459. $valArr = [
  460. 'label' => $label, // 标题
  461. 'state' => $devVal, // 值
  462. ];
  463. $dataArr['hoist'][$devTypeNum][$jsonKey][] = $valArr;
  464. }
  465. }
  466. $dataArr['hoist'][0]['state'] = $state;
  467. return $dataArr;
  468. }
  469. }