Skip to content

Commit 1b5e1da

Browse files
committed
Replace println with log4j
1 parent 3690293 commit 1b5e1da

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

src/main/kotlin/PlcMemory.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.slf4j.LoggerFactory
12
import java.util.InvalidPropertiesFormatException
23
import java.util.concurrent.ConcurrentHashMap
34

@@ -10,6 +11,10 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
1011
private var holdingRegister: ConcurrentHashMap<Int, Short> = ConcurrentHashMap()
1112
private var device = configurationParser.getConfiguredDevice()
1213

14+
companion object {
15+
val logger = LoggerFactory.getLogger("PlcMemory")
16+
}
17+
1318
init {
1419
device.configuration.registers.register.forEach { register ->
1520
when(register.addressType){
@@ -37,15 +42,15 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
3742

3843
//0x
3944
override fun forceMultipleCoils(addressValueList: MutableList<Pair<Int, Boolean>>) {
40-
println("(0x) forceMultipleCoils")
45+
logger.debug("(0x) forceMultipleCoils")
4146
addressValueList.forEach { coil ->
4247
coils[coil.first] = coil.second
4348
}
4449
}
4550

4651
//0x
4752
override fun forceSingleCoil(address: Int, value: Boolean) {
48-
println("(0x) forceSingleCoil")
53+
logger.debug("(0x) forceSingleCoil")
4954
coils[address] = value
5055
}
5156

@@ -61,7 +66,7 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
6166

6267
// 4x
6368
override fun presetMultipleRegisters(addressValueList: MutableList<Pair<Int, Short>>) {
64-
println("(4x) presetMultipleRegisters")
69+
logger.debug("(4x) presetMultipleRegisters")
6570
addressValueList.forEach { register ->
6671
holdingRegister[register.first] = register.second
6772
}
@@ -70,13 +75,13 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
7075

7176

7277
override fun presetSingleRegister(address: Int, value: Boolean) {
73-
println("presetSingleRegister")
78+
logger.debug("presetSingleRegister")
7479
TODO("Not yet implemented")
7580
}
7681

7782
// 0x Registers
7883
override fun readCoilStatus(startAddress: Int, numberOfRegisters: Int): List<Boolean> {
79-
println("(0x) readCoilStatus")
84+
logger.debug("(0x) readCoilStatus")
8085
val listCoils = mutableListOf<Boolean>()
8186
for(i in startAddress until startAddress + numberOfRegisters) {
8287
if(coils[i] != null){
@@ -90,14 +95,14 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
9095

9196
// 4x
9297
override fun readHoldingRegister(startAddress: Int, numberOfRegisters: Int): List<Short> {
93-
println("(4x) readHoldingRegister")
98+
logger.debug("(4x) readHoldingRegister")
9499
val listHoldingRegisters = mutableListOf<Short>()
95100
for(i in startAddress until startAddress + numberOfRegisters) {
96101
if(holdingRegister[i] != null){
97-
println("readHoldingRegister address $i value=${holdingRegister[i]}")
102+
logger.debug("readHoldingRegister address $i value=${holdingRegister[i]}")
98103
listHoldingRegisters.add(holdingRegister[i]!!)
99104
}else{
100-
println("readHoldingRegister address $i value=0")
105+
logger.debug("readHoldingRegister address $i value=0")
101106
listHoldingRegisters.add(0)
102107
}
103108
}
@@ -106,7 +111,7 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
106111

107112
// 3x register
108113
override fun readInputRegister(startAddress: Int, numberOfRegisters: Int): List<Short> {
109-
println("(3x) readInputRegister")
114+
logger.debug("(3x) readInputRegister")
110115
val listInputRegisters = mutableListOf<Short>()
111116
for(i in startAddress until startAddress + numberOfRegisters) {
112117
if(inputRegister[i] != null){
@@ -120,7 +125,7 @@ class PlcMemory(configurationParser: ConfigurationParser) : IModbusServerEventL
120125

121126
// 1x register
122127
override fun readInputStatus(startAddress: Int, numberOfRegisters: Int): List<Boolean> {
123-
println("readInputStatus")
128+
logger.debug("readInputStatus")
124129
val listCoils = mutableListOf<Boolean>()
125130
for(i in startAddress until startAddress + numberOfRegisters) {
126131
if(inputStatus[i] != null){

src/main/kotlin/operations/LinearOperation.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package operations
33
import Configuration
44
import PlcMemory
55
import Linear
6+
import org.slf4j.LoggerFactory
67
import java.util.concurrent.CancellationException
78

89
class LinearOperation {
910
private var linearVariables: MutableMap<String, Double> = mutableMapOf<String, Double>()
1011

12+
companion object {
13+
val logger = LoggerFactory.getLogger("LinearOperation")
14+
}
15+
1116
private fun getNextValue(linear: Linear): String {
1217
var x =0.0
1318
if(linear.endX > linear.startX) {
@@ -38,11 +43,11 @@ class LinearOperation {
3843
var nextValue = getNextValue(element)
3944
var variable = configuration.registers.getVarConfiguration(element.symbol)
4045
if (variable == null) {
41-
println("ERROR: Symbol ${element.symbol} not found during Linear execution")
46+
logger.error("Symbol ${element.symbol} not found during Linear execution")
4247
throw CancellationException("Error - Linear")
4348
} else {
4449
if (variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT) {
45-
println("ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Linear operation")
50+
logger.error("Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Linear operation")
4651
throw CancellationException("Error - Linear")
4752
}
4853

src/main/kotlin/operations/RandomOperation.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import AddressType
44
import Configuration
55
import PlcMemory
66
import Random
7+
import org.slf4j.LoggerFactory
78
import java.util.concurrent.CancellationException
89

910

1011
fun randomOperation(element: Random, configuration: Configuration, memory: PlcMemory){
11-
println("Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax}")
12+
val logger = LoggerFactory.getLogger("randomOperation")
13+
logger.debug("Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax}")
1214
var variable = configuration.registers.getVarConfiguration(element.symbol)
1315
if (variable == null) {
14-
println("ERROR: Symbol ${element.symbol} not found during Set execution")
16+
logger.error("Symbol ${element.symbol} not found during Set execution")
1517
throw CancellationException("Error - Random")
1618
} else {
1719
if(variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT){
18-
println("ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Random operation")
20+
logger.error("Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Random operation")
1921
throw CancellationException("Error - Random")
2022
}
2123

src/main/kotlin/operations/SetOperation.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ import Configuration
44
import EnvironmentVariables
55
import PlcMemory
66
import Set
7+
import org.slf4j.LoggerFactory
78
import toBooleanFromBinary
89
import java.util.concurrent.CancellationException
910

1011
class SetOperation(private val configuration: Configuration,private val memory: PlcMemory, environmentVariables: EnvironmentVariables
1112
) : BaseOperation(environmentVariables, configuration) {
13+
14+
companion object {
15+
val logger = LoggerFactory.getLogger("SetOperation")
16+
}
1217
fun setOperation(element: Set) {
13-
println("Set symbol ${element.symbol} value ${element.value}")
18+
logger.debug("Set symbol ${element.symbol} value ${element.value}")
1419
var value = processValue(element.value)
1520
var variable = configuration.registers.getVarConfiguration(element.symbol)
1621
if (variable == null) {
17-
println("ERROR: Symbol ${element.symbol} not found during Set execution")
22+
logger.error("Symbol ${element.symbol} not found during Set execution")
1823
throw CancellationException("Error - Set")
1924
} else {
2025
if ((variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT) && (variable.value != "0" && variable.value != "1")) {
21-
println("ERROR: Invalid value format. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${variable.value}")
26+
logger.error("Invalid value format. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${variable.value}")
2227
}
2328
when (variable.addressType) {
2429
AddressType.HOLDING_REGISTER -> {

0 commit comments

Comments
 (0)