Skip to content

Commit

Permalink
#124 Create plots from "PlotConfig.json" when loading configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eidekrist committed Dec 19, 2019
1 parent 12ebc6c commit c66ba7c
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 18 deletions.
35 changes: 27 additions & 8 deletions cse/cse.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func checkConfiguration(configPath string) (valid bool, message string, config c
return true, "", config
}

func initializeSimulation(sim *Simulation, configPath string, logDir string) (bool, string, string) {
func initializeSimulation(sim *Simulation, status *structs.SimulationStatus, configPath string, logDir string) (bool, string, string) {
valid, message, config := checkConfiguration(configPath)
if !valid {
return false, message, ""
Expand Down Expand Up @@ -635,6 +635,9 @@ func initializeSimulation(sim *Simulation, configPath string, logDir string) (bo
sim.OverrideManipulator = manipulator
sim.ScenarioManager = scenarioManager
sim.MetaData = &metaData

setupPlotsFromConfig(sim, status, config.configDir)

return true, "Simulation loaded successfully", config.configDir
}

Expand All @@ -645,7 +648,7 @@ func executeCommand(cmd []string, sim *Simulation, status *structs.SimulationSta
case "load":
status.Loading = true
var configDir string
success, message, configDir = initializeSimulation(sim, cmd[1], cmd[2])
success, message, configDir = initializeSimulation(sim, status, cmd[1], cmd[2])
if success {
status.Loaded = true
status.ConfigDir = configDir
Expand Down Expand Up @@ -678,7 +681,7 @@ func executeCommand(cmd []string, sim *Simulation, status *structs.SimulationSta
case "newtrend":
success, message = addNewTrend(status, cmd[1], cmd[2])
case "addtotrend":
success, message = addToTrend(sim, status, cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6])
success, message = addToTrend(sim, status, cmd[1], cmd[2], cmd[3])
case "untrend":
success, message = removeAllFromTrend(sim, status, cmd[1])
case "removetrend":
Expand Down Expand Up @@ -773,19 +776,35 @@ func setSignalSubscriptions(status *structs.SimulationStatus, cmd []string) (boo
return success, message
}

func findFmu(metaData *structs.MetaData, moduleName string) (foundFmu structs.FMU) {
func findFmu(metaData *structs.MetaData, moduleName string) (foundFmu structs.FMU, err error) {
for _, fmu := range metaData.FMUs {
if fmu.Name == moduleName {
foundFmu = fmu
return foundFmu, nil
}
}
return
return foundFmu, errors.New("Simulator with name " + moduleName + " does not exist.")
}

func findVariable(fmu structs.FMU, variableName string) (foundVariable structs.Variable, err error) {
for _, variable := range fmu.Variables {
if variable.Name == variableName {
foundVariable = variable
return foundVariable, nil
}
}
return foundVariable, errors.New("Variable with name " + variableName + " does not exist for simulator " + fmu.Name)
}

func findModuleData(status *structs.SimulationStatus, metaData *structs.MetaData, observer *C.cse_observer) (module structs.Module) {
if len(status.SignalSubscriptions) > 0 {

slaveIndex := findFmu(metaData, status.Module).ExecutionIndex
slave, err := findFmu(metaData, status.Module)
if err != nil {
log.Println(err.Error())
return
}
slaveIndex := slave.ExecutionIndex
realSignals := observerGetReals(observer, status.SignalSubscriptions, slaveIndex)
intSignals := observerGetIntegers(observer, status.SignalSubscriptions, slaveIndex)
boolSignals := observerGetBooleans(observer, status.SignalSubscriptions, slaveIndex)
Expand All @@ -809,8 +828,8 @@ func GetSignalValue(module string, cardinality string, signal string) int {
func GenerateJsonResponse(status *structs.SimulationStatus, sim *Simulation, feedback structs.CommandFeedback, shorty structs.ShortLivedData) structs.JsonResponse {
var response = structs.JsonResponse{
Loading: status.Loading,
Loaded: status.Loaded,
Status: status.Status,
Loaded: status.Loaded,
Status: status.Status,
}
if status.Loaded {
execStatus := getExecutionStatus(sim.Execution)
Expand Down
79 changes: 70 additions & 9 deletions cse/trending.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package cse

import (
"cse-server-go/structs"
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"time"
)
Expand Down Expand Up @@ -32,25 +36,30 @@ func addNewTrend(status *structs.SimulationStatus, plotType string, label string
return true, "Added new trend"
}

func addToTrend(sim *Simulation, status *structs.SimulationStatus, module string, signal string, causality string, valueType string, valueReference string, plotIndex string) (bool, string) {
func addToTrend(sim *Simulation, status *structs.SimulationStatus, module string, signal string, plotIndex string) (bool, string) {

idx, err := strconv.Atoi(plotIndex)
fmu := findFmu(sim.MetaData, module)

if err != nil {
message := strCat("Cannot parse plotIndex as integer", plotIndex, ", ", err.Error())
log.Println(message)
return false, message
}

varIndex, err := strconv.Atoi(valueReference)
fmu, err := findFmu(sim.MetaData, module)
if err != nil {
message := err.Error()
log.Println(message)
return false, message
}

variable, err := findVariable(fmu, signal)
if err != nil {
message := strCat("Cannot parse valueReference as integer ", valueReference, ", ", err.Error())
message := err.Error()
log.Println(message)
return false, message
}

err = observerStartObserving(sim.TrendObserver, fmu.ExecutionIndex, valueType, varIndex)
err = observerStartObserving(sim.TrendObserver, fmu.ExecutionIndex, variable.Type, variable.ValueReference)
if err != nil {
message := strCat("Cannot start observing variable ", lastErrorMessage())
log.Println(message)
Expand All @@ -61,9 +70,9 @@ func addToTrend(sim *Simulation, status *structs.SimulationStatus, module string
Module: module,
SlaveIndex: fmu.ExecutionIndex,
Signal: signal,
Causality: causality,
Type: valueType,
ValueReference: varIndex})
Causality: variable.Causality,
Type: variable.Type,
ValueReference: variable.ValueReference})

return true, "Added variable to trend"
}
Expand Down Expand Up @@ -151,3 +160,55 @@ func TrendLoop(sim *Simulation, status *structs.SimulationStatus) {
time.Sleep(1000 * time.Millisecond)
}
}

func parsePlotConfig(pathToFile string) (data structs.PlotConfig, err error) {
jsonFile, err := os.Open(pathToFile)

if err != nil {
log.Println("Can't open file:", err.Error(), pathToFile)
return data, err
}

defer jsonFile.Close()

bytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Println("Can't read file:", err.Error(), pathToFile)
return data, err
}
err = json.Unmarshal(bytes, &data)
if err != nil {
log.Println("Can't unmarshal PlotConfig json contents:", err.Error())
return data, err
}
return data, nil
}

func setupPlotsFromConfig(sim *Simulation, status *structs.SimulationStatus, configDir string) {
if hasFile(configDir, "PlotConfig.json") {
log.Println("We have a PlotConfig")
pathToFile := filepath.Join(configDir, "PlotConfig.json")
plotConfig, err := parsePlotConfig(pathToFile)

if err != nil {
log.Println("Can't parse PlotConfig.json:", err.Error())
return
}

for idx, plot := range plotConfig.Plots {
success, message := addNewTrend(status, plot.PlotType, plot.Label)
if !success {
log.Println("Could not add new plot:", message)
} else {
plotIdx := strconv.Itoa(idx)
for _, variable := range plot.PlotVariables {
success, message := addToTrend(sim, status, variable.Simulator, variable.Variable, plotIdx)
if !success {
log.Println("Could not add variable to plot:", message)
}
}
}
}

}
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module cse-server-go

require (
github.com/gobuffalo/packr v1.22.0
github.com/gobuffalo/buffalo-plugins v1.11.0 // indirect
github.com/gobuffalo/meta v0.0.0-20190120163247-50bbb1fa260d // indirect
github.com/gobuffalo/packr v1.30.1
github.com/gobuffalo/syncx v0.0.0-20181120194010-558ac7de985f // indirect
github.com/gorilla/mux v1.7.0
github.com/gorilla/websocket v1.4.0
github.com/kr/pty v1.1.3 // indirect
github.com/markbates/deplist v1.0.5 // indirect
github.com/spf13/afero v1.2.0 // indirect
github.com/ugorji/go/codec v1.1.7
)
Loading

0 comments on commit c66ba7c

Please sign in to comment.