Equipment2.php 5.9 KB
<?php
/***
 * 匠星设备采集数据
 */

namespace app\api\controller\reservoir;

use app\common\controller\Api;
use app\common\helper\HttpHelper;
use think\Exception;
use think\exception\PDOException;
use think\Request;
use think\Db;
use qx\Qx;
use addons\alisms\controller\Index;

/**
 * Class Equipment
 * @package app\api\controller\reservoir
 */
class Equipment2 extends Api
{
    protected $model = '';
    protected $noNeedLogin = ['getlcc', 'getflow','getRainEquipment', 'abnormal', 'day30', 'getQxList', 'equipmentType', 'getData', 'getDisplacement', 'getAllData', 'getIsotonic', 'getRainfall', 'getWaterLevel', 'getReservoirId', 'getSeepage', 'jx_url_data'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\reservoir\Equipment;
    }


    /**
     * 1. 采集流量硬件数据
     */
    public function getflow()
    {
        $list = Db::name('reservoir_equipment')->where('type', '1')->where('company_type', '2')->select();
        $url = "http://47.108.153.209:2010/api/device/data";
        $upt_succ = 0;
        // 启动事务
        Db::startTrans();
        try {

            foreach ($list as $k => $v) {

                $startTime = Db::name('reservoir_flow')->where(["number" => $v['deviceId'], "reservoir_id" => $v['reservoir_id']])->order('createtime desc')->value('createtime');
                $startTime = $startTime ? $startTime + 1 : strtotime("2024-07-15 10:00:00");//数据是从07-14开始才有的,所以默认这个时间点
                $time_arr = time_to_today_end($startTime);
                if (!empty($time_arr)) {
                    foreach ($time_arr as $k2 => $v2) {
                        if (!empty($v2)) {
                            $s_time = $v2[0];
                            $e_time = $v2[1];
                            $url = "http://47.108.153.209:2010/api/device/data";//. "?deviceId=" .$v['apiKey']. "&startTime=" . $s_time . "&endTime=" . $e_time;
                            $param = ["deviceId" => $v['apiKey'], "startTime" => $s_time, "endTime" => $e_time];
                            $res = HttpHelper::get($url, $param);
                            if (!empty($res)) {
                                $r = json_decode($res, true);
                                if (!empty($r) && $r['code'] == 0) {
                                    $instantaneous_data = $r['data']['L3_SL']['L3_SL_27'];
                                    $cumulative_data = $r['data']['L3_SL']['L3_SL_60'];

                                    if (!empty($instantaneous_data)) {
                                        //$this->warning($v['deviceId'], $instantaneous_data);
                                        foreach ($instantaneous_data as $k3 => $v3) {
                                            $insert_data = [];
                                            $insert_data = [
                                                'reservoir_id' => $v['reservoir_id'],
                                                'instantaneous_value' =>$v3['value'],
                                                'cumulative_value' => $cumulative_data[$k3]['value'],
                                                'createtime' => $v3['reportTime'] / 1000,
                                                'updatetime' => $v3['reportTime'] / 1000,
                                                'reporttime' => $v3['reportTime'],
                                                'number' => $v['apiKey']
                                            ];
                                            $rr = Db::name('reservoir_flow')->insertGetId($insert_data);
                                            if ($rr) {
                                                $upt_succ = $upt_succ + 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Db::commit();
            return "流量新增成功数量:" . $upt_succ;
//            $this->success("水位新增成功数量:" . $upt_succ, $upt_succ);
        } catch (PDOException $e) {
            Db::rollback();
            return $e->getMessage();
//            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            return $e->getMessage();
//            $this->error($e->getMessage());
        }
    }

    public function warning($deviceid, $data)
    {
        $type = $this->model->where('deviceId', $deviceid)->value('type');
        $threshold = Db::name('reservoir_threshold')->where('deviceId', $deviceid)->field('config,reservoir_id')->find();
        $config = json_decode($threshold['config'], true);
        $reservoir = Db::name('reservoir_list')->where('id', $threshold['reservoir_id'])->field('user_mobile,name')->find();

        if (!$config) {
            return;
        }
        //水位
        if ($type == '1') {
            $arr = explode(',', $config['value']);
            sort($arr);
            foreach ($data as $k => $v) {
                if ($v['value'] < $arr[0] || $v['value'] > $arr[1]) {
                    $insert = [
                        'value' => $v['value'],
                        'warning' => $arr[0] . ',' . $arr[1],
                        'equipment_id' => $deviceid,
                        'reservoir_id' => $threshold['reservoir_id'],
                        'createtime' => time(),
                        'reporttime' => $v['reportTime']
                    ];
                    Db::name('reservoir_warning_waterlevel')->insert($insert);
                    // $sms = new Index();
                    // $res = $sms->send_param($reservoir['user_mobile'],'SMS_227742188','云南智慧水库安全云平台',array('password'=>$reservoir['name']));
                }
            }
        }

    }

}