Equipment2.php 4.3 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-06-01 00: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) {
                                    $water_data = $r['data']['L3_SL'][$v['deviceId']];
                                    if (!empty($water_data)) {
                                        //$this->warning($v['deviceId'], $water_data);
                                        foreach ($water_data as $k3 => $v3) {
                                            $insert_data = [];
                                            $insert_data = [
                                                'reservoir_id' => $v['reservoir_id'],
                                                'value' => $v3['value'],
                                                'createtime' => $v3['reportTime'] / 1000,
                                                'updatetime' => $v3['reportTime'] / 1000,
                                                'reporttime' => $v3['reportTime'],
                                                'number' => $v['deviceId']
                                            ];
                                            $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());
        }
    }



}