diff --git a/.idea/gradle.xml b/.idea/gradle.xml index a2d7c21..f1b1967 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,6 +7,8 @@ + + diff --git a/app/build.gradle b/app/build.gradle index 8d29209..345583d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,14 +43,13 @@ android { } } - } dependencies { - - implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.github.licheedev:Modbus4Android:2.0.2' + implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.5.0' - implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.recyclerview:recyclerview:1.2.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' @@ -60,5 +59,5 @@ dependencies { implementation files('libs\\gson-2.2.2.jar') implementation files('libs\\sdkapi.jar') compileOnly files('libs/sdkapi.jar') - implementation files('libs/modbus4J.jar') + implementation files('libs/modbus4Android-1.2.jar') } \ No newline at end of file diff --git a/app/libs/modbus4Android-1.2.jar b/app/libs/modbus4Android-1.2.jar new file mode 100644 index 0000000..461d1dc Binary files /dev/null and b/app/libs/modbus4Android-1.2.jar differ diff --git a/app/libs/modbus4J.jar b/app/libs/modbus4J.jar deleted file mode 100644 index 0ced938..0000000 Binary files a/app/libs/modbus4J.jar and /dev/null differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e4ceec7..be5501c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,19 +6,25 @@ + + + + android:windowSoftInputMode="adjustPan|stateHidden" /> diff --git a/app/src/main/java/com/example/bpa/Model/IMessageLogNotify.java b/app/src/main/java/com/example/bpa/Model/IMessageLogNotify.java new file mode 100644 index 0000000..32baa89 --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/IMessageLogNotify.java @@ -0,0 +1,9 @@ +package com.example.bpa.Model; + +public interface IMessageLogNotify { + void ErrorMsg(String msg); + + void InfoMsg(String msg); + + void WarnMsg(String msg); +} diff --git a/app/src/main/java/com/example/bpa/Model/IThread.java b/app/src/main/java/com/example/bpa/Model/IThread.java new file mode 100644 index 0000000..c9e57b7 --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/IThread.java @@ -0,0 +1,7 @@ +package com.example.bpa.Model; + +public interface IThread { + void Run() throws InterruptedException; + + void RunComplete() throws InterruptedException; +} diff --git a/app/src/main/java/com/example/bpa/Model/ThreadModel.java b/app/src/main/java/com/example/bpa/Model/ThreadModel.java new file mode 100644 index 0000000..818e0db --- /dev/null +++ b/app/src/main/java/com/example/bpa/Model/ThreadModel.java @@ -0,0 +1,7 @@ +package com.example.bpa.Model; + +public class ThreadModel { + public IThread RunThread; + public boolean IsCancel; + public Thread ThreadObj; +} diff --git a/app/src/main/java/com/example/bpa/app/DataFormat.java b/app/src/main/java/com/example/bpa/app/DataFormat.java new file mode 100644 index 0000000..9b10a49 --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/DataFormat.java @@ -0,0 +1,8 @@ +package com.example.bpa.app; + +public enum DataFormat { + ABCD, + CDAB, + BADC, + DCBA +} diff --git a/app/src/main/java/com/example/bpa/app/DeviceData.java b/app/src/main/java/com/example/bpa/app/DeviceData.java new file mode 100644 index 0000000..0828db1 --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/DeviceData.java @@ -0,0 +1,354 @@ +package com.example.bpa.app; + +import android.renderscript.Sampler; + +import com.example.bpa.Model.IThread; +import com.example.bpa.helper.ThreadManager; + +import java.util.concurrent.ConcurrentHashMap; + +public class DeviceData { + private static volatile DeviceData _instance; + + public static DeviceData Get() { + if (_instance == null) + _instance = new DeviceData(); + return _instance; + } + + public void Init() { + ThreadManager.Get().StartLong("Plc设备数据监听", true, new IThread() { + @Override + public void Run() throws InterruptedException { + + + Thread.sleep(10); + } + + @Override + public void RunComplete() throws InterruptedException { + + } + }); + } + + /** + * 称当前重量 + */ + ConcurrentHashMap CallCurrentWeight = new ConcurrentHashMap(); + /** + * 手自动模式 + */ + boolean HandOrAutoMode; + + /** + * 清洗模式 + */ + boolean CleaningMode; + + /** + * 清洗完成 + */ + boolean CleaningComplete; + + /** + * 一键去皮完成状态 + */ + boolean OneClickPeelingComplete; + + /** + * 校准模式 + */ + boolean CalibrationMode; + + /** + * 校准值写入 + * ,PLC 地址VW100--VW118 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + public void setCalibrationValue(short value, int ch) { + if (ch >= 1 && ch <= 10) { + String add = "VW" + (98 + ch * 2); + ModbusTcpServer.get().WriteShort(add, value); + } + } + + /** + * 需求值写入 + * ,PLC 地址 VW200--VW218 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + public void setDemandValue(short value, int ch) { + if (ch >= 1 && ch <= 10) { + String add = "VW" + (198 + ch * 2); + ModbusTcpServer.get().WriteShort(add, value); + } + } + + /** + * 设置通道校准开关 + * ,PLC 地址 M2.0--M3.1 + * + * @param value 需要写入的值 + * @param ch 需要写入的通道编号,1--10 + */ + public void setChCalibrationSwitch(boolean value, int ch) { + if (ch >= 1 && ch <= 10) { + String add = ModbusTcpServer.get().getBitSingleAdd("M", 2, ch); + if (add.length() > 0) + ModbusTcpServer.get().WriteBool(add, value); + } + } + + /** + * 称去皮校准 + * + * @param value + * @param ch 设置称的通道号,ch1-ch8 + */ + public void setRemovePeelCalibration(boolean value, int ch) { + if (ch >= 1 && ch <= 8) { + String add = ModbusTcpServer.get().getBitSingleAdd("M", 5, ch); + if (add.length() > 0) + ModbusTcpServer.get().WriteBool(add, value); + } + } + + /** + * 砝码值写入 + * + * @param value + * @param ch 设置称的砝码通道号,ch1-ch8 + */ + public void setWeightValue(boolean value, int ch) { + if (ch >= 1 && ch <= 8) { + String add = ModbusTcpServer.get().getBitSingleAdd("M", 15, ch); + if (add.length() > 0) + ModbusTcpServer.get().WriteBool(add, value); + } + } + + /** + * 重量清零 + */ + public void setWeightClear() { + ModbusTcpServer.get().WriteBool("M6.0", true); + } + + /** + * 关闭写保护 + */ + public void setCloseWriteProtect() { + ModbusTcpServer.get().WriteBool("M6.1", true); + } + + /** + * 打开写保护 + */ + public void setOpenWriteProtect() { + ModbusTcpServer.get().WriteBool("M6.4", true); + } + + /** + * 零点校准 + */ + public void setZeroPointCalibration() { + ModbusTcpServer.get().WriteBool("M6.2", true); + } + + /** + * 砝码值写入 + */ + public void setWeightValueWrite() { + ModbusTcpServer.get().WriteBool("M6.3", true); + } + + /** + * 砝码校准模式 + */ + public void setWeightCalibrationMode() { + ModbusTcpServer.get().WriteBool("M6.5", true); + } + + /** + * 启动通道校准 + */ + public void setChStartCalibration() { + ModbusTcpServer.get().WriteBool("M0.0", true); + } + + /** + * 启动配料 + */ + public void setChargeMixtureStart() { + ModbusTcpServer.get().WriteBool("M0.1", true); + } + + /** + * 手自动切换 + * + * @param value true:自动模式,flase:手动模式 + */ + public void setHandOrAutoSwitch(boolean value) { + ModbusTcpServer.get().WriteBool("M0.2", value); + } + + /** + * 获取手自动模式的状态 + * + * @return true:自动模式,flase:手动模式 + */ + public boolean getHandOrAutoSwitch() { + return HandOrAutoMode; + } + + /** + * 设置清洗模式 + * + * @param value true:清洗模式,false:配料模式 + */ + public void setCleaningMode(boolean value) { + ModbusTcpServer.get().WriteBool("M0.4", value); + } + + /** + * 获取清洗模式 + * + * @return true:清洗模式,false:配料模式 + */ + public boolean getCleaningMode() { + return CleaningMode; + } + + /** + * 设置开始清洗 + */ + public void setStartCleaning() { + ModbusTcpServer.get().WriteBool("M0.5", true); + } + + /** + * 设置一键去皮 + */ + public void setOneClickPeeling() { + ModbusTcpServer.get().WriteBool("M1.2", true); + } + + /** + * 设置一键去皮完成状态 + * + * @param value + */ + public void setOneClickPeelingComplete(boolean value) { + OneClickPeelingComplete = value; + } + + /** + * 获取清洗完成的状态 + * + * @return + */ + public boolean getCleaningComplete() { + return CleaningComplete; + } + + /** + * 设置校准模式 + * + * @param value true:零点校准模式,false:清零去皮模式 + */ + public void setCalibrationMode(boolean value) { + ModbusTcpServer.get().WriteBool("M1.4", value); + } + + /** + * 获取校准模式 + * + * @return true:零点校准模式,false:清零去皮模式 + */ + public boolean getCalibrationMode() { + return CalibrationMode; + } + + /** + * 获取指定称的当前重量 + * + * @param ch 需要获取的通道号,称1--称9 + * @return + */ + public float getCallCurrentWeight(int ch) { + if (!CallCurrentWeight.containsKey(ch)) { + return 0.0f; + } + return CallCurrentWeight.get(ch); + } + + /** + * 设置校准基准时间 + * + * @param value 需要写入的值 + */ + public void setCalibrationReferenceTime(float value) { + ModbusTcpServer.get().WriteFloat("VD124", value); + } + + /** + * 设置排水时间 + * + * @param value + */ + public void setDrainageTime(short value) { + ModbusTcpServer.get().WriteShort("VW140", value); + } + + /** + * 设置清洗剂添加时间 + * + * @param value + */ + public void setAddCleaningAgentTime(short value) { + ModbusTcpServer.get().WriteShort("VW142", value); + } + + /** + * 设置进水时间 + * + * @param value + */ + public void setInletTime(short value) { + ModbusTcpServer.get().WriteShort("VW144", value); + } + + /** + * 设置循环清洗时间 + * + * @param value + */ + public void setCyclicCleaningTime(short value) { + ModbusTcpServer.get().WriteShort("VW146", value); + } + + /** + * 设置8路称校准砝码重量 + * + * @param value + */ + public void setCalibrationWeight(short value) { + ModbusTcpServer.get().WriteShort("250", value); + } + + /** + * 设置称重9校准砝码重量 + * + * @param value + */ + public void setCalibrationWeight9(short value) { + ModbusTcpServer.get().WriteShort("254", value); + } + + +} diff --git a/app/src/main/java/com/example/bpa/app/IReadCallBack.java b/app/src/main/java/com/example/bpa/app/IReadCallBack.java new file mode 100644 index 0000000..e196018 --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/IReadCallBack.java @@ -0,0 +1,5 @@ +package com.example.bpa.app; + +public interface IReadCallBack { + void onSuccess(T t); +} diff --git a/app/src/main/java/com/example/bpa/app/Main.java b/app/src/main/java/com/example/bpa/app/Main.java index f6ba85f..e649561 100644 --- a/app/src/main/java/com/example/bpa/app/Main.java +++ b/app/src/main/java/com/example/bpa/app/Main.java @@ -35,9 +35,9 @@ public class Main { //1.加载配置 LoadingPZ(); //2.初始化PLC - PLC.getInstance().InitMobus(); +// PLC.getInstance().InitMobus(); //3.初始化业务 - //4.初始化日志上报 + //4.初始化日志上报adad } diff --git a/app/src/main/java/com/example/bpa/app/ModbusTcpHelper.java b/app/src/main/java/com/example/bpa/app/ModbusTcpHelper.java new file mode 100644 index 0000000..9a9b42c --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/ModbusTcpHelper.java @@ -0,0 +1,35 @@ +package com.example.bpa.app; + +import com.licheedev.modbus4android.ModbusWorker; + + +public class ModbusTcpHelper extends ModbusWorker { + private static volatile ModbusTcpHelper instance = null; + + public static ModbusTcpHelper get() { + ModbusTcpHelper manager = instance; + if (manager == null) { + synchronized (ModbusTcpHelper.class) { + manager = instance; + if (manager == null) { + manager = new ModbusTcpHelper(); + instance = manager; + } + } + } + return manager; + } + + private ModbusTcpHelper() { + } + + /** + * 释放整个ModbusManager,单例会被置null + */ + public synchronized void release() { + super.release(); + instance = null; + } + + +} diff --git a/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java b/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java new file mode 100644 index 0000000..de5fad4 --- /dev/null +++ b/app/src/main/java/com/example/bpa/app/ModbusTcpServer.java @@ -0,0 +1,287 @@ +package com.example.bpa.app; + + +import android.util.Log; + +import com.example.bpa.helper.MessageLog; +import com.licheedev.modbus4android.ModbusCallback; +import com.licheedev.modbus4android.ModbusParam; +import com.licheedev.modbus4android.param.TcpParam; +import com.serotonin.modbus4j.ModbusMaster; +import com.serotonin.modbus4j.msg.ReadCoilsResponse; +import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; +import com.serotonin.modbus4j.msg.WriteCoilResponse; +import com.serotonin.modbus4j.msg.WriteRegistersResponse; + +import java.lang.reflect.Type; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.ShortBuffer; +import java.util.Arrays; +import java.util.LinkedList; + +import io.reactivex.SingleConverter; + + +public class ModbusTcpServer { + private static volatile ModbusTcpServer instance = null; + + public static ModbusTcpServer get() { + ModbusTcpServer manager = instance; + if (manager == null) { + synchronized (ModbusTcpServer.class) { + manager = instance; + if (manager == null) { + manager = new ModbusTcpServer(); + instance = manager; + } + } + } + return manager; + } + + private ModbusTcpServer() { + } + + ModbusParam param; + + private int GetAddress(String address) { + if (address == null) return -1; + if (address.length() > 0) { + address = address.trim(); + if (address.toUpperCase().contains("M") && address.length() >= 4) { + String[] res = address.substring(1).split("."); + if (res != null && res.length == 2) { + try { + int firstAdd = Integer.parseInt(res[0]); + int endAdd = Integer.parseInt(res[1]); + if (endAdd >= 0 && endAdd <= 7) { + return (firstAdd * 8) + 320 + endAdd; + } + } catch (NumberFormatException e) { + return -1; + } + } + } else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) { + String res = address.substring(2); + if (res != null) { + try { + int tempAdd = Integer.parseInt(res); + return (tempAdd / 2) + 100; + } catch (NumberFormatException e) { + return -1; + } + } + } else { + try { + return Integer.parseInt(address); + } catch (NumberFormatException e) { + return -1; + } + } + } + return -1; + } + + /** + * 获取布尔位地址信息 + * 列:M2.5 = getBitSingleAdd("M",2,5); + * + * @param Prefix 地址标头 + * @param startAdd 起始地址编号 + * @param num 要获取的第几位数量 + * @return + */ + public String getBitSingleAdd(String Prefix, int startAdd, int num) { + if (num > 0) { + int FirstAdd = num / 8; + int EndAdd = num % 8; + if (EndAdd == 0) { + FirstAdd--; + EndAdd = 7; + } else { + EndAdd--; + } + return Prefix + FirstAdd + startAdd + "." + EndAdd; + } + return ""; + } + + /*** + * Modbus 连接 + * @param host 设备IP + * @param port 设备端口号 + */ + public void Connect(String host, int port) { + param = TcpParam.create(host, port) + .setTimeout(1000) + .setRetries(0) + .setEncapsulated(false) + .setKeepAlive(true); + ModbusTcpHelper.get().init(param, new ModbusCallback() { + @Override + public void onSuccess(ModbusMaster modbusMaster) { + MessageLog.ShowInfo("设备 " + host + " 连接成功"); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("设备 " + host + " 连接失败:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + } + + /*** + *读取整形数据 + * @param Address the address + * @param length 读取的长度 + * @param callback 读取成功的回调 + */ + + public void ReadShort(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + ModbusTcpHelper.get().readHoldingRegisters(1, add, length, new ModbusCallback() { + @Override + public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { + short[] data = readHoldingRegistersResponse.getShortData(); + callback.onSuccess(data); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("ReadShort onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + } + }); + } + + public void ReadBool(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + ModbusTcpHelper.get().readCoil(1, add, length, new ModbusCallback() { + @Override + public void onSuccess(ReadCoilsResponse readCoilsResponse) { + boolean[] data = readCoilsResponse.getBooleanData(); + boolean[] result = Arrays.copyOfRange(data, 0, length); + callback.onSuccess(result); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("ReadBool onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + } + }); + } + + public void ReadFloat(String Address, int length, IReadCallBack callback) { + int add = GetAddress(Address); + if (add < 0) return; + ModbusTcpHelper.get().readHoldingRegisters(1, add, length * 2, new ModbusCallback() { + @Override + public void onSuccess(ReadHoldingRegistersResponse readHoldingRegistersResponse) { + byte[] data = readHoldingRegistersResponse.getData(); + float[] tempValues = new float[length]; + for (int i = 0; i < length; i++) { + byte[] tempData = new byte[4]; + for (int m = 0; m < 4; m++) { + tempData[m] = data[i * 4 + m]; + } + tempValues[i] = ByteBuffer.wrap(tempData).getFloat(); + } + callback.onSuccess(tempValues); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("ReadFloat onFailure,Address=" + Address + ",length=" + length + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + + } + + public void WriteShort(String Address, short Value) { + int add = GetAddress(Address); + if (add < 0) return; + short[] send = new short[1]; + send[0] = Value; + ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback() { + @Override + public void onSuccess(WriteRegistersResponse writeRegistersResponse) { + MessageLog.ShowInfo("WriteShort onSuccess,Address=" + Address + ",Value=" + Value); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("WriteShort onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + } + + public void WriteBool(String Address, boolean Value) { + int add = GetAddress(Address); + if (add < 0) return; + ModbusTcpHelper.get().writeCoil(1, add, Value, new ModbusCallback() { + @Override + public void onSuccess(WriteCoilResponse writeCoilResponse) { + MessageLog.ShowInfo("WriteBool onSuccess,Address=" + Address + ",Value=" + Value); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("WriteBool onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + } + + public void WriteFloat(String Address, float Value) { + int add = GetAddress(Address); + if (add < 0) return; + int intBits = Float.floatToRawIntBits(Value); + short[] send = new short[]{(short) ((intBits >> 16) & 0xffff), (short) (intBits & 0xffff)}; + ModbusTcpHelper.get().writeRegisters(1, add, send, new ModbusCallback() { + @Override + public void onSuccess(WriteRegistersResponse writeRegistersResponse) { + MessageLog.ShowInfo("WriteFloat onSuccess,Address=" + Address + ",Value=" + Value); + } + + @Override + public void onFailure(Throwable tr) { + MessageLog.ShowError("WriteFloat onFailure,Address=" + Address + ",Value=" + Value + ",msg:" + tr.getMessage()); + } + + @Override + public void onFinally() { + + } + }); + } + +} diff --git a/app/src/main/java/com/example/bpa/app/PLC.java b/app/src/main/java/com/example/bpa/app/PLC.java deleted file mode 100644 index e0e82de..0000000 --- a/app/src/main/java/com/example/bpa/app/PLC.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.example.bpa.app; - -import com.example.bpa.config.ConfigName; -import com.example.bpa.helper.MY; -import com.example.bpa.helper.T; -import com.example.bpa.modbus4j.ModbusParam; -import com.example.bpa.modbus4j.ModbusReq; -import com.example.bpa.modbus4j.OnRequestBack; - -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * PLC 控制 - * add fengyoufu 20230414 - */ -public class PLC { - - //region 变量 - /** - * Ip地址 - */ - public String Host = "192.168.0.104"; - /** - * 端口 - */ - public int Post = 502; - /** - * 是否已连接 - */ - public boolean IsConnect = false; - //endregion - - //region 私有 - private static PLC mInstance; //实例变量设置私有,防止直接通过类名访问 - - public static synchronized PLC getInstance() { //静态同步方法作为唯一的实例对象获取方式 - if (mInstance == null) { - mInstance = new PLC(); - } - return mInstance; - } - - private PLC() { //默认构造函数私有,防止类外直接new创建对象 - - } - //endregion - - //region 外部调用事件 - /** - * 初始化Modbus - */ - public void InitMobus() { - /** - ModbusReq.getInstance().setParam(new ModbusParam() - .setHost(Host)//TCP PLC_IP地址 - .setPort(Post)//端口默认502 - .setEncapsulated(false) - .setKeepAlive(true) - .setTimeout(2000) - .setRetries(0)) - .init(new OnRequestBack() { - @Override - public void onSuccess(String s) { - T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接成功!"); - } - - @Override - public void onFailed(String msg) { - T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接失败!" + msg); - } - }); - **/ - } - - /** - * 写PLC - * - * @param address - * @param value - */ - public void WritePlcData(String address, Object value) { - byte slaveAddress = 1; - int startAddress = 200;//起始地址 - //startAddress=address; - - short[] shorts = new short[2]; - shorts[0] = 100; - shorts[1] = 200; - //32位的float 拆成两个16位的short -// float value = 1000000.52f; -// int intBits = Float.floatToRawIntBits(value); -// int[] send = new int[]{(intBits >>> 16) & 0xFFFF, intBits & 0xFFFF}; -// -// //注意高位低位处理 -// short[] shorts = new short[2]; -// shorts[0]=(short) send[1]; -// shorts[1]=(short) send[0]; - try { - - - ModbusReq.getInstance().writeRegisters(new OnRequestBack() { - @Override - public void onSuccess(String s) { - } - - @Override - public void onFailed(String msg) { - } - }, slaveAddress, startAddress, shorts); - } catch (Exception e) { - T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败"); - } - } - - /** - * 读PLC数据 - * @param address - */ - public void ReadPlcData(String address) { - byte slaveAddress = 1; - int startAddress = 200;//起始地址 - try { - ModbusReq.getInstance().readInputRegisters(new OnRequestBack() { - @Override - public void onSuccess(short[] var1) { - } - @Override - public void onFailed(String var1) { - } - }, slaveAddress, startAddress, 2); - - } catch (Exception e) { - T.show(ConfigName.getInstance().dishesCon, "与PLC通信失败"); - } - } - //endregion -} diff --git a/app/src/main/java/com/example/bpa/db/QueryDB.java b/app/src/main/java/com/example/bpa/db/QueryDB.java index d75a7a0..ca33805 100644 --- a/app/src/main/java/com/example/bpa/db/QueryDB.java +++ b/app/src/main/java/com/example/bpa/db/QueryDB.java @@ -24,6 +24,8 @@ import com.example.bpa.db.mode.BPA_SYSTEMSET; import com.example.bpa.db.mode.BPA_USER; import com.example.bpa.db.mode.ModeBase; import com.example.bpa.helper.Tools; +import com.example.bpa.view.mode.ResGoodsRecipe; +import com.example.bpa.view.mode.ResSilosMode; import java.lang.reflect.Field; import java.text.SimpleDateFormat; @@ -134,6 +136,26 @@ public class QueryDB { } return data.size()>0; } + + /** + * 物料ID查询物料信息 + * @param id + * @return + */ + public static BPA_MATERIAL GetMaterialID(String id) + { + BPA_MATERIAL bpa_material=null; + boolean isSucess=false; + String orderby=Desc_Time_Up;//出料顺序 + String where="isDelete=? and id=?"; + String[] args=new String[] { "0" ,id}; + ArrayList data=new ArrayList<>(); + ArrayList obj=Get(BPA_MATERIAL.class,where,args,orderby); + for (Object k:obj) { + bpa_material=(BPA_MATERIAL)k; + } + return bpa_material; + } //endregion //region BPA_SILOS 料仓管理表 @@ -182,6 +204,98 @@ public class QueryDB { } return data; } + /** + * 获取料仓数据 + * @return + */ + public static ArrayList GetSilos() + { + ArrayList data=new ArrayList<>(); + //1.获取料仓数据 + ArrayList silos=GetSilosALL(); + //2.获取物料 + ArrayList materials= GetMaterialALL(); + //3.获取返回数据 + for (BPA_SILOS k:silos) { + ResSilosMode par=new ResSilosMode(); + par.id=k.id; + par.createTime=k.createTime; + par.updateTime=k.updateTime; + par.deleteTime=k.deleteTime; + par.deviceID=k.deviceID; + par.userID=k.userID; + par.isDelete=k.isDelete; + par.exp=k.exp; + + par.num=k.num; + par.name=k.name; + par.warningValue=k.warningValue; + par.thrsoleValue=k.thrsoleValue; + par.plcValue=k.plcValue; + par.bValue=k.bValue; + par.jValue=k.jValue; + par.status=k.status; + par.Sort=k.Sort; + + ArrayList gx= GetMaterialList(k.id); + if(!gx.isEmpty() && gx.size()>0) + { + String wlid= gx.get(0); + if(!wlid.isEmpty()) + { + BPA_MATERIAL wl=null; + for (BPA_MATERIAL m:materials) { + if(m.id.equals(wlid)) + { + wl=m; + break; + } + } + if(wl!=null) + { + par.materialID=wl.id; + par.materialimgUrl=wl.imgUrl; + par.materialname=wl.name; + } + } + } + + par.dvalue=0; + data.add(par); + } + return data; + } + /** + * 根据ID查询料仓管理 + * @return + */ + public static BPA_SILOS GetSilosID(String id) + { + BPA_SILOS data=null; + String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序 + String where="isDelete=? and id=?"; + String[] args=new String[] { "0",id }; + ArrayList obj=Get(BPA_SILOS.class,where,args,orderby); + for (Object k:obj) { + data=(BPA_SILOS)k; + } + return data; + } + + /** + * 修改校准值 + * @param id + * @param data + */ + public static void UpdateJYZ(String id, int data) + { + BPA_SILOS silos= GetSilosID(id); + if(silos!=null) + { + silos.jValue=data; + Update(BPA_SILOS.class,silos); + } + } //endregion //region BPA_SILOSANDMATERIAL 料仓物料关联表 @@ -312,6 +426,77 @@ public class QueryDB { } return data; } + /** + * 判断商品数据是否存在 + * @param name + * @return + */ + public static boolean GetGoodsIs(String name) + { + boolean isSucess=false; + String orderby=Desc_Time_Up;//出料顺序 + String where="isDelete=? and name=?"; + String[] args=new String[] { "0" ,name}; + ArrayList data=new ArrayList<>(); + ArrayList obj=Get(BPA_GOODS.class,where,args,orderby); + for (Object k:obj) { + data.add((BPA_GOODS)k); + } + return data.size()>0; + } + + /** + * 根据排序查询商品 + * @param sort + * @return + */ + public static BPA_GOODS GetGoodsSortIs(int sort) + { + boolean isSucess=false; + String orderby=Desc_Time_Up;//出料顺序 + String where="isDelete=? and sort=?"; + String[] args=new String[] { "0" ,sort+""}; + BPA_GOODS data=null; + ArrayList obj=Get(BPA_GOODS.class,where,args,orderby); + for (Object k:obj) { + data=(BPA_GOODS)k; + } + return data; + } + + /** + * 商品数据排序 + * @param data 数据 + * @param type 0 下移动 1 上移动 + * @return + */ + public static void GetGoodsSort(BPA_GOODS data,int type) + { + if(type==0) + { + BPA_GOODS q_data= data; + BPA_GOODS h_data= GetGoodsSortIs(data.sort+1); + if(h_data!=null) + { + h_data.sort=h_data.sort-1; + UpdateGoods(h_data); + } + q_data.sort=q_data.sort+1; + UpdateGoods(q_data); + + }else + { + BPA_GOODS q_data= GetGoodsSortIs(data.sort-1); + BPA_GOODS h_data= data; + if(q_data!=null) + { + q_data.sort=q_data.sort+1; + UpdateGoods(q_data); + } + h_data.sort=h_data.sort-1; + UpdateGoods(h_data); + } + } //endregion //region BPA_GOODSRECIPE 商品配方明细表 @@ -366,18 +551,53 @@ public class QueryDB { * @param id * @return */ - public static ArrayList GetGoodsSrecipeList(String id) + public static ArrayList GetGoodsSrecipeList(String id) { String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序 String where="isDelete=? and goodsID=?"; String[] args=new String[] { "0",id }; - ArrayList data=new ArrayList<>(); + ArrayList data=new ArrayList<>(); ArrayList obj=Get(BPA_GOODSRECIPE.class,where,args,orderby); - for (Object k:obj) { - data.add(((BPA_GOODSRECIPE)k)); + for (Object item:obj) { + BPA_GOODSRECIPE k=(BPA_GOODSRECIPE)item; + BPA_MATERIAL ma= GetMaterialID(k.materialID); + if(ma!=null) + { + ResGoodsRecipe par=new ResGoodsRecipe(); + par.id=k.id; + par.createTime=k.createTime; + par.updateTime=k.updateTime; + par.deleteTime=k.deleteTime; + par.deviceID=k.deviceID; + par.userID=k.userID; + par.isDelete=k.isDelete; + par.exp=k.exp; + par.materialName=ma.name; + + par.goodsID=k.goodsID; + par.materialID=k.materialID; + par.value=k.value; + par.sort=k.sort; + data.add(par); + } } return data; } + /** + * 根据商品id删除配料信息 + * @param id + * @return + */ + public static Boolean DeleteGoodsSrecipeList(String id) + { + if (id.isEmpty()) + return false; + SQLiteDatabase db = helper.getWritableDatabase(); + long delete=db.delete(BPA_GOODSRECIPE.class.getSimpleName(), "goodsID=?", + new String[] { id }); + db.close(); + return delete>0; + } //endregion //region BPA_ORDER 订单表 @@ -1022,25 +1242,28 @@ public class QueryDB { if (id.isEmpty()) return false; for (String key : map.keySet()) { - Object value = map.get(key); - if(value instanceof String) - { - cv.put(key, (String) value); - }else if (value instanceof Integer) - { - cv.put(key, ((Integer) value).intValue()); - }else if (value instanceof Double) + if(!key.equals("id")) { - cv.put(key, ((Double) value).doubleValue()); - }else if (value instanceof Float) - { - cv.put(key, ((Float) value).floatValue()); - }else if (value instanceof Long) - { - cv.put(key, ((Long) value).longValue()); - }else if (value instanceof Boolean) - { - cv.put(key, ((Boolean) value).booleanValue()); + Object value = map.get(key); + if(value instanceof String) + { + cv.put(key, (String) value); + }else if (value instanceof Integer) + { + cv.put(key, ((Integer) value).intValue()); + }else if (value instanceof Double) + { + cv.put(key, ((Double) value).doubleValue()); + }else if (value instanceof Float) + { + cv.put(key, ((Float) value).floatValue()); + }else if (value instanceof Long) + { + cv.put(key, ((Long) value).longValue()); + }else if (value instanceof Boolean) + { + cv.put(key, ((Boolean) value).booleanValue()); + } } // else if (value instanceof Date) // { @@ -1048,7 +1271,6 @@ public class QueryDB { // } } SQLiteDatabase db = helper.getWritableDatabase(); - long insert = db.insert(c.getSimpleName(), null, cv); db.update(c.getSimpleName(),cv,"id = ?", new String[] { id }); db.close(); return true; diff --git a/app/src/main/java/com/example/bpa/helper/MessageLog.java b/app/src/main/java/com/example/bpa/helper/MessageLog.java new file mode 100644 index 0000000..df6da1a --- /dev/null +++ b/app/src/main/java/com/example/bpa/helper/MessageLog.java @@ -0,0 +1,31 @@ +package com.example.bpa.helper; + +import com.example.bpa.Model.IMessageLogNotify; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MessageLog { + public static IMessageLogNotify MsgNotify; + + public static void ShowInfo(String msg) { + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date date = new Date(); + if (MsgNotify != null) + MsgNotify.InfoMsg(formatter.format(date) + ":" + msg); + } + + public static void ShowWarning(String msg) { + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date date = new Date(); + if (MsgNotify != null) + MsgNotify.WarnMsg(formatter.format(date) + ":" + msg); + } + + public static void ShowError(String msg) { + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date date = new Date(); + if (MsgNotify != null) + MsgNotify.ErrorMsg(formatter.format(date) + ":" + msg); + } +} diff --git a/app/src/main/java/com/example/bpa/helper/SdCart.java b/app/src/main/java/com/example/bpa/helper/SdCart.java index f78c5bb..fb6ff05 100644 --- a/app/src/main/java/com/example/bpa/helper/SdCart.java +++ b/app/src/main/java/com/example/bpa/helper/SdCart.java @@ -27,6 +27,8 @@ public class SdCart { } return mInstance; } + + //endregion /** * 初始化数据库到SD卡 @@ -44,7 +46,7 @@ public class SdCart { ConfigName.getInstance().dbPath = ConfigName.getInstance().appResRoot + "/hbl.db"; File rootFile = new File(ConfigName.getInstance().appResRoot); if (!rootFile.exists()) //创建目录 - { + { Log.i("日志",ConfigName.getInstance().sdCardPath); rootFile.mkdirs(); } File file = new File(ConfigName.getInstance().dbPath); diff --git a/app/src/main/java/com/example/bpa/helper/ThreadManager.java b/app/src/main/java/com/example/bpa/helper/ThreadManager.java new file mode 100644 index 0000000..889d24d --- /dev/null +++ b/app/src/main/java/com/example/bpa/helper/ThreadManager.java @@ -0,0 +1,75 @@ +package com.example.bpa.helper; + +import com.example.bpa.Model.IThread; +import com.example.bpa.Model.ThreadModel; + +import java.util.concurrent.ConcurrentHashMap; + +public class ThreadManager { + private static volatile ThreadManager _Instance; + + public static ThreadManager Get() { + if (_Instance == null) + _Instance = new ThreadManager(); + return _Instance; + } + + private ThreadManager() { + + } + + public long RestartInterval = 2000; + ConcurrentHashMap ts = new ConcurrentHashMap<>(); + + public void StartLong(String Key, boolean IsRestart, IThread _thread) { + if (!ts.containsKey(Key)) { + ts.put(Key, new ThreadModel()); + ts.get(Key).RunThread = _thread; + ts.get(Key).ThreadObj = new Thread(new Runnable() { + @Override + public void run() { + MessageLog.ShowInfo("启动线程:" + Key); + while (!ts.get(Key).IsCancel) { + if (IsRestart) { + try { + ts.get(Key).RunThread.Run(); + } catch (Exception ex) { + MessageLog.ShowError("多线程:" + Key + "运行发生异常,已重启,errorMsg:" + ex.toString()); + try { + Thread.sleep(RestartInterval); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } else { + try { + ts.get(Key).RunThread.Run(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + MessageLog.ShowInfo("线程:[" + Key + "]--执行完成"); + try { + ts.get(Key).RunThread.RunComplete(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + ts.remove(Key); + } + }); + ts.get(Key).ThreadObj.setName(Key); + ts.get(Key).ThreadObj.start(); + } else { + MessageLog.ShowWarning("任务-[" + Key + "]-已存在"); + } + } + + + public void Stop(String Key) { + if (ts.containsKey(Key)) { + ts.get(Key).IsCancel = true; + } + } + +} diff --git a/app/src/main/java/com/example/bpa/modbus4j/ModbusParam.java b/app/src/main/java/com/example/bpa/modbus4j/ModbusParam.java deleted file mode 100644 index eb1832a..0000000 --- a/app/src/main/java/com/example/bpa/modbus4j/ModbusParam.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.example.bpa.modbus4j; - -/** - * Modebus 链接参数 - */ -public class ModbusParam { - public String host; - public int port = 502; - public boolean encapsulated; - public int timeout = 500; - public int retries = 2; - public boolean keepAlive = true; - public boolean connected = false; - - public ModbusParam() { - } - - public ModbusParam setHost(String host) { - this.host = host; - return this; - } - - public ModbusParam setPort(int port) { - this.port = port; - return this; - } - - public ModbusParam setEncapsulated(boolean encapsulated) { - this.encapsulated = encapsulated; - return this; - } - - public ModbusParam setTimeout(int timeout) { - this.timeout = timeout; - return this; - } - - public ModbusParam setRetries(int retries) { - this.retries = retries; - return this; - } - - public ModbusParam setConnected(boolean connected) { - this.connected = connected; - return this; - } - - public ModbusParam setKeepAlive(boolean keepAlive) { - this.keepAlive = keepAlive; - return this; - } -} diff --git a/app/src/main/java/com/example/bpa/modbus4j/ModbusReq.java b/app/src/main/java/com/example/bpa/modbus4j/ModbusReq.java deleted file mode 100644 index 525adbc..0000000 --- a/app/src/main/java/com/example/bpa/modbus4j/ModbusReq.java +++ /dev/null @@ -1,364 +0,0 @@ -package com.example.bpa.modbus4j; - -import android.util.Log; - -import com.serotonin.modbus4j.ModbusFactory; -import com.serotonin.modbus4j.ModbusMaster; -import com.serotonin.modbus4j.exception.ModbusInitException; -import com.serotonin.modbus4j.exception.ModbusTransportException; -import com.serotonin.modbus4j.ip.IpParameters; -import com.serotonin.modbus4j.msg.ReadCoilsRequest; -import com.serotonin.modbus4j.msg.ReadCoilsResponse; -import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest; -import com.serotonin.modbus4j.msg.ReadDiscreteInputsResponse; -import com.serotonin.modbus4j.msg.ReadExceptionStatusRequest; -import com.serotonin.modbus4j.msg.ReadExceptionStatusResponse; -import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest; -import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; -import com.serotonin.modbus4j.msg.ReadInputRegistersRequest; -import com.serotonin.modbus4j.msg.ReadInputRegistersResponse; -import com.serotonin.modbus4j.msg.ReportSlaveIdRequest; -import com.serotonin.modbus4j.msg.ReportSlaveIdResponse; -import com.serotonin.modbus4j.msg.WriteCoilRequest; -import com.serotonin.modbus4j.msg.WriteCoilResponse; -import com.serotonin.modbus4j.msg.WriteCoilsRequest; -import com.serotonin.modbus4j.msg.WriteCoilsResponse; -import com.serotonin.modbus4j.msg.WriteMaskRegisterRequest; -import com.serotonin.modbus4j.msg.WriteMaskRegisterResponse; -import com.serotonin.modbus4j.msg.WriteRegisterRequest; -import com.serotonin.modbus4j.msg.WriteRegisterResponse; -import com.serotonin.modbus4j.msg.WriteRegistersRequest; -import com.serotonin.modbus4j.msg.WriteRegistersResponse; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public class ModbusReq { - private static final String TAG = ModbusReq.class.getSimpleName(); - private static ModbusReq modbusReq; - private ModbusMaster mModbusMaster; - private ModbusParam modbusParam = new ModbusParam(); - ExecutorService executorService = Executors.newFixedThreadPool(1); - private boolean isInit = false; - - private ModbusReq() { - } - - public static synchronized ModbusReq getInstance() { - if (modbusReq == null) { - modbusReq = new ModbusReq(); - } - - return modbusReq; - } - - public ModbusReq setParam(ModbusParam modbusParam) { - this.modbusParam = modbusParam; - return modbusReq; - } - - public void init(final OnRequestBack onRequestBack) { - ModbusFactory mModbusFactory = new ModbusFactory(); - IpParameters params = new IpParameters(); - params.setHost(this.modbusParam.host); - params.setPort(this.modbusParam.port); - params.setEncapsulated(this.modbusParam.encapsulated); - this.mModbusMaster = mModbusFactory.createTcpMaster(params, this.modbusParam.keepAlive); - this.mModbusMaster.setRetries(this.modbusParam.retries); - this.mModbusMaster.setTimeout(this.modbusParam.timeout); - this.executorService.submit(new Runnable() { - public void run() { - try { - ModbusReq.this.mModbusMaster.init(); - } catch (ModbusInitException var2) { - ModbusReq.this.mModbusMaster.destroy(); - ModbusReq.this.isInit = false; - Log.d(ModbusReq.TAG, "Modbus4Android init failed " + var2); - onRequestBack.onFailed("Modbus4Android init failed "); - return; - } - - Log.d(ModbusReq.TAG, "Modbus4Android init success"); - ModbusReq.this.isInit = true; - onRequestBack.onSuccess("Modbus4Android init success"); - } - }); - } - - public void destory() { - modbusReq = null; - this.mModbusMaster.destroy(); - this.isInit = false; - } - - public void readCoil(final OnRequestBack onRequestBack, final int slaveId, final int start, final int len) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReadCoilsRequest request = new ReadCoilsRequest(slaveId, start, len); - ReadCoilsResponse response = (ReadCoilsResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - boolean[] booleanData = response.getBooleanData(); - boolean[] resultByte = new boolean[len]; - System.arraycopy(booleanData, 0, resultByte, 0, len); - onRequestBack.onSuccess(resultByte); - } - } catch (ModbusTransportException var5) { - var5.printStackTrace(); - onRequestBack.onFailed(var5.toString()); - } - - } - }); - } - } - - public void readDiscreteInput(final OnRequestBack onRequestBack, final int slaveId, final int start, final int len) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, start, len); - ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - boolean[] booleanData = response.getBooleanData(); - boolean[] resultByte = new boolean[len]; - System.arraycopy(booleanData, 0, resultByte, 0, len); - onRequestBack.onSuccess(resultByte); - } - } catch (ModbusTransportException var5) { - var5.printStackTrace(); - onRequestBack.onFailed(var5.toString()); - } - - } - }); - } - } - - public void readHoldingRegisters(final OnRequestBack onRequestBack, final int slaveId, final int start, final int len) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len); - ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess(response.getShortData()); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void readInputRegisters(final OnRequestBack onRequestBack, final int slaveId, final int start, final int len) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId, start, len); - ReadInputRegistersResponse response = (ReadInputRegistersResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess(response.getShortData()); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void writeCoil(final OnRequestBack onRequestBack, final int slaveId, final int offset, final boolean value) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - WriteCoilRequest request = new WriteCoilRequest(slaveId, offset, value); - WriteCoilResponse response = (WriteCoilResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess("Success"); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void writeCoils(final OnRequestBack onRequestBack, final int slaveId, final int start, final boolean[] values) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - WriteCoilsRequest request = new WriteCoilsRequest(slaveId, start, values); - WriteCoilsResponse response = (WriteCoilsResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess("Success"); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void writeRegister(final OnRequestBack onRequestBack, final int slaveId, final int offset, final int value) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - WriteRegisterRequest request = new WriteRegisterRequest(slaveId, offset, value); - WriteRegisterResponse response = (WriteRegisterResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess("Success"); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void writeRegisters(final OnRequestBack onRequestBack, final int slaveId, final int start, final short[] values) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - WriteRegistersRequest request = new WriteRegistersRequest(slaveId, start, values); - WriteRegistersResponse response = (WriteRegistersResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess("Success"); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - - public void readExceptionStatus(final OnRequestBack onRequestBack, final int slaveId) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReadExceptionStatusRequest request = new ReadExceptionStatusRequest(slaveId); - ReadExceptionStatusResponse response = (ReadExceptionStatusResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess(response.getExceptionStatus()); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void reportSlaveId(final OnRequestBack onRequestBack, final int slaveId) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - ReportSlaveIdRequest request = new ReportSlaveIdRequest(slaveId); - ReportSlaveIdResponse response = (ReportSlaveIdResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess(response.getData()); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } - - public void writeMaskRegister(final OnRequestBack onRequestBack, final int slaveId, final int offset, final int and, final int or) { - if (!this.isInit) { - onRequestBack.onFailed("Modbus master is not inited successfully..."); - } else { - this.executorService.submit(new Runnable() { - public void run() { - try { - WriteMaskRegisterRequest request = new WriteMaskRegisterRequest(slaveId, offset, and, or); - WriteMaskRegisterResponse response = (WriteMaskRegisterResponse)ModbusReq.this.mModbusMaster.send(request); - if (response.isException()) { - onRequestBack.onFailed(response.getExceptionMessage()); - } else { - onRequestBack.onSuccess("Success"); - } - } catch (ModbusTransportException var3) { - var3.printStackTrace(); - onRequestBack.onFailed(var3.toString()); - } - - } - }); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/modbus4j/OnRequestBack.java b/app/src/main/java/com/example/bpa/modbus4j/OnRequestBack.java deleted file mode 100644 index 876dc24..0000000 --- a/app/src/main/java/com/example/bpa/modbus4j/OnRequestBack.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.example.bpa.modbus4j; - -public interface OnRequestBack { - void onSuccess(T var1); - void onFailed(String var1); -} diff --git a/app/src/main/java/com/example/bpa/view/adapter/lsjy_adapter.java b/app/src/main/java/com/example/bpa/view/adapter/lsjy_adapter.java new file mode 100644 index 0000000..2ee855a --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/adapter/lsjy_adapter.java @@ -0,0 +1,69 @@ +package com.example.bpa.view.adapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.example.bpa.R; +import com.example.bpa.db.mode.BPA_MATERIAL; +import com.example.bpa.helper.T; +import com.example.bpa.view.inteface.MyClickListener; +import com.example.bpa.view.mode.ResSilosMode; + +import java.util.List; + +/** + * 流速校验DataTab + */ +public class lsjy_adapter extends ArrayAdapter { + /** + * 内部点击事件 + */ + private MyClickListener mListener; + private List datas; + public lsjy_adapter(@NonNull Context context, int resource, @NonNull List objects, MyClickListener listener) { + super(context, resource, objects); + mListener = listener; + datas=objects; + } + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + ResSilosMode resSilosMode = (ResSilosMode) getItem(position);//得到当前项选中item实例 + //为每一个子项加载设定的布局 + View view = LayoutInflater.from(getContext()).inflate(R.layout.lsjy_item, parent, false); + //分别获取 image view 和 textview 的实例 + TextView name = view.findViewById(R.id.name); + TextView lc_num = view.findViewById(R.id.lc_num); + TextView lc_bzls = view.findViewById(R.id.lc_bzls); + TextView lc_xyls = view.findViewById(R.id.lc_xyls); + Button button = view.findViewById(R.id.button_item); + // 设置要显示的图片和文字 + name.setText(resSilosMode.materialname); + lc_num.setText(""+resSilosMode.num); + lc_bzls.setText(""+resSilosMode.bValue); + lc_xyls.setText(""+resSilosMode.jValue); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try + { + mListener.clickListener(view,resSilosMode); + } catch (Exception e) + { + T.show(view.getContext(),"校验流速出错:"+e.getMessage()); + } + } + }); + return view; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/view/adapter/pf_adapter.java b/app/src/main/java/com/example/bpa/view/adapter/pf_adapter.java new file mode 100644 index 0000000..cfb7d2b --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/adapter/pf_adapter.java @@ -0,0 +1,95 @@ +package com.example.bpa.view.adapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; +import android.widget.SpinnerAdapter; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.example.bpa.R; +import com.example.bpa.db.mode.BPA_MATERIAL; +import com.example.bpa.helper.T; +import com.example.bpa.view.inteface.MyClickListener; +import com.example.bpa.view.mode.ResGoodsRecipe; + +import java.util.ArrayList; +import java.util.List; + +/** + * 配料数据表格 + */ +public class pf_adapter extends ArrayAdapter { + /** + * 内部点击事件 + */ + private MyClickListener mListener; + private List datas; + + ArrayList wuliao=new ArrayList<>(); + + public pf_adapter(@NonNull Context context, int resource, @NonNull List objects, ArrayList wl, MyClickListener listener) { + super(context, resource, objects); + mListener = listener; + datas=objects; + if(wuliao.size()<=0) + { + wuliao=new ArrayList<>(); + for (BPA_MATERIAL me:wl) + { + wuliao.add(me.name); + } + } + } + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + ResGoodsRecipe resGoodsRecipe = (ResGoodsRecipe) getItem(position);//得到当前项选中item实例 + //为每一个子项加载设定的布局 + View view = LayoutInflater.from(getContext()).inflate(R.layout.pf_item, parent, false); + //分别获取 image view 和 textview 的实例 + Spinner name = view.findViewById(R.id.name); + TextView sort = view.findViewById(R.id.sort); + TextView value = view.findViewById(R.id.value); + Button button = view.findViewById(R.id.button_item); + // 设置要显示的图片和文字 + ArrayAdapter mAdapter = new android.widget.ArrayAdapter(view.getContext(), + android.R.layout.simple_spinner_item); + mAdapter.addAll(wuliao); + name.setAdapter(mAdapter); + + if(resGoodsRecipe.materialName!=null && !resGoodsRecipe.materialName.isEmpty()) + { + SpinnerAdapter apsAdapter= name.getAdapter(); + int k= apsAdapter.getCount(); + for(int i=0;i { + /** + * 内部点击事件 + */ + private MyClickListener mListener; + private List datas; + public sp_adapter(@NonNull Context context, int resource, @NonNull List objects, MyClickListener listener) { + super(context, resource, objects); + mListener = listener; + datas=objects; + } + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + BPA_GOODS bpa_goods = (BPA_GOODS) getItem(position);//得到当前项选中item实例 + //为每一个子项加载设定的布局 + View view = LayoutInflater.from(getContext()).inflate(R.layout.sp_item, parent, false); + //分别获取 image view 和 textview 的实例 + TextView name = view.findViewById(R.id.name); + TextView sort = view.findViewById(R.id.sort); + TextView time = view.findViewById(R.id.time); + + Button button = view.findViewById(R.id.button_item); + Button button_up = view.findViewById(R.id.button_item_Up); + Button button_down = view.findViewById(R.id.button_item_Down); + Button button_update = view.findViewById(R.id.button_item_update); + + // 设置要显示的图片和文字 + name.setText(bpa_goods.name); + time.setText(bpa_goods.createTime); + sort.setText(bpa_goods.sort+""); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try + { + mListener.clickListener(view,bpa_goods); + } catch (Exception e) + { + T.show(view.getContext(),"删除商品出错:"+e.getMessage()); + } + } + }); + button_up.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try + { + mListener.clickListener(view,bpa_goods); + } catch (Exception e) + { + T.show(view.getContext(),"商品排序出错:"+e.getMessage()); + } + } + }); + button_down.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try + { + mListener.clickListener(view,bpa_goods); + } catch (Exception e) + { + T.show(view.getContext(),"商品排序出错:"+e.getMessage()); + } + } + }); + button_update.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try + { + mListener.clickListener(view,bpa_goods); + } catch (Exception e) + { + T.show(view.getContext(),"商品修改出错:"+e.getMessage()); + } + } + }); + return view; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/view/fragment/SystemCapabilitiesFragment.java b/app/src/main/java/com/example/bpa/view/fragment/SystemCapabilitiesFragment.java index 8a57beb..c9c9aa8 100644 --- a/app/src/main/java/com/example/bpa/view/fragment/SystemCapabilitiesFragment.java +++ b/app/src/main/java/com/example/bpa/view/fragment/SystemCapabilitiesFragment.java @@ -2,7 +2,6 @@ package com.example.bpa.view.fragment; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; @@ -11,14 +10,10 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; -import com.example.bpa.LoginActivity; -import com.example.bpa.MainActivity; import com.example.bpa.R; -import com.example.bpa.config.ConfigName; import com.example.bpa.helper.T; import com.example.bpa.view.control.ItemClickListener; import com.example.bpa.view.control.MainMeunAdapter; diff --git a/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java b/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java index b2dfe08..2bda4cc 100644 --- a/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java +++ b/app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java @@ -2,24 +2,148 @@ package com.example.bpa.view.fragment; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import com.example.bpa.Model.IMessageLogNotify; +import com.example.bpa.Model.IThread; +import com.example.bpa.Model.ThreadModel; import com.example.bpa.R; +import com.example.bpa.app.IReadCallBack; +import com.example.bpa.app.ModbusTcpServer; +import com.example.bpa.helper.MessageLog; +import com.example.bpa.helper.ThreadManager; /** * 系统设置界面 */ public class SystemSetFragment extends Fragment { + + private Button ConBut; + private Button ReadBut; + private Button WriteBut; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_system_set, container,false); + View view = inflater.inflate(R.layout.fragment_system_set, container, false); + Init(view); return view; } + + private void Init(View tempView) { + ConBut = tempView.findViewById(R.id.ConnectDevice); + ReadBut = tempView.findViewById(R.id.ReadData); + WriteBut = tempView.findViewById(R.id.WriteData); + + MessageLog.MsgNotify = new IMessageLogNotify() { + @Override + public void ErrorMsg(String msg) { + Log.e("Error", msg); + } + + @Override + public void InfoMsg(String msg) { + Log.i("Info", msg); + } + + @Override + public void WarnMsg(String msg) { + Log.w("Warn", msg); + } + }; + + ConBut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + new Thread(new Runnable() { + @Override + public void run() { + ModbusTcpServer.get().Connect("192.168.1.14", 502); + } + }).start(); + } + }); + + ReadBut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + ThreadManager.Get().StartLong("数据读取监听", true, new IThread() { + @Override + public void Run() throws InterruptedException { +// ModbusTcpServer.get().ReadShort(0, 3, new IReadCallBack() { +// @Override +// public void onSuccess(short[] shorts) { +// for (int i = 0; i < shorts.length; i++) { +// MessageLog.ShowInfo("Shorts[" + i + "]=" + shorts[i]); +// } +// } +// }); +// +// ModbusTcpServer.get().ReadFloat(10, 3, new IReadCallBack() { +// @Override +// public void onSuccess(float[] floats) { +// for (int i = 0; i < floats.length; i++) { +// MessageLog.ShowInfo("Floats[" + i + "]=" + floats[i]); +// } +// } +// }); +// +// ModbusTcpServer.get().ReadBool(0, 3, new IReadCallBack() { +// @Override +// public void onSuccess(boolean[] booleans) { +// for (int i = 0; i < booleans.length; i++) { +// MessageLog.ShowInfo("Booleans[" + i + "]=" + booleans[i]); +// } +// } +// }); + + MessageLog.ShowInfo("开始执行"); + boolean[] bools = new boolean[10]; + for (int i = 0; i < 20; i++) { + bools[i] = true; + } + + Thread.sleep(3000); + + } + + @Override + public void RunComplete() throws InterruptedException { + MessageLog.ShowInfo("线程执行完成"); + } + }); + + + } + }); + + WriteBut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ThreadManager.Get().Stop("数据读取监听"); + ModbusTcpServer.get().WriteBool("0", true); + ModbusTcpServer.get().WriteBool("1", true); + ModbusTcpServer.get().WriteBool("2", true); + + ModbusTcpServer.get().WriteShort("0", (short) 10); + ModbusTcpServer.get().WriteShort("1", (short) 20); + ModbusTcpServer.get().WriteShort("2", (short) 30); + + ModbusTcpServer.get().WriteFloat("10", 10.10f); + ModbusTcpServer.get().WriteFloat("12", 20.20f); + ModbusTcpServer.get().WriteFloat("14", 30.30f); + } + }); + + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java b/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java new file mode 100644 index 0000000..63414bc --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java @@ -0,0 +1,227 @@ +package com.example.bpa.view.from; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import com.example.bpa.R; +import com.example.bpa.config.ConfigName; +import com.example.bpa.db.QueryDB; +import com.example.bpa.db.mode.BPA_GOODS; +import com.example.bpa.db.mode.BPA_GOODSRECIPE; +import com.example.bpa.db.mode.BPA_MATERIAL; +import com.example.bpa.helper.Json; +import com.example.bpa.helper.T; +import com.example.bpa.view.adapter.pf_adapter; +import com.example.bpa.view.adapter.sp_adapter; +import com.example.bpa.view.inteface.MyClickListener; +import com.example.bpa.view.mode.ResGoodsRecipe; + +import java.util.ArrayList; + +public class add_pf_activity extends AppCompatActivity implements View.OnClickListener, MyClickListener { + //region 变量 + /** + * 返回按钮 + */ + ImageView gongneng_fanhui; + /** + * 标题设置 + */ + TextView gongneng_title; + //endregion + + //region 操作变量 + /** + * 输入框 + */ + TextView edittext; + /** + * 增加按钮 + */ + Button buttonsave, buttonaddwuliao; + /** + * 表格显示 + */ + ListView datatab; + /** + * 配方数据 + */ + ArrayList bpa_pf = new ArrayList<>(); + /** + * 当前传入的商品数据 + */ + BPA_GOODS good = null; + + /** + * 商品数据 + */ + ArrayList bpa_goods=new ArrayList<>(); + /** + * wulaipo + */ + ArrayList wuliao=new ArrayList<>(); + //endregion + + //region 私有函数 + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_add_pf); + Init(); + initEvents(); + } + //endregion + + //region 公共函数 + + /** + * 初始化 + */ + private void Init() { + gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui); + gongneng_title = this.findViewById(R.id.gongneng_title); + edittext = this.findViewById(R.id.edittext); + buttonsave = this.findViewById(R.id.buttonsave); + buttonaddwuliao = this.findViewById(R.id.buttonaddwuliao); + datatab = this.findViewById(R.id.datatab); + //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据 + String msg = getIntent().getStringExtra("title"); + String data = getIntent().getStringExtra("data"); + if (!data.isEmpty()) { + good = new Json().jsonToobject(BPA_GOODS.class, data); + edittext.setText(good.name); + } + gongneng_title.setText(msg); + Initdata(); + bpa_goods= QueryDB.GetGoodsALL(); + wuliao=QueryDB.GetMaterialALL(); + } + + /** + * 初始化按钮事件 + */ + private void initEvents() { + gongneng_fanhui.setOnClickListener(this); + buttonsave.setOnClickListener(this); + buttonaddwuliao.setOnClickListener(this); + } + + /** + * 初始化数据加载 + */ + public void Initdata() { + bpa_pf = new ArrayList<>(); + try { + if (good != null)//修改 + { + bpa_pf= QueryDB.GetGoodsSrecipeList(good.id); + } + } catch (Exception e) { + + } + } + + /** + * 刷新DataTab + */ + public void DataTab() + { + pf_adapter adapter = new pf_adapter(add_pf_activity.this, R.layout.pf_item, bpa_pf,wuliao,this); + datatab.setAdapter(adapter); + } + //endregion + + //region 点击事件 + + /** + * 本页面点击事件监听22 + * + * @param v + */ + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.gongneng_fanhui://返回按钮 + this.finish(); + break; + case R.id.buttonaddwuliao://新增空白行 + ResGoodsRecipe recipe=new ResGoodsRecipe(); + recipe.sort=bpa_pf.size()+1; + bpa_pf.add(recipe); + DataTab(); + break; + case R.id.buttonsave://保存按钮点击 + if(bpa_pf.size()<=0) + { + T.show(this, "配方信息不能为空!"); + return; + } + if (good == null)//新增 + { + String name = edittext.getText().toString(); + if (name.isEmpty()) { + T.show(this, "商品名称不能为空!"); + return; + } + if (QueryDB.GetGoodsIs(name)) { + T.show(this, "商品名称重复,请重新输入后重试!"); + return; + } + BPA_GOODS goodx = new BPA_GOODS(); + goodx.name = name; + goodx.sort = bpa_goods.size() + 1; + goodx.status = 1; + goodx.deviceID = ConfigName.getInstance().DeviceId; + goodx.userID = ConfigName.getInstance().user.userID; + QueryDB.AddGoods(goodx); + good=goodx; + }else //修改 + { + QueryDB.UpdateGoods(good); + QueryDB.DeleteGoodsSrecipeList(good.id); + } + + for (ResGoodsRecipe item:bpa_pf) + { + BPA_GOODSRECIPE da=new BPA_GOODSRECIPE(); + da.goodsID = good.id; + da.materialID = item.materialID; + da.value = item.value; + da.sort =item.sort; + da.deviceID = ConfigName.getInstance().DeviceId; + da.userID = ConfigName.getInstance().user.userID; + QueryDB.AddGoodsSrecipe(da); + } + T.show(this, "操作成功!"); + this.finish(); + break; + } + } + + /** + * 接口方法,响应ListView按钮点击事件 + */ + @Override + public void clickListener(View v, Object data) { + switch (v.getId()) { + case R.id.button_item://删除 + bpa_pf.remove(data); + DataTab(); + break; + case R.id.button_item_Up://上移动 + + break; + case R.id.button_item_Down://下移动 + + break; + } + } + //endregion +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/view/from/dzcjy_activity.java b/app/src/main/java/com/example/bpa/view/from/dzcjy_activity.java index eefcf4e..579a718 100644 --- a/app/src/main/java/com/example/bpa/view/from/dzcjy_activity.java +++ b/app/src/main/java/com/example/bpa/view/from/dzcjy_activity.java @@ -1,13 +1,24 @@ package com.example.bpa.view.from; import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.RecyclerView; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.example.bpa.R; +import com.example.bpa.helper.T; +import com.example.bpa.view.control.ItemClickListener; +import com.example.bpa.view.control.MainMeunAdapter; +import com.example.bpa.view.control.MyLayoutManager; +import com.example.bpa.view.mode.MenuMode; + +import java.util.ArrayList; +import java.util.List; + /** * 电子秤校验 */ @@ -21,6 +32,9 @@ public class dzcjy_activity extends AppCompatActivity implements View.OnClickLis * 标题设置 */ TextView gongneng_title; + RecyclerView recyclerView; + MainMeunAdapter adapter; + List menuModes = new ArrayList<>(); //endregion //region 私有函数 @@ -45,7 +59,46 @@ public class dzcjy_activity extends AppCompatActivity implements View.OnClickLis String msg=getIntent().getStringExtra("data"); gongneng_title.setText(msg); - ((TextView)this.findViewById(R.id.test_view)).setText(msg); + recyclerView = this.findViewById(R.id.recycler_view); + + menuModes.clear(); + menuModes.add(new MenuMode("设置最大称重",R.mipmap.zuidachengzhong)); + menuModes.add(new MenuMode("设置分度参数",R.mipmap.shezhifenducanshu)); + menuModes.add(new MenuMode("归零",R.mipmap.guiling)); + menuModes.add(new MenuMode("设置标准重量",R.mipmap.biaozhunzhongliang)); + menuModes.add(new MenuMode("称重校验",R.mipmap.chengzhongjiaoyan)); + + MyLayoutManager layout = new MyLayoutManager(); + //必须,防止recyclerview高度为wrap时测量item高度0 + layout.setAutoMeasureEnabled(true); + recyclerView.setLayoutManager(layout); + adapter = new MainMeunAdapter(this, menuModes); + recyclerView.setAdapter(adapter); + recyclerView.addOnItemTouchListener(new ItemClickListener(recyclerView, new ItemClickListener.OnItemClickListener() { + @Override + public void onItemClick(View view, int position) { + TextView textView = (TextView) view.findViewById(R.id.meun_textview); + T.show(view.getContext(),"打开窗体:"+ textView.getText().toString()); + switch (textView.getText().toString()) + { + case "设置最大称重": + break; + case "设置分度参数": + break; + case "归零": + break; + case "设置标准重量": + break; + case "称重校验": + default: + break; + } + } + @Override + public void onItemLongClick(View view, int position) { + + } + })); } /** diff --git a/app/src/main/java/com/example/bpa/view/from/lsjy_activity.java b/app/src/main/java/com/example/bpa/view/from/lsjy_activity.java index 3c39472..375f0d5 100644 --- a/app/src/main/java/com/example/bpa/view/from/lsjy_activity.java +++ b/app/src/main/java/com/example/bpa/view/from/lsjy_activity.java @@ -4,14 +4,25 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; +import android.widget.ListView; import android.widget.TextView; import com.example.bpa.R; +import com.example.bpa.db.QueryDB; +import com.example.bpa.db.mode.BPA_MATERIAL; +import com.example.bpa.db.mode.BPA_SILOS; +import com.example.bpa.helper.T; +import com.example.bpa.view.adapter.lsjy_adapter; +import com.example.bpa.view.adapter.wl_adapter; +import com.example.bpa.view.inteface.MyClickListener; +import com.example.bpa.view.mode.ResSilosMode; + +import java.util.ArrayList; /** * 流速校验 */ -public class lsjy_activity extends AppCompatActivity implements View.OnClickListener{ +public class lsjy_activity extends AppCompatActivity implements View.OnClickListener, MyClickListener { //region 变量 /** * 返回按钮 @@ -21,6 +32,14 @@ public class lsjy_activity extends AppCompatActivity implements View.OnClickList * 标题设置 */ TextView gongneng_title; + /** + * 表格显示 + */ + ListView datatab; + /** + * 料仓数据 + */ + ArrayList resSilosModes=new ArrayList<>(); //endregion //region 私有函数 @@ -41,11 +60,11 @@ public class lsjy_activity extends AppCompatActivity implements View.OnClickList private void Init(){ gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui); gongneng_title = this.findViewById(R.id.gongneng_title); + datatab= this.findViewById(R.id.datatab); //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据 String msg=getIntent().getStringExtra("data"); gongneng_title.setText(msg); - - ((TextView)this.findViewById(R.id.test_view)).setText(msg); + Initdata(); } /** @@ -54,6 +73,20 @@ public class lsjy_activity extends AppCompatActivity implements View.OnClickList private void initEvents() { gongneng_fanhui.setOnClickListener(this); } + + /** + * 初始化数据加载 + */ + public void Initdata() + { + try{ + resSilosModes= QueryDB.GetSilos(); + lsjy_adapter adapter = new lsjy_adapter(lsjy_activity.this, R.layout.lsjy_item, resSilosModes,this); + datatab.setAdapter(adapter); + }catch(Exception e){ + + } + } //endregion //region 点击事件 @@ -70,5 +103,21 @@ public class lsjy_activity extends AppCompatActivity implements View.OnClickList break; } } + int k=404; + /** + * 接口方法,响应ListView按钮点击事件 + */ + @Override + public void clickListener(View v,Object data) { + switch (v.getId()) + { + case R.id.button_item://校验 + k=k+1; + T.show(this,((ResSilosMode)data).id); + QueryDB.UpdateJYZ(((ResSilosMode)data).id,k); + Initdata(); + break; + } + } //endregion } \ No newline at end of file diff --git a/app/src/main/java/com/example/bpa/view/from/wlgl_activity.java b/app/src/main/java/com/example/bpa/view/from/wlgl_activity.java index d89803b..e7e3d9f 100644 --- a/app/src/main/java/com/example/bpa/view/from/wlgl_activity.java +++ b/app/src/main/java/com/example/bpa/view/from/wlgl_activity.java @@ -35,8 +35,10 @@ public class wlgl_activity extends AppCompatActivity implements View.OnClickList * 标题设置 */ TextView gongneng_title; - //edittext buttonadd ListView datatab + //endregion + + //region 操作变量 /** * 输入框 */ @@ -53,8 +55,6 @@ public class wlgl_activity extends AppCompatActivity implements View.OnClickList * 物料数据 */ ArrayList bpa_materials=new ArrayList<>(); - //region 操作变量 - //endregion //region 私有函数 diff --git a/app/src/main/java/com/example/bpa/view/from/yfpf_activity.java b/app/src/main/java/com/example/bpa/view/from/yfpf_activity.java index 4894e56..e07aa53 100644 --- a/app/src/main/java/com/example/bpa/view/from/yfpf_activity.java +++ b/app/src/main/java/com/example/bpa/view/from/yfpf_activity.java @@ -2,17 +2,30 @@ package com.example.bpa.view.from; import androidx.appcompat.app.AppCompatActivity; +import android.content.Intent; import android.os.Bundle; +import android.util.Log; import android.view.View; +import android.widget.Button; import android.widget.ImageView; +import android.widget.ListView; import android.widget.TextView; import com.example.bpa.R; +import com.example.bpa.config.ConfigName; +import com.example.bpa.db.QueryDB; +import com.example.bpa.db.mode.BPA_GOODS; +import com.example.bpa.helper.Json; +import com.example.bpa.helper.T; +import com.example.bpa.view.adapter.sp_adapter; +import com.example.bpa.view.inteface.MyClickListener; + +import java.util.ArrayList; /** * 配方研发 */ -public class yfpf_activity extends AppCompatActivity implements View.OnClickListener{ +public class yfpf_activity extends AppCompatActivity implements View.OnClickListener, MyClickListener { //region 变量 /** * 返回按钮 @@ -24,6 +37,21 @@ public class yfpf_activity extends AppCompatActivity implements View.OnClickList TextView gongneng_title; //endregion + //region 操作变量 + /** + * 增加按钮 + */ + Button buttonaddpf; + /** + * 表格显示 + */ + ListView datatab; + /** + * 商品数据 + */ + ArrayList bpa_goods=new ArrayList<>(); + //endregion + //region 私有函数 @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,11 +70,12 @@ public class yfpf_activity extends AppCompatActivity implements View.OnClickList private void Init(){ gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui); gongneng_title = this.findViewById(R.id.gongneng_title); + buttonaddpf=this.findViewById(R.id.buttonaddpf); + datatab= this.findViewById(R.id.datatab); //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据 String msg=getIntent().getStringExtra("data"); gongneng_title.setText(msg); - - ((TextView)this.findViewById(R.id.test_view)).setText(msg); + Initdata(); } /** @@ -54,12 +83,27 @@ public class yfpf_activity extends AppCompatActivity implements View.OnClickList */ private void initEvents() { gongneng_fanhui.setOnClickListener(this); + buttonaddpf.setOnClickListener(this); + } + + /** + * 初始化数据加载 + */ + public void Initdata() + { + try{ + bpa_goods= QueryDB.GetGoodsALL(); + sp_adapter adapter = new sp_adapter(yfpf_activity.this, R.layout.sp_item, bpa_goods,this); + datatab.setAdapter(adapter); + }catch(Exception e){ + + } } //endregion //region 点击事件 /** - * 本页面点击事件监听 + * 本页面点击事件监听22 * * @param v */ @@ -69,6 +113,62 @@ public class yfpf_activity extends AppCompatActivity implements View.OnClickList case R.id.gongneng_fanhui://返回按钮 this.finish(); break; + case R.id.buttonaddpf://新建配方 + Intent intent = new Intent(this, add_pf_activity.class); + intent.putExtra("title", "新建配方"); + intent.putExtra("data", ""); + startActivity(intent); + break; + } + } + + /** + * 接口方法,响应ListView按钮点击事件 + */ + @Override + public void clickListener(View v,Object data) { + switch (v.getId()) + { + case R.id.button_item://删除按钮 + int sort=((BPA_GOODS)data).sort; + String id=((BPA_GOODS)data).id; + Log.i("日志",sort + "--- "+id); + QueryDB.DeleteGoods((BPA_GOODS)data); + Initdata(); + for (BPA_GOODS k:bpa_goods) { + if(k.sort>sort) + { + QueryDB.GetGoodsSort(k,1); + } + } + Initdata(); + break; + case R.id.button_item_Up://上移动按钮 + BPA_GOODS good=(BPA_GOODS)data; + if(good.sort==1) + { + T.show(this, "已是最顶部!"); + return; + } + QueryDB.GetGoodsSort((BPA_GOODS)data,1); + Initdata(); + break; + case R.id.button_item_Down://下移动按钮 + BPA_GOODS good2=(BPA_GOODS)data; + if(good2.sort==bpa_goods.size()) + { + T.show(this, "已是最底部!"); + return; + } + QueryDB.GetGoodsSort((BPA_GOODS)data,0); + Initdata(); + break; + case R.id.button_item_update://修改按钮 + Intent intent = new Intent(this, add_pf_activity.class); + intent.putExtra("title", "新建配方"); + intent.putExtra("data",new Json().objectToJson(BPA_GOODS.class,data)); + startActivity(intent); + break; } } //endregion diff --git a/app/src/main/java/com/example/bpa/view/mode/ResGoodsRecipe.java b/app/src/main/java/com/example/bpa/view/mode/ResGoodsRecipe.java new file mode 100644 index 0000000..d601f67 --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/mode/ResGoodsRecipe.java @@ -0,0 +1,11 @@ +package com.example.bpa.view.mode; + +import com.example.bpa.db.mode.BPA_GOODSRECIPE; + +/** + * 界面显示商品配方结构 + */ +public class ResGoodsRecipe extends BPA_GOODSRECIPE { + //物料名称 + public String materialName; +} diff --git a/app/src/main/java/com/example/bpa/view/mode/ResSilosMode.java b/app/src/main/java/com/example/bpa/view/mode/ResSilosMode.java new file mode 100644 index 0000000..c9dc1db --- /dev/null +++ b/app/src/main/java/com/example/bpa/view/mode/ResSilosMode.java @@ -0,0 +1,14 @@ +package com.example.bpa.view.mode; + +import com.example.bpa.db.mode.BPA_SILOS; + +public class ResSilosMode extends BPA_SILOS { + //物料ID + public String materialID; + //物料图片路径 + public String materialimgUrl; + //物料名称 + public String materialname; + //当前余量 + public int dvalue; +} diff --git a/app/src/main/res/layout/datatab/layout/lsjy_item.xml b/app/src/main/res/layout/datatab/layout/lsjy_item.xml new file mode 100644 index 0000000..ee594bf --- /dev/null +++ b/app/src/main/res/layout/datatab/layout/lsjy_item.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/datatab/layout/pf_item.xml b/app/src/main/res/layout/datatab/layout/pf_item.xml new file mode 100644 index 0000000..3ebbb12 --- /dev/null +++ b/app/src/main/res/layout/datatab/layout/pf_item.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/datatab/layout/sp_item.xml b/app/src/main/res/layout/datatab/layout/sp_item.xml new file mode 100644 index 0000000..2d10305 --- /dev/null +++ b/app/src/main/res/layout/datatab/layout/sp_item.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/datatab/values/strings.xml b/app/src/main/res/layout/datatab/values/strings.xml index 6048840..73862c4 100644 --- a/app/src/main/res/layout/datatab/values/strings.xml +++ b/app/src/main/res/layout/datatab/values/strings.xml @@ -1,4 +1 @@ - - - Hello blank fragment - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/from/layout/activity_add_pf.xml b/app/src/main/res/layout/from/layout/activity_add_pf.xml new file mode 100644 index 0000000..9dfb0ed --- /dev/null +++ b/app/src/main/res/layout/from/layout/activity_add_pf.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/from/layout/activity_dzcjy.xml b/app/src/main/res/layout/from/layout/activity_dzcjy.xml index d377595..b05e9df 100644 --- a/app/src/main/res/layout/from/layout/activity_dzcjy.xml +++ b/app/src/main/res/layout/from/layout/activity_dzcjy.xml @@ -79,13 +79,10 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"> - - + \ No newline at end of file diff --git a/app/src/main/res/layout/from/layout/activity_lsjy.xml b/app/src/main/res/layout/from/layout/activity_lsjy.xml index e3b4a37..1f1d5d3 100644 --- a/app/src/main/res/layout/from/layout/activity_lsjy.xml +++ b/app/src/main/res/layout/from/layout/activity_lsjy.xml @@ -80,13 +80,127 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/from/layout/activity_yfpf.xml b/app/src/main/res/layout/from/layout/activity_yfpf.xml index 6c446b1..953d101 100644 --- a/app/src/main/res/layout/from/layout/activity_yfpf.xml +++ b/app/src/main/res/layout/from/layout/activity_yfpf.xml @@ -79,13 +79,136 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/from/layout/fragment_system_set.xml b/app/src/main/res/layout/from/layout/fragment_system_set.xml index 5250157..1dbb56c 100644 --- a/app/src/main/res/layout/from/layout/fragment_system_set.xml +++ b/app/src/main/res/layout/from/layout/fragment_system_set.xml @@ -6,11 +6,23 @@ android:layout_height="match_parent" android:tag="系统设置" tools:context=".view.fragment.SystemSetFragment"> - - + android:text="连接设备"/> + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-xhdpi/biaozhunzhongliang.png b/app/src/main/res/mipmap-xhdpi/biaozhunzhongliang.png new file mode 100644 index 0000000..0b18972 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/biaozhunzhongliang.png differ diff --git a/app/src/main/res/mipmap-xhdpi/chengzhongjiaoyan.png b/app/src/main/res/mipmap-xhdpi/chengzhongjiaoyan.png new file mode 100644 index 0000000..ad99a27 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/chengzhongjiaoyan.png differ diff --git a/app/src/main/res/mipmap-xhdpi/guiling.png b/app/src/main/res/mipmap-xhdpi/guiling.png new file mode 100644 index 0000000..248b0d3 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/guiling.png differ diff --git a/app/src/main/res/mipmap-xhdpi/shezhifenducanshu.png b/app/src/main/res/mipmap-xhdpi/shezhifenducanshu.png new file mode 100644 index 0000000..92989eb Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/shezhifenducanshu.png differ diff --git a/app/src/main/res/mipmap-xhdpi/spgl.png b/app/src/main/res/mipmap-xhdpi/spgl.png new file mode 100644 index 0000000..8ea92a4 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/spgl.png differ diff --git a/app/src/main/res/mipmap-xhdpi/zuidachengzhong.png b/app/src/main/res/mipmap-xhdpi/zuidachengzhong.png new file mode 100644 index 0000000..143f273 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/zuidachengzhong.png differ diff --git a/build.gradle b/build.gradle index f5c74f7..5614606 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '7.4.1' apply false - id 'com.android.library' version '7.4.1' apply false + id 'com.android.application' version '7.0.0' apply false + id 'com.android.library' version '7.0.0' apply false } + diff --git a/settings.gradle b/settings.gradle index 125cf20..7d215bd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,6 +12,7 @@ dependencyResolutionManagement { google() jcenter() mavenCentral() + maven { url 'https://jitpack.io' } } } rootProject.name = "ChaBaiDao"