Files
EdgeManager/app/EDataCapture/EDataCapture.php
2022-07-14 00:13:57 +08:00

127 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace EdgeManager\EDataCapture;
class EDataCapture {
function __construct(
protected $dbconn,
protected $post = NULL,
protected $get = NULL,
public $check_res = NULL,
protected $working_subclass = NULL,
protected $code_type = [],
protected $data = []
) {
if (!is_null($this -> post)) {
if (!in_array(
$this -> post -> param -> working_subclass,
ENodeConfigure::get_working_subclasses($this -> dbconn)
)) {
$this -> check_res = 'WRONG_WORKING_SUBCLASS';
return;
} else {
$this -> working_subclass = $this -> post -> param -> working_subclass;
}
$res = pg_fetch_all(pg_query($this -> dbconn,
"SELECT code, type
FROM hf_mes_scada_data_capture_node_configure"
));
$code_type = &$this -> code_type;
array_walk($res, function(&$v, $k) use (&$code_type) {
$code_type[$v['code']] = $v['type'];
});
foreach ($this -> post -> param -> data as &$row) {
if (($code_type[$row -> code]) === 'float' and is_int($row -> value))
$row -> value = (float) number_format((float) $row -> value, 1, '.', '');
$check_func = 'is_' . $code_type[$row -> code];
if (!$check_func($row -> value)) {
$this -> check_res = 'MISMATCH_TYPE';
return;
} else {
$this -> data[$row -> code][] = [
'value' => $row -> value,
'device_code' => $row -> device_code ?? NULL,
'batch' => $row -> batch ?? NULL
];
}
}
unset($row);
}
}
function set_node_data() {
pg_query($this -> dbconn, "BEGIN");
foreach ($this -> data as $code => $value) {
foreach (array_chunk($value, 6710885, true) as $chunk) {
$sql_head = sprintf(
'INSERT INTO "hf_mes_scada_data_capture_node_data_%s" (code, v_%s, device_code, batch)
VALUES',
$this -> working_subclass,
$this -> code_type[$code]
);
foreach ($chunk as $row) {
$sql_values[] = sprintf(
"('%s', '%s', '%s', '%s')",
$code,
$row['value'] === false ? 'f' : $row['value'],
$row['device_code'] ?? NULL,
$row['batch'] ?? NULL
);
}
$res[] = pg_query($this -> dbconn, $sql_head . implode(',', $sql_values));
unset($row, $sql_values);
}
unset($chunk);
}
unset($code, $value);
if (in_array(false, $res)) {
pg_query($this -> dbconn, "ROLLBACK");
return false;
} else {
pg_query($this -> dbconn, "COMMIT");
return true;
}
}
function get_node_data() {
// 先把code和name的对应关系拿到快于OUTER JOIN
$res = pg_fetch_all(pg_query($this -> dbconn, sprintf(
"SELECT code, name
FROM hf_mes_scada_data_capture_node_configure
WHERE working_subclass = '%s'",
$this -> get['working_subclass']
)));
$code_name = [];
array_walk($res, function(&$v, $k) use (&$code_name) {
$code_name[$v['code']] = $v['name'];
});
$sql_cmd = sprintf(
'SELECT id, code, COALESCE(v_string, v_int::text, v_float::text, v_bool::text) AS value, device_code, batch, capture_time
FROM "hf_mes_scada_data_capture_node_data_%s"
WHERE capture_time >= \'%s\' AND capture_time <= \'%s\'',
$this -> get['working_subclass'],
date("Y-m-d H:i:s", $this -> get['start_time'] / 1000),
date("Y-m-d H:i:s", $this -> get['end_time'] / 1000)
);
if (isset($this -> get['code']))
$sql_cmd .= sprintf(" AND code = '%s'", $this -> get['code']);
$res = pg_fetch_all(pg_query($this -> dbconn, $sql_cmd));
if (count($res) > 2e6)
return "TOO_MANY";
array_walk($res, function(&$v, $k) use ($code_name) {
$v['name'] = $code_name[$v['code']];
});
return $res;
}
}