刚刚接触物联网,萤石云平台,编写萤石云获取登录的accessToken工具类

我接手写公司物联网的模块,入坑不少,主要不知道有哪些东西要写。

总结流程:

1.先边设备调试好(摄像头),在手机上看到画面

1.1获取获取appKey

萤石云开放平台appKey申请可在开放平台官网注册登录https://open.ys7.com/view/app/app_edit.html(官网),填写正确的信息后开放平台会发放到您的账号下。appKey和appSecret是开放平台应用的秘钥,请不要请泄露,如若泄露可将appSecret重置即可。

1.2手机上下载萤石云视频app

1.3如果设备是二手的(别人使用过),还需要修改设备的IP地址

1.4电脑下载设备网络搜索

1.4.1 百度网盘下载:https://pan.baidu.com/s/1HNC8l2Lvh5_4-OMO_8LWPQ 提取码:6j4u

下载:SADPToo V3.0.2.4.rar压缩包(我习惯工作上使用的软件都会上传到这个群,下次下载就直接下载速度快)

1.5安装设备网络搜索,直接解压,直接运行,下一步就行

1.6打开设备网络搜索

修改设备的IP,(查询自己电脑的IP:打开cmd命令:ipconfig,如果不会我以前有写过这文章)

python 萤石云 萤石云工具_apache


1.7,登录萤石云视频app,添加设备扫一扫就可以添加成功了

1.8,自己可以看到画面

2.现在开始写Java代码了(开心 高兴)

2.1萤石云官方文档:https://open.ys7.com/doc/zh/book/index/user.html

2.1获取登录的accessToken

package com.smartfarm.base.monitor.core.util;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * 获取登录的accessToken
 * @author lyq
 *
 */
public class GetTokenUtil {

	public static void main(String[] args) {
		//我自己的密匙
		String AppKey="7fec057876c445f9b919ed9bb8bfd821";
		String Secret="afa8b6fdced65f379c13e371c9967721";
		Token t=getSnapUrl(AppKey, Secret);
		System.out.println(t.getData().getAccessToken());
		System.out.println(t.getData().getExpireTime());
	}
	public static Token getSnapUrl(String AppKey,String Secret){
		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		
		// 封装参数
		StringBuffer params = new StringBuffer();
		params.append("appKey="+ AppKey);
		params.append("&");
		params.append("appSecret="+Secret);
		
		// 创建Post请求
		HttpPost httpPost = new HttpPost("https://open.ys7.com/api/lapp/token/get" + "?" + params);
		// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");
 
		// 响应模型
		CloseableHttpResponse response = null;
		
		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
		
			if (responseEntity != null) {
				//返回数据
				String responseString = EntityUtils.toString(responseEntity);
				//生成实体类(responseString就可以看到数据的)
				JSONObject jsonObject = JSONObject.fromObject(responseString);
				Token t = (Token) JSONObject.toBean(jsonObject, Token.class);
				return t;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}

2.3获取获取设备状态信息

package com.smartfarm.base.monitor.core.util;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * 获取设备状态信息
 * @author lyq
 *
 */
public class SnapUtil {

	public static void main(String[] args) {
		//我自己的密匙
		String AppKey="7fec057876c445f9b919ed9bb8bfd821";
		String Secret="afa8b6fdced65f379c13e371c9967721";
		String accessToken="at.60haqycfbqoizem15csgx1q21lmn85q6-4jmaids8bm-130mqh2-iamhp5emw";
		//获取登录的accessToken
		//String accessToken=GetTokenUtil.getSnapUrl(AppKey,Secret).getData().getAccessToken();
		int channel=1;
		String deviceSerial="C16499527";
		System.out.println(stateInformation(accessToken, deviceSerial,channel));
	}
	 
			

	/**  获取设备状态信息
	 * accessToken	String	授权过程获取的access_token	Y
	 * deviceSerial	String	设备序列号,存在英文字母的设备序列号,字母需为大写	Y
	 * channel	int	通道号,默认为1	N
	 * @param accessToken
	 * @param deviceSerial
	 * @return
	 */
	public static String stateInformation(String accessToken,String deviceSerial,int channel){
		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		// 封装参数
		StringBuffer params = new StringBuffer();
		params.append("accessToken="+ accessToken);
		params.append("&");
		params.append("deviceSerial="+deviceSerial);
		params.append("&");
		params.append("channel="+channel);
		// 创建Post请求
		HttpPost httpPost = new HttpPost("https://open.ys7.com/api/lapp/device/status/get" + "?" + params);
		// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		// 响应模型
		CloseableHttpResponse response = null;

		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			if (responseEntity != null) {
				String responseString = EntityUtils.toString(responseEntity);
				JSONObject jsonObject = JSONObject.fromObject(responseString);
				Picture p = (Picture) JSONObject.toBean(jsonObject, Picture.class);
				return p.getCode();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}

现在后台就可以操控设备了

如果有帮助就关注一下,没有帮助的就不强求,
滴水之恩,当涌泉相报,那是你没有绝望,
跳槽工作了3个星期了,星期一到星期五加班到7点,回到家9点
星期六星期日,不是自己就是学新的东西,
能不裸辞,就尽量不要裸辞了,裸辞之后,可能还没有上一家好,
我在以前的公司6点准时走 哈哈哈
加油干!!! 死刚Java