@@ -25,7 +25,6 @@ import com.bonait.bnframework.common.helper.Json; | |||
import com.bonait.bnframework.common.http.callback.json.JsonDialogCallback; | |||
import com.bonait.bnframework.common.image.utils.LocalCacheUtils; | |||
import com.bonait.bnframework.common.message.MessageManager; | |||
import com.bonait.bnframework.common.modbus.s7.SiemensHelper1; | |||
import com.bonait.bnframework.common.model.ResAPI; | |||
import com.bonait.bnframework.common.model.mode.ActionJsonMode; | |||
import com.bonait.bnframework.common.model.mode.BPA_GoodsAttributeValue; | |||
@@ -1111,7 +1110,7 @@ public class ConfigData { | |||
* 关闭PLC | |||
*/ | |||
public void ColsePLC() { | |||
SiemensHelper1.get().Close();//释放modbus | |||
// SiemensHelper1.get().Close();//释放modbus | |||
ConfigName.getInstance().PlcIsConnect = false; | |||
} | |||
@@ -19,7 +19,8 @@ import com.bonait.bnframework.common.helper.I.IRunT; | |||
import com.bonait.bnframework.common.helper.I.IThread; | |||
import com.bonait.bnframework.common.helper.I.IWriteCallBack; | |||
import com.bonait.bnframework.common.helper.ThreadManager; | |||
import com.bonait.bnframework.common.modbus.s7.SiemensHelper1; | |||
import com.bonait.bnframework.common.modbus.s7.CommHelper; | |||
import com.bonait.bnframework.common.modbus.s7.SiemensHelper; | |||
import com.bonait.bnframework.common.utils.ToastUtils; | |||
import com.qmuiteam.qmui.widget.dialog.QMUIDialog; | |||
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction; | |||
@@ -834,8 +835,8 @@ public class ExecuteTheRecipe { | |||
try { | |||
if (ConfigName.getInstance().PLC_Address.containsKey(name)) { | |||
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); | |||
if (!plcaddress.address.isEmpty() && ConfigName.getInstance().PlcIsConnect) { | |||
SiemensHelper1.get().writePLC(plcaddress.address, value); | |||
if (!plcaddress.address.isEmpty()) { | |||
CommHelper.get().writePLC(plcaddress.address, value); | |||
showlog(name+"_写入值_"+value); | |||
} | |||
} | |||
@@ -852,19 +853,19 @@ public class ExecuteTheRecipe { | |||
* @return | |||
*/ | |||
public static Object ReadPLC(String name) { | |||
final Object[] ReturnsVariable = {null}; | |||
Object ReturnsVariable=new Object(); | |||
try { | |||
if (ConfigName.getInstance().PLC_Address.containsKey(name)) { | |||
BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); | |||
if (!plcaddress.address.isEmpty() && ConfigName.getInstance().PlcIsConnect) { | |||
if (!plcaddress.address.isEmpty() ) { | |||
ReturnsVariable[0] = SiemensHelper1.get().readPLC(plcaddress.address); | |||
ReturnsVariable = CommHelper.get().readPLC(plcaddress.address); | |||
} | |||
} | |||
} catch (Exception ex) { | |||
ToastUtils.error("异常信息:" + ex.getMessage()); | |||
} finally { | |||
return ReturnsVariable[0]; | |||
return ReturnsVariable; | |||
} | |||
} | |||
@@ -61,7 +61,8 @@ public class MainInit { | |||
CrashHandler.getInstance().init(app); | |||
//1.设置程序active,初始化Main函数进程,初始化消息日志 | |||
InitMsgLog(); | |||
// InitMsgLog(); | |||
MessageLog.Init(); | |||
//2.初始化SD卡,数据库DB | |||
SdCart.getInstance().initSD(); | |||
@@ -12,6 +12,8 @@ public class BPA_LOG extends ModeBase{ | |||
* 3 数据接收 | |||
* 4 上传日志 | |||
* 5 订单处理日志 | |||
* 6 信息日志 | |||
* 7 错误日志 | |||
*/ | |||
public int type; | |||
//日志描述 | |||
@@ -1,5 +1,11 @@ | |||
package com.bonait.bnframework.common.helper; | |||
import android.util.Log; | |||
import com.bonait.bnframework.common.constant.ConfigName; | |||
import com.bonait.bnframework.common.db.QueryDB; | |||
import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG; | |||
import com.bonait.bnframework.common.db.mode.BPA_LOG; | |||
import com.bonait.bnframework.common.db.res.AlertLogEnum; | |||
import com.bonait.bnframework.common.db.res.UserLogEnum; | |||
import com.bonait.bnframework.common.helper.I.IMessageLogNotify; | |||
@@ -18,6 +24,75 @@ public class MessageLog { | |||
public static String MsgInfo = ""; | |||
public static void Init(){ | |||
MsgNotify = new IMessageLogNotify() { | |||
@Override | |||
public void ErrorMsg(String msg) { | |||
BPA_LOG msglog = new BPA_LOG(); | |||
msglog.type =7; | |||
msglog.text = msg; | |||
QueryDB.Addlog(msglog); | |||
Log.e("错误日志",msg); | |||
} | |||
@Override | |||
public void InfoMsg(String msg) { | |||
BPA_LOG msglog = new BPA_LOG(); | |||
msglog.type =6; | |||
msglog.text = msg; | |||
QueryDB.Addlog(msglog); | |||
Log.i("信息日志",msg); | |||
} | |||
@Override | |||
public void WarnMsg(String msg) { | |||
Log.w("警告日志",msg); | |||
} | |||
@Override | |||
public void UserMsg(UserLogEnum type, String msg) { | |||
BPA_LOG log = new BPA_LOG(); | |||
log.userID = ConfigName.getInstance().user.userID; | |||
switch (type.toString()) | |||
{ | |||
case "登录日志":log.type=1; | |||
break; | |||
case "角色操作日志":log.type=2; | |||
break; | |||
case "数据接收":log.type=3; | |||
break; | |||
case "上传日志":log.type=4; | |||
break; | |||
case "订单处理日志":log.type=5; | |||
break; | |||
} | |||
log.text = msg; | |||
QueryDB.Addlog(log); | |||
Log.i("用户日志",msg); | |||
} | |||
@Override | |||
public void AlertMsg(AlertLogEnum type, String msg) { | |||
BPA_ALERTLOG log = new BPA_ALERTLOG(); | |||
log.userID = ConfigName.getInstance().user.userID; | |||
switch (type.toString()) | |||
{ | |||
case "异常订单未制作日志":log.type=1; | |||
break; | |||
case "料仓缺料日志":log.type=2; | |||
break; | |||
case "传感器异常日志":log.type=3; | |||
break; | |||
case "其他":log.type=4; | |||
break; | |||
} | |||
log.text = msg; | |||
QueryDB.AddAlertlog(log); | |||
Log.i("订单错误日志",msg); | |||
} | |||
}; | |||
} | |||
public static void ShowInfo(String msg) { | |||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |||
Date date = new Date(); | |||
@@ -0,0 +1,5 @@ | |||
package com.bonait.bnframework.common.helper; | |||
public class Singleton<T extends Class> { | |||
// private static readonly | |||
} |
@@ -0,0 +1,44 @@ | |||
package com.bonait.bnframework.common.modbus.s7; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.io.LineNumberReader; | |||
public class CommBase { | |||
/** | |||
* Ping PLC地址是否通畅 | |||
* @param address | |||
* @param pingTimes ping的次数 | |||
* @param timeOut 超时时间 10 | |||
* @return | |||
*/ | |||
public boolean NetPing(String address, int pingTimes, int timeOut) { | |||
Process process = null; | |||
try { | |||
process = Runtime.getRuntime().exec( "ping " + "-c " + pingTimes + " -w " + timeOut+ " "+address); | |||
InputStreamReader r = new InputStreamReader(process.getInputStream()); | |||
LineNumberReader returnData = new LineNumberReader(r); | |||
String returnMsg=""; | |||
String line = ""; | |||
while ((line = returnData.readLine()) != null) { | |||
System.out.println(line); | |||
returnMsg += line; | |||
} | |||
if(returnMsg.indexOf("100% packet loss")!=-1){ | |||
System.out.println("与 " +address +" 连接不畅通."); | |||
//ToastUtils.info("与 " +address +" 连接不畅通."); | |||
return false; | |||
} else{ | |||
System.out.println("与 " +address +" 连接畅通."); | |||
//ToastUtils.info("与 " +address +" 连接不畅通."); | |||
return true; | |||
} | |||
} catch (IOException e) { | |||
// e.printStackTrace(); | |||
} | |||
return false; | |||
} | |||
} |
@@ -0,0 +1,94 @@ | |||
package com.bonait.bnframework.common.modbus.s7; | |||
import android.util.Log; | |||
import com.bonait.bnframework.common.constant.ConfigName; | |||
import com.bonait.bnframework.common.helper.I.IRun; | |||
import com.bonait.bnframework.common.helper.MessageLog; | |||
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.io.LineNumberReader; | |||
public class CommHelper { | |||
private static CommHelper mInstance; //实例变量设置私有,防止直接通过类名访问 | |||
private CommHelper() { //默认构造函数私有,防止类外直接new创建对象 | |||
} | |||
public static synchronized CommHelper get() { //静态同步方法作为唯一的实例对象获取方式 | |||
if (mInstance == null) { | |||
mInstance = new CommHelper(); | |||
} | |||
return mInstance; | |||
} | |||
public IRun ConnectOk; | |||
SiemensHelper siemens=new SiemensHelper(EPlcType.S200_SMART, ConfigName.getInstance().Address); | |||
public void Connect(){ | |||
new Thread(new Runnable() { | |||
@Override | |||
public void run() { | |||
MessageLog.ShowInfo("开始连接PLC"); | |||
int tempFlag=0; | |||
while (!siemens.checkConnected()){ | |||
try{ | |||
tempFlag++; | |||
siemens.connect(); | |||
}catch (Exception e){ | |||
if(tempFlag==1)MessageLog.ShowInfo("PLC连接失败:"+e.getMessage()); | |||
siemens.Delay(1000); | |||
} | |||
} | |||
if(ConnectOk!=null)ConnectOk.Run(); | |||
MessageLog.ShowInfo("PLC连接成功"); | |||
} | |||
}).start(); | |||
} | |||
public void writePLC(String add,Object value){ | |||
if(value==null) { | |||
MessageLog.ShowInfo("writePLC:写入值为空"); | |||
return; | |||
} | |||
try{ | |||
if(add.toUpperCase().contains("VD")){ | |||
Integer tempVD= Integer.parseInt(value.toString()); | |||
siemens.WriteInt32(add,tempVD); | |||
} | |||
else if (add.toUpperCase().contains("VW")){ | |||
Integer tempVW= Integer.parseInt(value.toString()); | |||
siemens.WriteUInt16(add,tempVW); | |||
} | |||
else if (add.toUpperCase().contains("V")){ | |||
Boolean tempV= Boolean.parseBoolean(value.toString()); | |||
siemens.WriteBoolean(add,tempV); | |||
} | |||
}catch (Exception e){ | |||
MessageLog.ShowInfo("writePLC:写入失败,"+e.getMessage()); | |||
} | |||
} | |||
public Object readPLC(String add){ | |||
try{ | |||
if(add.toUpperCase().contains("VD")){ | |||
return siemens.ReadInt32(add); | |||
} | |||
else if (add.toUpperCase().contains("VW")){ | |||
return siemens.ReadUInt16(add); | |||
} | |||
else if (add.toUpperCase().contains("V")){ | |||
return siemens.ReadBoolean(add); | |||
} | |||
}catch (Exception e){ | |||
MessageLog.ShowInfo("readPLC:读取失败,"+e.getMessage()); | |||
} | |||
return new Object(); | |||
} | |||
} |
@@ -1,169 +0,0 @@ | |||
package com.bonait.bnframework.common.modbus.s7; | |||
import android.util.Log; | |||
import com.bonait.bnframework.common.constant.ConfigName; | |||
import com.github.xingshuangs.iot.exceptions.SocketRuntimeException; | |||
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; | |||
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.io.LineNumberReader; | |||
/** | |||
* S7 | |||
*/ | |||
public class SiemensHelper1 extends S7PLC { | |||
//region 单例 | |||
private static volatile SiemensHelper1 instance = null; | |||
public static SiemensHelper1 get() { | |||
SiemensHelper1 manager = instance; | |||
if (manager == null) { | |||
synchronized (SiemensHelper1.class) { | |||
manager = instance; | |||
if (manager == null) { | |||
manager = new SiemensHelper1(); | |||
instance = manager; | |||
} | |||
} | |||
} | |||
return manager; | |||
} | |||
private SiemensHelper1() { | |||
} | |||
//endregion | |||
//region 变量 | |||
/** | |||
* 默认PLC类型 | |||
*/ | |||
public static EPlcType type = EPlcType.S1500; | |||
public static S7PLC s7PLC = null; | |||
//endregion | |||
//region 公共方法 | |||
/** | |||
* Ping PLC地址是否通畅 | |||
* @param address | |||
* @param pingTimes ping的次数 | |||
* @param timeOut 超时时间 10 | |||
* @return | |||
*/ | |||
public static boolean ping2(String address, int pingTimes, int timeOut) { | |||
Process process = null; | |||
try { | |||
process = Runtime.getRuntime().exec( "ping " + "-c " + pingTimes + " -w " + timeOut+ " "+address); | |||
InputStreamReader r = new InputStreamReader(process.getInputStream()); | |||
LineNumberReader returnData = new LineNumberReader(r); | |||
String returnMsg=""; | |||
String line = ""; | |||
while ((line = returnData.readLine()) != null) { | |||
System.out.println(line); | |||
returnMsg += line; | |||
} | |||
if(returnMsg.indexOf("100% packet loss")!=-1){ | |||
System.out.println("与 " +address +" 连接不畅通."); | |||
//ToastUtils.info("与 " +address +" 连接不畅通."); | |||
return false; | |||
} else{ | |||
System.out.println("与 " +address +" 连接畅通."); | |||
//ToastUtils.info("与 " +address +" 连接不畅通."); | |||
return true; | |||
} | |||
} catch (IOException e) { | |||
// e.printStackTrace(); | |||
} | |||
return false; | |||
} | |||
/** | |||
* 连接plc | |||
*/ | |||
public static void Connect() { | |||
try { | |||
s7PLC = new S7PLC(type, ConfigName.getInstance().Address); | |||
s7PLC.connect(); | |||
ConfigName.getInstance().PlcIsConnect = true; | |||
} catch (SocketRuntimeException ex) { | |||
Log.e("SocketRuntimeException", ex.getMessage()); | |||
Close(); | |||
} catch (Exception ex) { | |||
Log.e("Exception", ex.getMessage()); | |||
Close(); | |||
} | |||
} | |||
/** | |||
* 关闭 | |||
*/ | |||
public static void Close() { | |||
try { | |||
if (s7PLC != null) { | |||
s7PLC.close(); | |||
} | |||
} catch (SocketRuntimeException ex) { | |||
Log.e("SocketRuntimeException", ex.getMessage()); | |||
} catch (Exception ex) { | |||
Log.e("Exception", ex.getMessage()); | |||
} | |||
ConfigName.getInstance().PlcIsConnect = false; | |||
} | |||
/** | |||
* 读取数据 | |||
* @param address | |||
* @return | |||
*/ | |||
public Object readPLC(String address) { | |||
Object res=null; | |||
try { | |||
if (address.contains("V")) { | |||
res = s7PLC.readBoolean(address); | |||
} else if (address.contains("D")) { | |||
res = s7PLC.readUInt32(address); | |||
} | |||
} catch (SocketRuntimeException ex) { | |||
Close(); | |||
} catch (Exception ex) { | |||
} | |||
return res; | |||
} | |||
/** | |||
* 写数据 | |||
* @param address | |||
* @return | |||
*/ | |||
public boolean writePLC(String address,Object data) { | |||
boolean res=false; | |||
try { | |||
if (address.contains("V")) { | |||
s7PLC.writeBoolean(address,(boolean)data); | |||
} else if (address.contains("D")) { | |||
s7PLC.writeInt32(address,(int)data); | |||
} | |||
res=true; | |||
} catch (SocketRuntimeException ex) { | |||
Close(); | |||
} catch (Exception ex) { | |||
} | |||
return res; | |||
} | |||
//endregion | |||
} |
@@ -1,6 +1,5 @@ | |||
package com.bonait.bnframework.modules.home.activity; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
import androidx.fragment.app.Fragment; | |||
import androidx.viewpager.widget.ViewPager; | |||
@@ -12,10 +11,10 @@ import com.bonait.bnframework.business.ConfigData; | |||
import com.bonait.bnframework.common.base.BaseActivity; | |||
import com.bonait.bnframework.common.constant.ConfigName; | |||
import com.bonait.bnframework.common.helper.I.IThread; | |||
import com.bonait.bnframework.common.helper.MQTT; | |||
import com.bonait.bnframework.common.helper.MessageLog; | |||
import com.bonait.bnframework.common.helper.ThreadManager; | |||
import com.bonait.bnframework.common.modbus.s7.SiemensHelper1; | |||
import com.bonait.bnframework.common.modbus.s7.CommHelper; | |||
import com.bonait.bnframework.common.modbus.s7.SiemensHelper; | |||
import com.bonait.bnframework.common.tabbar.MainNavigateTabBar; | |||
import com.bonait.bnframework.common.utils.NetworkUtils; | |||
import com.bonait.bnframework.modules.home.adapter.FragmentAdapter; | |||
@@ -101,49 +100,50 @@ public class BottomNavigationMainActivity extends BaseActivity { | |||
//判断连接环境 | |||
ConfigData.getInstance().ToggleEnvironment(); | |||
//2.初始化PLC | |||
ReconnectModbus(); | |||
// ReconnectModbus(); | |||
CommHelper.get().Connect(); | |||
} | |||
/** | |||
* 重新连接plc | |||
*/ | |||
public void ReconnectModbus() { | |||
try { | |||
ThreadManager.Get().StartLong("PLC断线重连线程", true, new IThread() { | |||
@Override | |||
public void Run() throws InterruptedException { | |||
try { | |||
if (ConfigName.getInstance().PlcIsConnect) { | |||
//ping 不通 | |||
boolean status = SiemensHelper1.get().ping2(ConfigName.getInstance().Address, 1, 1); | |||
if (!status) //ping 不通 连接 | |||
{ | |||
MessageLog.ShowInfo("PLC状态断开,尝试连接..."); | |||
ConfigName.getInstance().PlcIsConnect = false; | |||
} | |||
} else { | |||
boolean status = SiemensHelper1.get().ping2(ConfigName.getInstance().Address, 1, 1); | |||
if (status) { | |||
MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " PLC通讯正常,准备连接!"); | |||
SiemensHelper1.get().Connect(); | |||
} else { | |||
MessageLog.ShowInfo("PLC状态断开,尝试连接..."); | |||
ConfigName.getInstance().PlcIsConnect = false; | |||
} | |||
} | |||
Thread.sleep(10000); | |||
} catch (Exception e) { | |||
Log.i("PLC", "PLC重连接失败!" + e.getMessage()); | |||
} | |||
} | |||
@Override | |||
public void RunComplete() throws InterruptedException { | |||
} | |||
}); | |||
} catch (Exception e) { | |||
MessageLog.ShowInfo("重新连接Modbus异常," + e.getMessage()); | |||
} | |||
// try { | |||
// ThreadManager.Get().StartLong("PLC断线重连线程", true, new IThread() { | |||
// @Override | |||
// public void Run() throws InterruptedException { | |||
// try { | |||
// if (ConfigName.getInstance().PlcIsConnect) { | |||
// //ping 不通 | |||
// boolean status = CommHelper.NetPing(ConfigName.getInstance().Address, 1, 1); | |||
// if (!status) //ping 不通 连接 | |||
// { | |||
// MessageLog.ShowInfo("PLC状态断开,尝试连接..."); | |||
// ConfigName.getInstance().PlcIsConnect = false; | |||
// } | |||
// } else { | |||
// boolean status = CommHelper.NetPing(ConfigName.getInstance().Address, 1, 1); | |||
// if (status) { | |||
// MessageLog.ShowInfo("设备 " + ConfigName.getInstance().Address + " PLC通讯正常,准备连接!"); | |||
// SiemensHelper1.get().Connect(); | |||
// } else { | |||
// MessageLog.ShowInfo("PLC状态断开,尝试连接..."); | |||
// ConfigName.getInstance().PlcIsConnect = false; | |||
// } | |||
// } | |||
// Thread.sleep(10000); | |||
// } catch (Exception e) { | |||
// Log.i("PLC", "PLC重连接失败!" + e.getMessage()); | |||
// } | |||
// } | |||
// | |||
// @Override | |||
// public void RunComplete() throws InterruptedException { | |||
// } | |||
// }); | |||
// } catch (Exception e) { | |||
// MessageLog.ShowInfo("重新连接Modbus异常," + e.getMessage()); | |||
// } | |||
} | |||
@@ -125,6 +125,8 @@ public class LogActivity extends BaseActivity { | |||
lx_map.put("数据接收",3); | |||
lx_map.put("上传日志",4); | |||
lx_map.put("订单处理日志",5); | |||
lx_map.put("信息日志",6); | |||
lx_map.put("错误日志",7); | |||
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(lx_map.keySet())); | |||