field not finished, connection works
This commit is contained in:
		| @@ -36,8 +36,6 @@ public abstract class DataPoint{ | ||||
|  | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Just get the label of this DataPoint | ||||
|      * @return the label in a string | ||||
|   | ||||
| @@ -8,16 +8,17 @@ public class BooleanRegister extends ModbusRegister{ | ||||
|  | ||||
|     public BooleanRegister(String label, boolean isOutput, int address){ | ||||
|         this.bdp = new BooleanDataPoint(label, isOutput); | ||||
|         value = bdp.getValue(); | ||||
|         updateMapOfRegisters(bdp, address); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void read() { | ||||
|         bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address)); | ||||
|         bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void write() { | ||||
|         ModbusAccessor.getMySelf().writeBoolean(address, bdp.getValue()); | ||||
|         ModbusAccessor.getMySelf().writeBoolean(this.getAddress(), bdp.getValue()); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -16,21 +16,22 @@ public class FieldConnector implements DataPointListener { | ||||
|         } | ||||
|         return mySelf; | ||||
|     } | ||||
|  | ||||
|     public void initialize(String host, int port){ | ||||
|         ModbusAccessor mbA = ModbusAccessor.getMySelf(); | ||||
|         mbA.connect(host, port); | ||||
|          | ||||
|     } | ||||
|  | ||||
|     private void pushToField(DataPoint dp){ | ||||
|         ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); | ||||
|         mr.write(); | ||||
|     } | ||||
|     @Override | ||||
|     public void onNewValue(DataPoint dp) { | ||||
|         pushToField(dp); | ||||
|         ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); | ||||
|         mr.write(); | ||||
|     } | ||||
|     public void periodicalPolling(){ | ||||
|     public void startPeriodicalPolling(){ | ||||
|         Timer pollTimer = new Timer(); | ||||
|         PollTask pollTask = new PollTask(); | ||||
|         pollTimer.scheduleAtFixedRate(pollTask,0,2000); | ||||
|         pollTimer.scheduleAtFixedRate(pollTask,0,100); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,20 +4,21 @@ import ch.hevs.isi.core.FloatDataPoint; | ||||
|  | ||||
| public class FloatRegister extends ModbusRegister{ | ||||
|     private Float value; | ||||
|     private FloatDataPoint dataPoint; | ||||
|     private FloatDataPoint fdp; | ||||
|  | ||||
|     public FloatRegister(String label, boolean isOutPut, int address) { | ||||
|         this.dataPoint = new FloatDataPoint(label, isOutPut); | ||||
|         updateMapOfRegisters(dataPoint,address); | ||||
|         this.fdp = new FloatDataPoint(label, isOutPut); | ||||
|         value = fdp.getValue(); | ||||
|         updateMapOfRegisters(fdp,address); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void read() { | ||||
|         dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address)); | ||||
|         fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void write() { | ||||
|         ModbusAccessor.getMySelf().writeFloat(address, dataPoint.getValue()); | ||||
|         ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue()); | ||||
|     } | ||||
| } | ||||
| @@ -3,24 +3,25 @@ package ch.hevs.isi.field; | ||||
| import ch.hevs.isi.core.DataPoint; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| public abstract class ModbusRegister { | ||||
|     protected int address; | ||||
|     public static HashMap<DataPoint, ModbusRegister> map = new HashMap<>(); | ||||
|     private int address; | ||||
|     public static HashMap<DataPoint, ModbusRegister> mapOfRegisters = new HashMap<>(); | ||||
|     public void updateMapOfRegisters(DataPoint dp, int address){ | ||||
|         this.address = address; | ||||
|         map.put(dp,this); | ||||
|     } | ||||
|     public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ | ||||
|         return map.get(dp); | ||||
|         mapOfRegisters.put(dp,this); | ||||
|     } | ||||
|  | ||||
|     public int getAddress() { | ||||
|         return address; | ||||
|     } | ||||
|  | ||||
|     public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ | ||||
|         return mapOfRegisters.get(dp); | ||||
|     } | ||||
|  | ||||
|     public static void poll(){ | ||||
|         for (ModbusRegister mr : map.values()){ | ||||
|         for (ModbusRegister mr : mapOfRegisters.values()){ | ||||
|             mr.read();                              //read all values (registers) of the map | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user