SwooleCommandMeTcpController.php 4.94 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Support\Facades\DB;
use App\Laravue\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\DevicesController;

class  SwooleCommandMeTcpController extends Controller
{
    //获取tcp链接返回数据 //进去redis
    public function swooletcplist($data = '')
    {
        if ($data != '') {
            $datadevice = $this->stringdata($data);
冯超鹏's avatar
冯超鹏 committed
19 20 21 22 23 24 25 26
            Redis::set('dbtype',0);
            if(Redis::get('sbjc:' . $datadevice[0]) == ''){
                Redis::set('sbjc:' . $datadevice[0], $data);
                $this->dbdevice($this->stringdata($data));
            }else if (Redis::get('sbjc:' . $datadevice[0]) != $data){
                $this->dbdevice($this->stringdata($data));
                Redis::set('sbjc:' . $datadevice[0], $data);
            }
冯超鹏's avatar
冯超鹏 committed
27 28 29
            Redis::lpush('police',$data);
            $this->police();
        }
冯超鹏's avatar
冯超鹏 committed
30 31 32 33 34 35
        $dalen = Redis::llen('Devicesdata');//返回队列长度
        if ($dalen > 5000 ) { //当队列值达到巅峰以后切换数据库
            Redis::select(1);//使用备用数据库//释放主数据库
            Redis::set('dbtype',1);
            $this->deleteDbRedis();
        }
冯超鹏's avatar
冯超鹏 committed
36

冯超鹏's avatar
冯超鹏 committed
37 38 39 40 41
        if($dalen > 5000 && Redis::get('dbtype') == 1){
            Redis::select(0);
            Redis::set('dbtype',0);
            $this->deleteDb();
        }
冯超鹏's avatar
冯超鹏 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    }

    //分割字符串
    private function stringdata($data = ''): array
    {
        if ($data != '') {
            return explode('/', $data);
        }
    }

    //查询redis所有的值
    private function devicedata(): array
    {
        $keys = Redis::keys("*");//获取所有的键
        $data = Redis::mget($keys);//获取所有的键的值
        $arrdata = [];
        foreach ($data as $key => $vel) {
            $expdata = explode('/', $vel);
            array_push($arrdata, $expdata);
        }
        return $arrdata;
    }
冯超鹏's avatar
冯超鹏 committed
64 65 66 67 68 69 70 71 72 73 74 75
    //清楚指定数据库 redis
    private function deleteDbRedis (){
        Redis::select(0);
        Redis::flushdb();
        Redis::select(1);
    }

    private function deleteDb(){
        Redis::select(1);
        Redis::flushdb();
        Redis::select(0);
    }
冯超鹏's avatar
冯超鹏 committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89

    //查询出返回值
    private function dbdevice($data = [])
    {
        $Devices = new DevicesController();
        if ($data != []) {
            $devicedata = DB::table('device as d')
                ->where('d.devicenum', '=', $data[0])
                ->leftjoin('device_type as t', 't.tid', '=', 'd.dtype')
                ->leftjoin('gas as g', 'g.id', '=', 'd.status')
                ->leftjoin('danwei as c', 'c.id', '=', 'd.devicemonad')
                ->leftjoin('status as s', 's.id', '=', 'd.devicepolice')
                ->select('d.devicenum', 'd.username', 'd.deviceremark', 'd.devicelinkman', 'd.devicephone', 'd.deviceinfo', 't.tname', 'g.gas', 'c.danwei', 's.status_name')
                ->first();
冯超鹏's avatar
冯超鹏 committed
90 91
            if($devicedata != ''){
                Redis::lPush('Devicesdata', serialize($devicedata));//进入队列进行
冯超鹏's avatar
冯超鹏 committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
            }
        }
    }

    //处理报警
    private function police()
    {
        $davicedata = $this->stringdata(Redis::lpop('police'));
//        处理发送数据报警
        $isdavice = DB::table('reportpolice')
            ->where('devicenumber', '=', $davicedata[0])
            ->select('status', 'endtime')
            ->first();
        $type = json_decode(json_encode($isdavice), true);

        //设备再次报警
        if ($type['endtime'] != '' && $type['status'] == 2 && $davicedata[1] != 1){
            $up = DB::table('reportpolice')
                ->where('devicenumber', '=', $davicedata[0])
                ->update(['endtime'=>'','status' => 1,'policestatus'=> $davicedata[1],'concentration' => $davicedata[2]]);
        }
        if($type['status'] == 1 && $type['endtime'] == '' && $davicedata[1] == 1){
            $up = DB::table('reportpolice')
                ->where('devicenumber', '=', $davicedata[0])
                ->update(['endtime'=>time(),'status' => 2,'policestatus'=> $davicedata[1]]);
        }
        if ($isdavice) {
            //改成报警状态
            if ($davicedata[1] != 1 && $type['endtime'] == '') {
                $up = DB::table('reportpolice')
                    ->where('devicenumber', '=', $davicedata[0])
                    ->update(['concentration' => $davicedata[2], 'policestatus' => $davicedata[1], 'status' => 1]);
            }
        } else {
            if ($davicedata[1] != 1) {
                $add = DB::table('reportpolice')
                    ->insertGetId(['devicenumber' => $davicedata[0], 'starttime' => time(), 'concentration' => $davicedata[2], 'policestatus' => $davicedata[1], 'status' => 1]);
            }

        }
        $updavice = DB::table('device')
            ->where('devicenum', '=', $davicedata[0])
            ->update(['devicepolice' => $davicedata[1], 'nd' => $davicedata[2]]);
    }
}