繁易平台直接对接监测点的数据(除MQTT方式外第二种方式)

繁易平台直接对接监测点的数据(除MQTT方式外第二种方式)

一: 注册开发者秘钥信息

  1. 登录繁易平台,点击设置

在这里插入图片描述

  1. 点击开发者账号

在这里插入图片描述

  1. 点击添加开发者秘钥:

    注意:

    添加开发者秘钥后,一定要保存client_secret,因为密钥原文只会显示一次,请注意保存

二: 获取access_token

具体实现如下:

/**
     * 1. 登录繁易平台获取accsess_token
     *
     * @return
     * @throws Exception
     */
    public String bengFangGetToken() throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        params.put("scope", "fbox");
        params.put("client_id", "获取到的秘钥id");
        params.put("client_secret", "注册的密钥secret");
        params.put("grant_type", "client_credentials");
        //要发送的POST请求url?Key=Value&Key2=Value2&Key3=Value3的形式
        String sb = sendPost("https://account.flexem.com/core/connect/token", map2Url(params), "application/x-www-form-urlencoded;charset=UTF-8", "POST");

        // 获取到的access_token对象
        AccessToken accessToken = JSONObject.parseObject(sb, AccessToken.class);
        return accessToken.getAccess_token();
    }

涉及到的bean类如下:

public class AccessToken {

    private String access_token;
    private String expires_in;
    private String token_type;
    private String scope;


    // getter和setter
}

三: 开始获取指定监测点的采集数据

代码如下:

   /**
     *
     * @param token     获取到的access_token
     * @param boxNumber  繁易盒子的序列号
     * @param jianCeDian  监测点的名称
     * @param group         该监测点所属分组
     * @return
     * @throws IOException
     */
    public StationMessage getMessageByStationId(String token,String boxNumber,String jianCeDian,String group) throws IOException {
        String url = "http://fbcs101.fbox360.com/api/v2/dmon/value/get?boxNo=" + boxNumber;
        //请求的body信息
//        String requestBody = "{"+"names"+": ["+jianCeDian+"],"+"groupnames"+": ["+group+"],"+"timeOut"+": " +null +"}";
        String requestBody =  "{\n" +
                "    \"names\": [\"监测点名称\"],\n" +
                "    \"groupnames\": [\"所属组名称\"],\n" +
                "    \"timeOut\": null\n" +
                "}";
		// 因为请求的格式是固定,这里用了字符串的替换为指定监测点名称
        requestBody = requestBody.replaceAll("监测点名称",jianCeDian);
        // 因为请求的格式是固定,这里用了字符串的替换为指定监测点的所属分组的名称
        requestBody = requestBody.replaceAll("所属组名称",group);

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        //添加header
        httpPost.addHeader("User-Agent", "PostmanRuntime/7.28.1");
        httpPost.addHeader("Accept", "*/*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Connection", "keep-alive");

        httpPost.addHeader("Authorization", "Bearer " + token);
        //添加body
        ByteArrayEntity entity = null;
        try {
            entity = new ByteArrayEntity(requestBody.getBytes("UTF-8"));
            entity.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("向服务器承保接口发起http请求,封装请求body时出现异常", e);
        }
        httpPost.setEntity(entity);
        //执行post请求
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            throw new RuntimeException("提交给服务器的请求,不符合HTTP协议", e);
        } catch (IOException e) {
            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
        }


        String resutl = EntityUtils.toString(response.getEntity(), "utf-8");

        List<StationMessage> stationMessageList = null;
        try {
            stationMessageList = JSON.parseArray(resutl, StationMessage.class);
        } catch (Exception e) {
            return null;
        }

        StationMessage stationMessage = null;
        if (stationMessageList != null && stationMessageList.size() > 0){
            stationMessage = stationMessageList.get(0);
        }

        return  stationMessage;

    }

涉及到的bean类如下:

public class StationMessage {
    private String id;
    private String timestamp;
    private String dataType;
    private String name;
    private String value;
    private String boxId;
    private String status;
    private String connState;
    private String connStateTimestamp;

    // getter和setter
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值