OPCDataTurboController.php 4.4 KB

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