Preview.php 14.2 KB
<?php


namespace app\api\controller\v7\screen;


use app\common\controller\Api;
use fast\Http;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * 管网预演
 * Class Preview
 * @package app\api\controller\v7\screen
 */
class Preview extends Api
{
    public $noNeedLogin = ['*'];
    public $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

    }

    //控制阀门总览
    public function overview(){
        $result = [
            'total_num' => 159,
            'normal_num' => 0,
            'warning_num' => 0,
            'rate' => '0%',
        ];
        $this->success('成功', $result);
    }

    //流量数据
    public function flowData(){
        $result = [
            'x' => ['01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00'],
            'in' => [58,132,134,120,122,150,128,56],
            'out' => [135,162,158,122,120,152,132,68],
        ];
        $this->success('成功', $result);
    }

    //水压数据
    public function pressureData(){
        $reportLevelList = [
            '0' => '无',
            '1' => '轻微',
            '2' => '严重',
        ];
        $order = [
            'a.createtime' => 'DESC'
        ];
        $field = [
            'b.name',
            'a.number',
            'a.value',
            'a.report_level',
            'a.deal_note',
            'a.createtime',
            'b.longitude',
            'b.latitude'
        ];
        //导出数据列表返回50条
        $tableName = 'new_equip_hydraulicpress';
        $tableNameData = 'new_equip_hydraulicpress_data';
        $list = Db::name($tableNameData)
            ->alias('a')
            ->field($field)
            ->join("{$tableName} b", 'a.number=b.deviceId', 'LEFT')
            ->order($order)
            ->limit(0,50)
            ->select();
        foreach ($list as &$row){
            $row['createtime_text'] = date('Y-m-d H:i:s');
            $row['report_level_text'] = $reportLevelList[$row['report_level']];
        }
        $this->success('成功', $list);
//        $result = [
//            [
//                'device_name' => '水压计No.1',
//                'device_code' => 'ex86sa',
//                'device_position' => '勐甸村31公里处',
//                'pressure' => '1.25MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.2',
//                'device_code' => 'de87x5',
//                'device_position' => '勐甸村11公里处',
//                'pressure' => '0.8MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.3',
//                'device_code' => 'w25csa',
//                'device_position' => '勐甸村5公里处',
//                'pressure' => '1.0MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.4',
//                'device_code' => '5s5ax6',
//                'device_position' => '勐甸村9公里处',
//                'pressure' => '1.6MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.5',
//                'device_code' => 'deghs4',
//                'device_position' => '勐甸村11公里处',
//                'pressure' => '2.0MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.6',
//                'device_code' => 'ex8745',
//                'device_position' => '勐甸村28公里处',
//                'pressure' => '2.5MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.7',
//                'device_code' => 'eg246w',
//                'device_position' => '勐甸村29公里处',
//                'pressure' => '0.63MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.8',
//                'device_code' => 'esc46w',
//                'device_position' => '勐甸村34公里处',
//                'pressure' => '1.10MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.9',
//                'device_code' => 'eyuh25',
//                'device_position' => '勐甸村10公里处',
//                'pressure' => '0.93MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.10',
//                'device_code' => '256hjj',
//                'device_position' => '勐甸村22公里处',
//                'pressure' => '0.83MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//            [
//                'device_name' => '水压计No.11',
//                'device_code' => '687ety',
//                'device_position' => '勐甸村31公里处',
//                'pressure' => '0.73MPa',
//                'times' => date('Y-m-d H:i'),
//            ],
//        ];
//        $this->success('成功', $result);
    }

    //预演模型
    public function models(){
        $result = [
            'intro' => '管网预演模型是依托管网拓扑结构,应用水力学原理建立的教学模型并加入智能算法。通过水力模型计算、历史数据分析可以全面了解供水管网任意位置点位的流量、压力和流速等水力状态,为管网规划、设计、改造、维护、分析、调度提供了全面的、细节话的动态模拟。',
            'lists' => [
                '/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
                '/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
                '/uploads/20231224/FoN06J19nMnSf0KXoPQYMLkK8MbH.jpg',
            ]
        ];
        $this->success('成功', $result);
    }

    //历史预演及预案
    public function history(){
        $result = [
            [
                'title' => '水库阀门预演',
                'times' => date('Y-m-d H:i'),
                'level' => '1级预警',
                'programme' => '启用橙色自动应急预案',
            ],
            [
                'title' => '水压测试预演',
                'times' => date('Y-m-d H:i'),
                'level' => '1级预警',
                'programme' => '启用橙色自动应急预案',
            ],
            [
                'title' => '渗漏测试预演',
                'times' => date('Y-m-d H:i'),
                'level' => '1级预警',
                'programme' => '启用橙色自动应急预案',
            ],
            [
                'title' => '供水量测试预演',
                'times' => date('Y-m-d H:i'),
                'level' => '1级预警',
                'programme' => '启用橙色自动应急预案',
            ],
            [
                'title' => '监控测试预演',
                'times' => date('Y-m-d H:i'),
                'level' => '1级预警',
                'programme' => '启用橙色自动应急预案',
            ],
        ];
        $this->success('成功', $result);
    }

    //损失分析
    public function loss(){
        $result = [
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ],
            [
                'position' => '勐甸村36公里处阀门',
                'content' => '阀门处水压问题',
            ]
        ];
        $this->success('成功', $result);
    }

    //读取模型数据
    public function readModelData(){
        $mid = $this->request->param('mid');
        $type = $this->request->param('type');
        if (!in_array($type, [0,1,2,3,4,5])){
            $this->error('type参数错误');
        }
        if (empty($mid)){
            $this->error('模型ID必须');
        }
        $dataFile = Db::name('pipes_rehearsalmodel')->where(['id' => $mid])->value('data_file');
        if (empty($dataFile)){
            $data = [];
        }else{
            $config = get_addon_config('qiniu');
            $url = $config['cdnurl'].$dataFile;
            $dataStr = file_get_contents($url);
            $data = json_decode($dataStr, true);
            if (!empty($data)){
                if ($type == 0){
                    foreach ($data as $key=>$value){
                        if ($value['code'] == 'LJKdm6' || $value['type'] == 3 || $value['type'] == 4 || $value['type'] == 5){//todo 该节点会报错,暂时去除掉
                            unset($data[$key]);
                        }
                    }
                }else{
                    foreach ($data as $key=>$value){
                        if ($value['type'] != $type || $value['code'] == 'LJKdm6'){//todo 该节点会报错,暂时去除掉
                            unset($data[$key]);
                        }
                    }
                }
            }
        }
        $this->success('成功', array_values($data));
    }
    //预演模型参数
    public function parameters(){
        $params = $this->request->param();
        $validate = new Validate([
            'name|方案名称' => 'require|chsDash',
            'duration|模型运行的时间长度' => 'require|integer',
            'timestep|模拟间隔时间' => 'require|integer',
            'mid|模型ID' => 'require|integer|gt:0',
            'junctions|节点' => 'require',
        ]);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
        if (is_array($params['junctions'])){
            $params['junctions'] = json_encode($params['junctions']);
        }
        Db::startTrans();
        try {
            $params['createtime'] = time();
            $id = Db::name('pipes_preview_parameters')->insertGetId($params);
            if (!$id){
                exception('数据保存失败');
            }

            //调用python接口,传送数据
            $url = "http://121.37.3.63:5000/run_model";
            $data = [
                'mid' => $params['mid'],
                'param' => [
                    'junctions' => json_decode($params['junctions'],true),
                    'pipes' => [],
                    'pumps' => [],
                    'tanks' => [
                        [
                            'name' => 'HEoF4C',
                            'init_level' => 0
                        ]
                    ],
                    'valves' => [],
                    'reservoirs' => [],
                    'model' => []
                ]
            ];
            $data = json_encode($data);
            $option = [
                CURLOPT_HTTPHEADER => [    // 设置请求头部信息
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length:' . strlen($data),
                    'Cache-Control: no-cache',
                    'Pragma: no-cache'
                ]
            ];

            $response = Http::post($url,$data,$option);
            if (is_string($response)){
                $response = json_decode($response, true);
            }
            //记录运行日志
            $log = [
                'params' => json_decode($data, true),
                'response' => $response,
            ];
            file_put_contents(RUNTIME_PATH.'pipes_preview_log_'.date('Y-m-d-H-i-s') . '.log', 'json串格式:'.PHP_EOL.json_encode($log).PHP_EOL.'PHP数组格式:'.PHP_EOL.var_export($log,true), FILE_APPEND);
            if (empty($response['code']) || $response['code'] !== 200){
                exception('获取预演结果失败:'.$response['msg']);
            }
            //把返回的结果加上坐标
            if(isset($response['data']['junctions']) && is_array($response['data']['junctions'])){
                foreach ($response['data']['junctions'] as &$item){
                    //默认写死查type=2的表
                    $find = Db::name("pipes_well")->where(['well_code' => $item['name']])->whereNull('deletetime')->find();
                    if ($find){
                        $item['lng'] = $find['well_lng'];
                        $item['lat'] = $find['well_lat'];
                    }else{
                        $item['lng'] = '';
                        $item['lat'] = '';
                    }
                }
                unset($item);
            }
            $updateData = [
                'junctions_result' => json_encode($response['data']['junctions'] ?? []),
                'model_result' => json_encode($response['data']['model'] ?? []),
                'pipes_result' => json_encode($response['data']['pipes'] ?? []),
                'pumps_result' => json_encode($response['data']['pumps'] ?? []),
                'reservoirs_result' => json_encode($response['data']['reservoirs'] ?? []),
                'tanks_result' => json_encode($response['data']['tanks'] ?? []),
                'valves_result' => json_encode($response['data']['valves'] ?? []),
            ];
            Db::name('pipes_preview_parameters')->where(['id' => $id])->update($updateData);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('操作成功', $response['data']);
    }
}