张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.thhy.mobile.modules.biz.temdevice.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thhy.general.common.BasicMessage;
import com.thhy.general.common.BasicResult;
import com.thhy.general.exception.BasicException;
import org.apache.commons.lang3.StringUtils;
 
import java.util.HashMap;
import java.util.Map;
 
public class TemTask {
 
    public static String token;
 
 
 
    public static String login(){
        Map<String,Object> map = new HashMap<>();
        map.put("loginName","h230905jzsy");
        map.put("password","h230905jzsy");
        String result = HttpUtils.get(TemApiConstant.LOGIN,map);
        if(StringUtils.isEmpty(result)){
            throw new BasicException(new BasicMessage("199902","登录云平台超时"));
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONObject dataJson = jsonObject.getJSONObject("data");
        //redis
        token = dataJson.getString("token");
        return token;
    }
 
    public static Map<String,Object> getDeviceList(){
        Map<String,Object> map = new HashMap<>();
        map.put("groupId","");
        String result = HttpUtils.get(TemApiConstant.DEVICE_LIST,map);
        System.out.println(result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("data"));
 
        Map<String,Object> dataMap = new HashMap<>();
        for(Object obj : jsonArray){
            JSONObject deviceJSON = JSONArray.parseObject(obj.toString());
            String deviceAddr = deviceJSON.getString("deviceAddr");
            JSONArray factorArrayJSON = JSONArray.parseArray(deviceJSON.getString("factors"));
            for(Object device : factorArrayJSON){
                JSONObject deviceJson = JSONObject.parseObject(device.toString());
                int nodeId = deviceJson.getIntValue("nodeId");
                int registerId = deviceJson.getIntValue("registerId");
                String factorName = deviceJson.getString("factorName");
                dataMap.put(deviceAddr+":"+nodeId+":"+registerId,factorName);
            }
        }
        return dataMap;
    }
 
    public static String getRealTimeData(){
        Map<String,Object> map = new HashMap<>();
        map.put("groupId","");
        String result = HttpUtils.get(TemApiConstant.REAL_TIME_DATA,map);
        System.out.println(result);
        return result;
    }
 
    public static void main(String[] args) {
        String token = login();
        getRealTimeData();
 
    }
}