Files

244 lines
9.3 KiB
PHP

<?php
session_start();
if( !defined('IN') ) die('bad request!');
include_once( AROOT . 'controller' . DS . 'app.class.php');
include_once( AROOT . 'lib' . DS . 'phpMQTT.php');
header("Content-Type: text/html; charset=utf-8");
class apiController extends appController
{
public function __construct()
{
parent::__construct();
}
// 发送错误结果集
public function send_error($number,$msg)
{
$obj = array();
$obj['code'] = intval($number);
$obj['result'] = $msg;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 发送结果集
public function send_result($data)
{
$obj = array();
$obj['code'] = 200;
$obj['result'] = $data;
if( g('API_EMBED_MODE') == 1 ){
return json_encode($obj);
}else{
header("Content-Type: application/json");
die(json_encode($obj));
}
}
// 接收工控机发送过来的json RawData
public function computerGateRawData()
{
// 要带工控机的UUID
$type = 1;
$is_analysis = 1;
$current_time = date("Y-m-d H:i:s",time());
$json_string = file_get_contents('php://input');
$json_array = json_decode($json_string,true);
$machineId = $json_array['machineId'];
$sql_device_id = prepare("select id from device where sn = ?s limit 1",array($machineId));
$device_id = get_var($sql_device_id);
$did_sql = prepare("select f_did from device where sn = ?s limit 1",array($machineId));
$did = get_var($did_sql);
foreach ($json_array as $key => $value)
{
if($key == 'A相电压'){$result_json['va'] = $value;}
if($key == 'B相电压'){$result_json['vb'] = $value;}
if($key == 'C相电压'){$result_json['vc'] = $value;}
if($key == 'A相电流'){$result_json['ia'] = $value;}
if($key == 'B相电流'){$result_json['ib'] = $value;}
if($key == 'C相电流'){$result_json['ic'] = $value;}
if($key == '总有功功率'){$result_json['tap'] = $value;}
if($key == '总无功功率'){$result_json['trp'] = $value;}
if($key == '当前总有功电能'){$result_json['ctae'] = $value;}
if($key == '电导率值'){$result_json['cv'] = $value;}
if($key == '电阻率值'){$result_json['rv'] = $value;}
if($key == '电导率温度'){$result_json['ct'] = $value;}
if($key == '电导率TDS'){$result_json['tds'] = $value;}
if($key == '雨量值'){$result_json['rfv'] = $value;}
if($key == '雨雪值'){$result_json['rsv'] = $value;}
if($key == '液位值'){$result_json['llv'] = $value;}
if($key == 'pH值'){$result_json['phv'] = $value;}
if($key == 'pH温度'){$result_json['pht'] = $value;}
if($key == 'pH电流'){$result_json['phi'] = $value;}
if($key == '氨氮值'){$result_json['nh4v'] = $value;}
if($key == '氨氮温度'){$result_json['nh4t'] = $value;}
if($key == '氨氮电流'){$result_json['nh4i'] = $value;}
if($key == '主污泵状态'){$result_json['mps'] = $value;}
if($key == '主污泵运行次数'){$result_json['mprc'] = $value;}
if($key == '备污泵状态'){$result_json['sps'] = $value;}
if($key == '备污泵运行次数'){$result_json['sprc'] = $value;}
if($key == '雨水泵状态'){$result_json['rps'] = $value;}
if($key == '雨水泵运行次数'){$result_json['rprc'] = $value;}
if($key == '闸门上升状态'){$result_json['gus'] = $value;}
if($key == '闸门上升次数'){$result_json['gurc'] = $value;}
if($key == '闸门下降状态'){$result_json['gds'] = $value;}
if($key == '闸门下降次数'){$result_json['gdrc'] = $value;}
if($key == '闸门动作总次数'){$result_json['tgac'] = $value;}
if($key == '自动按钮状态'){$result_json['abs'] = $value;}
if($key == '主污泵过载'){$result_json['mpo'] = $value;}
if($key == '备污泵过载'){$result_json['spo'] = $value;}
if($key == '雨水泵过载'){$result_json['rpo'] = $value;}
if($key == '闸门过载'){$result_json['go'] = $value;}
if($key == '急停按钮报警'){$result_json['esba'] = $value;}
if($key == '急停按钮'){$result_json['esb'] = $value;}
if($key == '手自动按钮'){$result_json['mab'] = $value;}
if($key == '复位按钮'){$result_json['rb'] = $value;}
if($key == '主污泵启动按钮'){$result_json['mpsb'] = $value;}
if($key == '主污泵停止按钮'){$result_json['mpcb'] = $value;}
if($key == '主污泵过载报警'){$result_json['mpoa'] = $value;}
if($key == '备污泵启动按钮'){$result_json['spsb'] = $value;}
if($key == '备污泵停止按钮'){$result_json['spcb'] = $value;}
if($key == '备污泵过载报警'){$result_json['spoa'] = $value;}
if($key == '雨水泵启动按钮'){$result_json['rpsb'] = $value;}
if($key == '雨水泵停止按钮'){$result_json['rpcb'] = $value;}
if($key == '雨水泵过载报警'){$result_json['rpoa'] = $value;}
if($key == '闸门上升按钮'){$result_json['gub'] = $value;}
if($key == '闸门下降按钮'){$result_json['gdb'] = $value;}
if($key == '闸门停止按钮'){$result_json['gsb'] = $value;}
if($key == '闸门过载报警'){$result_json['goa'] = $value;}
if($key == '闸门上限'){$result_json['gul'] = $value;}
if($key == '闸门下限'){$result_json['gll'] = $value;}
if($key == '浮球上限'){$result_json['ful'] = $value;}
if($key == '浮球下限'){$result_json['fll'] = $value;}
if($key == '主污泵接触器'){$result_json['mpc'] = $value;}
if($key == '备污泵接触器'){$result_json['spc'] = $value;}
if($key == '雨水泵接触器'){$result_json['rpc'] = $value;}
if($key == '闸门上升接触器'){$result_json['guc'] = $value;}
if($key == '闸门下降接触器'){$result_json['gdc'] = $value;}
if($key == '复位按钮指示灯'){$result_json['rbil'] = $value;}
if($key == '自动运行指示灯'){$result_json['aril'] = $value;}
if($key == '手动运行指示灯'){$result_json['mril'] = $value;}
if($key == '主污泵运行指示灯'){$result_json['mpril'] = $value;}
if($key == '备污泵运行指示灯'){$result_json['spril'] = $value;}
if($key == '雨水泵运行指示灯'){$result_json['rpril'] = $value;}
if($key == '闸门上升指示灯'){$result_json['guil'] = $value;}
if($key == '闸门下降指示灯'){$result_json['gdil'] = $value;}
if($key == 'machineId'){$result_json['machineId'] = $value;}
if($key == '超声波液位安装高度'){$result_json['d210'] = $value;}
if($key == '主泵触发液位上限'){$result_json['d212'] = $value;}
if($key == '主泵触发液位下限'){$result_json['d214'] = $value;}
if($key == '备泵触发液位上限'){$result_json['d230'] = $value;}
if($key == '备泵触发液位下限'){$result_json['d232'] = $value;}
if($key == '闸门开启雨量触发阈值'){$result_json['d216'] = $value;}
if($key == '本地0远程1'){$result_json['m130'] = $value;}
if($key == 'PLC液位主污泵液位控制策略生效开关'){$result_json['m500'] = $value;}
if($key == 'PLC雨量闸门控制策略生效开关'){$result_json['m501'] = $value;}
if($key == 'PLC液位备污泵液位控制策略生效开关'){$result_json['m502'] = $value;}
if($key == 'PLC雨量清零策略生效开关'){$result_json['m503'] = $value;}
if($key == 'PLC雨雪为0时清空雨量策略生效开关'){$result_json['m504'] = $value;}
}
if($did==""){$did = "1000";} //bug fix
$result_json['did'] = $did;
self::mqtt_pub($did,$result_json);
if($json_string)
{
$sql = prepare("insert into rawdata_new (id,device_id,rawdata,is_analysis,type,created_at) values (null,?i,?s,?i,?i,?s)",array($device_id,$json_string,$is_analysis,$type,$current_time));
$impact = run_sql($sql);
if($impact)
{
$result = 'success';
return self::send_result($result);
}else
{
$result = 'false';
return self::send_error(0,$result);
}
}
else
{
$result = 'false';
return self::send_error(0,$result);
}
}
// 返回当前设备的模式
public function getGateDeviceCurrentModelType()
{
// 带工控机的UUID/设备编号
$machineId = $_GET['machineId'];
$sql = prepare("select strategy.name as strategy_name,device.control_model from device left join strategy on device.strategy_id = strategy.id where device.sn = ?s limit 1",array($machineId));
$data= get_line($sql);
if(!empty($data))
{
if($data['control_model']==1){$result['control_model']='手动模式';}else{$result['control_model']='自动模式';}
$result['strategy_name'] = $data['strategy_name'];
}
return self::send_result($result);
}
// 补全mid和did的对应关系
public function mid_did()
{
$mid = v('mid');
$did = v('did');
$current_time = date("Y-m-d H:i:s",time());
// 修改device表中mid
$sql = prepare("update device set sn = ?s where f_did = ?s",array($mid,$did));
run_sql($sql);
// 存入一个新表,进行存储
$sql_insert = prepare("insert into mid_did (id,mid,did,created_at) values (null,?s,?i,?s)",array($mid,$did,$current_time));
run_sql($sql_insert);
}
static function mqtt_pub($did,$json)
{
$server = 'mqtt.gpic.opmonitor.com';
$port = 1883;
$username = '';
$password = '';
$client_id = 'http-2-mqtt';
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish('gpic_d/'.$did, json_encode($json), 0, false);
$mqtt->publish('gpic_d/', 'http-2-mqtt', 0, false);
$mqtt->close();
} else {
echo "Timeout!\n";
}
}
}