$factor_meter = (int) $row['factor_meter'];
    $lineloss_meter = (int) $row['lineloss_meter'];
    $tmp = array('unit_meter' => $unitmeter, 'count_point_meter' => $countpoint, 'factor_meter' => $factor_meter, 'lineloss_meter' => $lineloss_meter);
    $data['meter'] = $tmp;
}
$sql = 'SELECT `import_kw`, `export_kw`, `q1`, `q2`, `q3`, `q4`, `rate1`, `rate2`, 
				`rate3`, `rate4`, `rate5`, `rate6`, `cd1`, `cd2`,`full_time`, `full_time_d`  
				FROM `history_value` 
    		WHERE `full_time_d` > ' . $st . ' AND `serial_meter` = "' . $serial . '" 
    		ORDER BY `full_time_d` ASC LIMIT 0,1';
$result = mysql_query($sql) or die('0');
while ($row = mysql_fetch_array($result)) {
    $tmp = array('import_kw' => getdecimalnumber($unitmeter, (double) $row['import_kw'], $countpoint), 'rate1' => getdecimalnumber($unitmeter, (double) $row['rate1'], $countpoint), 'rate2' => getdecimalnumber($unitmeter, (double) $row['rate2'], $countpoint), 'rate3' => getdecimalnumber($unitmeter, (double) $row['rate3'], $countpoint), 'q1' => getdecimalnumber($unitmeter, (double) $row['q1'], $countpoint), 'q2' => getdecimalnumber($unitmeter, (double) $row['q2'], $countpoint), 'cd1' => getdecimalnumber($unitmeter, (double) $row['cd1'], $countpoint), 'export_kw' => getdecimalnumber($unitmeter, (double) $row['export_kw'], $countpoint), 'rate4' => getdecimalnumber($unitmeter, (double) $row['rate4'], $countpoint), 'rate5' => getdecimalnumber($unitmeter, (double) $row['rate5'], $countpoint), 'rate6' => getdecimalnumber($unitmeter, (double) $row['rate6'], $countpoint), 'q3' => getdecimalnumber($unitmeter, (double) $row['q3'], $countpoint), 'q4' => getdecimalnumber($unitmeter, (double) $row['q4'], $countpoint), 'cd2' => getdecimalnumber($unitmeter, (double) $row['cd2'], $countpoint), 'full_time' => $row['full_time']);
    $data['dauky'] = $tmp;
}
$sql = 'SELECT `import_kw`, `export_kw`, `q1`, `q2`, `q3`, `q4`, `rate1`, `rate2`, 
				`rate3`, `rate4`, `rate5`, `rate6`, `cd1`, `cd2`,`full_time`, `full_time_d`  
				FROM `history_value` 
    		WHERE `full_time_d` > ' . $et . ' AND `serial_meter` = "' . $serial . '" 
    		ORDER BY `full_time_d` ASC LIMIT 0,1';
$result = mysql_query($sql) or die('0');
while ($row = mysql_fetch_array($result)) {
    $tmp = array('import_kw' => getdecimalnumber($unitmeter, (double) $row['import_kw'], $countpoint), 'rate1' => getdecimalnumber($unitmeter, (double) $row['rate1'], $countpoint), 'rate2' => getdecimalnumber($unitmeter, (double) $row['rate2'], $countpoint), 'rate3' => getdecimalnumber($unitmeter, (double) $row['rate3'], $countpoint), 'q1' => getdecimalnumber($unitmeter, (double) $row['q1'], $countpoint), 'q2' => getdecimalnumber($unitmeter, (double) $row['q2'], $countpoint), 'cd1' => getdecimalnumber($unitmeter, (double) $row['cd1'], $countpoint), 'export_kw' => getdecimalnumber($unitmeter, (double) $row['export_kw'], $countpoint), 'rate4' => getdecimalnumber($unitmeter, (double) $row['rate4'], $countpoint), 'rate5' => getdecimalnumber($unitmeter, (double) $row['rate5'], $countpoint), 'rate6' => getdecimalnumber($unitmeter, (double) $row['rate6'], $countpoint), 'q3' => getdecimalnumber($unitmeter, (double) $row['q3'], $countpoint), 'q4' => getdecimalnumber($unitmeter, (double) $row['q4'], $countpoint), 'cd2' => getdecimalnumber($unitmeter, (double) $row['cd2'], $countpoint), 'full_time' => $row['full_time']);
    $data['cuoiky'] = $tmp;
}
echo json_encode($data);
CLOSE_DB();
unset($sql, $result, $rs);
?>
 
        $ppkc = getdecimalnumber(0, $row['ppk_pha_c'], 2);
        $ppks = getdecimalnumber(0, $row['ppk_sum'], 2);
    }
    if ($row['pbk_pha_a'] > 1000) {
        $donvipbk = '(MVA)';
        $pbka = getdecimalnumber(1, $row['pbk_pha_a'], 2);
        $pbkb = getdecimalnumber(1, $row['pbk_pha_b'], 2);
        $pbkc = getdecimalnumber(1, $row['pbk_pha_c'], 2);
        $pbks = getdecimalnumber(1, $row['pbk_sum'], 2);
    } else {
        $pbka = getdecimalnumber(0, $row['pbk_pha_a'], 2);
        $pbkb = getdecimalnumber(0, $row['pbk_pha_b'], 2);
        $pbkc = getdecimalnumber(0, $row['pbk_pha_c'], 2);
        $pbks = getdecimalnumber(0, $row['pbk_sum'], 2);
    }
    $tmp = array('u_pha_a' => $ua, 'u_pha_b' => $ub, 'u_pha_c' => $uc, 'i_pha_a' => getdecimalnumber(0, $row['i_pha_a'], 2), 'i_pha_b' => getdecimalnumber(0, $row['i_pha_b'], 2), 'i_pha_c' => getdecimalnumber(0, $row['i_pha_c'], 2), 'f_pha_a' => getdecimalnumber(0, $row['f_pha_a'], 2), 'f_pha_b' => getdecimalnumber(0, $row['f_pha_b'], 2), 'f_pha_c' => getdecimalnumber(0, $row['f_pha_c'], 2), 'cosphi_a' => getdecimalnumber(0, $row['cosphi_a'], 2), 'cosphi_b' => getdecimalnumber(0, $row['cosphi_b'], 2), 'cosphi_c' => getdecimalnumber(0, $row['cosphi_c'], 2), 'p_pha_a' => $pa, 'p_pha_b' => $pb, 'p_pha_c' => $pc, 'ppk_pha_a' => $ppka, 'ppk_pha_b' => $ppkb, 'ppk_pha_c' => $ppkc, 'pbk_pha_a' => $pbka, 'pbk_pha_b' => $pbkb, 'pbk_pha_c' => $pbkc, 'p_sum' => $ps, 'ppk_sum' => $ppks, 'pbk_sum' => $pbks, 'cosphi_mean' => getdecimalnumber(0, $row['cosphi_mean'], 2), 'full_time' => $row['full_time_h'], 'unitu' => $donviu, 'unitp' => $donvip, 'unitppk' => $donvippk, 'unitpbk' => $donvipbk);
    array_push($data, $tmp);
}
$du = array();
$di = array();
$df = array();
if ($default) {
    $di1 = convertarray($di1);
    $di2 = convertarray($di2);
    $di3 = convertarray($di3);
    $du1 = convertarray($du1);
    $du2 = convertarray($du2);
    $du3 = convertarray($du3);
    $df1 = convertarray($df1);
    $df2 = convertarray($df2);
    $df3 = convertarray($df3);
function get_cuoiky($serial, $lmstr, $st, $et, $unitmeter, $countpoint)
{
    $sql = 'SELECT `import_kw`, `export_kw`, `q1`, `q2`, `q3`, `q4`, `rate1`, `rate2`, 
					`rate3`, `rate4`, `rate5`, `rate6`, `cd1`, `cd2`,`full_time`, `full_time_d`  
				FROM `history_value` 
	    		WHERE `full_time_d` > ' . $et . ' AND `serial_meter` = "' . $serial . '" 
	    		ORDER BY `full_time_d` ASC LIMIT 0,1';
    $result = mysql_query($sql) or die('0');
    //$tmp=array();
    $tmp = new stdClass();
    while ($row = mysql_fetch_array($result)) {
        $tmp->{'import_kw'} = getdecimalnumber($unitmeter, (double) $row['import_kw'], $countpoint);
        $tmp->{'rate1'} = getdecimalnumber($unitmeter, (double) $row['rate1'], $countpoint);
        $tmp->{'rate2'} = getdecimalnumber($unitmeter, (double) $row['rate2'], $countpoint);
        $tmp->{'rate3'} = getdecimalnumber($unitmeter, (double) $row['rate3'], $countpoint);
        $tmp->{'cd1'} = getdecimalnumber($unitmeter, (double) $row['cd1'], $countpoint);
        $tmp->{'q1'} = getdecimalnumber($unitmeter, (double) $row['q1'], $countpoint);
        $tmp->{'q2'} = getdecimalnumber($unitmeter, (double) $row['q2'], $countpoint);
        $tmp->{'export_kw'} = getdecimalnumber($unitmeter, (double) $row['export_kw'], $countpoint);
        $tmp->{'rate4'} = getdecimalnumber($unitmeter, (double) $row['rate4'], $countpoint);
        $tmp->{'rate5'} = getdecimalnumber($unitmeter, (double) $row['rate5'], $countpoint);
        $tmp->{'rate6'} = getdecimalnumber($unitmeter, (double) $row['rate6'], $countpoint);
        $tmp->{'cd2'} = getdecimalnumber($unitmeter, (double) $row['cd2'], $countpoint);
        $tmp->{'q3'} = getdecimalnumber($unitmeter, (double) $row['q3'], $countpoint);
        $tmp->{'q4'} = getdecimalnumber($unitmeter, (double) $row['q4'], $countpoint);
        $tmp->{'full_time'} = $row['full_time'];
    }
    //print_r($tmp);
    $localvalue['cuoiky'] = $tmp;
    $localvalue['sourcecuoiky'] = 'h1online';
    return $localvalue;
}
mysql_query("SET NAMES utf8");
$unitmeter = 0;
$countpoint = 3;
$factor_meter = 0;
$lineloss_meter = 0;
$sql = 'SELECT `import_kw`, `export_kw`, `q1`, `q2`, `q3`, `q4`, `rate1`, `rate2`, 
					`rate3`, `rate4`, `rate5`, `rate6`, `cd1`, `cd2`,`full_time`, `full_time_d`  
				FROM `history_value` 
	    		WHERE `full_time_d` > ' . $et . ' AND `serial_meter` = "' . $serial . '" 
	    		ORDER BY `full_time_d` ASC LIMIT 0,1';
$result = mysql_query($sql) or die('0');
//$tmp=array();
$tmp = new stdClass();
while ($row = mysql_fetch_array($result)) {
    $tmp->{'rate1'} = (double) $row['rate1'];
    $tmp->{'rate11'} = getdecimalnumber($unitmeter, (double) $row['rate1'], $countpoint);
    $tmp->{'rate111'} = number_format((double) $row['rate1'], 3, ',', ' ');
    // $tmp ->{'import_kw'} = getdecimalnumber($unitmeter,(double)$row['import_kw'],$countpoint);
    // $tmp ->{'rate1'} = getdecimalnumber($unitmeter,(double)$row['rate1'],$countpoint);
    // $tmp ->{'rate2'} = getdecimalnumber($unitmeter,(double)$row['rate2'],$countpoint);
    // $tmp ->{'rate3'} = getdecimalnumber($unitmeter,(double)$row['rate3'],$countpoint);
    // $tmp ->{'cd1'} = getdecimalnumber($unitmeter,(double)$row['cd1'],$countpoint);
    // $tmp ->{'q1'} = getdecimalnumber($unitmeter,(double)$row['q1'],$countpoint);
    // $tmp ->{'q2'} = getdecimalnumber($unitmeter,(double)$row['q2'],$countpoint);
    // $tmp ->{'export_kw'} = getdecimalnumber($unitmeter,(double)$row['export_kw'],$countpoint);
    // $tmp ->{'rate4'} = getdecimalnumber($unitmeter,(double)$row['rate4'],$countpoint);
    // $tmp ->{'rate5'} = getdecimalnumber($unitmeter,(double)$row['rate5'],$countpoint);
    // $tmp ->{'rate6'} = getdecimalnumber($unitmeter,(double)$row['rate6'],$countpoint);
    // $tmp ->{'cd2'} = getdecimalnumber($unitmeter,(double)$row['cd2'],$countpoint);
    // $tmp ->{'q3'} = getdecimalnumber($unitmeter,(double)$row['q3'],$countpoint);
    // $tmp ->{'q4'} = getdecimalnumber($unitmeter,(double)$row['q4'],$countpoint);