<?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;

class  ZehongTcpController extends Controller
{

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

    protected function substrdata($data)
    {
        $deviceId = substr($data, 0, 36); // 设备编号
        $cmd = substr($data, 40, 2); // 命令：01:开机 02: 正常交互
        $deviceStatus = substr($data, 42, 2); // 状态
        $value = substr($data, 44, 4); // 浓度值
        $reserved = substr($data, 48, 20); // 预留
        $battery = substr($data, 68, 2); // 电池
        $signal = substr($data, 70, 2); // 信号强度
        $control = substr($data, 72, 2); // 监测控制
        $time = substr($data, 74, 12); // 测量时间
        $alarmLow = substr($data, 86, 4); // 低报阈值
        $alarmHight = substr($data, 90, 4); // 高报阈值
        $cycle = substr($data, 94, 4); // 检测周期
        $timeSampling = substr(98, 4); // 采样时间
        $reserved1 = substr($data, 102, 20); // 预留
        // 设备状态对照表
        /**
         * 1- 正常
         * 2- 检测中
         * 3- 低报
         * 4- 高报
         * 5- 电池欠压
         * 6- 水位报警
         * 7- 传感器故障
         * 8- 泵故障
         * 9- 设备故障
         */
        switch (base_convert($deviceStatus, 16, 10)) {
            case '1':
            case '2':
                $status = 1;
                break;
            case '3':
                $status = 5;
                break;
            case '4':
                $status = 6;
                break;
            case '5':
                $status = 14;
                break;
            case '6':
                $status = 4;
                break;
            case '7':
                $status = 3;
                break;
            default:
                $status = 15;
        }
//        return explode('0',$deviceId);
        return hexdec($deviceId);
    }

}