2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-01-05 d5c71f3c85e9370100559e86e1081c44b1cc15df
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.sandu.ximon.admin.service;
 
import com.sandu.ximon.admin.config.VnnoxConstant;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.admin.utils.VnnoxAPIUtil;
import com.sandu.ximon.admin.utils.request.VnnoxScreenStatusType;
import com.sandu.ximon.admin.utils.response.VnnoxPlayerListResponse;
import com.sandu.ximon.admin.utils.response.VnnoxPlayerResponse;
import com.sandu.ximon.admin.utils.response.VnnoxResult;
import com.sandu.ximon.dao.domain.LedPlayerEntity;
import com.sandu.ximon.dao.domain.LedV2RegisterResultEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @Author liuhaonan
 * @Date 2021/12/22 14:18
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class VnnoxService {
 
   // @Autowired
    private VnnoxAPIUtil vnnoxAPIUtil;
   // @Autowired
    private RedisUtils redisUtils;
   // @Autowired
    private LedPlayerEntityService ledPlayerEntityService;
 
    /**
     * 设备校验注册
     *
     * @param sn
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public LedV2RegisterResultEntity validateSN(String sn) {
        LedV2RegisterResultEntity ledV2RegisterResultEntity = new LedV2RegisterResultEntity();
 
        //  判断数据库是否存在,若不存在,判断是否已经注册到Vnnox服务器
        //LedPlayerEntity ledPlayerEntity = vnnoxDao.getBySN(sn);
        // LambdaQueryWrapper<LedPlayerEntity> eq = Wrappers.lambdaQuery(LedPlayerEntity.class).eq(LedPlayerEntity::getSn, sn);
        LedPlayerEntity ledPlayerEntity = ledPlayerEntityService.getBySn(sn);
        if (null != ledPlayerEntity) {
            ledV2RegisterResultEntity.setRegister(1);
            ledV2RegisterResultEntity.setMsg("设备已存在!");
            return ledV2RegisterResultEntity;
        }
 
        Integer page = 0;
        Integer limit = 200;
        List<VnnoxPlayerResponse> playerList;
        //  获取Vnnox服务器列表
        VnnoxPlayerListResponse response = vnnoxAPIUtil.getPlayerList(limit, page);
        playerList = response.getRows();
        Integer total = response.getTotal() - limit;
        while (total > 0) {
            page = page + 1;
            total = total - limit;
            response = vnnoxAPIUtil.getPlayerList(limit, page);
            playerList.addAll(response.getRows());
        }
        ledPlayerEntity = new LedPlayerEntity();
        for (VnnoxPlayerResponse res : playerList) {
            if (res.getSn().equals(sn)) {
                ledPlayerEntity.setSn(sn);
                ledPlayerEntity.setPlayerId(res.getPlayerId());
                ledPlayerEntity.setPlayerName(res.getName());
                ledPlayerEntity.setCreateTimestamp(new Date().getTime());
                ledPlayerEntityService.saveLed(ledPlayerEntity);
                ledV2RegisterResultEntity.setRegister(1);
                ledV2RegisterResultEntity.setMsg("设备校验通过,设备注册成功!");
                return ledV2RegisterResultEntity;
            }
        }
 
        ledV2RegisterResultEntity.setRegister(0);
        ledV2RegisterResultEntity.setMsg("设备未注册到平台!");
        return ledV2RegisterResultEntity;
    }
 
 
    /**
     * 屏幕状态调整-POST-JSON
     *
     * @param playerList
     * @param screenStatus
     * @return
     */
    public VnnoxResult screenStatusChange(List<LedPlayerEntity> playerList, Integer screenStatus) {
        VnnoxScreenStatusType type;
        if (screenStatus.equals(0)) {
            type = VnnoxScreenStatusType.CLOSE;
        } else {
            type = VnnoxScreenStatusType.OPEN;
        }
 
        VnnoxResult vnnoxResult = vnnoxAPIUtil.screenStatus(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                type
        );
        //  根据屏幕状态调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_SCREEN_STATUS + playerId, screenStatus, VnnoxConstant.REDIS_MAX_SAVE_TIME);
        }
        return vnnoxResult;
    }
 
    public VnnoxResult volChange(List<LedPlayerEntity> playerList, Integer vol) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                vol
        );
        //  根据音量调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_VOL + playerId, vol, VnnoxConstant.REDIS_MAX_SAVE_TIME);
        }
        return vnnoxResult;
    }
 
    public VnnoxResult brightnessChange(List<LedPlayerEntity> playerList, Integer brightness) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.brightnessChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                brightness
        );
        //  根据亮度调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_BRIGHTNESS + playerId, brightness, VnnoxConstant.REDIS_MAX_SAVE_TIME);
        }
        return vnnoxResult;
 
    }
 
    public Map<String, String> getScreenShotUrl(Integer id) {
        Map<String, String> map = new HashMap();
 
        LedPlayerEntity playerEntity = ledPlayerEntityService.getById(id);
        try {
            redisUtils.delete(VnnoxConstant.REDIS_SCREEN_SHOT + playerEntity.getPlayerId());
        } catch (Exception e) {
 
        }
        VnnoxResult vnnoxResult = vnnoxAPIUtil.screenShot(playerEntity.getPlayerId());
 
        if (null == vnnoxResult) {
            map.put("code", "500");
            map.put("msg", "设备已下线");
            return map;
        }
 
        if (vnnoxResult.getSuccess().size() == 0) {
            map.put("code", "500");
            map.put("msg", "获取缩略图失败!");
            return map;
        }
        String url = null;
        Integer checkCount = 0;
        while (checkCount < 10) {
            url = redisUtils.get(VnnoxConstant.REDIS_SCREEN_SHOT + playerEntity.getPlayerId());
            if (null != url) {
                break;
            }
            try {
                Thread.sleep(2000);
                checkCount = checkCount + 1;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        map.put("url", url);
        map.put("code", "200");
        return map;
    }
 
 
    public VnnoxResult reboot(List<LedPlayerEntity> playerList) {
        return vnnoxAPIUtil.reboot(
                playerList.stream().map(
                        item -> item.getPlayerId()
                ).collect(Collectors.toList())
        );
    }
 
}