diff --git a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java index 096d1e2a..8a013d66 100644 --- a/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java +++ b/app/src/main/java/com/bonait/bnframework/business/ExecuteTheRecipe.java @@ -1,55 +1,35 @@ package com.bonait.bnframework.business; -import android.app.Activity; -import android.content.Context; -import android.content.ContextWrapper; -import android.media.MediaPlayer; import android.os.Handler; import android.os.Looper; -import android.util.Log; import androidx.annotation.NonNull; -import com.bonait.bnframework.R; +import com.bonait.bnframework.business.deviceControl.seasonBar.PLCControl; +import com.bonait.bnframework.business.deviceControl.seasonBar.ScaleDevice; import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.constant.DataBus; import com.bonait.bnframework.common.db.QueryDB; -import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE; -import com.bonait.bnframework.common.db.mode.BPA_MATERIAL; import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS; import com.bonait.bnframework.common.db.mode.BPA_SILOS; -import com.bonait.bnframework.common.db.res.AlertLogEnum; import com.bonait.bnframework.common.db.res.ResGoodsMake; import com.bonait.bnframework.common.db.res.ResGoodsRecipe; -import com.bonait.bnframework.common.db.res.SilosLsjyMode; import com.bonait.bnframework.common.db.res.UserLogEnum; -import com.bonait.bnframework.common.helper.ByteHelper; -import com.bonait.bnframework.common.helper.DataFormat; -import com.bonait.bnframework.common.helper.I.IReadCallBack; import com.bonait.bnframework.common.helper.I.IRun; 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.MediaPlayerHelper; import com.bonait.bnframework.common.helper.MessageLog; -import com.bonait.bnframework.common.helper.RTrig; import com.bonait.bnframework.common.helper.ThreadManager; -import com.bonait.bnframework.common.modbus.ModbusTcpServer; -import com.bonait.bnframework.common.utils.AlertDialogUtils; +import com.bonait.bnframework.common.modbus.ModbusMaster; import com.bonait.bnframework.common.utils.ToastUtils; -import com.qmuiteam.qmui.widget.dialog.QMUIDialog; -import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction; import java.util.ArrayList; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import kotlin.UShort; - /** * 执行配方 */ @@ -554,7 +534,7 @@ public class ExecuteTheRecipe { ToastUtils.info("检测到PLC连接超时,尝试执行超时处理。"); //ModbusTcpHelper.get().release(); //ModbusRTUServer.get().ConnectPLC(); - PLCControl.get().initalPLC(new IWriteCallBack() { + PLCControl.get().inital(new IWriteCallBack() { @Override public void onSuccess() { ToastUtils.success("超时处理--复位PLC变量完成。"); @@ -617,48 +597,6 @@ public class ExecuteTheRecipe { } } - /** - * RTU模式向写PLC数据 - * - * @param name - * @param value - */ - public static void WritePLCForRTU(String name, Object value, IWriteCallBack callback) { - try { - int plcAddress=ConfigName.getInstance().SerialPLCAddress; - if (plcAddress<1) return; - if (ConfigName.getInstance().PLC_Address.containsKey(name)) { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); - if (!plcaddress.address.isEmpty() && !isTimeOut) { - if (plcaddress.address.toUpperCase().startsWith("D"))//int - { - int startAddress=ModbusRTUServer.GetAddress(plcaddress.address); - short newvalue=Short.parseShort(value.toString()); - ModbusRTUServer.get().WriteShort(plcAddress,startAddress, newvalue, callback); - } else if (plcaddress.address.toUpperCase().startsWith("M"))//bool - { - int startAddress=ModbusRTUServer.GetAddress(plcaddress.address); - boolean newValue=Boolean.parseBoolean(value.toString()); - ModbusRTUServer.get().WriteBool(plcAddress,startAddress, newValue, callback); - } else if (plcaddress.address.toUpperCase().startsWith("X"))//bool - { - int startAddress=ModbusRTUServer.GetAddress(plcaddress.address); - boolean newValue=Boolean.parseBoolean(value.toString()); - ModbusRTUServer.get().WriteBool(plcAddress,startAddress, newValue, callback); - } else if (plcaddress.address.toUpperCase().startsWith("Y"))//bool - { - int startAddress=ModbusRTUServer.GetAddress(plcaddress.address); - boolean newValue=Boolean.parseBoolean(value.toString()); - ModbusRTUServer.get().WriteBool(plcAddress,startAddress, newValue, callback); - } - } - } - } catch (Exception ex) { - ToastUtils.error("异常信息:" + ex.getMessage()); - } finally { - - } - } /** * 读PLC数据 @@ -1048,7 +986,7 @@ public class ExecuteTheRecipe { final boolean[] issucess2 = {false}; final boolean[] issucess3 = {false}; // 1. 关闭写保护。 - PLCControl.get().closeWriteProtect(new IWriteCallBack() { + ScaleDevice.get().closeWriteProtect(new IWriteCallBack() { @Override public void onSuccess() { issucess1[0]=true; @@ -1060,7 +998,7 @@ public class ExecuteTheRecipe { }); // 2.称台清零。 - PLCControl.get().zeroScale(new IWriteCallBack() { + ScaleDevice.get().zeroScale(new IWriteCallBack() { @Override public void onSuccess() { issucess2[0]=true; @@ -1151,7 +1089,7 @@ public class ExecuteTheRecipe { final boolean[] issucess1 = {false}; // 1. 打开写保护 - PLCControl.get().openWriteProtect(new IWriteCallBack() { + ScaleDevice.get().openWriteProtect(new IWriteCallBack() { @Override public void onSuccess() { issucess1[0]=true; diff --git a/app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCController.java b/app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCControl.java similarity index 95% rename from app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCController.java rename to app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCControl.java index 5761da0b..5664a1c6 100644 --- a/app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCController.java +++ b/app/src/main/java/com/bonait/bnframework/business/deviceControl/seasonBar/PLCControl.java @@ -11,24 +11,24 @@ import com.bonait.bnframework.common.modbus.ModbusMaster; import com.bonait.bnframework.common.utils.ToastUtils; /**PLC控制类*/ -public class PLCController{ +public class PLCControl{ //region 单例模式 - private static volatile PLCController instance = null; - public static PLCController get() { - PLCController manager = instance; + private static volatile PLCControl instance = null; + public static PLCControl get() { + PLCControl manager = instance; if (manager == null) { - synchronized (PLCController.class) { + synchronized (PLCControl.class) { manager = instance; if (manager == null) { - manager = new PLCController(); + manager = new PLCControl(); instance = manager; } } } return manager; } - private PLCController(){}; + private PLCControl(){}; //endregion //region 私有变量 @@ -39,7 +39,7 @@ public class PLCController{ //endregion - public void Inital(IWriteCallBack callBack) { + public void inital(IWriteCallBack callBack) { //TODO:确定初始化逻辑再写。 } diff --git a/app/src/main/java/com/bonait/bnframework/business/devicesControl/PLCControl.java b/app/src/main/java/com/bonait/bnframework/business/devicesControl/PLCControl.java deleted file mode 100644 index 6b71941a..00000000 --- a/app/src/main/java/com/bonait/bnframework/business/devicesControl/PLCControl.java +++ /dev/null @@ -1,340 +0,0 @@ -package com.bonait.bnframework.business.devicesControl; - -import android.os.Handler; -import android.os.Looper; - -import com.bonait.bnframework.business.ExecuteTheRecipe; -import com.bonait.bnframework.common.constant.ConfigName; -import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS; -import com.bonait.bnframework.common.helper.DataFormat; -import com.bonait.bnframework.common.helper.I.IWriteCallBack; -import com.bonait.bnframework.common.modbus.ModbusRTUServer; -import com.bonait.bnframework.common.utils.ToastUtils; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -public class PLCControl { - //region 单例模式 - private static volatile PLCControl instance = null; - - public static PLCControl get() { - PLCControl manager = instance; - if (manager == null) { - synchronized (PLCControl.class) { - manager = instance; - if (manager == null) { - manager = new PLCControl(); - instance = manager; - } - } - } - return manager; - } - private PLCControl() { - } - //endregion - - /**语音模块的从站地址,这个最好不改,设备端不好修改默认地址*/ - private final int voiceAddress=1; - /**电子秤的从站地址*/ - private final int scaleAddress=2; - /**PLC的从站地址*/ - private final int PLCAddress=3; - - //region 语音模块控制 , 从站地址:1 - - public void Speak(String content,IWriteCallBack callBack){ - try { - if (!content.isEmpty() /*&& !ExecuteTheRecipe.isTimeOut*/) { - int address = 0; - short[] values=getGB2312(content); - ModbusRTUServer.get().WriteString(voiceAddress, address, values, callBack); - } - } - catch (Exception ex) { - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - //endregion - - //region 电子秤控制, 从站地址:2 - - /**获取称当前重量、当前AD值、当前零位,文档建议150ms以上轮询一次*/ - public int getCurrentWeight(){ - //int readLength=3; - int[] result=new int[]{0}; - try { - ModbusRTUServer.get().ReadInts(scaleAddress, 0, 1, DataFormat.BADC, val-> { - result[0]=val[0]; - }); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - }finally { - return result[0]; - } - } - - /**写入砝码重量*/ - public void writeCounterWeight(short counterWeight,IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 6,counterWeight,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**去皮*/ - public void tare(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 21,(short) 1,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**恢复去皮*/ - public void cancelTare(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 21,(short) 2,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**零点校准,写入前需要先关闭写保护*/ - public void zeroScale(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 22,(short) 1,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**恢复出厂设置,写入前需要先关闭写保护*/ - public void factoryReset(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 22,(short) 9,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**打开写保护*/ - public void openWriteProtect(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 23,(short) 0,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - /**关闭写保护,零点校准、写入砝码重量、恢复出厂设置都需要先执行此操作*/ - public void closeWriteProtect(IWriteCallBack callBack){ - try { - ModbusRTUServer.get().WriteShort(scaleAddress, 23,(short) 1,callBack); - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - } - } - - //endregion - - //region PLC控制 设备从站地址:3 - - - - /**复位PLC所有输出以及心跳,[如果输出更换了点位的话,无法复位所有电机]*/ - public void initalPLC(IWriteCallBack callBack){ - try { - short[] values = new short[14]; - - final boolean[] isSuccess={false,false,false}; - - int startAddress = ModbusRTUServer.GetAddress("D10"); - ModbusRTUServer.get().WriteShorts(PLCAddress, startAddress, values, new IWriteCallBack() { - @Override - public void onSuccess() { - isSuccess[0]=true; - } - - @Override - public void onFailure(String ErrorMsg) { - - } - }); - - int timeoutFlag = ModbusRTUServer.GetAddress("M0"); - ModbusRTUServer.get().WriteBool(PLCAddress, timeoutFlag, false, new IWriteCallBack() { - @Override - public void onSuccess() { - isSuccess[1]=true; - } - - @Override - public void onFailure(String ErrorMsg) { - - } - }); - - clrHeartbeat("心跳时间", new IWriteCallBack() { - @Override - public void onSuccess() { - isSuccess[2]=true; - } - - @Override - public void onFailure(String ErrorMsg) { - - } - }); - if (callBack != null) { - if (isSuccess[0] && isSuccess[1] && isSuccess[2]){ - callBack.onSuccess(); - }else{ - callBack.onFailure("Initial Failure"); - } - } - } catch (Exception ex) { - if (callBack != null) { - callBack.onFailure(ex.getMessage()); - } - } - - } - - /**打开电机,指定时间后关闭 - * @param delayTime 关闭延迟时间,单位:ms - * */ - public void openMotorAndClose(int index,int delayTime,IWriteCallBack callBack){ - try { - openMotor(index, new IWriteCallBack() { - @Override - public void onSuccess() { - new Handler(Looper.getMainLooper()).postDelayed(()->{ - closeMotor(index, new IWriteCallBack() { - @Override - public void onSuccess() { - callBack.onSuccess(); - } - - @Override - public void onFailure(String ErrorMsg) { - callBack.onFailure(ErrorMsg); - } - }); - },delayTime); - } - @Override - public void onFailure(String ErrorMsg) { - callBack.onFailure(ErrorMsg); - } - }); - } catch (Exception ex) { - ToastUtils.error("OpenMotorAndClose Exception:" + ex.getMessage()); - } - } - - /**打开电机*/ - public void openMotor(int index, IWriteCallBack callBack) { - String name="通道"+index+"开关"; - try { - if (ConfigName.getInstance().PLC_Address.containsKey(name)) { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); - if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) { - int address = ModbusRTUServer.GetAddress(plcaddress.address); - ModbusRTUServer.get().WriteShort(PLCAddress, address, (short)1, callBack); - } - } - } catch (Exception ex) { - ToastUtils.error("OpenMotor Exception:" + ex.getMessage()); - } - } - - /**关闭电机*/ - public void closeMotor(int index, IWriteCallBack callBack) { - String name="通道"+index+"开关"; - try { - if (ConfigName.getInstance().PLC_Address.containsKey(name)) { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); - if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) { - int address = ModbusRTUServer.GetAddress(plcaddress.address); - ModbusRTUServer.get().WriteShort(PLCAddress, address, (short)0, callBack); - } - } - } catch (Exception ex) { - ToastUtils.error("CloseMotor Exception:" + ex.getMessage());} - } - - /**获取所有电机的状态,为true表示正在转,反之,为false。*/ - public boolean[] getAllMotorStatus(){ - //一共有14个电机 - int length = 14; - boolean[] result = new boolean[length]; - try { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get("通道1开关"); - if (!plcaddress.address.isEmpty() && !ExecuteTheRecipe.isTimeOut) { - int startAddress = ModbusRTUServer.GetAddress(plcaddress.address); - ModbusRTUServer.get().ReadShorts(PLCAddress, startAddress, length, val -> { - for (int i = 0; i < length; i++) { - result[i] = val[i] > 0 ? true : false; - } - }); - } - }catch (Exception ex){ - ToastUtils.error("GetAllMotorStatus Exception:" + ex.getMessage()); - }finally { - return result; - } - } - - /**复位心跳*/ - public void clrHeartbeat(String name,IWriteCallBack callBack){ - try { - if (ConfigName.getInstance().PLC_Address.containsKey(name)) { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); - if (!plcaddress.address.isEmpty()/* && !ExecuteTheRecipe.isTimeOut*/) { - int address = ModbusRTUServer.GetAddress(plcaddress.address); - ModbusRTUServer.get().WriteShort(PLCAddress , address ,(short) 0 , callBack); - } - } - } catch (Exception ex) { - ToastUtils.error("ClrHeartbeat Exception:" + ex.getMessage()); - } - } - - /**读取是否超时*/ - public boolean isTimeOut(String name){ - final boolean[] result = {true}; - try{ - if (ConfigName.getInstance().PLC_Address.containsKey(name)) { - BPA_PLCADDRESS plcaddress = ConfigName.getInstance().PLC_Address.get(name); - if (!plcaddress.address.isEmpty() /*&& ConfigName.getInstance().SerialIsConnect*/) { - ModbusRTUServer.get().ReadBools(PLCAddress, plcaddress.address, 1, val-> { - result[0] =val[0]; - }); - } - } - }catch (Exception ex){ - ToastUtils.error("Exception message:" + ex.getMessage()); - }finally { - return result[0]; - } - } - //endregion - - - - /**字符串转换为GB2312编码,需要测试,可能不对*/ - public short[] getGB2312(String str){ - try{ - byte[] bytes=str.getBytes("gb2312"); - short[] result=new short[bytes.length/2]; - ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(result); - return result; - }catch (Exception e){ - return null; - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/Silos_item_jiaoyan_fragment.java b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/Silos_item_jiaoyan_fragment.java index 5c2bdf10..3eda2b21 100644 --- a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/Silos_item_jiaoyan_fragment.java +++ b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/Silos_item_jiaoyan_fragment.java @@ -18,7 +18,7 @@ import androidx.annotation.Nullable; import com.bonait.bnframework.R; import com.bonait.bnframework.business.ExecuteTheRecipe; -import com.bonait.bnframework.business.devicesControl.PLCControl; +import com.bonait.bnframework.business.deviceControl.seasonBar.PLCControl; import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.db.QueryDB; import com.bonait.bnframework.common.db.res.lcMode; diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/add_silos_ck.java b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/add_silos_ck.java index 130f5b6a..9de03af8 100644 --- a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/add_silos_ck.java +++ b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/add_silos_ck.java @@ -24,7 +24,7 @@ import androidx.annotation.Nullable; import com.bonait.bnframework.R; import com.bonait.bnframework.business.ExecuteTheRecipe; -import com.bonait.bnframework.business.devicesControl.PLCControl; +import com.bonait.bnframework.business.deviceControl.seasonBar.PLCControl; import com.bonait.bnframework.common.db.QueryDB; import com.bonait.bnframework.common.db.mode.BPA_MATERIAL; import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL; diff --git a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/silos_jiaoyan.java b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/silos_jiaoyan.java index d4555c76..5cbb5936 100644 --- a/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/silos_jiaoyan.java +++ b/app/src/main/java/com/bonait/bnframework/modules/home/fragment/mode/silos_jiaoyan.java @@ -2,56 +2,49 @@ package com.bonait.bnframework.modules.home.fragment.mode; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.graphics.Color; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.os.SystemClock; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Chronometer; import android.widget.EditText; -import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.RelativeLayout; -import android.widget.Spinner; import android.widget.TextView; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentManager; import com.bonait.bnframework.R; import com.bonait.bnframework.business.ExecuteTheRecipe; +import com.bonait.bnframework.business.deviceControl.seasonBar.PLCControl; import com.bonait.bnframework.business.deviceControl.seasonBar.ScaleDevice; import com.bonait.bnframework.common.constant.ConfigName; import com.bonait.bnframework.common.db.QueryDB; -import com.bonait.bnframework.common.db.mode.BPA_MATERIAL; import com.bonait.bnframework.common.db.mode.BPA_SILOS; -import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL; +import com.bonait.bnframework.common.db.res.UserLogEnum; import com.bonait.bnframework.common.db.res.lcMode; import com.bonait.bnframework.common.helper.I.IRun; import com.bonait.bnframework.common.helper.I.IWriteCallBack; import com.bonait.bnframework.common.helper.I.MyClickListener; -import com.bonait.bnframework.common.model.mode.ResMenuLeft; +import com.bonait.bnframework.common.helper.MessageLog; +import com.bonait.bnframework.common.utils.AlertDialogUtils; import com.bonait.bnframework.common.utils.ToastUtils; -import com.qmuiteam.qmui.arch.QMUIFragment; -import com.qmuiteam.qmui.arch.QMUIFragmentPagerAdapter; -import com.qmuiteam.qmui.widget.QMUIViewPager; import com.qmuiteam.qmui.widget.dialog.QMUIDialog; -import com.qmuiteam.qmui.widget.tab.QMUIBasicTabSegment; -import com.qmuiteam.qmui.widget.tab.QMUITabSegment; +import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction; import com.suke.widget.SwitchButton; -import java.math.BigDecimal; -import java.text.DecimalFormat; +import org.apache.commons.math3.fitting.PolynomialCurveFitter; +import org.apache.commons.math3.fitting.WeightedObservedPoints; + import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife;