ZehongTcpController.php 5.04 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11
<?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;
use Illuminate\Support\Facades\Config;
12
use App\Http\Controllers\Auth\SwooleCommandMeTcpController;
冯超鹏's avatar
冯超鹏 committed
13
use App\Http\Controllers\SmsailiyunController;
冯超鹏's avatar
冯超鹏 committed
14
use Illuminate\Support\Facades\Log;
冯超鹏's avatar
冯超鹏 committed
15

冯超鹏's avatar
冯超鹏 committed
16 17 18 19 20 21 22 23 24 25 26
class  ZehongTcpController extends Controller
{

    //获取 tcp 上传数据
    public function swooletcplist($data = '')
    {
        if ($data != '') {
            return $this->substrdata($data);
        }
    }

冯超鹏's avatar
冯超鹏 committed
27
//    解开数据包
冯超鹏's avatar
冯超鹏 committed
28 29
    protected function substrdata($data)
    {
冯超鹏's avatar
冯超鹏 committed
30 31 32 33 34 35 36 37 38
        if (is_string($data) && strlen($data) == 26) {
            $deviceId = substr($data, 0, 15); // 设备编号
            $status = substr($data, 15, 1); // 状态
            $devicenp = substr($data, 16, 3); // 浓度
            $devicefamen = substr($data, 19, 1);
            $devicechongqi = substr($data, 20, 1);
            $devicezijian = substr($data, 21, 1);
            $devicecrc = substr($data, 22, 4);
            $isdata = substr($data, 0, 22);
冯超鹏's avatar
冯超鹏 committed
39
//            return $this->jsonSuccessData([$this->getPrc($isdata),$devicecrc]);
冯超鹏's avatar
冯超鹏 committed
40 41 42 43
            if ($this->getPrc($isdata) == $devicecrc) {
                $data = ['id' => $deviceId, 'status' => $status, 'np' => $devicenp, 'famen' => $devicefamen, 'chongqi' => $devicechongqi, 'zijian' => $devicezijian];
                $totcp = json_decode($this->mysqldb($data), true);
                if ($totcp == "No equipment") {
冯超鹏's avatar
冯超鹏 committed
44 45
                    return $totcp;
                }
冯超鹏's avatar
冯超鹏 committed
46
                $totcpdata = $totcp['devicenum'] . $totcp['device_control'] . $devicechongqi . $devicezijian;
冯超鹏's avatar
冯超鹏 committed
47
                $crcdata = $this->getPrc($totcpdata);
冯超鹏's avatar
冯超鹏 committed
48
                return $totcpdata . $crcdata;
冯超鹏's avatar
冯超鹏 committed
49 50
            }
        } else {
冯超鹏's avatar
冯超鹏 committed
51
            return "Data format error";
冯超鹏's avatar
冯超鹏 committed
52 53 54 55 56 57
        }
    }

    //解出prc
    protected function getPrc($msg)
    {
冯超鹏's avatar
冯超鹏 committed
58
        /**
冯超鹏's avatar
冯超鹏 committed
59
         * If your input string is HEX-formatted you should comment #15 line
冯超鹏's avatar
冯超鹏 committed
60
         * for example: $msg="0123" equals   to 0x0123 without leading 0x
冯超鹏's avatar
冯超鹏 committed
61
         */
冯超鹏's avatar
冯超鹏 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

        //conversion input string to plain HEX string. See comment above
        $msg = bin2hex($msg);
        //pack HEX-formatted $msg to the pure HEX array
        $data = pack('H*', $msg);
        //initialize crc as start HEX value;
        $crc = 0xFFFF;
        //loop $data array of HEX bytes
        for ($i = 0; $i < strlen($data); $i++) {
            //xor bits in the first byte of HEX value
            $crc ^= ord($data[$i]);
            //loop to shift every of 8 bits
            for ($j = 8; $j != 0; $j--) {
                //shift bits to the right and xor with polynome
                if (($crc & 0x0001) != 0) {
                    $crc >>= 1;
                    $crc ^= 0xA001;
                } else $crc >>= 1;
            }
冯超鹏's avatar
冯超鹏 committed
81
        }
冯超鹏's avatar
冯超鹏 committed
82
        //return the result as HEX-formatted string
冯超鹏's avatar
冯超鹏 committed
83
        $data = strrev(sprintf('%04X', $crc));
冯超鹏's avatar
冯超鹏 committed
84 85 86
        $crc1 = strrev(substr($data, 0, 2));
        $crc2 = strrev(substr($data, 2, 2));
        return $crc1 . $crc2;
冯超鹏's avatar
冯超鹏 committed
87
    }
冯超鹏's avatar
冯超鹏 committed
88

冯超鹏's avatar
冯超鹏 committed
89
    //获取上报数据从数据库查询
冯超鹏's avatar
冯超鹏 committed
90 91 92
    public function mysqldb($data)
    {
        if ($data != []) {
93
            // 报警提醒
冯超鹏's avatar
冯超鹏 committed
94 95 96
            $meTcp = new SwooleCommandMeTcpController();
            $alarmData = $data['id'] . '/' . $data['status'] . '/' . $data['np'];
            $meTcp->swooletcplist($alarmData);
冯超鹏's avatar
冯超鹏 committed
97
            $this->smsphone($data);
冯超鹏's avatar
冯超鹏 committed
98
            $datainfo = DB::table('device')
冯超鹏's avatar
冯超鹏 committed
99 100
                ->where('devicenum', '=', $data['id'])
                ->update(['nd' => intval($data['np']), 'devicepolice' => $data['status'] == 0 ? '1' : $data['status'], 'update_time' => time(), 'shutoff_status' => $data['famen']]);
冯超鹏's avatar
冯超鹏 committed
101
            $todata = DB::table('device')
冯超鹏's avatar
冯超鹏 committed
102 103
                ->where('devicenum', '=', $data['id'])
                ->select('devicenum', 'nd', 'device_control', 'devicepolice')
冯超鹏's avatar
冯超鹏 committed
104
                ->first();
冯超鹏's avatar
冯超鹏 committed
105
            if ($todata) {
冯超鹏's avatar
冯超鹏 committed
106
                return json_encode($todata);
冯超鹏's avatar
冯超鹏 committed
107
            } else {
冯超鹏's avatar
冯超鹏 committed
108
                return json_encode("No equipment");
冯超鹏's avatar
冯超鹏 committed
109
            }
冯超鹏's avatar
冯超鹏 committed
110

冯超鹏's avatar
冯超鹏 committed
111 112
        }
    }
冯超鹏's avatar
冯超鹏 committed
113

冯超鹏's avatar
冯超鹏 committed
114
    // 发送手机报警信息
冯超鹏's avatar
冯超鹏 committed
115 116
    public function smsphone($data)
    {
冯超鹏's avatar
冯超鹏 committed
117
        if (Redis::get($data['id']) == '') {
冯超鹏's avatar
冯超鹏 committed
118
            if ($data['status'] > 1) {
冯超鹏's avatar
冯超鹏 committed
119 120 121 122 123 124 125 126 127 128
                $sms = new SmsailiyunController();
                $code1 = json_encode($sms->sendSms());
                $code = json_decode($code1,true);
                Log::channel('slack')->info('短信发送初始化 =>' . $code1 . '设备号=>' . $data['id']);
                if ($code['Message'] == 'OK' && $code['Code'] == 'OK') {
                    Redis::set($data['id'], $code1);
                    Log::channel('slack')->info('短信发送结束 =>' . $code1 . '设备号=>' . $data['id']);
                }
            }
        } else {
冯超鹏's avatar
冯超鹏 committed
129
            if ($data['status'] == 1 || $data['status'] == 0) {
冯超鹏's avatar
冯超鹏 committed
130
                Log::channel('slack')->info('设备正常 =>' . '' . '设备号=>' . $data['id']);
冯超鹏's avatar
冯超鹏 committed
131
                Redis::del($data['id']);
冯超鹏's avatar
冯超鹏 committed
132 133 134
            }
        }
    }
135
}