import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.UpdateObjectUtil;
import com.ho.datacollect.api.vo.req.Meter;
import com.ho.datacollect.api.vo.req.mqtt.DataJson;
import com.ho.datacollect.api.vo.req.mqtt.MqttJson;
import com.ho.common.tools.util.MqttUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description 云端模拟发送消息测试
 * Author yule
 * Date 2022/12/7 11:28
 */
@ContextConfiguration
@Slf4j
public class CloudSendMsg {



    @Test
    public void testReadRequest() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        try {
            MqttClient client = new MqttClient("tcp://192.168.1.198:1883", clientId);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName("admin");
            options.setPassword("public".toCharArray());
            options.setCleanSession(true);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("mqtt建立连接：{}", complete);

            MqttJson json = new MqttJson();
            json.setToken(new MqttUtil().getToken());
            json.setDataType(CommonConstant.MQTT_DATATYPE.READ_REQUEST);
            json.setIsZip(CommonConstant.MQTT_ISZIP.NOT_IS);
            DataJson data = new DataJson();
            data.setCmdId("abcd1234");
            List<String> list = new ArrayList<>();
            list.add("station_251/para/");
            list.add("station_251/bay_252/device_254/para/");
            list.add("station_251/bay_252/device_254/device_257/para/");
            data.setList(list);
            String dataString = JSONObject.toJSONString(data);
            json.setData(dataString);
            String jsonString = JSONObject.toJSONString(json);
            MqttMessage mqttMessageResponse = new MqttMessage();
            mqttMessageResponse.setPayload(jsonString.getBytes());
            mqttMessageResponse.setQos(0);
            mqttMessageResponse.setRetained(false);
            client.publish("251/cloud/APO1231564/read/request", mqttMessageResponse);
            log.info("topic:{},msg:{}", "251/cloud/APO1231564/read/request", jsonString);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Test
    public void testWriteRequest() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        try {
            MqttClient client = new MqttClient("tcp://192.168.1.198:1883", clientId);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName("admin");
            options.setPassword("public".toCharArray());
            options.setCleanSession(true);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("mqtt建立连接：{}", complete);

            MqttJson json = new MqttJson();
            json.setToken(new MqttUtil().getToken());
            json.setDataType(CommonConstant.MQTT_DATATYPE.WRITE_REQUEST);
            json.setIsZip(CommonConstant.MQTT_ISZIP.NOT_IS);
            JSONObject data = new JSONObject();
            data.put("cmdId", "adcd1234");
            Map<String, Object> station = new HashMap<>();
            station.put("id", 1);
            Map<String, Object> para = new HashMap<>();
            para.put("name", "上海奉贤电厂New");
            station.put("para", para);
            data.put("station", station);
            String dataString = JSONObject.toJSONString(data);
            json.setData(dataString);
            String jsonString = JSONObject.toJSONString(json);
            MqttMessage mqttMessageResponse = new MqttMessage();
            mqttMessageResponse.setPayload(jsonString.getBytes());
            mqttMessageResponse.setQos(2);
            mqttMessageResponse.setRetained(false);
            client.publish("1/cloud/APO1231564/write/request", mqttMessageResponse);
            log.info("topic:{},msg:{}", "1/cloud/APO1231564/write/request", jsonString);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Test
    public void testDisconnect() {
        String clientId = IdUtil.simpleUUID();
        log.info("clientId:" + clientId);
        try {
            MqttClient client = new MqttClient("tcp://192.168.1.198:1883", clientId);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName("admin");
            options.setPassword("public".toCharArray());
            options.setCleanSession(true);
            IMqttToken iMqttToken = client.connectWithResult(options);
            boolean complete = iMqttToken.isComplete();
            log.info("mqtt建立连接：{}", complete);

            MqttJson json = new MqttJson();
            json.setToken(new MqttUtil().getToken());
            json.setDataType(CommonConstant.MQTT_DATATYPE.DISCONNECT);
            json.setIsZip(CommonConstant.MQTT_ISZIP.NOT_IS);
            Map<String, Date> map = new HashMap<>();
            map.put("sendTime", new Date());
            String data = JSONObject.toJSONString(map);
            json.setData(data);
            String jsonString = JSONObject.toJSONString(json);
            String topic = "251/cloud/APO1231564/control/request";
            MqttMessage mqttMessageResponse = new MqttMessage();
            mqttMessageResponse.setPayload(jsonString.getBytes());
            mqttMessageResponse.setQos(2);
            mqttMessageResponse.setRetained(false);
            log.info("topic:{},msg:{}", topic, jsonString);
            client.publish(topic, mqttMessageResponse);
            RedisService redisService = new RedisService();
            //redisService.set(RedisKeyConstant.);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Test
    public void getString() {
        DateTime parse = DateUtil.parse("2022-12-12/11:24:30", CommonConstant.DATE);
        System.out.println(parse);
    }

    @Test
    public void object() throws IllegalAccessException {
        Meter meter1 = new Meter();
        meter1.setBackwardActE0(new BigDecimal(0));
        meter1.setBackwardActE1(new BigDecimal(1));
        meter1.setBackwardActE2(new BigDecimal(2));
        System.out.println(meter1);
        Meter meter2 = new Meter();
        meter1.setBackwardActE0(new BigDecimal(5));
        meter1.setBackwardActE1(new BigDecimal(6));
        meter1.setBackwardActE2(new BigDecimal(7));
        meter2.setBackwardActE3(new BigDecimal(3));
        meter2.setBackwardActE4(new BigDecimal(4));

        UpdateObjectUtil util = new UpdateObjectUtil();
        Meter meter  = (Meter) util.updateObjectAttribute(meter1, meter2);
        System.out.println(meter);
    }

    @Test
    public void json(){
  String date = "2023-01-08 13:12:12.333";
        String substring = date.substring(0, 7);
        String substring1 = date.substring(0, 4);
        System.out.println(substring);
        System.out.println(substring1);

    }



}
