千寻位置平台使用入门总结

1、千寻平台简介:

依托北斗地基增强基站提供定位服务,根据精度不同分为:亚米级、厘米级、静态毫米级的位置监控,自己有一套后台原始数据系统,并对初始数据进行处理,形成新的服务,称之FindS(云踪)、FindS等等,同时支持开发平台有Android、IOS、Linux嵌入式、Web API。

2、如何开始

a)        首先需要注册千寻的账号,有试用期,实名认证试用期会长一些。

b)        购买或者试用对应的服务

c)        激活服务实例

d)        获取对应的appkey 和 appScrete, 或者 sik和sis  接入千寻平台需要的

e)        下载千寻的开发的demo

3、  下面以千寻云踪FindS的WEB开发为例

Step1 : 进入千寻官网:https://www.qxwz.com/

Step2:注册或登录

Step3:跳过注册部分直接登录


Step 4:获取 sik 和sis


Step5:了解一下API概述


Step6:下载demo


Step7.这是开发就可以参考手册

实体管理:创建一个实体对象

位置管理:GPS位置相关数据

配置管理:主要是抽稀配置(也就是去掉部分数据换取性能)地理围栏配置(是否支持地理围栏)

事件管理:终端事件发生,例如共享单车的急刹车,急转弯

行程管理:骑行的过程,从开始到结束

地理围栏:划分一个GPS地理区域

规则引擎:配合规则引擎和消息通道使用,触发地理围栏需要自动做一些事情

消息通道:支持阿里云MNS

关系管理:将规则引擎和地理围栏与对应实体进行相应的关联

统计报表:统计一段时间内关于实体的相关的属性

3、 开发的代码:

a)  工程结构:




a)  代码展示:

pox.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jack</groupId>
	<artifactId>qianxun</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.26</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.3</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
	</dependencies>
</project>


Consts.java
package com.jack.util;

public class Consts {

	public final static String SIK = "你自己sik";
	public final static String SIS = "你自己的sis";
	public final static String QXWZURL = "http://openapi.qxwz.com";
}

Utils.java 

package com.jack.util;

import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Utils {
	
	/**
	 * @param paramMap 参数列表(加签的时候需要进行字典序升序排序)
	 * @param apiName 访问的API接口名称
	 * @throws Exception 
	 */
	public static void doWork(Map<String, String> paramMap, String apiName) throws Exception {
		
		String apiPath = "/rest/" + apiName + "/" + "sik" + "/"+Consts.SIK;// API路径,注意没有问号
		// 毫秒级时间戳,一定是毫秒级!重要!!!
		String timestamp = String.valueOf(System.currentTimeMillis());
		String signatureStr = doHmacSHA2(apiPath, paramMap, Consts.SIS, timestamp);
		System.out.println("加签值:"+signatureStr);//打印sign值

		String qxwzUrl = "http://openapi.qxwz.com";//千寻的服务器地址
		String queryUrl = qxwzUrl + apiPath + "?_sign=" + signatureStr;
		System.out.println(queryUrl);
		//发送http请求,获取返回值
		doHttpPost(queryUrl,paramMap,timestamp);
	}
	
	public static void doHttpPost(String url,Map<String, String> paramMap, String timestamp) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();	
		HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept-Encoding", "gzip, deflate");
        httpPost.setHeader("wz-acs-timestamp", timestamp);//此处注意加上时间戳,否则http请求将无效
      	List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
        for (Map.Entry<String, String> entry : paramMap.entrySet()){
             if (entry.getValue() == null) continue;
              	nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));//处理中文编码的问题
       System.out.println(httpPost.getEntity().toString());
        
		try {
			CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost); 
			try {  
                HttpEntity entity = response.getEntity();   
                if (entity != null) {    
                    System.out.println("返回值:"+EntityUtils.toString(entity));  
                    /*
                     * 此处针对返回结果进行不同的处理
                     */
                    EntityUtils.consume(entity);  
                }  
            } finally {  
                response.close();  
            } 

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (httpPost != null) {
				httpPost.releaseConnection();//释放资源
			}
			if(httpClient != null){
				httpClient.close();//释放资源
			}
		}
	}

	/*
	 * 将字节数组转换成16进制字符串
	 * */
	public static String encodeHexStr(final byte[] bytes){
		if (bytes == null) {
			return null;
		}
		char[] digital  = "0123456789ABCDEF".toCharArray();
		char[] result = new char[bytes.length * 2];
		for (int i = 0; i < bytes.length; i++) {
			result[i * 2] = digital[(bytes[i] & 0xf0) >> 4];
			result[i * 2 + 1] = digital[bytes[i] & 0x0f];
		}
		return new String(result);
	}

	/*
	 * 加签算法
	 * */
	public static <T> String doHmacSHA2(String path, Map<String, T> params, String key, String timestamp) {
		
		List<Map.Entry<String, T>> parameters = new ArrayList<Map.Entry<String,T>>(params.entrySet());
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
		Charset CHARSET_UTF8 = Charset.forName("UTF-8");
		Mac mac;
		try {
			mac = Mac.getInstance("HmacSHA256");
			mac.init(signingKey);
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e.getMessage(), e);
		} catch (InvalidKeyException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
		if(path != null && path.length() > 0){
			mac.update(path.getBytes(CHARSET_UTF8));
		}
		if(parameters != null){
			Collections.sort(parameters, new MapEntryComparator<String, T>());
			for (Map.Entry<String, T> parameter : parameters) {
				byte[] name = parameter.getKey().getBytes(CHARSET_UTF8);
				Object value = parameter.getValue();
				if(value instanceof Collection){
					for (Object o : (Collection)value){
						mac.update(name);
						if(o != null){
							mac.update(o.toString().getBytes(CHARSET_UTF8));
						}
					}
				}else{
					mac.update(name);
					if(value != null){
						mac.update(value.toString().getBytes(CHARSET_UTF8));
					}
				}
			}
		}
		if(timestamp != null && timestamp.length() > 0){
			mac.update(timestamp.toString().getBytes(CHARSET_UTF8));
		}
		return encodeHexStr(mac.doFinal());
	}
}

/*
 * Map参数排序类
 * */

class MapEntryComparator<K, V> implements Comparator<Map.Entry<K, V>> {

    public int compare(Entry<K, V> o1, Entry<K, V> o2) {
        if (o1 == o2) {
            return 0;
        }
        final String k1 = o1.getKey().toString();
        final String k2 = o2.getKey().toString();
        int l1 = k1.length();
        int l2a = k2.length();
        for (int i = 0; i < l1; i++) {
            char c1 = k1.charAt(i);
            char c2;
            if (i < l2a) {
                c2 = k2.charAt(i);
            } else {
                return 1;
            }
            if (c1 > c2) {
                return 1;
            } else if (c1 < c2) {
                return -1;
            }
        }
        if (l1 < l2a) {
          return -1;
        }else if(l1 == l2a) {
          return 0;
        }else {
          return -1;
        }
    }
}

EntiManager.java 以实体测试为例

package com.jack.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.jack.test.entity.Entity;
import com.jack.util.Utils;

public class EntityManager {
	
	Map paramMap = new HashMap();
	
	/**
	 * 创建实体
	 * @throws Exception
	 */
	@Test
	public void createEntity() throws Exception{
		 String API_NAME= "gpsp.entity.createEntity";
		Entity entity = new Entity();
		entity.setEntityName("yerewererw");
		entity.setEntityType("奶制品");
		Map<String, String> attr = new HashMap<String ,String>();
		attr.put("color", "白色");
		attr.put("contain", "蛋白质");
		entity.setAttributes(attr);
		paramMap.put("entity",JSONObject.toJSONString(entity) );
		Utils.doWork(paramMap, API_NAME);
		
	}
	
	/**
	 * 创建JT808实体
	 * @throws Exception
	 */
	@Test
	public void createJT808Entity()throws Exception{
		 String API_NAME= "gpsp.jt808.entity.createEntity";
		Entity entity = new Entity();
		entity.setEntityName("大卡");
		entity.setEntityType("湘A00110");
		Map<String, String> attr = new HashMap<String, String>();
		attr.put("qx_simCard", "1872451551");
		attr.put("qx_terminalId", "01");
		attr.put("qx_plateNumber", "湘A00110");
		entity.setAttributes(attr);
		paramMap.put("entity",JSONObject.toJSONString(entity) );
		Utils.doWork(paramMap, API_NAME);
	}
	
	/**
	 * 删除实体
	 * @throws Exception
	 */
	@Test 
	public void deleteEntity() throws Exception{
		 String API_NAME= "gpsp.entity.deleteEntity";
		paramMap.put("entityName","y" );
		Utils.doWork(paramMap, API_NAME);
	}
	/**
	 * 更新实体
	 * @throws Exception
	 */
	@Test 
	public void updateEntity() throws Exception{
		String API_NAME= "gpsp.entity.updateEntity";
		
		Map<String, String> attr = new HashMap<String ,String>();
		attr.put("颜色", "白色");
		attr.put("哈哈", "蛋白质");
		System.out.println(JSONObject.toJSONString(attr) );
		paramMap.put("attributes",JSONObject.toJSONString(attr) );
		paramMap.put("entityName","阳" );
		Utils.doWork(paramMap, API_NAME);
	}
	/**
	 * 查询单个实体
	 * @throws Exception
	 */
	@Test 
	public void queryEntity() throws Exception{
		String API_NAME= "gpsp.entity.queryEntity";
		paramMap.put("entityName","阳" );
		Utils.doWork(paramMap, API_NAME);
	}
	/**
	 * 查询多个实体
	 * @throws Exception
	 */
	@Test 
	public void queryMoreEntity() throws Exception{
		String API_NAME= "gpsp.entity.queryEntities";
		List<String> list = new ArrayList<String>();
		list.add("阳");
		list.add("牛奶");
		list.add("demo");
		paramMap.put("entityNames",JSONObject.toJSONString(list) );
		System.out.println(paramMap);
		Utils.doWork(paramMap, API_NAME);
	}
	@Test 
	public void queryEntityList() throws Exception{
		String API_NAME= "gpsp.entity.queryEntitiesByApp";
		paramMap.put("pageNum", "1");
		paramMap.put("pageSize", "10");
		paramMap.put("returnType", "ALL");
		
		Utils.doWork(paramMap, API_NAME);
	}
	@Test 
	public void queryEntityCount() throws Exception{
		String API_NAME= "gpsp.entity.getCountByApp";
		
		paramMap.put("type","ALL" );
		Utils.doWork(paramMap, API_NAME);
	}
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值