import java.util.ArrayList;
import java.util.List;

import cproc.datacube.client.api.ClientDevelopApi;
import cproc.datacube.client.api.ClientDevelopApiService;
import cproc.datacube.client.entity.AppInfo;
import cproc.datacube.client.entity.CompareType;
import cproc.datacube.client.entity.Conditions;
import cproc.datacube.client.entity.PageQueryCondition;
import cproc.datacube.client.entity.ReObject;
import cproc.datacube.client.entity.RePageObject;
import cproc.datacube.client.entity.Row;
import cproc.datacube.client.entity.SubstrKeyCondition;
import cproc.datacube.client.entity.UserInfo;


public class test_Client_DevelopApi {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception  {
		 UserInfo user = new UserInfo();
		 user.setUserName("cstor");
		 user.setAccessId("Y3N0B3IXNDQWMDG5ODAWMDQX");
		 AppInfo app = new AppInfo();
		 app.setAppId("0166429095");
		 app.setUserInfo(user);
		 String startPK="";//起始主键值
		 String endPK="";//结束主键值
		 List<String> colNames=null;//要查询的列名称，若为空则默认查询所有列
		 Conditions con = new Conditions();//设置查询条件
		 List<SubstrKeyCondition> kcs = new ArrayList<SubstrKeyCondition>();//截字符串比较对象
		 PageQueryCondition pqc=new PageQueryCondition();//分页查询条件
		 pqc.setPageSize(10);//设置每次返回查询结果的条数
	        pqc.setStartPK(startPK);//设置查询的起始主键
	        con.setSubstrKeyCons(kcs);//主键截字符串比较条件集合，无该条件传空
	        con.setPageQueryCondition(pqc);//分页查询条件
	        con.setSplitKeyCons(null);//主键字符串按分隔符比较条件集合，无该条件传空
	        con.setStarttime(0);//开始时间 时间的绝对秒数 ,如果没有条件默认填0
	        con.setEndtime(0);//结束时间
	        con.setResultCount(10);// 设置返回结果个数(不指定默认1000，当指定endkey的时候不使用默认值)
		 
		 
		   //查询该应用下所有设备
	     ClientDevelopApi cda=new ClientDevelopApiService();
		 ReObject reObject=cda.getAllAppDevices(app);
		 if (reObject!=null && reObject.isSuccess() && reObject.getData() != null
                 && !reObject.getData().isEmpty())
         {
             List<Row> dvs = reObject.getData();
             for (int i = 0; i < dvs.size(); i++)
             {
                 Row row = (Row) dvs.get(i);
                //查询设备号
               System.out.println( row.getValues().get("DEVID"));
               //查询版本号
               System.out.println( row.getValues().get("VRS")); 
               //接入服务
               System.out.println( row.getValues().get("AGENTID"));  
               System.out.println( row.getValues().get("IP"));  
               System.out.println( row.getValues().get("OTHER"));  
             }
                     
	}
		   
			  //分页查询应用数据
		  ClientDevelopApi develApi = new ClientDevelopApiService();  
			RePageObject reObject1=develApi.getTableRowsByNewPageCondition(app, "DATA", con, colNames);
            if(reObject1!=null && reObject1.isSuccess() && reObject1.getData()!=null
            		 && !reObject1.getData().isEmpty()){
            	
            	List<Row> dvs=reObject1.getData();
            	for(Row r:dvs){
            		
            		 String rk = r.getColumn("DEV_ID");
                     String time = rk.substring(0, 19);//入库的时间  
                     String did = rk.substring(19, rk.length());//设备id
                    System.out.println("设备号："+did); 
            		System.out.println("PM1="+r.getColumn("PM1"));
            		System.out.println("PM2="+r.getColumn("PM2"));
            		                    
            	}
            	  
            	System.out.println("返回下一页的起始主键："+reObject1.getNextStartPK());//下次查询将startPK传回后台
            	System.out.println("判断是否为最后一页:"+reObject1.isLastPage());//作为查询结束符标志
            }
            
            // 查询设备数据
            ReObject reObject2=develApi.getTableRows(app, "DATA", con, colNames);
            if(reObject2!=null && reObject2.isSuccess() && reObject2.getData()!=null
           		 && !reObject2.getData().isEmpty()){
           	
           	List<Row> dvs=reObject2.getData();
           	for(Row r:dvs){
           		
           		 String rk = r.getColumn("DEV_ID");
                    String time = rk.substring(0, 19);//入库的时间  
                    String did = rk.substring(19, rk.length());//设备id
                   System.out.println("设备号："+did); 
           		System.out.println("PM1="+r.getColumn("PM1"));
           		//System.out.println("PM2="+r.getColumn("PM2"));
           	}
           		                    
        }
            
            
               //查询满足条件的最近一条数据
            Conditions con1 = new Conditions();//设置查询条件
   		    List<SubstrKeyCondition> kcs1 = new ArrayList<SubstrKeyCondition>();//截字符串比较对象
   		    SubstrKeyCondition skc=new SubstrKeyCondition();
   		
   		    skc.setComp(CompareType.GREATER_OR_EQUAL);//设置比较类型
   		    skc.setValue("mqtt2");//设置比较字符串的值
   		    skc.setOffset(0);//设置开始截字符串位置，从0开始
   		    skc.setLen(0);//设置截字符串的长度，为0表示截到末尾
   		    kcs1.add(skc);
   		    PageQueryCondition pqc1=new PageQueryCondition();//分页查询条件
//   		    pqc1.setPageSize(5);//设置每次返回查询结果的条数
//   	        pqc1.setStartPK("9223370592484187559a0010");//设置查询的起始主键
   	        con1.setSubstrKeyCons(kcs1);//主键截字符串比较条件集合，无该条件传空
   	        con1.setPageQueryCondition(pqc);//分页查询条件
   	        con1.setSplitKeyCons(null);//主键字符串按分隔符比较条件集合，无该条件传空
   	        con1.setStarttime(System.currentTimeMillis()/1000);//绝对时间秒数，而不是毫秒数
   	       // con1.setEndtime(0);//结束时间
   	        con1.setResultCount(100);// 设置返回结果个数(不指定默认1000，当指定endkey的时候不使用默认值)
         
            ReObject reObject3=develApi.getTopOneResult(app, "DATA", con, colNames);
            if(reObject3!=null && reObject3.isSuccess() && reObject3.getData()!=null
           		 && !reObject3.getData().isEmpty()){
           	
           	List<Row> dvs=reObject3.getData();
           	for(Row r:dvs){
           		 //实际上本次查询只会返回一条记录的
           		 String rk = r.getColumn("DEV_ID");
                    String time = rk.substring(0, 19);//入库的时间  
                    String did = rk.substring(19, rk.length());//设备id
                   System.out.println("设备号："+did); 
           		System.out.println("PM1="+r.getColumn("PM1"));
           		System.out.println("PM2="+r.getColumn("PM2"));
           		                    
           	}
           	  
           }
	}
}