| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace Modules\OpcData\Http\Controllers\Api;
- use App\Http\Controllers\Api\BaseController;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- use Illuminate\Support\Facades\Redis;
- class OPCDataTurboController extends BaseController
- {
- public function getData(Request $request) {
- $sys_key = isset($request->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)
- {
- //
- }
- }
|