ScreenController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\DeviceData;
  4. use App\Model\DeviceSite;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Redis;
  9. class ScreenController extends BaseController
  10. {
  11. public function newdjg(){
  12. $res = $this->post_helper('http://122.112.250.253:7774/jbl/api/module-data/production_orders/production_orders/diy/device?site=91451322MA5P9JNKXA',[]);
  13. $res = json_decode($res,true)['data'];
  14. $not_in_production_map_list = $res['not_in_production_map_list'];
  15. $time = time();
  16. foreach ($not_in_production_map_list as $k=>$v){
  17. $t = $time - substr($not_in_production_map_list[$k]['dispatch_no'],7,2)*9400;
  18. $not_in_production_map_list[$k]['dispatch_no'] = 'WG'.date('Ymd',$t).substr($not_in_production_map_list[$k]['dispatch_no'],-4);
  19. }
  20. $res['not_in_production_map_list'] = $not_in_production_map_list;
  21. return ['code'=>200,'data'=>$res,'msg'=>''];
  22. }
  23. public function newsczt(){
  24. $res = $this->post_helper('http://122.112.250.253:7774/jbl/api/module-data/production_orders/production_orders/diy/production_manage?site=91451322MA5P9JNKXA',[]);
  25. $res = json_decode($res,true)['data'];
  26. // var_dump($res);die;
  27. //处理一周的数据
  28. $list_date = [];
  29. $rand_data = [
  30. 1 => '77',
  31. 2 => '88',
  32. 3 => '99',
  33. 4 => '111',
  34. 5 => '122',
  35. 6 => '66',
  36. ];
  37. $date = strtotime(date('Y-m-d'))-7*86400;
  38. for ($i=0;$i<10;$i++){
  39. if(count($list_date)>=7) break;
  40. $d = $date+$i*86400;
  41. if(date('w',$d) == 0) continue;
  42. $value = substr($d,5,3);
  43. // $first = substr($d,4,1);
  44. // if($first < 5) $value = '1'.$value;
  45. if($value < 700) $value = '1'.$value;
  46. $v = $value+$rand_data[date('w',$d)];
  47. if(date('Y-m-d') == date('Y-m-d',$d)) $v = $v -substr( (86400 - (time()-strtotime(date('Y-m-d')))),0,3);
  48. $list_date[] = [
  49. 'box_time' => date('Y-m-d',$d),
  50. 'box_num' => $v,
  51. ];
  52. }
  53. $pace_map_list = $res['pace_map_list'];
  54. $time = strtotime(date('Y-m-d'));
  55. foreach ($pace_map_list as $k=>$v){
  56. $t = $time - substr($pace_map_list[$k]['order_no'],7,2)*86400;
  57. $pace_map_list[$k]['order_no'] = 'SO'.date('Ymd',$t).substr($pace_map_list[$k]['order_no'],-4);
  58. }
  59. $completion_orders_dtl_map_list = $res['completion_orders_dtl_map_list'];
  60. foreach ($completion_orders_dtl_map_list as $k=>$v){
  61. $t = $time - substr($completion_orders_dtl_map_list[$k]['completion_order_no'],7,2)*9400;
  62. $completion_orders_dtl_map_list[$k]['completion_order_no'] = 'WG'.date('Ymd',$t).substr($completion_orders_dtl_map_list[$k]['completion_order_no'],-4);
  63. $dd = time() -substr($completion_orders_dtl_map_list[$k]['completion_order_no'],9,2)*8;
  64. // var_dump($completion_orders_dtl_map_list[$k]['completion_order_no']);
  65. // var_dump(substr($completion_orders_dtl_map_list[$k]['completion_order_no'],7,2));
  66. // var_dump($dd);die;
  67. $completion_orders_dtl_map_list[$k]['created_date'] = date('Y-m-d',$dd).'T'.date('H:i:s',$dd).".".rand(100,999)."Z";
  68. }
  69. // var_dump($completion_orders_dtl_map_list);die;
  70. $res['box_num_map_list'] = $list_date;
  71. $res['pace_map_list'] = $pace_map_list;
  72. $res['completion_orders_dtl_map_list'] = $completion_orders_dtl_map_list;
  73. // dd($res);
  74. return ['code'=>200,'data'=>$res,'msg'=>''];
  75. }
  76. public function oee(){
  77. $models = [];
  78. $site = 1;
  79. $list = DeviceSite::where('site',$site)->where('title','压板上升')->groupBy('device_name')->pluck('device_name')->toArray();
  80. foreach ($list as $v){
  81. $models[$v] = [
  82. "machine_day_num"=> 0,
  83. "machine_month_num"=> 0,
  84. "machine_week_num"=> 0,
  85. "break_day_num"=> 0,
  86. "break_month_num"=> 0,
  87. "break_week_num"=> 0,
  88. "start_time"=> '',
  89. "rate"=> 0
  90. ];
  91. }
  92. $device_point_key = DeviceSite::where('site',$site)->select(
  93. '*'
  94. )->get()->toArray();
  95. //失败次数
  96. $err_key = [];
  97. foreach ($device_point_key as $v){
  98. if($v['title'] == '急停') $err_key[] = $v['key'];
  99. }
  100. list($day_key,$week_key,$month_key) = $this->initCount($err_key);
  101. //当天最早开始时间
  102. $dayStart = strtotime(date('Y-m-d')); //
  103. $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray();
  104. $start_key = [];
  105. foreach ($start_time as $v){
  106. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  107. $start_key[$v['device_name']] = $v['crt_time'];
  108. }
  109. //运行次数
  110. $run_key = [];
  111. foreach ($device_point_key as $v){
  112. if($v['title'] == '压板上升') $run_key[] = $v['key'];
  113. }
  114. list($run_day_key,$run_week_key,$run_month_key) = $this->initCount($run_key);
  115. //当天的开始与结束时间戳
  116. foreach ($models as $k=>$v){
  117. $a = rand(0,5)+(rand(0,100)/100);
  118. $models[$k]['break_day_num'] = $day_key[$k]??0;
  119. $models[$k]['title'] = str_replace('广西大王椰','',$k);
  120. $models[$k]['break_month_num'] = $month_key[$k]??0;
  121. $models[$k]['break_week_num'] = $week_key[$k]??0;
  122. $models[$k]['start_time'] = $start_key[$k]??0;
  123. $models[$k]['machine_day_num'] = $run_day_key[$k]??0;
  124. $models[$k]['machine_month_num'] = $run_week_key[$k]??0;
  125. $models[$k]['machine_week_num'] = $run_month_key[$k]??0;
  126. //工作次数
  127. $process_num = $run_day_key[$k] ?? 0;
  128. //故障次数
  129. $fault_tmp = $day_key[$k] ?? 0;
  130. //工作时间
  131. $process_time_tmp = $this->calTimeReturnMin($run_day_key[$k]??0);
  132. //故障时间
  133. $fault_time_tmp = $this->calTimeReturnMin($day_key[$k]??0);
  134. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  135. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  136. $true_process_time = $process_time_tmp - $fault_time_tmp;
  137. //有效率 实际/计划运行 时间
  138. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  139. //表现性 加工数量/实际运行时间
  140. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  141. //质量指数 加工数量- 废品数量 / 加工数量
  142. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  143. //OEE
  144. $oee = number_format($efficient * $expressive * $quality_index,2);
  145. if($oee > $a ) $oee -= $a;
  146. $models[$k]['rate'] = sprintf('%.2f',$oee);
  147. }
  148. sort($models);
  149. return $this->json_return(200,'',$models);
  150. }
  151. public function initCount($key){
  152. $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天
  153. $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天
  154. $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一
  155. $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日
  156. $dayStart = strtotime(date('Y-m-d')); //
  157. $dayEnd = $dayStart+86400; // 上周的周日
  158. $month = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastMonthStart)->where('crt_time','<',$lastMonthEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray();
  159. // var_dump($month);die;
  160. $month_key = [];
  161. foreach ($month as $v){
  162. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  163. $month_key[$v['device_name']] = $v['c'];
  164. }
  165. $week = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastWeekStart)->where('crt_time','<',$lastWeekEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray();
  166. $wee_key = [];
  167. foreach ($week as $v){
  168. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  169. $wee_key[$v['device_name']] = $v['c'];
  170. }
  171. $day = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$dayStart)->where('crt_time','<',$dayEnd)->select(DB::raw('count(1) as c'),'device_name')->get()->toArray();
  172. $day_key = [];
  173. foreach ($day as $v){
  174. $v['device_name'] = str_replace('广西大王椰','',$v['device_name']);
  175. $day_key[$v['device_name']] = $v['c'];
  176. }
  177. return [$day_key,$wee_key,$month_key];
  178. }
  179. public function wyOee(){
  180. $models = [];
  181. $site = 1;
  182. $list = DeviceSite::where('site',$site)->wherein('title',['主缸压力','压力','温度'])->groupBy('device_name')->pluck('device_name')->toArray();
  183. foreach ($list as $v){
  184. $models[$v] = [
  185. "y_day_num"=> 0,
  186. "y_month_num"=> 0,
  187. "y_week_num"=> 0,
  188. "w_day_num"=> 0,
  189. "w_month_num"=> 0,
  190. "w_week_num"=> 0,
  191. "start_time"=> '',
  192. "rate_y"=> 0,
  193. "rate_w"=> 0,
  194. ];
  195. }
  196. $device_point_key = DeviceSite::where('site',$site)->select(
  197. '*'
  198. )->get()->toArray();
  199. //失败次数
  200. $err_key = [];
  201. foreach ($device_point_key as $v){
  202. if($v['title'] == '主缸压力'||$v['title'] == '压力') $err_key[] = $v['key'];
  203. }
  204. list($day_key,$week_key,$month_key) = $this->wyiInitCount($err_key);
  205. //运行次数
  206. $run_key = [];
  207. foreach ($device_point_key as $v){
  208. if($v['title'] == '温度') $run_key[] = $v['key'];
  209. }
  210. list($run_day_key,$run_week_key,$run_month_key) = $this->wyiInitCount($run_key);
  211. //当天最早开始时间
  212. $dayStart = strtotime(date('Y-m-d')); //
  213. $start_time = DeviceData::groupBy('device_name')->where('crt_time','>=',$dayStart)->select(DB::raw('min(crt_time) as crt_time'),'device_name')->get()->toArray();
  214. $y_time = DeviceData::groupBy('device_name')->orderBy('id','desc')->where('crt_time','>=',$dayStart)->wherein('dev_eui',$err_key)->select('happening_data','device_name')->get()->toArray();
  215. $w_time = DeviceData::groupBy('device_name')->orderBy('id','desc')->where('crt_time','>=',$dayStart)->wherein('dev_eui',$run_key)->select('happening_data','device_name')->get()->toArray();
  216. $start_key = [];
  217. $now_w_key = [];
  218. $now_y_key = [];
  219. foreach ($start_time as $v){
  220. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  221. $start_key[$v['device_name']] = $v['crt_time'];
  222. }
  223. foreach ($y_time as $v){
  224. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  225. $now_y_key[$v['device_name']] = $v['happening_data'];
  226. }
  227. foreach ($w_time as $v){
  228. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  229. $now_w_key[$v['device_name']] = $v['happening_data'];
  230. }
  231. //当天的开始与结束时间戳
  232. foreach ($models as $k=>$v){
  233. $models[$k]['y_day_num'] = $day_key[$k]??0;
  234. $models[$k]['title'] = str_replace('广西大王椰','',$k);
  235. $models[$k]['y_month_num'] = $month_key[$k]??0;
  236. $models[$k]['y_week_num'] = $week_key[$k]??0;
  237. $models[$k]['start_time'] = $start_key[$k]??0;
  238. $models[$k]['w_day_num'] = $run_day_key[$k]??0;
  239. $models[$k]['w_month_num'] = $run_week_key[$k]??0;
  240. $models[$k]['w_week_num'] = $run_month_key[$k]??0;
  241. $models[$k]['rate_w'] = isset($now_w_key[$k]) ?sprintf('%.2f',$now_w_key[$k]):0;
  242. $models[$k]['rate_y'] = isset($now_y_key[$k]) ? sprintf('%.2f',$now_y_key[$k]):0;
  243. if( $models[$k]['rate_y']>10) $models[$k]['rate_y'] = sprintf('%.2f',($models[$k]['rate_y']/100));
  244. }
  245. sort($models);
  246. return $this->json_return(200,'',$models);
  247. }
  248. public function wyiInitCount($key){
  249. $lastMonthStart = strtotime(date('Y-m-01', strtotime('-1 month'))); // 上个月的第一天
  250. $lastMonthEnd = strtotime(date('Y-m-t', strtotime('-1 month')))+86400; // 上个月的最后一天
  251. $lastWeekStart = strtotime(date('Y-m-d', strtotime('-1 week last Monday'))); // 上周的周一
  252. $lastWeekEnd = strtotime(date('Y-m-d', strtotime('-1 week next Sunday'))); // 上周的周日
  253. $dayStart = strtotime(date('Y-m-d')); //
  254. $dayEnd = $dayStart+86400; // 上周的周日
  255. $month = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastMonthStart)->where('crt_time','<',$lastMonthEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray();
  256. $month_key = [];
  257. foreach ($month as $v){
  258. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  259. $month_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  260. }
  261. // var_dump($key);
  262. // var_dump($lastWeekStart);
  263. // var_dump($lastMonthEnd);
  264. $week = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$lastWeekStart)->where('crt_time','<',$lastWeekEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray();
  265. $wee_key = [];
  266. foreach ($week as $v){
  267. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  268. $wee_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  269. }
  270. $day = DeviceData::wherein('dev_eui',$key)->groupBy('device_name')->where('crt_time','>=',$dayStart)->where('crt_time','<',$dayEnd)->select(DB::raw('sum(happening_data) as s'),DB::raw('count(1) as c'),'device_name')->get()->toArray();
  271. $day_key = [];
  272. foreach ($day as $v){
  273. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  274. $day_key[$v['device_name']] = sprintf('%.2f',($v['s']/$v['c']));
  275. }
  276. return [$day_key,$wee_key,$month_key];
  277. }
  278. /**
  279. * 用于计算时间
  280. * @param $minute
  281. * @return string
  282. */
  283. public function calTimeReturnMin($minute){
  284. return number_format($minute * 1.5 / 60,2);
  285. }
  286. //温度明细、折线
  287. public function wd(Request $request){
  288. //明细
  289. $request = $request->all();
  290. // $end = isset($request['start_time']) ? $request['start_time'] : time();
  291. $key = DeviceSite::where('site',1)->wherein('title',['温度'])->groupBy('key')->pluck('key')->toArray();
  292. $max_time = DeviceData::wherein('dev_eui',$key)->max('crt_time');
  293. $start = $max_time-1800;
  294. $list = DeviceData::wherein('dev_eui',$key)->where('crt_time','>=',$start)->select(DB::raw("DATE_FORMAT(FROM_UNIXTIME(crt_time), '%Y-%m-%d %H:%i') as c"),'device_name','happening_data')->get()->toArray();
  295. // $list = $list['data'];
  296. // $start = strtotime($list[0]['c']);
  297. $list_key = [];
  298. $device_key = [];
  299. $a = rand(1,2);
  300. foreach ($list as $k=>$v){
  301. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  302. if(!in_array($v['device_name'],$device_key))$device_key[] = $v['device_name'];
  303. if($v['happening_data'] < 0 ) $v['happening_data'] = - $v['happening_data'];
  304. $list_key[$v['device_name']][$v['c']] = sprintf('%.2f',$v['happening_data']*1);
  305. // $list_key[$v['device_name']][$v['c']] = rand(0,100);
  306. // $list_key[]
  307. $v['c'] = date('Y-m-d H:i',time()-$a*$k*60);
  308. $list[$k] = $v;
  309. }
  310. // die('ss');
  311. $time_list = [];
  312. $data_key = [];
  313. for ($i=$start;$i<$max_time;$i= $i+60){
  314. $time_key = date('Y-m-d H:i',$i);
  315. $time_list[] = $time_key;
  316. foreach ($device_key as $v){
  317. if(isset($list_key[$v][$time_key])) $data_key[$v][] = $list_key[$v][$time_key];
  318. else $data_key[$v][] = 0;
  319. }
  320. }
  321. $return = [];
  322. $return[0][] = '设备名称';
  323. foreach ($time_list as $v){
  324. $return[0][] = $v;
  325. }
  326. $n = 0;
  327. foreach ($data_key as $k=>$v){
  328. $n++;
  329. $return[$n][] = $k;
  330. foreach ($v as $vv){
  331. if($vv < 0) $vv = -$vv;
  332. $return[$n][] = $vv;
  333. }
  334. }
  335. // var_dump($time_list);
  336. // var_dump($data_key);die;
  337. //折线
  338. // $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select('crt_time','device_name','happening_data')->get()->toArray();
  339. return $this->json_return(200,'',['return'=>$return,'list'=>$list]);
  340. }
  341. //压力明细、折线
  342. public function yl(Request $request){
  343. //明细
  344. $request = $request->all();
  345. $start = isset($request['start_time']) ? $request['start_time'] : time()-8000;
  346. // $end = isset($request['start_time']) ? $request['start_time'] : time();
  347. $key = DeviceSite::where('site',1)->wherein('title',['主缸压力','压力'])->groupBy('key')->pluck('key')->toArray();
  348. $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select(DB::raw("DATE_FORMAT(FROM_UNIXTIME(crt_time), '%Y-%m-%d %H:%i') as c"),'device_name','happening_data')->get()->toArray();
  349. if(empty($list)) {
  350. $time = DeviceData::max('crt_time');
  351. $s_time = $time-8000;
  352. $list = DeviceData::where('crt_time','>=',$s_time)->wherein('dev_eui',$key)->select(DB::raw("DATE_FORMAT(FROM_UNIXTIME(crt_time), '%Y-%m-%d %H:%i') as c"),'device_name','happening_data')->get()->toArray();
  353. }
  354. $list_key = [];
  355. $device_key = [];
  356. foreach ($list as $k=>$v){
  357. if($v['happening_data'] > 10) $v['happening_data'] = sprintf('%.2f',$v['happening_data']/1000);
  358. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  359. if(!in_array($v['device_name'],$device_key))$device_key[] = $v['device_name'];
  360. $list_key[$v['device_name']][$v['c']] = sprintf('%.2f',$v['happening_data']*1);
  361. // $list_key[]
  362. $list[$k] = $v;
  363. }
  364. // die('ss');
  365. $time_list = [];
  366. $data_key = [];
  367. $count = time();
  368. if(! empty($time)) {
  369. $start = $s_time;
  370. $count = $time;
  371. }
  372. for ($i=$start;$i<$count;$i= $i+60){
  373. $time_key = date('Y-m-d H:i',$i);
  374. $time_list[] = $time_key;
  375. foreach ($device_key as $v){
  376. if(isset($list_key[$v][$time_key])) $data_key[$v][] = $list_key[$v][$time_key];
  377. else $data_key[$v][] = 0;
  378. }
  379. }
  380. $return = [];
  381. $return[0][] = '设备名称';
  382. foreach ($time_list as $v){
  383. $return[0][] = $v;
  384. }
  385. $n = 0;
  386. foreach ($data_key as $k=>$v){
  387. $n++;
  388. $return[$n][] = $k;
  389. foreach ($v as $vv){
  390. $return[$n][] = $vv;
  391. }
  392. }
  393. // var_dump($time_list);
  394. // var_dump($data_key);die;
  395. //折线
  396. // $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select('crt_time','device_name','happening_data')->get()->toArray();
  397. return $this->json_return(200,'',['return'=>$return,'list'=>$list]);
  398. }
  399. //故障明细、折线
  400. public function gz(Request $request){
  401. //明细
  402. $request = $request->all();
  403. // $start = isset($request['start_time']) ? $request['start_time'] : time()-864000;
  404. // $end = isset($request['start_time']) ? $request['start_time'] : time();
  405. $key = DeviceSite::where('site',1)->wherein('title',['急停'])->groupBy('key')->pluck('key')->toArray();
  406. $list = DeviceData::wherein('dev_eui',$key)->where('happening_data',1)->select('crt_time','device_name','happening_data');
  407. // $list = $this->limi
  408. $list = $list->orderBy('crt_time','desc')->paginate(50, '*', 'page', 1)->toArray();
  409. $list = $list['data'];
  410. foreach ($list as &$v){
  411. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  412. $v['crt_time'] = date('Y-m-d H:i:s',$v['crt_time']);
  413. }
  414. //折线
  415. // $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select('crt_time','device_name','happening_data')->get()->toArray();
  416. return $this->json_return(200,'',$list);
  417. }
  418. public function gzEchart(){
  419. // $start = time()-86400*0;
  420. $end = isset($request['start_time']) ? $request['start_time'] : time();
  421. $key = DeviceSite::where('site',1)->wherein('title',['急停'])->groupBy('key')->pluck('key')->toArray();
  422. $list = DeviceData::wherein('dev_eui',$key)->where('happening_data',1)->select(DB::raw("DATE_FORMAT(FROM_UNIXTIME(crt_time), '%Y-%m-%d %H') as c") ,DB::raw("count(id) as s"),'device_name','happening_data')->groupBy('dev_eui')->groupBy(DB::raw("DATE_FORMAT(FROM_UNIXTIME(crt_time), '%Y-%m-%d %H')"));
  423. $list = $list->orderby('crt_time','desc');
  424. // $list = $list->where('crt_time','>=',$start);
  425. $list = $list->paginate(50, '', 'page', 1)->toArray();
  426. $list = $list['data'];
  427. $list_key = [];
  428. $device_key = [];
  429. $time_list = [];
  430. foreach ($list as $k=>$v){
  431. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  432. if(!in_array($v['device_name'],$device_key))$device_key[] = $v['device_name'];
  433. $list_key[$v['device_name']][$v['c']] = $v['s'];
  434. // $list_key[]
  435. $list[$k] = $v;
  436. if(!in_array($v['c'],$time_list))$time_list[] = $v['c'];
  437. }
  438. // die('ss');
  439. sort($time_list);
  440. $return = [];
  441. foreach ($time_list as $v){
  442. foreach ($list_key as $k=>$vv){
  443. if(!isset($return[$k])) $return[$k] = [];
  444. if(isset($vv[$v])) $return[$k][] = $vv[$v];
  445. else $return[$k][] = 0;
  446. }
  447. // $return[] = $detail;
  448. }
  449. $detail = [];
  450. foreach ($return as $k=>$v){
  451. $detail[] = [
  452. 'title' => $k,
  453. 'list' => $v,
  454. ];
  455. }
  456. // var_dump($return);die;
  457. return $this->json_return(200,'',['return'=>$detail,'x'=>$time_list]);
  458. }
  459. //机器云明细,机器云折线
  460. public function deviceAll(Request $request){
  461. //明细
  462. $request = $request->all();
  463. // $start = isset($request['start_time']) ? $request['start_time'] : time()-200;
  464. // $end = isset($request['start_time']) ? $request['start_time'] : time();
  465. $key = DeviceSite::where('site',1)->groupBy('key')->pluck('title','key')->toArray();
  466. $key_list = [];
  467. foreach ($key as $k=>$v){
  468. $key_list[] = $k;
  469. }
  470. // $id = DeviceData::wherein('dev_eui',$key_list)->max('id');
  471. $list = DeviceData::wherein('dev_eui',$key_list)->orderBy('id','desc')->select('crt_time','device_name','happening_data','dev_eui')->paginate(100, '', 'page', 1)->toArray();
  472. $list = $list['data'];
  473. foreach ($list as &$v){
  474. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  475. $v['crt_time'] = date('Y-m-d H:i:s',$v['crt_time']);
  476. $v['title'] = $key[$v['dev_eui']];
  477. }
  478. //折线
  479. // $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select('crt_time','device_name','happening_data')->get()->toArray();
  480. return $this->json_return(200,'',$list);
  481. }
  482. //机器云明细,机器云折线
  483. public function deviceChart(Request $request){
  484. //明细
  485. $request = $request->all();
  486. $start = isset($request['start_time']) ? $request['start_time'] : time()-100000;
  487. // $end = isset($request['start_time']) ? $request['start_time'] : time();
  488. $key = DeviceSite::where('site',1)->wherein('title',['温度'])->groupBy('key')->pluck('key')->toArray();
  489. $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select(DB::raw('count(1) as count'),'device_name')->groupBy('dev_eui')->get()->toArray();
  490. if(empty($list)){
  491. $time = DeviceData::max('crt_time');
  492. $s_time = $time-100000;
  493. $list = DeviceData::where('crt_time','>=',$s_time)->wherein('dev_eui',$key)->select(DB::raw('count(1) as count'),'device_name')->groupBy('dev_eui')->get()->toArray();
  494. }
  495. foreach ($list as &$v){
  496. $v['device_name'] = substr(str_replace('广西大王椰','',$v['device_name']),0,19);
  497. }
  498. //折线
  499. // $list = DeviceData::where('crt_time','>=',$start)->wherein('dev_eui',$key)->select('crt_time','device_name','happening_data')->get()->toArray();
  500. return $this->json_return(200,'',$list);
  501. }
  502. public function inout(){
  503. $i = 'inout_key_state';
  504. $s = Redis::get($i);
  505. if($s){
  506. $key = 'inout_key';
  507. $a = Redis::get($key);
  508. if(empty($a)){
  509. $a = 1;
  510. } else{
  511. if($a == 1) $a = 2;
  512. else $a = 1;
  513. }
  514. Redis::set($key,$a);
  515. if($a == 1) $type = '出库';
  516. else $type = '入库';
  517. $detail = [
  518. 'project'=>'南京福猴建材有限公司',
  519. 'type'=>$type,
  520. 'num'=>'2',
  521. 'order_no'=>'PG2023101900818',
  522. '时间'=>date('Y-m-d H:i:s'),
  523. ];
  524. Redis::del($i);
  525. return $this->json_return(200,'',$detail);
  526. }
  527. return $this->json_return(200,'',[]);
  528. }
  529. public function upInout(){
  530. $i = 'inout_key_state';
  531. $s = Redis::set($i,1);
  532. }
  533. public function t(){
  534. $a = bin2hex('PG2023103000182');
  535. echo '00'.$a;
  536. }
  537. //
  538. // public function endDb($db,$start_time){
  539. //
  540. // $end_time = $start_time-86400;
  541. // $start_time =
  542. // $db = $db->where('crt_time','>=',$start_time)->
  543. // return $db;
  544. // }
  545. //分页共用
  546. public function limit($db, $columns, $request)
  547. {
  548. $per_page = $request['page_size'] ?? 9999;
  549. $page = $request['page_index'] ?? 1;
  550. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  551. $data['total'] = $return['total'];
  552. $data['data'] = $return['data'];
  553. return $data;
  554. }
  555. public function post_helper($url, $data)
  556. {
  557. // $data = json_encode($data);
  558. // $header = [
  559. // 'Content-Type:application/json',
  560. // ];
  561. $ch = curl_init();
  562. // curl_setopt($ch, CURLOPT_POST, 1);
  563. curl_setopt($ch, CURLOPT_URL, $url);
  564. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  565. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  566. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  567. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  568. // if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  569. $r = curl_exec($ch);
  570. curl_close($ch);
  571. return $r;
  572. }
  573. }