package com.dy.pipirrComCreator.console;
|
|
import com.dy.common.mw.protocol.p206V1.CodeV1;
|
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
|
import com.dy.pipirrComCreator.ServerProperties;
|
import com.dy.pipirrComCreator.p206V1.Cd02;
|
import com.dy.pipirrComCreator.p206V1.Cd10;
|
import com.dy.pipirrComCreator.p206V1.Cd92;
|
import com.dy.pipirrComCreator.p206V1.Cd93;
|
|
import java.io.PrintWriter;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
public class Command {
|
|
private static String[] commands ;
|
static{
|
commands = new String[]{
|
"config 查看配置信息",
|
"XY ... 协议命令",
|
"exit 退出",
|
};
|
}
|
|
|
public static boolean dealCommand(String command, PrintWriter prtWrt) throws Exception{
|
boolean exit = false ;
|
command = command.trim() ;
|
if (command.equals("help")) {
|
outHelp(prtWrt);
|
}else if (command.equals("config")) {
|
outConfig(prtWrt);
|
}else if(command.equals("exit")){
|
exit = true ;
|
}else if(isProtocolCom(command)){
|
dealProtocolCom(command, prtWrt);
|
}else {
|
outNoIdentify(prtWrt);
|
}
|
return exit ;
|
}
|
private static boolean isProtocolCom(String command){
|
if(ServerProperties.protocolName.equals(ProtocolConstantV206V1.protocolName)){
|
String[] coms = command.split(" ");
|
for (String com : coms){
|
if(!com.equals("")){
|
if(CodeV1.isValid(com)){
|
return true ;
|
}else{
|
return false ;
|
}
|
}
|
}
|
}
|
return false ;
|
}
|
private static void dealProtocolCom(String command, PrintWriter prtWrt) throws Exception{
|
String[] coms = command.split(" ");
|
List<String> comList = new ArrayList<>();
|
for (String com : coms){
|
if(!com.equals("")){
|
comList.add(com) ;
|
}
|
}
|
String com = comList.get(0) ;
|
switch (com) {
|
case CodeV1.cd_02: cd02(comList, prtWrt); break;
|
case CodeV1.cd_10: cd10(comList, prtWrt); break;
|
case CodeV1.cd_92: cd92(comList, prtWrt); break;
|
case CodeV1.cd_93: cd93(comList, prtWrt); break;
|
default: outNoIdentify(prtWrt); break;
|
}
|
}
|
|
private static void cd02(List<String> comList, PrintWriter prtWrt)throws Exception{
|
if(comList.size() > 1){
|
String p = comList.get(1);
|
if(p.equals("-h")){
|
prtWrt.println("02[Enter](心跳命令应答(链路维持报应答))");
|
}else{
|
outNoIdentify(prtWrt) ;
|
}
|
}else{
|
out(new Cd02().hex(ServerProperties.rtuAddr), prtWrt);
|
}
|
}
|
private static void cd10(List<String> comList, PrintWriter prtWrt)throws Exception{
|
if(comList.size() > 1){
|
String p1 = comList.get(1);
|
if(p1.equals("-h")){
|
prtWrt.println("10 *...*[Enter](设置控制器地址)");
|
}else{
|
String[] ps = params2Grp(comList) ;
|
Cd10 cd = new Cd10() ;
|
String msg = cd.checkParams(ps) ;
|
if(msg == null){
|
out(new Cd10().hex(ServerProperties.rtuAddr, ps), prtWrt);
|
}else{
|
out(msg, prtWrt);
|
}
|
}
|
}else{
|
outNoParams(prtWrt); ;
|
}
|
}
|
private static void cd92(List<String> comList, PrintWriter prtWrt)throws Exception{
|
if(comList.size() > 1){
|
String p = comList.get(1);
|
if(p.equals("-h")){
|
prtWrt.println("92[Enter](平台远程开启阀门)");
|
}else{
|
outNoIdentify(prtWrt) ;
|
}
|
}else{
|
out(new Cd92().hex(ServerProperties.rtuAddr), prtWrt);
|
}
|
}
|
private static void cd93(List<String> comList, PrintWriter prtWrt)throws Exception{
|
if(comList.size() > 1){
|
String p = comList.get(1);
|
if(p.equals("-h")){
|
prtWrt.println("93[Enter](平台远程关闭阀门)");
|
}else{
|
outNoIdentify(prtWrt) ;
|
}
|
}else{
|
out(new Cd93().hex(ServerProperties.rtuAddr), prtWrt);
|
}
|
}
|
|
private static String[] params2Grp(List<String> comList){
|
String[] ps = new String[comList.size()-1] ;
|
for(int i = 1; i < comList.size(); i++){
|
ps[i-1] = comList.get(i) ;
|
}
|
return ps ;
|
}
|
|
private static void out(String str, PrintWriter prtWrt){
|
prtWrt.println(str==null?"":str);
|
}
|
private static void outHelp(PrintWriter prtWrt){
|
prtWrt.println("");
|
prtWrt.println("命令");
|
for(String s : commands){
|
prtWrt.println(" " + s);
|
}
|
prtWrt.println("");
|
}
|
private static void outConfig(PrintWriter prtWrt){
|
prtWrt.println("");
|
prtWrt.println("预先设置");
|
prtWrt.println(" 协议:" + ServerProperties.protocolName);
|
prtWrt.println(" 协议版本号:" + ServerProperties.protocolVersion);
|
prtWrt.println(" Rtu地址:" + ServerProperties.rtuAddr);
|
prtWrt.println(" IC卡地址:" + ServerProperties.icCardAddr);
|
prtWrt.println(" IC卡编号:" + ServerProperties.icCardNo);
|
prtWrt.println("");
|
}
|
private static void outNoIdentify(PrintWriter prtWrt){
|
prtWrt.println("");
|
prtWrt.println("命令不可识别!");
|
prtWrt.println("");
|
}
|
private static void outNoParams(PrintWriter prtWrt){
|
prtWrt.println("");
|
prtWrt.println("请输入命令参数!");
|
prtWrt.println("");
|
}
|
|
|
|
|
public static void main(String[] args) {
|
String com = " 02 123 345 789";
|
com = com.trim() ;
|
String[] coms = com.split(" ");
|
for(String s : coms){
|
System.out.println(s);
|
}
|
}
|
|
}
|