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 |      * Just get the label of this DataPoint | ||||||
|      * @return the label in a string |      * @return the label in a string | ||||||
|   | |||||||
| @@ -8,16 +8,17 @@ public class BooleanRegister extends ModbusRegister{ | |||||||
|  |  | ||||||
|     public BooleanRegister(String label, boolean isOutput, int address){ |     public BooleanRegister(String label, boolean isOutput, int address){ | ||||||
|         this.bdp = new BooleanDataPoint(label, isOutput); |         this.bdp = new BooleanDataPoint(label, isOutput); | ||||||
|  |         value = bdp.getValue(); | ||||||
|         updateMapOfRegisters(bdp, address); |         updateMapOfRegisters(bdp, address); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void read() { |     public void read() { | ||||||
|         bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address)); |         bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void write() { |     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; |         return mySelf; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void initialize(String host, int port){ |     public void initialize(String host, int port){ | ||||||
|         ModbusAccessor mbA = ModbusAccessor.getMySelf(); |          | ||||||
|         mbA.connect(host, port); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void pushToField(DataPoint dp){ |     private void pushToField(DataPoint dp){ | ||||||
|  |         ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); | ||||||
|  |         mr.write(); | ||||||
|     } |     } | ||||||
|     @Override |     @Override | ||||||
|     public void onNewValue(DataPoint dp) { |     public void onNewValue(DataPoint dp) { | ||||||
|         pushToField(dp); |         pushToField(dp); | ||||||
|         ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); |  | ||||||
|         mr.write(); |  | ||||||
|     } |     } | ||||||
|     public void periodicalPolling(){ |     public void startPeriodicalPolling(){ | ||||||
|         Timer pollTimer = new Timer(); |         Timer pollTimer = new Timer(); | ||||||
|         PollTask pollTask = new PollTask(); |         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{ | public class FloatRegister extends ModbusRegister{ | ||||||
|     private Float value; |     private Float value; | ||||||
|     private FloatDataPoint dataPoint; |     private FloatDataPoint fdp; | ||||||
|  |  | ||||||
|     public FloatRegister(String label, boolean isOutPut, int address) { |     public FloatRegister(String label, boolean isOutPut, int address) { | ||||||
|         this.dataPoint = new FloatDataPoint(label, isOutPut); |         this.fdp = new FloatDataPoint(label, isOutPut); | ||||||
|         updateMapOfRegisters(dataPoint,address); |         value = fdp.getValue(); | ||||||
|  |         updateMapOfRegisters(fdp,address); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void read() { |     public void read() { | ||||||
|         dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address)); |         fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void write() { |     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 ch.hevs.isi.core.DataPoint; | ||||||
|  |  | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.Map; |  | ||||||
|  |  | ||||||
| public abstract class ModbusRegister { | public abstract class ModbusRegister { | ||||||
|     protected int address; |     private int address; | ||||||
|     public static HashMap<DataPoint, ModbusRegister> map = new HashMap<>(); |     public static HashMap<DataPoint, ModbusRegister> mapOfRegisters = new HashMap<>(); | ||||||
|     public void updateMapOfRegisters(DataPoint dp, int address){ |     public void updateMapOfRegisters(DataPoint dp, int address){ | ||||||
|         this.address = address; |         this.address = address; | ||||||
|         map.put(dp,this); |         mapOfRegisters.put(dp,this); | ||||||
|     } |  | ||||||
|     public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ |  | ||||||
|         return map.get(dp); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public int getAddress() { |     public int getAddress() { | ||||||
|         return address; |         return address; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ | ||||||
|  |         return mapOfRegisters.get(dp); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public static void poll(){ |     public static void poll(){ | ||||||
|         for (ModbusRegister mr : map.values()){ |         for (ModbusRegister mr : mapOfRegisters.values()){ | ||||||
|             mr.read();                              //read all values (registers) of the map |             mr.read();                              //read all values (registers) of the map | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
							
								
								
									
										34
									
								
								src/test/java/Field.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/test/java/Field.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | import ch.hevs.isi.core.BooleanDataPoint; | ||||||
|  | import ch.hevs.isi.core.FloatDataPoint; | ||||||
|  | import ch.hevs.isi.field.BooleanRegister; | ||||||
|  | import ch.hevs.isi.field.FieldConnector; | ||||||
|  | import ch.hevs.isi.field.FloatRegister; | ||||||
|  |  | ||||||
|  | public class Field { | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); | ||||||
|  |         BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609); | ||||||
|  |         FieldConnector.getMySelf().startPeriodicalPolling(); | ||||||
|  |  | ||||||
|  |         FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); | ||||||
|  |         BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401); | ||||||
|  |  | ||||||
|  |         /* | ||||||
|  |         fRO.fdp.setValue(0.8F); | ||||||
|  |         bRO.bdp.setValue(true); | ||||||
|  |          */ | ||||||
|  |  | ||||||
|  |         while(true) | ||||||
|  |         { | ||||||
|  |             /* | ||||||
|  |             float value = fRI.dataPoint.getValue()*1000; | ||||||
|  |             System.out.println(value); | ||||||
|  |              */ | ||||||
|  |             /* | ||||||
|  |             System.out.println(bRI.bdp.getValue()); | ||||||
|  |             */ | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user