Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-package / src / test / java / CommonTest.java
@xiongkai xiongkai 29 days ago 17 KB 数据接收程序
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.img.gif.AnimatedGifEncoder;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.newfiber.common.core.constant.SecurityConstants;
import com.newfiber.common.core.utils.JwtUtils;
import com.newfiber.common.core.utils.file.FileUtils;
import com.newfiber.common.core.utils.uuid.IdUtils;
import com.newfiber.common.security.utils.PwdUtil;
import com.newfiber.common.security.utils.SecurityUtils;
import com.newfiber.termite.domain.jingchu.JingChuUpload;
import com.newfiber.termite.domain.request.deviceInfo.DeviceInfoStateSyncRequest;
import com.newfiber.termite.service.impl.DataPublishService.MqttPublishConfig;
import com.newfiber.termite.service.impl.DataPublishService.PublishConfig;
import com.newfiber.termite.util.CosUtil;
import io.jsonwebtoken.Claims;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class CommonTest {

    @Test
    public void fileUploadTest(){
        MultipartFile file = FileUtils.getMultipartFile(new File("C:\\Users\\Administrator\\Pictures\\201210133348076.jpg"));
        System.out.println(CosUtil.putFile(file));
    }

    @Test
    public void jingchuTest(){
        String url = "https://sk.hubeishuiyi.cn/bylr";
        String token = "eyJraWQiOiJrMSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJhZG1pbkBnaXN1bmkuY29tIiwiYXVkIjoidXVtcyIsImp0aSI6IjYwMGZiYmMyLWE1NDgtNGZmNC04ZGNhLTZkNTdkZjY1OTYxNyIsImlhdCI6MTcxNzcyNDA1MywibmJmIjoxNzE3NzIzNDUzLCJzdWIiOiJzdXBlciIsImV4cCI6MTcxNzc4NTQ5Mywia2V5IjoiMmQ5ZWY2MTNlYTkxNDc4MDhlOGZiYTMxYWNlOTE0NmEiLCJpZCI6IjEiLCJuYW1lIjoic3VwZXIiLCJuaWNrTmFtZSI6Iui2hee6p-euoeeQhuWRmCIsImFjY291bnQiOiJzdXBlciIsInR5cGUiOiIyIiwiY2xpZW50IjpudWxsLCJjbGllbnRfaWQiOiIiLCJ0b2tlblR5cGUiOiJVU0VSIn0.VzETse76m1rkRsMjst9kDr1oCnHrPq81pRs-34bjVXPDRiPT6ekVCm-63hLfoH4GAp25oTL9dWA1ax-t1KWlSzHUn82myEBokwCVWlkZZPVMzFMCgP_SNdFeYlvguVhige1nQ6B0wo5crGYKKsfllt0_bp98TFg3I3zF03WsORk7HlsZfsY7ROwYkXXkIgbdw5gIRYlZJ6W1MiMET9TOyAzWGJvEyTPewaW4XMoq-okZoAe_7gV2u_tylngqRCohxTSuA7xf59gK8OrstwTeId_qQp4remoEDdLJfYyzLXCX5xBKns8gw2i1MZfpq9rizDT-Qdz23VQDQdmeHWK7zQ";

        JingChuUpload jingChuUpload = new JingChuUpload();
        jingChuUpload.setMnNo("4202813006");
        jingChuUpload.setObDate(DateUtil.formatDateTime(new Date()).replace(" ", "T"));
        jingChuUpload.setObTime(DateUtil.format(new Date(), "yyMMddHHmm"));
        jingChuUpload.setOrder("123456");
        jingChuUpload.setReceiveType(0);
        jingChuUpload.setStationId(1625);
        jingChuUpload.setStationName("大晚垅水库");
        jingChuUpload.setDeviceId(5858);
        jingChuUpload.setDeviceName("大晚垅水库白蚁监测站");

        List<JingChuUpload> jingChuUploadList = new ArrayList<>();
        jingChuUploadList.add(jingChuUpload);
        HttpResponse httpResponse = HttpUtil.createPost(url.concat("/waterDataTermites/insertDataBatch")).
                body(JSONObject.toJSONString(jingChuUploadList)).timeout(1000).
                header("Authorization", token).execute();
        log.info("同步荆楚监测数据.请求:{},响应:{}", JSONObject.toJSONString(jingChuUploadList), httpResponse.body());

    }

    @Test
    public void imgUtil(){
        Font font = new Font("微软雅黑", Font.BOLD, 50);

//        ImgUtil.pressText(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"),
//                new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"),
//                "Hello\r\n" + "World", Color.WHITE, font, -1, -1, 1f);

        Image srcImage = ImgUtil.read(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"));
        Img img = Img.from(srcImage);
        img.setPositionBaseCentre(false);
        img = img.pressText("项目:皇田水库", Color.WHITE, font, 0, 900, 1f);
        img = img.pressText("位置:3-5", Color.WHITE, font, 0, 1000, 1f);
        img = img.pressText("时间:2024/05/01 13:00", Color.WHITE, font, 0, 1100, 1f);

        ImgUtil.write(img.getImg(), new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"));
    }

    @Test
    public void jsonTest(){
        String str = "{\"sn\":\"123\",\"AP\":\"123\"}";
        DeviceInfoStateSyncRequest request = JSONObject.parseObject(str, DeviceInfoStateSyncRequest.class);
        System.out.println(1);
    }

    @Test
    public void jwtTest(){
        Map<String, Object> claimsMap = new HashMap<String, Object>();
        claimsMap.put(SecurityConstants.USER_KEY, IdUtils.fastUUID());
        claimsMap.put(SecurityConstants.DETAILS_USER_ID, 1);
        claimsMap.put(SecurityConstants.DETAILS_USERNAME, "admin");

        // 接口返回信息
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put("access_token", JwtUtils.createToken(claimsMap));
        rspMap.put("expires_in", 1);

        System.out.println(rspMap.get("access_token"));
    }

    @Test
    public void checkToken(){
        Claims claims = JwtUtils.parseToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjJkNWVlNTM1LWViMDAtNGE3My05ZjY5LWE3YmJhZjY5ZDBhOCIsInVzZXJuYW1lIjoiYWRtaW4ifQ.lNVhnljLEsJKQCtvWazCDpWHDPKCshz6XUgAnpVkCbN0fwM3wZ_wU3aKl8YS3ravoZ1ifW7MVFArphDaQeIAQg");

        System.out.println("1");
    }

    @Test
    public void passTest(){
        String password = SecurityUtils.transferAesEncrypt("Q+AX0BXUxw/bn9mZRVib2w==");
        PwdUtil.passwordCheck(password);
        System.out.println(SecurityUtils.encryptPassword(password));
    }

    @Test
    public void gifTest(){
        try {
            List<File> fileList = FileUtil.loopFiles("C:\\Users\\Administrator\\Desktop\\GIF");
            AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
            animatedGifEncoder.start(Files.newOutputStream(Paths.get("C:\\Users\\Administrator\\Desktop\\GIF\\result.gif")));
            animatedGifEncoder.setDelay(1000);
            animatedGifEncoder.setRepeat(0);
            for(File file : fileList){
                if (file.exists()) {
                    BufferedImage image = ImageIO.read(file);
                    animatedGifEncoder.addFrame(image);
                }
            }
            animatedGifEncoder.finish();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Test
    public void jsonTest2(){
        String json = "[{\"1\":\"POLYGON ((113.08238569074805 30.886677342980505, 113.08228169066227 30.886651342957077, 113.08226152857034 30.886644279821677, 113.0822435029332 30.886633792943307, 113.08222839219839 30.886620335204313, 113.08221684893083 30.88660448778531, 113.0822093716314 30.88658693506639, 113.0822062832092 30.886568435071617, 113.0822077170369 30.88654978673324, 113.08222571713107 30.886459786747526, 113.0822308770896 30.88644342740129, 113.08223952188014 30.886428189356902, 113.08225134448178 30.886414613792912, 113.08226592501421 30.886403182844706, 113.08228274564996 30.88639430248162, 113.08230120900498 30.886388288089012, 113.08232065935462 30.88638535326751, 113.08234040592147 30.886385602247024, 113.08235974740813 30.886389026185057, 113.08258374722601 30.88644802613211, 113.08260190559976 30.886454462071587, 113.0826183451007 30.88646369351549, 113.0826324879332 30.886475396008546, 113.08264383702151 30.88648915824599, 113.08265199348048 30.886504496529515, 113.08265667063515 30.88652087176762, 113.08265770409662 30.886537708422903, 113.08265505754008 30.88655441474031, 113.0826330576217 30.886635414757578, 113.08262641477903 30.886652186869323, 113.08261606987212 30.88666751614437, 113.08260242427865 30.886680807811377, 113.08258600744259 30.88669154615739, 113.08256745633197 30.88669931453743, 113.08254749072479 30.88670381154033, 113.08252688528151 30.88670486268348, 113.08250643948831 30.88670242718278, 113.08239043938856 30.886678427160877, 113.08238569074805 30.886677342980505))\"},{\"2\":\"POLYGON ((113.08426649884075 30.88700205825135, 113.08377949875292 30.88664105799484, 113.08377768575333 30.886639682370653, 113.08358968572963 30.886493682269016, 113.08357619677848 30.886480995007233, 113.08356576423999 30.88646633848035, 113.08355876860088 30.88645024723088, 113.08355546499926 30.88643330812723, 113.0835559739197 30.88641613895984, 113.08356027679918 30.88639936590952, 113.08356821670452 30.886383600709873, 113.08357950405616 30.886369418336685, 113.08359372718958 30.8863573360379, 113.0836103673695 30.88634779446911, 113.08362881770864 30.886341141622363, 113.08364840530167 30.886337620134615, 113.08366841576657 30.88633735843857, 113.08368811929886 30.886340366078567, 113.08370679728816 30.886346533362538, 113.08372376852648 30.886355635362566, 113.08426776835951 30.886713635095848, 113.08428765336738 30.886730833821627, 113.08441865342343 30.886879833737368, 113.08442931209186 30.88689479565861, 113.08443638465587 30.8869112482805, 113.08443960218771 30.886928566011616, 113.0844388423424 30.886946090365445, 113.08443413401 30.886963154998533, 113.08442565621755 30.886979111047413, 113.08441373132192 30.886993351801014, 113.08439881275288 30.887005335770166, 113.08438146777198 30.88701460727731, 113.0843623559031 30.887020813783252, 113.08434220385469 30.887023719292106, 113.08432177788725 30.887023213324966, 113.08430185467681 30.887019315120682, 113.08428319178248 30.887012172904395, 113.08426649884075 30.88700205825135))\"},{\"3\":\"POLYGON ((113.0829207971742 30.88438550689384, 113.0819247970957 30.885189507443656, 113.08190848827313 30.885200289675584, 113.08189003785925 30.885208127483097, 113.08187015795454 30.885212718362947, 113.08184961583169 30.8852138851283, 113.08182920432219 30.885211582747367, 113.08180971121637 30.885205900081495, 113.08179188885818 30.885197056455397, 113.08177642510785 30.885185393192238, 113.08176391679369 30.885171360440076, 113.08175484667721 30.885155499798, 113.08174956482088 30.88513842341293, 113.08174827507759 30.885120790353465, 113.08175102722319 30.885103281172835, 113.08175771503568 30.885086571642802, 113.08195771533056 30.884711571789335, 113.08197188441557 30.88469206384406, 113.08239788454087 30.884245064109745, 113.08241174303755 30.884233035257385, 113.08242798109733 30.884223446393758, 113.08244602421425 30.884216636774468, 113.08246523401965 30.88421284732494, 113.08248493086755 30.884212212116505, 113.08285493052185 30.88423021209963, 113.08287500730594 30.884232881657, 113.08289410653968 30.884238825783534, 113.08291151032977 30.88424782105423, 113.08292656450993 30.884259529358946, 113.08293870322946 30.88427351061118, 113.08294747022204 30.88428923928966, 113.08295253595587 30.88430612419135, 113.08295371002015 30.88432353065324, 113.08295094828253 30.8843408044078, 113.08294435454812 30.88435729617527, 113.0829341766581 30.884372386068662, 113.0829207971742 30.88438550689384))\"}]";
        JSONArray jsonArray = JSONObject.parseArray(json);
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "POLYGON ((113.08238569074805 30.886677342980505, 113.08228169066227 30.886651342957077, 113.08226152857034 30.886644279821677, 113.0822435029332 30.886633792943307, 113.08222839219839 30.886620335204313, 113.08221684893083 30.88660448778531, 113.0822093716314 30.88658693506639, 113.0822062832092 30.886568435071617, 113.0822077170369 30.88654978673324, 113.08222571713107 30.886459786747526, 113.0822308770896 30.88644342740129, 113.08223952188014 30.886428189356902, 113.08225134448178 30.886414613792912, 113.08226592501421 30.886403182844706, 113.08228274564996 30.88639430248162, 113.08230120900498 30.886388288089012, 113.08232065935462 30.88638535326751, 113.08234040592147 30.886385602247024, 113.08235974740813 30.886389026185057, 113.08258374722601 30.88644802613211, 113.08260190559976 30.886454462071587, 113.0826183451007 30.88646369351549, 113.0826324879332 30.886475396008546, 113.08264383702151 30.88648915824599, 113.08265199348048 30.886504496529515, 113.08265667063515 30.88652087176762, 113.08265770409662 30.886537708422903, 113.08265505754008 30.88655441474031, 113.0826330576217 30.886635414757578, 113.08262641477903 30.886652186869323, 113.08261606987212 30.88666751614437, 113.08260242427865 30.886680807811377, 113.08258600744259 30.88669154615739, 113.08256745633197 30.88669931453743, 113.08254749072479 30.88670381154033, 113.08252688528151 30.88670486268348, 113.08250643948831 30.88670242718278, 113.08239043938856 30.886678427160877, 113.08238569074805 30.886677342980505))");
        map.put("2", "POLYGON ((113.08426649884075 30.88700205825135, 113.08377949875292 30.88664105799484, 113.08377768575333 30.886639682370653, 113.08358968572963 30.886493682269016, 113.08357619677848 30.886480995007233, 113.08356576423999 30.88646633848035, 113.08355876860088 30.88645024723088, 113.08355546499926 30.88643330812723, 113.0835559739197 30.88641613895984, 113.08356027679918 30.88639936590952, 113.08356821670452 30.886383600709873, 113.08357950405616 30.886369418336685, 113.08359372718958 30.8863573360379, 113.0836103673695 30.88634779446911, 113.08362881770864 30.886341141622363, 113.08364840530167 30.886337620134615, 113.08366841576657 30.88633735843857, 113.08368811929886 30.886340366078567, 113.08370679728816 30.886346533362538, 113.08372376852648 30.886355635362566, 113.08426776835951 30.886713635095848, 113.08428765336738 30.886730833821627, 113.08441865342343 30.886879833737368, 113.08442931209186 30.88689479565861, 113.08443638465587 30.8869112482805, 113.08443960218771 30.886928566011616, 113.0844388423424 30.886946090365445, 113.08443413401 30.886963154998533, 113.08442565621755 30.886979111047413, 113.08441373132192 30.886993351801014, 113.08439881275288 30.887005335770166, 113.08438146777198 30.88701460727731, 113.0843623559031 30.887020813783252, 113.08434220385469 30.887023719292106, 113.08432177788725 30.887023213324966, 113.08430185467681 30.887019315120682, 113.08428319178248 30.887012172904395, 113.08426649884075 30.88700205825135))");
        map.put("3", "POLYGON ((113.0829207971742 30.88438550689384, 113.0819247970957 30.885189507443656, 113.08190848827313 30.885200289675584, 113.08189003785925 30.885208127483097, 113.08187015795454 30.885212718362947, 113.08184961583169 30.8852138851283, 113.08182920432219 30.885211582747367, 113.08180971121637 30.885205900081495, 113.08179188885818 30.885197056455397, 113.08177642510785 30.885185393192238, 113.08176391679369 30.885171360440076, 113.08175484667721 30.885155499798, 113.08174956482088 30.88513842341293, 113.08174827507759 30.885120790353465, 113.08175102722319 30.885103281172835, 113.08175771503568 30.885086571642802, 113.08195771533056 30.884711571789335, 113.08197188441557 30.88469206384406, 113.08239788454087 30.884245064109745, 113.08241174303755 30.884233035257385, 113.08242798109733 30.884223446393758, 113.08244602421425 30.884216636774468, 113.08246523401965 30.88421284732494, 113.08248493086755 30.884212212116505, 113.08285493052185 30.88423021209963, 113.08287500730594 30.884232881657, 113.08289410653968 30.884238825783534, 113.08291151032977 30.88424782105423, 113.08292656450993 30.884259529358946, 113.08293870322946 30.88427351061118, 113.08294747022204 30.88428923928966, 113.08295253595587 30.88430612419135, 113.08295371002015 30.88432353065324, 113.08295094828253 30.8843408044078, 113.08294435454812 30.88435729617527, 113.0829341766581 30.884372386068662, 113.0829207971742 30.88438550689384))");
        System.out.println(JSONObject.toJSONString(map));
        for (Object o : jsonArray) {
            JSONObject jsonObject = (JSONObject) o;
        }
        System.out.println(1);
    }

    @Test
    public void tokenTest(){

        Claims claims = JwtUtils.parseToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjhkOTIyMTczLTVlZjAtNGI2My04NTc3LTI3ZmYwYWUxOWMwYyIsInVzZXJuYW1lIjoiYWRtaW4ifQ.JJqSIam9TBFhsvsybkdlqJgmz0CD1ciuavuLZ4zxB8OApFEbBL3tDH_Ilv_8tKpPUCKkF5QkzqHzaWhQS5yQ6g");
        String userkey = JwtUtils.getUserKey(claims);
        System.out.println(userkey);
    }

    @Test
    public void jsonTest3(){
        PublishConfig publishConfig = new PublishConfig();
        MqttPublishConfig mqttPublishConfig = new MqttPublishConfig();
        mqttPublishConfig.setPort(8383);
        mqttPublishConfig.setUrl("139.155.49.237");
        mqttPublishConfig.setUserName("xiongkai");
        mqttPublishConfig.setUserPassword("xk123");

        publishConfig.setPublishType("mqtt");
        publishConfig.setMqttPublishConfig(mqttPublishConfig);
        System.out.println(JSONObject.toJSONString(publishConfig));
    }

    @Test
    public void excelTest(){
        String format = "update ter_device_info set lonandlat = '%s,%s' where sn = '%s';";
        List<List<Object>> excelData = ExcelUtil.getReader("E:\\白蚁防治安装点位(高峰寺水库).xlsx").read(2);

        for(List<Object> row : excelData){
            String sn = row.get(1).toString();
//            String lon = row.get(3).toString().replace("°E", "");
//            String lat = row.get(2).toString().replace("°N", "");
//            System.out.printf((format) + "%n", lon, lat, sn);

            String lonlat = row.get(2).toString();
            System.out.printf(("update ter_device_info set lonandlat = '%s' where sn = '%s';") + "%n", lonlat, sn);
        }
    }
}