获取空间数据汇总 七月 10, 2019

 功能描述

获取空间汇总数据(Get Channel Feed Summaries)接口用于从指定空间(channel)中的所有传感器字段读取汇总数据。此方法可以返回JSON或CSV对象。每个汇总数据记录由每小时的总和、平均值、记录数量、标准差、每个字段的最大值和最小值组成。

请求地址

返回JSON对象:

https://api.ubibot.cn/channels/CHANNEL_ID/summary.json?parameters

返回CVS文件格式:

https://api.ubibot.cn/channels/CHANNEL_ID/summary.csv?parameters 

将以上URL中的CHANNEL_ID替换成空间编号。

请求方式

GET

请求参数

名称 类型 是否必须 描述
api_key或account_key String 根据空间权限决定 指定空间的写入或读取密钥,(公开空间不需要密钥) ,或使用account_key可对所有空间进行管理
results Integer 需要查询的条目数。最多8000 。
start Datetime 开始时间,格式 YYYY-MM-DD%20HH:NN:SS
end Datetime 结束时间,格式 YYYY-MM-DD%20HH:NN:SS
timezone String 此请求的时区引用的标识符。
callback String 用于JSONP跨域请求的函数名。

返回说明

返回 200 即成功。Body包含一个JSON对象,其中包含请求格式的汇总数据若返回中is_truncated为true, 表明此请求的时间区间段内还有更多数据,由于最大返回值限制,无法返回全部数据。您可以更改起始值和结束值来缩小范围。

错误:详细信息请参阅错误代码列表。

调用示例

GET https://api.ubibot.cn/channels/123/summary?api_key=XXXXXXXXXXXXX

返回JSON对象,例如:

{"result":"success","server_time":"2019-02-07T13:13:15Z","is_truncated":false,"start":"2019-02-07T02:00:00+00:00","end":"2019-02-07T11:00:00+00:00","timezone":"Europe/London","num_records":10,"results":10,"channel":{"channel_id":"1419","name":"C-1419","field1":"Temperature","field2":"Humidity","field3":"Light","field4":"Voltage","field5":"WIFI RSSI","field6":"Vibration Index","field7":"Knocks","field8":"External Temperature Probe","field9":"Reed Sensor","field10":null,"latitude":"41.7922","longitude":"123.4328","elevation":null,"created_at":"2018-12-07T03:15:40Z","public_flag":"false","user_id":"8D5F3ACB-87A5-4D80-AA5F-FC64E8647990","last_entry_date":"2019-02-07T13:10:26Z","last_entry_id":"50982","vconfig":"{\"field1\":{\"h\":\"0\",\"u\":\"1\"},\"field2\":{\"h\":\"0\",\"u\":\"3\"},\"field3\":{\"h\":\"0\",\"u\":\"4\"},\"field4\":{\"h\":\"0\",\"u\":\"5\"},\"field5\":{\"h\":\"0\",\"u\":\"6\"},\"field6\":{\"h\":\"0\",\"u\":\"7\"},\"field7\":{\"h\":\"0\",\"u\":\"8\"},\"field8\":{\"h\":\"0\",\"u\":\"1\"},\"field9\":{\"h\":\"0\",\"u\":\"9\"}}","full_dump":"0","plan_code":"ubibot_free","username":"cloudleader"},"feeds":[{"created_at":"2019-02-07T11:00:00+00:00","field3":{"sum":0.24,"avg":0.06,"count":4,"sd":0,"max":0.06,"min":0.06},"field1":{"sum":94.515136,"avg":23.628784,"count":4,"sd":0.018257971122225,"max":23.646141,"min":23.603416},"field2":{"sum":40,"avg":10,"count":4,"sd":0,"max":10,"min":10},"field5":{"sum":-160,"avg":-40,"count":4,"sd":0,"max":-40,"min":-40}},{"created_at":"2019-02-07T10:00:00+00:00","field3":{"sum":0.69,"avg":0.062727272727273,"count":11,"sd":0.0044536177141512,"max":0.07,"min":0.06},"field1":{"sum":260.85257,"avg":23.71387,"count":11,"sd":0.035359001690453,"max":23.803696,"min":23.675514},"field2":{"sum":110,"avg":10,"count":11,"sd":0,"max":10,"min":10},"field5":{"sum":-487,"avg":-44.272727272727,"count":11,"sd":4.3294112362875,"max":-40,"min":-49},"field4":{"sum":4.472982,"avg":4.472982,"count":1,"sd":0,"max":4.472982,"min":4.472982}},{"created_at":"2019-02-07T09:00:00+00:00","field3":{"sum":22.48,"avg":11.24,"count":2,"sd":1.74,"max":12.98,"min":9.5},"field1":{"sum":48.264282,"avg":24.132141,"count":2,"sd":0.021362,"max":24.153503,"min":24.110779},"field2":{"sum":20,"avg":10,"count":2,"sd":0,"max":10,"min":10},"field5":{"sum":-80,"avg":-40,"count":2,"sd":0,"max":-40,"min":-40}},{"created_at":"2019-02-07T08:00:00+00:00","field3":{"sum":457.879989,"avg":38.15666575,"count":12,"sd":12.868984722494,"max":57.32,"min":16.779999},"field1":{"sum":294.736777,"avg":24.561398083333,"count":12,"sd":0.27719641719199,"max":25.056076,"min":24.209579},"field2":{"sum":113,"avg":9.4166666666667,"count":12,"sd":0.49300664859163,"max":10,"min":9},"field5":{"sum":-512,"avg":-42.666666666667,"count":12,"sd":4.0892813821284,"max":-40,"min":-51},"field4":{"sum":4.475632,"avg":4.475632,"count":1,"sd":0,"max":4.475632,"min":4.475632}},{"created_at":"2019-02-07T07:00:00+00:00","field3":{"sum":200.879997,"avg":100.4399985,"count":2,"sd":2.6000025,"max":103.040001,"min":97.839996},"field1":{"sum":56.227211,"avg":28.1136055,"count":2,"sd":0.2456705,"max":28.359276,"min":27.867935},"field2":{"sum":16,"avg":8,"count":2,"sd":0,"max":8,"min":8},"field5":{"sum":-90,"avg":-45,"count":2,"sd":4,"max":-41,"min":-49}},{"created_at":"2019-02-07T06:00:00+00:00","field3":{"sum":31344.398927,"avg":2612.0332439167,"count":12,"sd":2824.6816531297,"max":7016.959961,"min":116.199997},"field1":{"sum":378.384835,"avg":31.532069583333,"count":12,"sd":2.9701401037999,"max":35.892273,"min":26.377892},"field2":{"sum":87,"avg":7.25,"count":12,"sd":1.0103629710818,"max":9,"min":6},"field5":{"sum":-491,"avg":-40.916666666667,"count":12,"sd":0.27638539919628,"max":-40,"min":-41},"field4":{"sum":4.487029,"avg":4.487029,"count":1,"sd":0,"max":4.487029,"min":4.487029}},{"created_at":"2019-02-07T05:00:00+00:00","field3":{"sum":197.159996,"avg":98.579998,"count":2,"sd":1.579998,"max":100.159996,"min":97},"field1":{"sum":46.082627,"avg":23.0413135,"count":2,"sd":0.0146865,"max":23.056,"min":23.026627},"field2":{"sum":20,"avg":10,"count":2,"sd":0,"max":10,"min":10},"field5":{"sum":-89,"avg":-44.5,"count":2,"sd":4.5,"max":-40,"min":-49}},{"created_at":"2019-02-07T04:00:00+00:00","field3":{"sum":1133.039978,"avg":94.419998166667,"count":12,"sd":6.4416674668395,"max":115.040001,"min":89.68},"field1":{"sum":277.075209,"avg":23.08960075,"count":12,"sd":0.015318618498007,"max":23.114746,"min":23.069351},"field2":{"sum":120,"avg":10,"count":12,"sd":0,"max":10,"min":10},"field5":{"sum":-535,"avg":-44.583333333333,"count":12,"sd":5.3456888133232,"max":-40,"min":-52},"field4":{"sum":4.469537,"avg":4.469537,"count":1,"sd":0,"max":4.469537,"min":4.469537}},{"created_at":"2019-02-07T03:00:00+00:00","field3":{"sum":153.099998,"avg":76.549999,"count":2,"sd":0.549999,"max":77.099998,"min":76},"field1":{"sum":46.242844,"avg":23.121422,"count":2,"sd":0.0066760000000006,"max":23.128098,"min":23.114746},"field2":{"sum":20,"avg":10,"count":2,"sd":0,"max":10,"min":10},"field5":{"sum":-82,"avg":-41,"count":2,"sd":0,"max":-41,"min":-41}},{"created_at":"2019-02-07T02:00:00+00:00","field3":{"sum":1153.739984,"avg":96.144998666667,"count":12,"sd":50.714404305812,"max":256.320007,"min":73.059998},"field1":{"sum":278.933775,"avg":23.24448125,"count":12,"sd":0.069417701157708,"max":23.352409,"min":23.141449},"field2":{"sum":122,"avg":10.166666666667,"count":12,"sd":0.37267799624997,"max":11,"min":10},"field5":{"sum":-536,"avg":-44.666666666667,"count":12,"sd":7.3861732687201,"max":-40,"min":-66},"field4":{"sum":4.497895,"avg":4.497895,"count":1,"sd":0,"max":4.497895,"min":4.497895}}]}

示例代码(PHP)

<?php
// +----------------------------------------------------------------------
// | UbiBot
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 https://www.ubibot.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: UbiBot <support@ubibot.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// Get Channel Summaries (Get Channel Summaries)
// To view a channel feed, send an HTTP GET replacing CHANNEL_ID with the ID of your channel
// https://api.ubibot.cn/docs/
//----------------------------------
 
header('Content-type:text/html;charset=utf-8');
 
 
//yours API key
$apikey = "XXXXXXXXXXXXXXXXXXXXXXX";
 

$channel_id = 'XXXX;
$url = 'https://api.ubibot.cn/channels/'.$channel_id.'/summary';

$params = array(
		"api_key" => $apikey,// (string) is Read or Write key for this specific channel (no key required for public channels) 
 		"results" => 30,//(integer) Number of entries to retrieve, 8000 max (optional)
// 		"start" => "",//(datetime) Start date in format YYYY-MM-DD%20HH:NN:SS (optional)
// 		"end" => "",//(datetime) End date in format YYYY-MM-DD%20HH:NN:SS (optional)
// 		"timezone" => "",//(string) Identifier from Time Zones Reference for this request (optional)
// 		"callback" => "",//(string) Function name to be used for JSONP cross-domain requests (optional)
);
$paramstring = http_build_query($params);
$content = getcurl($url,$paramstring);
$result = json_decode($content,true);

if($result){
    if($result['result']=='success'){
    	
    	$result['channel']['vconfig'] = json_decode($result['channel']['vconfig'],true);
    	//Filter channels that need to be displayed
    	$filter_fields = array('field1','field2','field3','field4','field5','field6','field7','field8','field9','field10');
    	//Sensor unit
    	$unit = array('摄氏温度','华氏温度','湿度','光照','电压','WIFI信号强度','震动幅度','敲击','门磁','土壤绝对湿度');
    	
    	$list = array();
    	foreach ($filter_fields as $value) {
    		$list[$value]['name'] = $result['channel'][$value];
    		
    		if (isset($result['channel']['vconfig'][$value])) {
    			$list[$value]['show'] = $result['channel']['vconfig'][$value]['h']?0:1;
    			$list[$value]['unit'] = $unit[$result['channel']['vconfig'][$value]['u']];
    		} else {
    			$list[$value]['show'] = 0;
    			$list[$value]['unit'] = null;
    		}
    	}
    	
    	foreach ($result['feeds'] as $value) {
    		
    		foreach ($value as $k => $val) {
    			$tmp = array();
    			if (in_array($k, $filter_fields)) {
    				$val['created_at'] = $value['created_at'];
    				$list[$k]['data'][] = $val;
    			}
    		}
    	}
    }else{
        echo $result['errorCode'].":".$result['desp'];
    }
}else{
    echo "failed";
}
//**************************************************
 
 
 
 
 
/**
 * @param  string $url
 * @param  string $params 
 * @param  int $ispost 
 * @return  string
 */
function getcurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}

?>


<html>
<head>
<style type="text/css">
table
{
	border-collapse: collapse;
	border: none;
	width: 100%;
}
td,th
{
	border: solid #000 1px;
	text-align:center;
}
li{ display:inline}
</style>
</head>

<body>
<table>
<tr>
<th>KEY</th>
<th>传感器名</th>
<th>是否显示</th>
<th>单位</th>
<th>数据集</th>
</tr>
 	 	<?php foreach ($list as $key => $value) :?>
 	 		<tr>
 	 			<td><?php echo $key;?></td>
				<td><?php echo $value['name'];?></td>
				<td><?php echo $value['show']?'是':'否';?></td>
				<td><?php echo $value['unit'];?></td>
				<td style="text-align: left;">
				<?php if (empty($value['data'])) :?>
					所选时段暂无数据
				<?php else :?>
					<?php foreach ($value['data'] as $val):?>
					<ul>
						<li>avg:<?php echo $val['avg'];?></li>
						<li style="margin-left:20px;">sum:<?php echo $val['sum'];?></li>
						<li style="margin-left:20px;">count:<?php echo $val['count'];?></li>
						<li style="margin-left:20px;">sd:<?php echo $val['sd'];?></li>
						<li style="margin-left:20px;">min:<?php echo $val['min'];?></li>
						<li style="margin-left:20px;">max:<?php echo $val['max'];?></li>
						<li style="margin-left:20px;">created_at:<?php echo $val['created_at'];?></li>
					</ul>
					<?php endforeach;?>
				<?php endif;?>
				</td>
 	 		</tr>
 	 	<?php endforeach;?>
 		</table>
 	 </body>
 </html>