获取空间数据 九月 29, 2017

功能描述

获取空间数据(Get Channel Feed)接口是用来读取指定空间(channel)所存储的传感器感知数据。

注意: 由于设备的传感器采样率每个相对独立,例如温湿度5分钟采集一次,光照10分钟采集一次,所以接口返回的数据中不是每个时间点timestamp都有对应传感器数据值。 

请求方式

HTTP GET

请求地址

GET https://api.ubibot.cn/channels/CHANNEL_ID/feeds.json  

将以上URL中的CHANNEL_ID替换成空间编号。若需要返回数据为CSV格式:

GET https://api.ubibot.cn/channels/CHANNEL_ID/feeds.csv

请求参数

名称 类型 是否必须 描述
api_key或 account_key String 根据空间权限决定 指定空间的写入或读取密钥,(公开空间不需要密钥) ,或使用account_key可对所有空间进行管理
entry_id String 若为true,则返回中增加entry_id, 默认为false(不返回)
results Integer 需要查询的条目数。最多8000 。
start Datetime 检索开始时间,格式 YYYY-MM-DD%20HH:NN:SS
end Datetime 检索结束时间,格式 YYYY-MM-DD%20HH:NN:SS
field String 仅返回指定field数据,例如field=field2, 与sum, average选项不能同时使用
status Boolean Include status updates in feed by setting
timezone String 检索的时区
min Decimal 返回数据的最小值
max Decimal 返回数据的最大值
sum String 按指定分钟数作为时间间隔返回合计数值。 仅支持有效值为:10, 15, 20, 30, 60, 240, 720, 1440, “daily” 。
average String 按指定分钟数作为时间间隔返回平均数值。 仅支持有效值为:10, 15, 20, 30, 60, 240, 720, 1440, “daily” 。
round Integer Round to this many decimal places (optional)
callback String JSONP 跨域请求的方法名

返回说明

  • 若返回中is_truncated为true, 表明此请求的时间区间段内还有更多数据,由于最大返回值限制,无法返回全部数据。
  • 返回中timezone为默认或指定请求的时区名。
  • channel_id为每个空间的唯一编号

调用示例

GET https://api.ubibot.cn/channels/CHANNEL_ID/feeds.csv?api_key=xxxxx-xxxxx-xxxxx-xxxx

GET https://api.ubibot.cn/channels/CHANNEL_ID/feeds.csv?account_key=xxxxx-xxxxx-xxxxx-xxxx

{"result":"success","server_time":"2017-09-29T09:20:28Z","is_truncated":false,"start":null,"end":"2017-09-29T09:20:28+00:00","sum":null,"average":null,"min":null,"max":null,"timezone":"Asia/Shanghai","num_records":5,"results":5,"channel":{"channel_id":"390","name":"\u7814\u53d1\u4e2d\u5fc31","field1":"\u6e29\u5ea6","field2":"\u6e7f\u5ea6","field3":"\u5149\u7167","field4":"\u7535\u91cf","field5":"\u4fe1\u53f7\u5f3a\u5ea6","field6":"\u52a0\u901f\u5ea6","field7":"\u6572\u51fb","field8":"\u5916\u63a5\u6e29\u5ea6","field9":"\u95e8\u78c1","field10":null,"latitude":null,"longitude":null,"elevation":null,"created_at":"2017-07-06T08:11:46Z","public_flag":"false","user_id":"A874A453-87B0-482D-B8C5-EC45FC1B53D0","last_entry_date":"2017-09-29T09:18:03Z","last_entry_id":"4030876","vconfig":"{\"field1\":{\"h\":\"0\"},\"field2\":{\"h\":\"0\"},\"field3\":{\"h\":\"0\"},\"field4\":{\"h\":\"0\"},\"field5\":{\"h\":\"0\"},\"field6\":{\"h\":\"0\"},\"field7\":{\"h\":\"0\"},\"field8\":{\"h\":\"0\"},\"field9\":{\"h\":\"0\"},\"field10\":{\"h\":\"0\"}}","username":"wangmao"},"feeds":[{"created_at":"2017-09-29T17:18:01+08:00","field5":-43,"field1":null,"field2":null,"field3":null,"field4":null,"field6":null,"field7":null,"field8":null,"field9":null,"entry_id":4030876},{"created_at":"2017-09-29T17:13:41+08:00","field1":26.810486,"field2":49,"field3":null,"field4":null,"field5":null,"field6":null,"field7":null,"field8":null,"field9":null,"entry_id":4030875},{"created_at":"2017-09-29T17:08:41+08:00","field1":26.839859,"field2":49,"field3":null,"field4":null,"field5":null,"field6":null,"field7":null,"field8":null,"field9":null,"entry_id":4030873},{"created_at":"2017-09-29T17:08:01+08:00","field5":-46,"field1":null,"field2":null,"field3":null,"field4":null,"field6":null,"field7":null,"field8":null,"field9":null,"entry_id":4030871},{"created_at":"2017-09-29T17:03:41+08:00","field1":26.826508,"field2":49,"field3":null,"field4":null,"field5":null,"field6":null,"field7":null,"field8":null,"field9":null,"entry_id":4030870}]}

示例代码(PHP)

<?php
// +----------------------------------------------------------------------
// | UbiBot
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 https://www.ubibot.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: UbiBot <support@ubibot.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// Get Channel Feeds
// 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 = "XXXXXXXXXXXXXXXXXXXXXX";
 

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

$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)
// 		"min" => "",//(decimal) Minimum value to include in response (optional)
// 		"max" => "",//(decimal) Maximum value to include in response (optional)
// 		"sum" => "",//(integer or string) Get sum of this many minutes, valid values: 10, 15, 20, 30, 60, 240, 720, 1440, "daily" (optional)
// 		"round" => "",//(integer) Round to this many decimal places (optional)
// 		"average" => "",//(integer or string) Get average of this many minutes, valid values: 10, 15, 20, 30, 60, 240, 720, 1440, "daily" (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)) {

    				$tmp['value'] = $value[$k];
    				$tmp['created_at'] = $value['created_at'];
    				$list[$k]['data'][] = $tmp;
    			}
    		}
    	}
    }else{ 
        echo $result['errorCode'].":".$result['desp'];
    }

}else{
    echo "failed";
}
//**************************************************
echo '<pre>';

 
 
/**
 * @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><?php echo $val['created_at'];?></li>
						<li style="margin-left:20px;"><?php echo $val['value'];?></li>
					</ul>
					<?php endforeach;?>
				<?php endif;?>
				</td>
 	 		</tr>
 	 	<?php endforeach;?>
 		</table>
 	 </body>
 </html>

导出全部空间数据

功能描述

导出全部空间数据(Export All Channel Feeds)接口是用来以CSV形式导出指定空间(channel)所存储的所有传感器感知数据。

请求地址

https://api.ubibot.cn/channels/CHANNEL_ID/full_feeds.csv

将CHANNEL_ID替换成空间编号。若需要返回数据为CSV格式:

请求方式

HTTP GET

注意事项

由于全部导出功能会对平台产生流量及带宽波动,所以全部导出接口目前对于每个设备每月会有相应次数限制。