package net.vtstar.zhongtong.avi;

import jcifs.smb.SmbFile;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.utils.SmbFileUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;

@ActiveProfiles("xdw")
@SpringBootTest
@Slf4j
public class ApplicationTests {

    @Test
    public void contextLoads() {
    }
    private String baseUrl = "D:/scada/assemblynewenergy/";
    @Test
    public void shareTestxgsk() throws Exception {
        Date startOfDay = DateUtils.getStartOfDay(0);
        String dateString = DateUtils.parseDateToString(startOfDay, "yyyy-MM-dd");
//        String url = "smb://10.1.4.107/数控下料/下料计划01.06.xls";
        log.error(String.valueOf(new Date().getTime()));
        String url = "smb://admin:123456@10.1.25.128/an9637h/testdata@2019-04-10.mdb";
        SmbFile file = new SmbFile(url);
        if (file.exists()) {
            SmbFileUtil.smbGet(url, baseUrl);
//            SmbFileUtil.smbPut("smb://10.1.4.107/数控下料/", "D:/error.xls");
        }else {
            log.error("日期: {} 的总装车间新能源检测点共享文件不存在!", dateString);
        }
        log.error(String.valueOf(new Date().getTime()));
    }

    @Test
    public void socketTest() throws Exception{
        String aByte = "<get><var>.P107</var><auto>yes</auto></get>\n";
        Socket socket = new Socket("172.18.81.191",62944);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(aByte.getBytes());
        outputStream.flush();
        socket.shutdownOutput();

        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = inputStream.read(bytes)) != -1) {
            //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
            sb.append(new String(bytes, 0, len,"UTF-8"));
        }
        System.out.println("get message from server: " + sb);

        inputStream.close();
        outputStream.close();
        socket.close();
    }

    @Test
    public void testMoodbus1() throws Exception {
        Integer newValue = 3;
        int j = 0;
        while (j < 8){
            System.out.println((newValue >> j & 0x01));
            j ++;
        }
    }

}
