OPCDataTurboController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\BaseController;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Routing\Controller;
  7. use Illuminate\Support\Facades\Redis;
  8. class OPCDataTurboController extends BaseController
  9. {
  10. public function getData(Request $request) {
  11. $sys_key = isset($request->sys_key) ? $request->sys_key : $request->mine_code;
  12. $sys_name = isset($request->sys_name) ? $request->sys_name : $request->system_type;
  13. if (!isset($sys_key) || !isset($sys_name)) {
  14. return $this->error(-1, '参数错误');
  15. }
  16. $redis_key = $sys_key.'_'.$sys_name;
  17. $redis = Redis::connection('default'); //指定连接user配置节点信息
  18. $data = $redis->get($redis_key);
  19. if (isset($data)) {
  20. return $data;
  21. } else {
  22. return $this->error(-1, "接口数据获取错误");
  23. }
  24. return $data;
  25. // $json_data = $this->get_redis_data($redis_key);
  26. $db_conf = config('database');
  27. $api_key = 'get';
  28. $api_url = 'http://'.$db_conf['python_api'][$sys_key]['url'].'/'.$api_key.'/?sys_key='.$sys_key.'&sys_name='.$sys_name;
  29. $response = $this->request_post($api_url);
  30. if (gettype($response) == 'integer') {
  31. switch ($response) {
  32. case 3:
  33. $error_info = 'CURLE_URL_MALFORMAT';
  34. break;
  35. case 7:
  36. $error_info = 'CURLE_COULDNT_CONNECT';
  37. break;
  38. default:
  39. $error_info = '未知错误:'.$response;
  40. }
  41. return $this->error($response, $error_info);
  42. } else if($data = json_decode($response, true)){
  43. if ($data == null) {
  44. return $this->error(500, preg_replace('/<[^>]*>/','',$response));
  45. } else {
  46. $arr = $this->data_handling($data);
  47. return $this->success($arr);
  48. }
  49. } else {
  50. return $this->error(500, '接口服务错误');
  51. }
  52. }
  53. // 取OPC数据接口
  54. private function request_post($url = '', $param = '') {
  55. if (empty($url)) {
  56. return false;
  57. }
  58. $postUrl = $url;
  59. $curlPost = $param;
  60. $curl = curl_init();//初始化curl
  61. curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
  62. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  63. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  64. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  65. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
  66. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  67. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  68. $data = curl_exec($curl);//运行curl
  69. $errno = curl_errno($curl);
  70. curl_close($curl);
  71. if ($errno > 0) return $errno;
  72. return $data;
  73. }
  74. private function data_handling($data) {
  75. // $redis = new Redis();
  76. // $redis->connect();
  77. $redis = Redis::connection('default'); //指定连接user配置节点信息
  78. foreach ($data['sys_point'] as $key => $val) {
  79. foreach ($val as $k => $v) {
  80. $data_value = $v['val'];
  81. $data_key = $v['key'];
  82. if ($data_value) {
  83. // $userData = $redis->get($data_key);
  84. $data['sys_point'][$key][$k]['val'] = round($data_value, 2);
  85. # 数据持久化
  86. }
  87. }
  88. }
  89. return $data;
  90. }
  91. private function get_redis_data($key) {
  92. $redis = Redis::connection('default'); //指定连接user配置节点信息
  93. $data = $redis->get($key);
  94. if (isset($data)) {
  95. return $data;
  96. } else {
  97. return $this->error(-1, "接口数据获取错误");
  98. }
  99. }
  100. /**
  101. * Display a listing of the resource.
  102. * @return Renderable
  103. */
  104. public function index()
  105. {
  106. return view('opcdata::index');
  107. }
  108. /**
  109. * Show the form for creating a new resource.
  110. * @return Renderable
  111. */
  112. public function create()
  113. {
  114. return view('opcdata::create');
  115. }
  116. /**
  117. * Store a newly created resource in storage.
  118. * @param Request $request
  119. * @return Renderable
  120. */
  121. public function store(Request $request)
  122. {
  123. //
  124. }
  125. /**
  126. * Show the specified resource.
  127. * @param int $id
  128. * @return Renderable
  129. */
  130. public function show($id)
  131. {
  132. return view('opcdata::show');
  133. }
  134. /**
  135. * Show the form for editing the specified resource.
  136. * @param int $id
  137. * @return Renderable
  138. */
  139. public function edit($id)
  140. {
  141. return view('opcdata::edit');
  142. }
  143. /**
  144. * Update the specified resource in storage.
  145. * @param Request $request
  146. * @param int $id
  147. * @return Renderable
  148. */
  149. public function update(Request $request, $id)
  150. {
  151. //
  152. }
  153. /**
  154. * Remove the specified resource from storage.
  155. * @param int $id
  156. * @return Renderable
  157. */
  158. public function destroy($id)
  159. {
  160. //
  161. }
  162. }