Newer
Older
huludao / src / test / java / com / newfiber / led / test.java
package com.newfiber.led;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.newfiber.led.business.dao.LedDao;
import com.newfiber.led.business.entity.*;
import com.newfiber.led.business.util.Constant;
import com.newfiber.led.business.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.newfiber.led.business.util.MD5Util.MD;

/**
 * Created by Xy on 2019/12/19.
 */



@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class test {

    @Autowired
    private LedDao ledDao;

    @Test
    public void send1() {
        List<LedInformation> ledInformations = ledDao.querySubtitle(null, "0661611000");
        System.out.println(ledInformations.toString());
    }


    @Test
    public void send() throws Exception {
        /**登录,返回key*/
        String login = login();
        /**获取当前时间戳到秒*/
        String ts = String.valueOf(getSecondTimestamp(new Date()));

        String idsDev = "1216210008";
        /**提交任务*/
        JSONObject result = JSONObject.parseObject(sendCommonProgram(login, ts, idsDev));
        if (result.containsKey("sno")) {
            String sno = result.get("sno").toString();
            Thread.sleep(2000);

            /**获取提交任务的状态码*/
            String state = getState(login, ts, sno);

        }
    }


    /**
     * 登录,返回key
     */
    public static String login() {
        JSONObject json = new JSONObject();
        json.put("user", Constant.USER_NAME);
        json.put("pwd", MD(Constant.PASSWORD));
        String result = HttpUtils.sendPost(Constant.LOGIN, json.toJSONString());
        System.out.println("登录返回:" + result);
        return result;
    }


    /**
     * 提交任务
     */
    public String sendCommonProgram(String login, String ts, String idsDev) {
        /**登录,返回key*/
        JSONObject json = JSON.parseObject(login);
        /**获取数据*/
        String level = String.valueOf(ledDao.queryZ(idsDev).getZ());

        String type = "cmd";
        LedNewOrderEntity ledNewOrderEntity = new LedNewOrderEntity();
        ledNewOrderEntity.setTs(ts);
        ledNewOrderEntity.setUser(Constant.USER_NAME);
        ledNewOrderEntity.setType(type);

        LedNewInfoPosEntity ledNewInfoPosEntity = new LedNewInfoPosEntity();
        ledNewInfoPosEntity.setX(0);
        ledNewInfoPosEntity.setY(0);
        ledNewInfoPosEntity.setW(128);
        ledNewInfoPosEntity.setH(64);

        List<LedNewListItemEntity> ledNewListItemEntitys = new LinkedList<>();

        LedNewListItemEntity ledNewListItemEntity = new LedNewListItemEntity();
        ledNewListItemEntity.setSize(16);

        /**根据设备号查询当前led设备需要显示的字幕*/
        List<LedInformation> ledInformations = ledDao.querySubtitle(idsDev, null);
        String display = "";
        if (ledInformations.size() > 0) {
            display = ledInformations.get(0).getDisplay();
        }
        display = display.replace("/", level);

        ledNewListItemEntity.setText(display);
        ledNewListItemEntity.setType_item("text");
        ledNewListItemEntitys.add(ledNewListItemEntity);

        LedNewResShowEntity ledNewResShowEntity = new LedNewResShowEntity();
        ledNewResShowEntity.setId_rect(1);
        ledNewResShowEntity.setInfo_pos(ledNewInfoPosEntity);
        ledNewResShowEntity.setList_item(ledNewListItemEntitys);

        LedNewRtShowEntity ledNewRtShowEntity = new LedNewRtShowEntity();
        ledNewRtShowEntity.setId_pro(1);
        List<LedNewResShowEntity> ledNewResShowEntities = new LinkedList<>();
        ledNewResShowEntities.add(ledNewResShowEntity);
        ledNewRtShowEntity.setRes_show(ledNewResShowEntities);

        LedNewCmdEntity ledNewCmdEntity = new LedNewCmdEntity();
        ledNewCmdEntity.setRt_show(ledNewRtShowEntity);

        LedNewSendmsgEntity ledNewSendmsgEntity = new LedNewSendmsgEntity();
        ledNewSendmsgEntity.setCmd(ledNewCmdEntity);
        /**led设备号的设置*/
        ledNewSendmsgEntity.setIds_dev(idsDev);
        ledNewSendmsgEntity.setSno(2);

        String ledNewSendmsgEntityStr = "";
        ObjectMapper mapper1 = new ObjectMapper();
        mapper1.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            ledNewSendmsgEntityStr = mapper1.writeValueAsString(ledNewSendmsgEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        ledNewOrderEntity.setSendmsg(ledNewSendmsgEntityStr);

        StringBuilder sb = new StringBuilder();
        sb.append("sendmsg=" + ledNewOrderEntity.getSendmsg());
        sb.append("&ts=" + ts);
        sb.append("&type=" + type);
        sb.append("&user=" + Constant.USER_NAME);
        sb.append("&key=" + json.get("key"));
        String stringSignTemp = sb.toString();
        String token = MD(stringSignTemp).toUpperCase();
        ledNewOrderEntity.setToken(token);

        String send = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            send = mapper.writeValueAsString(ledNewOrderEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        String result = HttpUtils.sendPost(Constant.SUBMIT_TASKS, send);
        System.out.println("提交任务成功返回:" + result);

        return result;
    }

    /**
     * 获取提交任务状态
     *
     * @return
     */
    public String getState(String login, String ts, String sno) {
        JSONObject json = JSON.parseObject(login);

        LedNewOrderEntity ledNewOrderEntity = new LedNewOrderEntity();
        ledNewOrderEntity.setSno(sno);
        ledNewOrderEntity.setTs(ts);
        ledNewOrderEntity.setUser(Constant.USER_NAME);

        StringBuilder sb = new StringBuilder();
        sb.append("sno=" + sno);
        sb.append("&ts=" + ts);
        sb.append("&user=" + Constant.USER_NAME);
        sb.append("&key=" + json.get("key"));
        String stringSignTemp = sb.toString();
        String token = MD(stringSignTemp).toUpperCase();
        ledNewOrderEntity.setToken(token);

        String send = "";
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            send = mapper.writeValueAsString(ledNewOrderEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String result = HttpUtils.sendPost(Constant.QUERY_STATE, send);
        System.out.println("返回任务提交的状态:" + result);
        return result;
    }


    /**
     * 获取精确到秒的时间戳
     *
     * @return
     */
    public static int getSecondTimestamp(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0, length - 3));
        } else {
            return 0;
        }
    }
}