Newer
Older
pichan-haerbin / src / main / java / com / casic / service / impl / AepCommandSend.java
package com.casic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.casic.config.AEPParamConfig;
import com.casic.config.DeviceGroup;
import com.casic.model.AlarmConfigParam;
import com.casic.model.BusConfigParam;
import com.casic.model.DataGasConfigParam;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.aep_device_command.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class AepCommandSend {

    @Autowired
    private AEPParamConfig aepParamConfig;

    public void sendStatusNb(List<DataGasConfigParam> dataGasConfigList) {
        Map<String, Object> dataGasMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("query", 1);
        dataGasMap.put("params", queryMap);
        dataGasMap.put("serviceIdentifier", "DeviceQuery");
        dataGasConfigList.forEach(
                dataGasConfigParam -> {
                    dataGasConfigParam.setLevel(1);
                    Optional<DeviceGroup> deviceGroups = aepParamConfig.getDeviceGroups().stream()
                            .filter(deviceGroup -> deviceGroup.getProductId().equals(dataGasConfigParam.getProductId())).findFirst();
                    if (deviceGroups.isPresent()) {
                        DeviceGroup deviceGroup = deviceGroups.get();
                        AepDeviceCommandClient client = AepDeviceCommandClient.newClient()
                                .appKey(deviceGroup.getAepKey()).appSecret(deviceGroup.getAepSecret())
                                .build();
                        try {
                            dataGasConfigParam.setContent(dataGasMap);
                            dataGasConfigParam.setOperator("casic");
                            dataGasConfigParam.setTtl(deviceGroup.getTtl());
                            CreateCommandRequest request = new CreateCommandRequest();
                            request.setParamMasterKey(deviceGroup.getMasterKey());
                            request.setBody(JSONObject.toJSONString(dataGasConfigParam).getBytes());
                            System.out.println(client.CreateCommand(request));
                            client.shutdown();
                        } catch (Exception ex) {
                            log.error("状态下发配置异常,下发配置内容{},aepKey{},aepScret{},异常信息{}",
                                    JSONObject.toJSONString(dataGasConfigParam), deviceGroup.getAepKey(), deviceGroup.getAepSecret());
                            return;
                        }
                    } else {
                        return;
                    }
                }
        );
    }

    public String sendConfig(BusConfigParam busConfigParam) {
        Optional<DeviceGroup> deviceGroups = aepParamConfig.getDeviceGroups().stream()
                .filter(deviceGroup -> deviceGroup.getProductId().equals(busConfigParam.getProductId())).findFirst();
        if (deviceGroups.isPresent()) {
            DeviceGroup deviceGroup = deviceGroups.get();
            AepDeviceCommandClient client = AepDeviceCommandClient.newClient()
                    .appKey(deviceGroup.getAepKey()).appSecret(deviceGroup.getAepSecret())
                    .build();
            Map<String, Object> dataGasMap = new HashMap<>();
            dataGasMap.put("serviceIdentifier", "config");
            dataGasMap.put("params", busConfigParam.getQueryContent());
            busConfigParam.setContent(dataGasMap);
            busConfigParam.setLevel(1);
            busConfigParam.setOperator("casic");
            try {
                busConfigParam.setTtl(deviceGroup.getTtl());
                CreateCommandRequest request = new CreateCommandRequest();
                request.setParamMasterKey(deviceGroup.getMasterKey());    // single value
                request.setBody(JSONObject.toJSONString(busConfigParam).getBytes());
                CreateCommandResponse msgResponse = client.CreateCommand(request);
                log.debug("-----" + msgResponse.getMessage());
                return msgResponse.getStatusCode() == 200 ? "未下发" : "下发异常";
            } catch (Exception ex) {
                log.error("设备命令下发配置异常,下发配置内容{},aepKey{},aepScret{},异常信息{}",
                        JSONObject.toJSONString(busConfigParam), deviceGroup.getAepKey(), deviceGroup.getAepSecret());
                return "下发异常";
            } finally {
                client.shutdown();
            }
        } else {
            return "下发异常";
        }
    }

    public String sendAlarmConfig(AlarmConfigParam alarmConfigParam) {
        Optional<DeviceGroup> deviceGroups = aepParamConfig.getDeviceGroups().stream()
                .filter(deviceGroup -> deviceGroup.getProductId().equals(alarmConfigParam.getProductId())).findFirst();
        if (deviceGroups.isPresent()) {
            DeviceGroup deviceGroup = deviceGroups.get();
            AepDeviceCommandClient client = AepDeviceCommandClient.newClient()
                    .appKey(deviceGroup.getAepKey()).appSecret(deviceGroup.getAepSecret())
                    .build();
            Map<String, Object> dataGasMap = new HashMap<>();
            dataGasMap.put("serviceIdentifier", "alarmconfig");
            dataGasMap.put("params", alarmConfigParam.getQueryContent());
            alarmConfigParam.setContent(dataGasMap);
            alarmConfigParam.setLevel(1);
            alarmConfigParam.setOperator("casic");
            try {
                alarmConfigParam.setTtl(deviceGroup.getTtl());
                CreateCommandRequest request = new CreateCommandRequest();
                request.setParamMasterKey(deviceGroup.getMasterKey());    // single value
                request.setBody(JSONObject.toJSONString(alarmConfigParam).getBytes());    //具体格式见前面请求body说明
                CreateCommandResponse msgResponse = client.CreateCommand(request);
                System.out.println(msgResponse);
                client.shutdown();
                log.debug("-----" + msgResponse.getMessage());
                return msgResponse.getStatusCode() == 200 ? "未下发" : "下发异常";
            } catch (Exception ex) {
                log.error("设备报警下发配置异常,下发配置内容{},aepKey{},aepScret{},异常信息{}",
                        JSONObject.toJSONString(alarmConfigParam), deviceGroup.getAepKey(), deviceGroup.getAepSecret());
                return "下发异常";
            } finally {
                client.shutdown();
            }
        } else {
            return "下发异常";
        }

    }


}