sys_key) ? $request->sys_key : $request->mine_code; $sys_name = isset($request->sys_name) ? $request->sys_name : $request->system_type; if (!isset($sys_key) || !isset($sys_name)) { return $this->error(-1, '参数错误'); } $redis_key = $sys_key.'_'.$sys_name; $redis = Redis::connection('default'); //指定连接user配置节点信息 $data = $redis->get($redis_key); if (isset($data)) { return $data; } else { return $this->error(-1, "接口数据获取错误"); } return $data; // $json_data = $this->get_redis_data($redis_key); $db_conf = config('database'); $api_key = 'get'; $api_url = 'http://'.$db_conf['python_api'][$sys_key]['url'].'/'.$api_key.'/?sys_key='.$sys_key.'&sys_name='.$sys_name; $response = $this->request_post($api_url); if (gettype($response) == 'integer') { switch ($response) { case 3: $error_info = 'CURLE_URL_MALFORMAT'; break; case 7: $error_info = 'CURLE_COULDNT_CONNECT'; break; default: $error_info = '未知错误:'.$response; } return $this->error($response, $error_info); } else if($data = json_decode($response, true)){ if ($data == null) { return $this->error(500, preg_replace('/<[^>]*>/','',$response)); } else { $arr = $this->data_handling($data); return $this->success($arr); } } else { return $this->error(500, '接口服务错误'); } } // 取OPC数据接口 private function request_post($url = '', $param = '') { if (empty($url)) { return false; } $postUrl = $url; $curlPost = $param; $curl = curl_init();//初始化curl curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0);//设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_POST, 1);//post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($curl);//运行curl $errno = curl_errno($curl); curl_close($curl); if ($errno > 0) return $errno; return $data; } private function data_handling($data) { // $redis = new Redis(); // $redis->connect(); $redis = Redis::connection('default'); //指定连接user配置节点信息 foreach ($data['sys_point'] as $key => $val) { foreach ($val as $k => $v) { $data_value = $v['val']; $data_key = $v['key']; if ($data_value) { // $userData = $redis->get($data_key); $data['sys_point'][$key][$k]['val'] = round($data_value, 2); # 数据持久化 } } } return $data; } private function get_redis_data($key) { $redis = Redis::connection('default'); //指定连接user配置节点信息 $data = $redis->get($key); if (isset($data)) { return $data; } else { return $this->error(-1, "接口数据获取错误"); } } /** * 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) { // } }