JRT监听程序

本次设计避免以往设计缺陷,老的主要为了保持兼容性,在用的设计就不好调了。

JRT监听程序_第1张图片

首先,接口抽象时候就不在给参数放仪器ID和处理类了,直接放仪器配置实体,接口实现想用什么属性就用什么属性,避免老方式要扩参数时候就波动接口定义,导致不兼容。
JRT监听程序_第2张图片

对返回命令取图或者上传文件等不在组串了,直接约定命令,返回查询上传数据或保存数据返回时候直接返回命令的JSON列表,再有监听提供的命令虚拟机执行命令

/*
本框架版权归属于JRT计划,任何单位或个人未经许可,不得以任何方式复制、传播、展示、发布、分发、重新分发、修改、反编译、
反向编译或以其他方式使用本框架的任何部分,包括但不限于源代码、二进制文件、文档、演示文稿、示例代码和API。
使用本框架的用户需遵守以下条款:
用户只能以个人学习和研究为目的使用本框架,不得将其用于商业用途。
用户在使用本框架时,应遵守所有适用的法律和法规,包括但不限于版权法、商标法、专利法和隐私权法。
用户在使用本框架时,应自行承担风险和责任,并确保不会侵犯任何知识产权或个人权利。
本框架的使用仅限于用户自己使用,不得将其分发给其他用户或将其用于任何形式的共享或传播。
在使用本框架时,用户应尊重和保护其他用户的隐私和个人信息,不得将其泄露给任何第三方。
违反以上条款将视为侵权行为,将采取法律手段维护JRT合法权益。
 */
package JRTMachineImpl.Cmd;

import java.util.List;
import JRTMachineImpl.Base.CmdDto;

/**
 * 构造指令,监听再也不按以前固定拼串方式来实现附加功能了,转而提供一系列指令约定,让后台虚拟M按需要返回指令列表,
 * 由监听实现的简单指令执行虚拟机执行,从而达到解耦和实现更灵活和强大的功能
 */
public class MakeCmd {
    /**
     * 添加一个SQL更新或者插入数据的指令到指令列表
     * @param cmdList 指令列表
     * @param setStatusKey 设置状态主键,有的话用该注解调用SetQryStatus设置状态
     * @param sql SQL语句
     * @param Address 连接串,如果上传要单独指定地址的话就传
     * @param UserName 用户名,如果上传要单独指定地址的话就传
     * @param UserPass 密码,如果上传要单独指定地址的话就传
     * @param Driver 驱动,如果上传要单独指定地址的话就传
     */
    public static void AddSqlCmd(List<CmdDto> cmdList,String setStatusKey,String sql,String Address,String UserName,String UserPass,String Driver)
    {
        CmdDto dto=new CmdDto();
        dto.Cmd="SQL";
        dto.P0=sql;
        dto.P1=Address;
        dto.P2=UserName;
        dto.P3=UserPass;
        dto.P4=Driver;
        dto.SetStatusKey=setStatusKey;
        cmdList.add(dto);
    }

    /**
     * 添加一个SQL更新或者插入数据的指令到指令列表
     * @param cmdList 指令列表
     * @param setStatusKey 设置状态主键,有的话用该注解调用SetQryStatus设置状态
     * @param sql SQL语句
     */
    public static void AddSqlCmd(List<CmdDto> cmdList,String setStatusKey,String sql)
    {
        AddSqlCmd(cmdList,setStatusKey,sql,"","","","");
    }

    /**
     * 添加一个写文本数据的指令到指令列表
     * @param cmdList 指令列表
     * @param setStatusKey 设置状态主键,有的话用该注解调用SetQryStatus设置状态
     * @param path 文本路径
     * @param str 写入的串
     * @param isAppend 是否追加
     * @param encode 编码 空默认系统编码,可为:UTF-8 UTF-16 ISO-8859-1 US-ASCII GB2312 GBK Big5 Shift-JIS EUC-KR Windows-1252
     */
    public static void AddTxtCmd(List<CmdDto> cmdList,String setStatusKey,String path,String str,String isAppend,String encode)
    {
        CmdDto dto=new CmdDto();
        dto.Cmd="TXT";
        dto.P0=path;
        dto.P1=str;
        dto.P2=isAppend;
        dto.P3=encode;
        dto.SetStatusKey=setStatusKey;
        cmdList.add(dto);
    }

    /**
     * 添加一个删除文件或者文件夹的指令到指令列表
     * @param cmdList 指令列表
     * @param path 路径
     */
    public static void AddRMCmd(List<CmdDto> cmdList,String path)
    {
        CmdDto dto=new CmdDto();
        dto.Cmd="RM";
        dto.P0=path;
        dto.SetStatusKey="";
        cmdList.add(dto);
    }

    /**
     * 添加一个拷贝文件的指令到指令列表
     * @param cmdList 指令列表
     * @param filePathOld 源文件路径
     * @param filePathNew 新文件路径
     */
    public static void AddCPCmd(List<CmdDto> cmdList,String filePathOld,String filePathNew)
    {
        CmdDto dto=new CmdDto();
        dto.Cmd="CP";
        dto.P0=filePathOld;
        dto.P1=filePathNew;
        dto.SetStatusKey="";
        cmdList.add(dto);
    }

    /**
     * 添加一个获取图片的指令到指令列表
     * @param cmdList 指令列表
     * @param epis 流水号
     * @param imgClass 图片类别代码
     * @param imgPath 图片路径
     * @param newName 新名称,不给的话就是文件名
     */
    public static void AddGetImageCmd(List<CmdDto> cmdList,String epis,String imgClass,String imgPath,String newName)
    {
        if(newName==null)
        {
            newName="";
        }
        CmdDto dto=new CmdDto();
        dto.Cmd="GETIMAGE";
        dto.P0=epis;
        dto.P1=imgClass;
        dto.P2=imgPath;
        dto.P3=newName;
        dto.SetStatusKey="";
        cmdList.add(dto);
    }
}

命令执行器

/*
本框架版权归属于JRT计划,任何单位或个人未经许可,不得以任何方式复制、传播、展示、发布、分发、重新分发、修改、反编译、
反向编译或以其他方式使用本框架的任何部分,包括但不限于源代码、二进制文件、文档、演示文稿、示例代码和API。
使用本框架的用户需遵守以下条款:
用户只能以个人学习和研究为目的使用本框架,不得将其用于商业用途。
用户在使用本框架时,应遵守所有适用的法律和法规,包括但不限于版权法、商标法、专利法和隐私权法。
用户在使用本框架时,应自行承担风险和责任,并确保不会侵犯任何知识产权或个人权利。
本框架的使用仅限于用户自己使用,不得将其分发给其他用户或将其用于任何形式的共享或传播。
在使用本框架时,用户应尊重和保护其他用户的隐私和个人信息,不得将其泄露给任何第三方。
违反以上条款将视为侵权行为,将采取法律手段维护JRT合法权益。
 */
package JRTMachineImpl.Cmd;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

import JRTMachineImpl.Base.BaseDeal;
import JRTMachineImpl.Base.CmdDto;
import JRTMachineImpl.Base.ConfDto;
import JRTMachineImpl.Util.FileService;
import JRTMachineImpl.Util.LogUtils;
import JRTMachineImpl.WebService.OutValue;
import JRTMachineImpl.WebService.Parameters;
import JRTMachineImpl.WebService.WebGetData;
import JRTMachineImpl.DB.DBHelper;

/**
 * 指令执行的虚拟机,按指令约定执行指令
 */
public class CmdVM {
    /**
     * 执行指令
     * @param cmdList 指令列表
     * @param conf 监听配置
     * @return 返回值,正常返回空,否则返回错误
     */
    public static String ExeCmd(List<CmdDto> cmdList, JRTMachineImpl.Base.ConfDto conf) throws Exception
    {
        //循环执行指令
        for(CmdDto cmd:cmdList)
        {
            try {
                ExeOneCmd(cmd, conf);
            }
            catch (Exception ex)
            {
                StringWriter stringWriter = new StringWriter();
                ex.printStackTrace(new PrintWriter(stringWriter));
                LogUtils.WriteDebugLog("执行命令异常:" + stringWriter.toString());
                LogUtils.WriteExceptionLog("执行命令异常", ex);
            }
        }
        return "";
    }

    /**
     * 执行指令
     * @param cmd 指令
     * @param conf 监听配置
     * @return 返回值,正常返回空,否则返回错误
     */
    public static String ExeOneCmd(CmdDto cmd, JRTMachineImpl.Base.ConfDto conf) throws Exception
    {
        //执行SQL
        if(cmd.Cmd.equals("SQL"))
        {
            ConfDto confNew=new ConfDto();
            confNew.Address=conf.Address;
            confNew.UserName=conf.UserName;
            confNew.UserPass=conf.UserPass;
            confNew.Driver=conf.Driver;
            //命令指定了要换驱动
            if(cmd.P1!=null&&!cmd.P1.isEmpty())
            {
                confNew.Address=cmd.P1;
                confNew.UserName=cmd.P2;
                confNew.UserPass=cmd.P3;
                confNew.Driver=cmd.P4;
            }
            JRTMachineImpl.DB.DBHelper dbHelper=new DBHelper(confNew);
            LogUtils.WriteDebugLog("执行SQL:"+cmd.P0);
            //执行SQL语句
            int ret=dbHelper.ExecUpdateSQL(cmd.P0);
            LogUtils.WriteDebugLog("执行SQL返回:"+ret);
            //设置状态
            if(ret==1&&!cmd.SetStatusKey.isEmpty())
            {
                Parameters param=new Parameters();
                param.P0=conf.MachID;
                param.P1=cmd.SetStatusKey;
                OutValue session = new OutValue();
                OutValue out = new OutValue();
                LogUtils.WriteDebugLog("调用:"+conf.DealProcess+"的:SetQryStatus设置状态");
                String setStatusRet=WebGetData.GetData(BaseDeal.WebServicAddress,conf.DealProcess,"SetQryStatus",param,session,out);
                LogUtils.WriteDebugLog("设置状态返回:"+setStatusRet);
            }
        }
        //写文件
        else if(cmd.Cmd.equals("TXT"))
        {
            File fi=new File(cmd.P0);
            boolean isAppend=false;
            if(cmd.P2.equals("1"))
            {
                isAppend=true;
            }
            String model=",模式:覆盖";
            if(isAppend==true)
            {
                model=",模式:追加";
            }
            LogUtils.WriteDebugLog("往文件:"+cmd.P0+",写入:"+cmd.P1+model+",编码:"+cmd.P3);
            JRTMachineImpl.Util.TxtUtil.WriteText2File(fi,cmd.P1,isAppend,cmd.P3);
            //设置状态
            if(!cmd.SetStatusKey.isEmpty())
            {
                Parameters param=new Parameters();
                param.P0=conf.MachID;
                param.P1=cmd.SetStatusKey;
                OutValue session = new OutValue();
                OutValue out = new OutValue();
                LogUtils.WriteDebugLog("调用:"+conf.DealProcess+"的:SetQryStatus设置状态");
                String setStatusRet=WebGetData.GetData(BaseDeal.WebServicAddress,conf.DealProcess,"SetQryStatus",param,session,out);
                LogUtils.WriteDebugLog("设置状态返回:"+setStatusRet);
            }
        }
        //删除文件和目录
        else if(cmd.Cmd.equals("RM"))
        {
            LogUtils.WriteDebugLog("删除:"+cmd.P0);
            JRTMachineImpl.Util.DirUtil.DeleteFileOrDir(new File(cmd.P0));
        }
        //拷贝文件
        else if(cmd.Cmd.equals("CP"))
        {
            Path source = Paths.get(cmd.P0);
            Path target = Paths.get(cmd.P1);
            LogUtils.WriteDebugLog("拷贝:"+cmd.P0+"到:"+cmd.P1);
            Files.copy(source,target, StandardCopyOption.REPLACE_EXISTING);
        }
        //得到图片
        else if(cmd.Cmd.equals("GETIMAGE"))
        {
            FileService fileService=new FileService();
            Parameters param=new Parameters();
            param.P0=conf.MachID;
            OutValue session = new OutValue();
            OutValue out = new OutValue();
            LogUtils.WriteDebugLog("调用:"+conf.DealProcess+"的:GetFileService得到文件服务路径");
            String fileServerPath=WebGetData.GetData(BaseDeal.WebServicAddress,conf.DealProcess,"GetFileService",param,session,out);
            LogUtils.WriteDebugLog("文件服务路径:"+fileServerPath);
            File fi=new File(cmd.P2);
            if(fi.exists())
            {
                String [] arr=fileServerPath.split("\\^");
                LogUtils.WriteDebugLog("准备上传:"+cmd.P2+",新名称:"+cmd.P3+",相对路径:"+arr[1]+"到:"+arr[0]);
                String ret=fileService.Upload(arr[0],cmd.P2,cmd.P3,arr[1]);
                LogUtils.WriteDebugLog("上传返回:"+ret);
                if(ret.isEmpty())
                {
                    param=new Parameters();
                    param.P0=conf.MachID;
                    param.P1=cmd.P0;
                    param.P2=cmd.P1;
                    param.P3=fi.getName();
                    param.P4=fi.getAbsolutePath();
                    LogUtils.WriteDebugLog("调用:"+conf.DealProcess+"的:SaveImage保存文件路径");
                    String saveRet=WebGetData.GetData(BaseDeal.WebServicAddress,conf.DealProcess,"SaveImage",param,session,out);
                    LogUtils.WriteDebugLog("保存文件返回:"+saveRet);
                }
                else
                {
                    LogUtils.WriteDebugLog("上传文件服务失败:"+ret);
                }
            }
            else
            {
                LogUtils.WriteDebugLog("文件:"+cmd.P2+"不存在");
            }
        }
        return "";
    }
}

定时上传执行命令
JRT监听程序_第3张图片

保存返回执行命令
JRT监听程序_第4张图片

仪器业务处理示例

import JRT.Core.Dto.CmdDto;
import JRT.Core.Dto.OutValue;
import JRT.Core.Util.LogUtils;
import JRT.Core.Util.MakeCmdUtil;
import JRT.Core.Util.TimeParser;
import JRTBLLBase.BaseHttpHandlerNoSession;
import JRTBLLBase.Helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 监听模式的仪器测试
 */
public class JRTMachineTest extends BaseHttpHandlerNoSession {
    /**
     * 记录已经上传的数据
     */
    private static HashMap<String, Boolean> hasUpData = new HashMap();

    /**
     * 保存仪器数据
     *
     * @param mi        仪器主键
     * @param data      数据
     * @param epis      流水号
     * @param fileName  文件全名
     * @param DBColName 数据库列名
     * @param index     序号,-1为最后一行
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     * @throws Exception
     */
    public String SaveData(String mi, String data, String epis, String fileName, String DBColName, String index, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //先写Log测试
        LogUtils.WriteDebugLog("mi:" + mi + ",data:" + data + ",epis:" + epis + ",fileName:" + fileName + ",DBColName:" + DBColName);
        //返回的数据
        List<CmdDto> cmdList = new ArrayList<>();
        MakeCmdUtil.AddGetImageCmd(cmdList, "998", "P2", "D:\\OUT\\2.bmp", "");
        return Helper.Object2Json(cmdList);
    }

    /**
     * 得到文件服务地址供接口上传图片
     *
     * @param mi
     * @param P1
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     * @throws Exception
     */
    public String GetFileService(String mi, String P1, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //返回文件服务路径
        return "http://localhost:8080/JRTWeb/FileService/^/zlzmach/" + TimeParser.GetNowDate();
    }

    /**
     * 保存文件到数据库
     *
     * @param mi
     * @param epis
     * @param ImageClass
     * @param fileName
     * @param FullName
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     * @throws Exception
     */
    public String SaveImage(String mi, String epis, String ImageClass, String fileName, String FullName, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //先写Log测试
        LogUtils.WriteDebugLog("mi:" + mi + ",epis:" + epis + ",ImageClass:" + ImageClass + ",fileName:" + fileName + ",FullName:" + FullName);
        return "";
    }

    /**
     * 查询要上传的指令
     *
     * @param mi      仪器
     * @param P1
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     * @throws Exception
     */
    public String QryUpdata(String mi, String P1, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //先写Log测试
        LogUtils.WriteDebugLog("mi:" + mi + ",调用上传查询");
        //返回的数据
        List<CmdDto> cmdList = new ArrayList<>();
        //读文本仪器
        if (mi.equals("1")) {
            if (!hasUpData.containsKey("0947809")) {
                MakeCmdUtil.AddTxtCmd(cmdList, "0947809", "D:\\OUT\\uptxt.dttmp", "这是JRT上传的文本串", "0", "");
                MakeCmdUtil.AddCPCmd(cmdList, "D:\\OUT\\uptxt.dttmp", "D:\\OUT\\uptxt.dt");
                MakeCmdUtil.AddRMCmd(cmdList, "D:\\OUT\\uptxt.dttmp");
            }
        }
        //读数据库仪器
        else if (mi.equals("2")) {
            if (!hasUpData.containsKey("0947810")) {
                MakeCmdUtil.AddSqlCmd(cmdList, "0947810", "insert into DBUpHistory(DataCode,KeyData,DateStr,Data) values('0947809','1','这是JRT用SQL插入的数据','1')");
            }
        }
        MakeCmdUtil.AddGetImageCmd(cmdList, "999", "P1", "D:\\OUT\\1.bmp", "");
        return Helper.Object2Json(cmdList);
    }

    /**
     * 设置上传指令执行状态
     *
     * @param mi           仪器
     * @param setStatusKey 设置状态的主键
     * @param P2
     * @param P3
     * @param P4
     * @param P5
     * @param P6
     * @param P7
     * @param P8
     * @param P9
     * @param P10
     * @param P11
     * @param P12
     * @param P13
     * @param Session
     * @param Output
     * @return
     * @throws Exception
     */
    public String SetQryStatus(String mi, String setStatusKey, String P2, String P3, String P4, String P5, String P6, String P7, String P8, String P9, String P10, String P11, String P12, String P13, OutValue Session, OutValue Output) throws Exception {
        //先写Log测试
        LogUtils.WriteDebugLog("mi:" + mi + ",setStatusKey:" + setStatusKey + "设置状态");
        hasUpData.put(setStatusKey, true);
        return "";
    }
}

这样,一套超越以前设计的监听架构就出来了。JRT拥有Web、打印导出Client、JRT浏览器、JRTMachine、jrt运维命令、齐全了

你可能感兴趣的:(python,java,数据库)