MESHelper.java 4.65 KB
package com.huaheng.api.mes.service;

import com.alibaba.fastjson.JSONObject;
import com.huaheng.api.mes.domain.MESUri;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.system.config.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

@Service
public class MESHelper {
    private ApiLog apiLog;
    @Autowired
    private IConfigService configService;
    public synchronized HttpURLConnection initUrlConn(String url, String paras)
            throws Exception {

        System.out.println(paras);
        URL postUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) postUrl
                .openConnection();
        String MESCookie = configService.selectConfigByKey("MESCookie");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST");// 设置请求方式
        connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式
        connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式
        connection.setRequestProperty("Authorization", MESCookie);
        apiLog = ApiLogAspect.initApiLog(connection, paras, "MES");
//        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        //        out.writeBytes(paras);
        OutputStreamWriter out = null;
        out = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");// utf-8编码
        out.append(paras);
        out.flush();
        out.close();
        return connection;
    }

    public void insertAgvPort(String param) throws Exception {
        HttpURLConnection connection = initUrlConn(MESUri.INSERT_AGV_PORT.getApiUri(), param);
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        String sResult=null;
        while ((line = reader.readLine()) != null) {
            sResult = new String(line.getBytes(), "UTF-8");
            System.out.println(sResult);
        }
        reader.close();
        connection.disconnect();
        ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
//      HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
    }

    public void insertContainer(String param) throws Exception {
        HttpURLConnection connection = initUrlConn(MESUri.INSERT_CONTAINER.getApiUri(), param);
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        String sResult=null;
        while ((line = reader.readLine()) != null) {
            sResult = new String(line.getBytes(), "UTF-8");
            System.out.println(sResult);
        }
        reader.close();
        connection.disconnect();
        ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
//      HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
    }

    public void returnMESEmptyFinsh(String param) throws Exception {
        HttpURLConnection connection = initUrlConn(MESUri.returnEmptyFinish.getApiUri(), param);
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        String sResult=null;
        while ((line = reader.readLine()) != null) {
            sResult = new String(line.getBytes(), "UTF-8");
            System.out.println(sResult);
        }
        reader.close();
        connection.disconnect();
        ApiLogAspect.finishApiLog(apiLog,connection, AjaxResult.success(sResult).toString());
//      HttpUtils.bodypost(MESUri.INSERT_AGV_PORT.getApiUri(),param,"CS0001");
    }

    public JSONObject returnOrderCode(String param) throws Exception {
        HttpURLConnection connection = initUrlConn(MESUri.returnOrderCode.getApiUri(), param);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line;
        String sResult = null;
        while ((line = reader.readLine()) != null) {
            sResult= new String(line.getBytes(), "utf-8");
            System.out.println(sResult);
        }
        JSONObject jsonObject = JSONObject.parseObject(sResult);
        connection.disconnect();
        return jsonObject;
    }
}