Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Trace operation #8

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion docs/getting-started/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ Example using a parameter as a value
</ifEqual>
```

Please note the **symbol** and **value** datatype needs to match.
Please note the **symbol** and **value** datatype needs to match.


## Trace

Trace operation provides a tool for debugging, by printing (tracing) the current
value of symbols

Supported registers: **HOLDING_REGISTER**, **COIL**, **DISCRETE_INPUT**, **INPUT_REGISTER**

```
<trace symbol="RPM_MOTOR" />
```
2 changes: 2 additions & 0 deletions examples/configuration_simulation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
<csv symbol="TEMPERATURE_MOTOR4" file="test_data.csv" column="2" step="2" startRow="2" endRow="5" replay="true"/>
<set symbol="RELAYON">1</set>
<set symbol="RELAY_STATUS">1</set>
<trace symbol="RPM_MOTOR" />
<set symbol="RPM_MOTOR">400</set>
<trace symbol="RPM_MOTOR" />
<delay>1000</delay>
<add symbol="MOTOR_SPEED1">15</add>
<add symbol="RPM7">1</add>
Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/ConfigurationParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ConfigurationParser {
}
private fun load(): Device? {
try {
val context = JAXBContext.newInstance(Device::class.java, Set::class.java, Random::class.java, Delay::class.java, Linear::class.java, Add::class.java, Sub::class.java, Csv::class.java, IfEqual::class.java, Parameters::class.java, Parameter::class.java)
val context = JAXBContext.newInstance(Device::class.java, Set::class.java, Random::class.java, Delay::class.java, Linear::class.java, Add::class.java, Sub::class.java, Csv::class.java, IfEqual::class.java, Parameters::class.java, Parameter::class.java, Trace::class.java)
val unmarshaller = context.createUnmarshaller()
return if(fileName.isEmpty() ) {
val reader = StringReader(this::class.java.classLoader.getResource("configuration.xml")!!.readText())
Expand Down Expand Up @@ -151,6 +151,17 @@ data class Sub(
}


//<trace symbol="MOTOR_SPEED1"></trace>
@XmlRootElement(name="trace")
data class Trace(
@field:XmlAttribute(required = true)
val symbol: String,
){
constructor(): this("")
}



//<add symbol="MOTOR_SPEED1">RPM_MOTOR1</add>
@XmlRootElement(name="add")
data class Add(
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/PlcSimulation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PlcSimulation(
): BaseOperation(parameters,configurationParser.getConfiguredDevice().configuration ) {
val linearOperations = LinearOperation()
val csvOperations = CsvOperation()
var traceOperation = TraceOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
val addOperation = AddOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
val setOperation = SetOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
val subOperation = SubOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
Expand Down Expand Up @@ -83,6 +84,10 @@ class PlcSimulation(
ifEqual(element, configuration, memory)
}

is Trace -> {
traceOperation.traceOperation(element)
}

else -> throw UnsupportedOperationException("Unknown simulation step type")
}
}
Expand Down
70 changes: 70 additions & 0 deletions src/main/kotlin/operations/TraceOperation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package operations

import Configuration
import EnvironmentVariables
import PlcMemory
import Trace
import org.slf4j.LoggerFactory
import java.util.concurrent.CancellationException

class TraceOperation(
private val configuration: Configuration, private val memory: PlcMemory, environmentVariables: EnvironmentVariables

) : BaseOperation(environmentVariables, configuration) {
companion object {
val logger = LoggerFactory.getLogger("TraceOperation")
}

fun traceOperation(element: Trace) {
var valueToTrace : String = ""
var variable = configuration.registers.getVarConfiguration(element.symbol)
if (variable == null) {
TraceOperation.logger.error("Symbol ${element.symbol} not found during Trace execution")
throw CancellationException("Error - Trace")
} else {
when (variable.addressType) {

AddressType.HOLDING_REGISTER -> {
//get the current value

if (variable.datatype == "FLOAT32") {
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2)
if (currentValue.isEmpty()) {
SubOperation.logger.error("Trace Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
throw CancellationException("Error - Trace")
}
val intValue = ((currentValue[1].toInt() shl 16) or (currentValue[0].toInt() and 0xFFFF))
val currentFloatValue = java.lang.Float.intBitsToFloat(intValue)
valueToTrace = currentValue.toString()
} else {
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1)
if (currentValue.isEmpty()) {
SubOperation.logger.error("Trace Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
throw CancellationException("Error - Add")
}
valueToTrace = currentValue.first().toInt().toString()
}
}

AddressType.INPUT_REGISTER -> {
val currentValue = memory.readInputRegister(variable.address.toInt(), 1)
valueToTrace = currentValue.first().toString()
}

AddressType.COIL -> {
val currentValue = memory.readCoilStatus(variable.address.toInt(), 1)
valueToTrace = currentValue.first().toString()
}

AddressType.DISCRETE_INPUT -> {
val currentValue = memory.readInputStatus(variable.address.toInt(), 1)
valueToTrace = currentValue.first().toString()
}

}
TraceOperation.logger.info("TRACE - Symbol: ${element.symbol} Value: $valueToTrace")

}
}

}