| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace Modules\OpcData\Http\Controllers\Api;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Facades\DB;
- use Modules\OpcData\Entities\OpcDataDict;
- class OpcDataController extends Controller
- {
- protected $opcDB;
- // private $opcDictArr = array();
- protected $opcConnectName = 'mysql_opc_jinjiaqu';
- // private $dictTableName = 'tb_fan_dict_list';
- // private $dataTableName = 'equipment';
-
- public function __construct() {
- $this->opcDB = DB::connection($this->opcConnectName);
- }
- /**
- * Display a listing of the resource.
- * @return Renderable
- */
- public function index()
- {
- return view('opcdata::index');
- }
- /**
- * Show the form for creating a new resource.
- * @return Renderable
- */
- public function create()
- {
- return view('opcdata::create');
- }
- /**
- * Store a newly created resource in storage.
- * @param Request $request
- * @return Renderable
- */
- public function store(Request $request)
- {
- //
- }
- /**
- * Show the specified resource.
- * @param int $id
- * @return Renderable
- */
- public function show($id)
- {
- return view('opcdata::show');
- }
- /**
- * Show the form for editing the specified resource.
- * @param int $id
- * @return Renderable
- */
- public function edit($id)
- {
- return view('opcdata::edit');
- }
- /**
- * Update the specified resource in storage.
- * @param Request $request
- * @param int $id
- * @return Renderable
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * Remove the specified resource from storage.
- * @param int $id
- * @return Renderable
- */
- public function destroy($id)
- {
- //
- }
- public function getData(Request $request)
- {
- $getDataType = $request->system_type; // 获取需要的数据
- $dataArr = array(); // 返回数据数组
- $result;
- if ($getDataType == 'ventilation_zb'){
- // 中部风机
- $groupId = 1;
- $result = $this->getFanData($groupId);
- } else if ($getDataType == 'ventilation_bb') {
- // 北部风机
- $groupId = 2;
- $result = $this->getFanData($groupId);
- } else if ($getDataType == 'pump_zy') {
- // 水泵风机
- $groupId = 3;
- $result = $this->getPumpData($groupId);
- } else {
- return;
- }
- return response()->json($result);
- }
- // 水泵
- public function getFanData($groupId) {
- $sqlStr = "
- select t1.fan_type_id,
- t1.fan_type_name,
- t1.device_num,
- t1.device_name,
- case t.val
- when 'True' then 1
- when 'False' then 2
- else t.val
- end val,
- t1.json_key,
- t1.row_number,
- t1.unit
- from equipment t
- join tb_fan_dict_list t1 on t.selItem = t1.selItem
- where t1.group_id = ".$groupId."
- and t1.is_show = 1
- order by t1.fan_type_id, t1.row_number, t1.device_num
- ";
-
- $dbResult = $this->opcDB->select($sqlStr);
- // dd($dbResult);
- foreach ($dbResult as $key => $val) {
- $fanNum = $val->fan_type_id - 1; // 风机号
- $label = $val->device_name; // 设备名
- $devVal = $val->val; // 数值
- $jsonKey = $val->json_key; // json键值
- $unit = $val->unit; // 单位
- $dataArr[$fanNum]['title'] = $val->fan_type_name; // 风机名
- $valArr = [
- 'label' => $label, // 标题
- 'unit' => $unit // 单位
- ];
- // 状态与value
- if($jsonKey == 'state_list') {
- $valArr['state'] = $devVal;
- } else {
- $valArr['value'] = $devVal;
- }
- // 根据风量判断风机开关
- if($label == '风量') {
- if ($devVal <= 0 ) {
- $dataArr[$fanNum]['state'] = 2;
- } else {
- $dataArr[$fanNum]['state'] = 1;
- }
- }
- $dataArr[$fanNum][$jsonKey][] = $valArr;
-
- }
- return $dataArr;
- }
- public function getPumpData($groupId) {
- $sqlStr = "
- select t1.fan_type_id,
- t1.fan_type_name,
- t1.device_num,
- t1.device_name,
- case t.val
- when 'True' then 1
- when 'False' then 2
- else t.val
- end val,
- t1.json_key,
- t1.row_number,
- t1.unit
- from equipment t
- join tb_fan_dict_list t1 on t.selItem = t1.selItem
- where t1.group_id = ".$groupId."
- and t1.is_show = 1
- order by t1.fan_type_id, t1.row_number, t1.device_num
- ";
-
- $dbResult = $this->opcDB->select($sqlStr);
- $timeArr = array(); // 用于拼接时分秒
- $timeJsonKey; // 记录time字段key名
- foreach ($dbResult as $key => $val) {
- $fanName = $val->fan_type_name; // 泵名
- $rowNumber = $val->row_number; // 排序
- $jsonKey = $val->json_key; // json键值
- $label = $val->device_name; // 设备名
- $devNum = $val->fan_type_id - 1; // 设备号
- $unit = $val->unit; // 单位
- $devVal = $val->val; // 数值
- if ($rowNumber <= 4) {
- $valArr = [
- 'label' => $label, // 标题
- 'value' => $devVal, // 值
- 'unit' => $unit // 单位
- ];
- $dataArr[$jsonKey][] = $valArr;
- } else if ($rowNumber == 5) {
- $valArr = [
- 'label' => $label, // 标题
- 'state' => $devVal // 值
- ];
- $dataArr[$jsonKey][] = $valArr;
- } else if ($rowNumber == 10 or $rowNumber == 11) {
- $timeArr[$label][$devNum][$unit] = $devVal;
- $timeJsonKey = $jsonKey;
- } else if ($rowNumber > 5) {
- $valArr = [
- 'label' => $label, // 标题
- 'value' => $devVal, // 值
- 'unit' => $unit // 单位
- ];
- $dataArr['water_pump'][$devNum][$jsonKey][] = $valArr;
- }
-
- }
- foreach ($timeArr as $key => $value) {
- for ($i=0; $i < count($value); $i++) {
- $timeFormatStr = $value[$i]['HH'].'时'.$value[$i]['MM'].'分'.$value[$i]['SS'].'秒';
- $valArr = [
- 'label' => $key, // 标题
- 'value' => $timeFormatStr, // 值
- 'unit' => null // 单位
- ];
- $dataArr['water_pump'][$i][$timeJsonKey][] = $valArr;
- }
- }
- return $dataArr;
- }
- }
|