OpcDataController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_fan_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 {
  99. return;
  100. }
  101. return response()->json($result);
  102. }
  103. // 水泵
  104. public function getFanData($groupId) {
  105. $sqlStr = "
  106. select t1.fan_type_id,
  107. t1.fan_type_name,
  108. t1.device_num,
  109. t1.device_name,
  110. case t.val
  111. when 'True' then 1
  112. when 'False' then 2
  113. else t.val
  114. end val,
  115. t1.json_key,
  116. t1.row_number,
  117. t1.unit
  118. from equipment t
  119. join tb_fan_dict_list t1 on t.selItem = t1.selItem
  120. where t1.group_id = ".$groupId."
  121. and t1.is_show = 1
  122. order by t1.fan_type_id, t1.row_number, t1.device_num
  123. ";
  124. $dbResult = $this->opcDB->select($sqlStr);
  125. // dd($dbResult);
  126. foreach ($dbResult as $key => $val) {
  127. $fanNum = $val->fan_type_id - 1; // 风机号
  128. $label = $val->device_name; // 设备名
  129. $devVal = $val->val; // 数值
  130. $jsonKey = $val->json_key; // json键值
  131. $unit = $val->unit; // 单位
  132. $dataArr[$fanNum]['title'] = $val->fan_type_name; // 风机名
  133. $valArr = [
  134. 'label' => $label, // 标题
  135. 'unit' => $unit // 单位
  136. ];
  137. // 状态与value
  138. if($jsonKey == 'state_list') {
  139. $valArr['state'] = $devVal;
  140. } else {
  141. $valArr['value'] = $devVal;
  142. }
  143. // 根据风量判断风机开关
  144. if($label == '风量') {
  145. if ($devVal <= 0 ) {
  146. $dataArr[$fanNum]['state'] = 2;
  147. } else {
  148. $dataArr[$fanNum]['state'] = 1;
  149. }
  150. }
  151. $dataArr[$fanNum][$jsonKey][] = $valArr;
  152. }
  153. return $dataArr;
  154. }
  155. public function getPumpData($groupId) {
  156. $sqlStr = "
  157. select t1.fan_type_id,
  158. t1.fan_type_name,
  159. t1.device_num,
  160. t1.device_name,
  161. case t.val
  162. when 'True' then 1
  163. when 'False' then 2
  164. else t.val
  165. end val,
  166. t1.json_key,
  167. t1.row_number,
  168. t1.unit
  169. from equipment t
  170. join tb_fan_dict_list t1 on t.selItem = t1.selItem
  171. where t1.group_id = ".$groupId."
  172. and t1.is_show = 1
  173. order by t1.fan_type_id, t1.row_number, t1.device_num
  174. ";
  175. $dbResult = $this->opcDB->select($sqlStr);
  176. $timeArr = array(); // 用于拼接时分秒
  177. $timeJsonKey; // 记录time字段key名
  178. foreach ($dbResult as $key => $val) {
  179. $fanName = $val->fan_type_name; // 泵名
  180. $rowNumber = $val->row_number; // 排序
  181. $jsonKey = $val->json_key; // json键值
  182. $label = $val->device_name; // 设备名
  183. $devNum = $val->fan_type_id - 1; // 设备号
  184. $unit = $val->unit; // 单位
  185. $devVal = $val->val; // 数值
  186. if ($rowNumber <= 4) {
  187. $valArr = [
  188. 'label' => $label, // 标题
  189. 'value' => $devVal, // 值
  190. 'unit' => $unit // 单位
  191. ];
  192. $dataArr[$jsonKey][] = $valArr;
  193. } else if ($rowNumber == 5) {
  194. $valArr = [
  195. 'label' => $label, // 标题
  196. 'state' => $devVal // 值
  197. ];
  198. $dataArr[$jsonKey][] = $valArr;
  199. } else if ($rowNumber == 10 or $rowNumber == 11) {
  200. $timeArr[$label][$devNum][$unit] = $devVal;
  201. $timeJsonKey = $jsonKey;
  202. } else if ($rowNumber > 5) {
  203. $valArr = [
  204. 'label' => $label, // 标题
  205. 'value' => $devVal, // 值
  206. 'unit' => $unit // 单位
  207. ];
  208. $dataArr['water_pump'][$devNum][$jsonKey][] = $valArr;
  209. }
  210. }
  211. foreach ($timeArr as $key => $value) {
  212. for ($i=0; $i < count($value); $i++) {
  213. $timeFormatStr = $value[$i]['HH'].'时'.$value[$i]['MM'].'分'.$value[$i]['SS'].'秒';
  214. $valArr = [
  215. 'label' => $key, // 标题
  216. 'value' => $timeFormatStr, // 值
  217. 'unit' => null // 单位
  218. ];
  219. $dataArr['water_pump'][$i][$timeJsonKey][] = $valArr;
  220. }
  221. }
  222. return $dataArr;
  223. }
  224. }