Skip to content

Commit

Permalink
Reset simulation (#163)
Browse files Browse the repository at this point in the history
* #146 First version, force reload of a configuration when reset is pressed.

* #146
added reset button (that navigates to index and sends a command to reset the simulation)
added "reset" command on server-go (command will stop, terminate and initialize a simulation)
  • Loading branch information
cesarcarc authored May 29, 2020
1 parent 90d6f3a commit d6e378f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
37 changes: 37 additions & 0 deletions libcosim/libcosim.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,30 @@ func initializeSimulation(sim *Simulation, status *structs.SimulationStatus, con
return true, "Simulation loaded successfully", config.configDir
}

func resetSimulation(sim *Simulation, status *structs.SimulationStatus, configPath string, logDir string) (bool, string, string) {
var success = false
var message = ""
var configDir = ""

success, message = executionStop(sim.Execution)
log.Println(message)

if success{
status.Loaded = false
status.Status = "stopped"
status.ConfigDir = ""
status.Trends = []structs.Trend{}
status.Module = ""
success, message = simulationTeardown(sim)
log.Println(message)
}

success, message, configDir = initializeSimulation(sim, status, configPath, logDir)
log.Println(message)

return success, message, configDir
}

func executeCommand(cmd []string, sim *Simulation, status *structs.SimulationStatus) (shorty structs.ShortLivedData, feedback structs.CommandFeedback) {
var success = false
var message = "No feedback implemented for this command"
Expand All @@ -670,6 +694,19 @@ func executeCommand(cmd []string, sim *Simulation, status *structs.SimulationSta
status.Module = ""
success, message = simulationTeardown(sim)
shorty.ModuleData = sim.MetaData
case "reset":
status.Loading = true
var configDir string
success, message, configDir = resetSimulation(sim, status, cmd[1], cmd[2])
if success {
status.Loaded = true
status.ConfigDir = configDir
status.Status = "pause"
shorty.ModuleData = sim.MetaData
scenarios := findScenarios(status)
shorty.Scenarios = &scenarios
}
status.Loading = false
case "pause":
success, message = executionStop(sim.Execution)
status.Status = "pause"
Expand Down
7 changes: 6 additions & 1 deletion src/client/controller.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@
(fn [{:keys [db]} [folder log-folder]]
(let [paths (distinct (conj (:prev-paths db) folder))]
(storage/set-item! "cosim-paths" (pr-str paths))
(merge {:db (assoc db :prev-paths paths :plot-config-changed? false)}
(merge {:db (assoc db :prev-paths paths :plot-config-changed? false :log-dir (or log-folder ""))}
(socket-command ["load" folder (or log-folder "")])))))

(k/reg-event-fx ::reset
(fn [{:keys [db]} [folder log-folder]]
(merge {:navigate-to [:index]}
(socket-command ["reset" folder (or log-folder "")]))))

(k/reg-event-fx ::delete-prev
(fn [{:keys [db]} [path]]
(let [paths (remove #(= path %) (:prev-paths db))]
Expand Down
5 changes: 5 additions & 0 deletions src/client/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
(rf/reg-sub :time simulation-time)
(rf/reg-sub :loading? (comp :loading :state))
(rf/reg-sub :loaded? (comp :loaded :state))
(rf/reg-sub :loaded-dir (fn [db]
(-> db :state :configDir)))
(rf/reg-sub :log-dir (fn [db]
(:log-dir db)))
(rf/reg-sub :overview status-data)
(rf/reg-sub :prev-paths (fn [db]
(:prev-paths db)))
(rf/reg-sub :status (comp :status :state))
Expand Down
4 changes: 4 additions & 0 deletions src/client/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@
(defn root-comp []
(let [socket-state (rf/subscribe [:kee-frame.websocket/state socket-url])
loaded? (rf/subscribe [:loaded?])
load-dir (rf/subscribe [:loaded-dir])
log-dir (rf/subscribe [:log-dir])
status (rf/subscribe [:status])
module (rf/subscribe [:current-module])
trends (rf/subscribe [:trend-info])
Expand Down Expand Up @@ -357,6 +359,8 @@
[:a.item {:on-click #(rf/dispatch [::controller/play])} "Play"])
(when (and @loaded? (= @status "play"))
[:a.item {:on-click #(rf/dispatch [::controller/pause])} "Pause"])
(when @loaded?
[:a.item {:on-click #(rf/dispatch [::controller/reset @load-dir @log-dir])} "Reset"])
[:div.ui.simple.dropdown.item
[:i.question.circle.icon]
[:div.menu
Expand Down

0 comments on commit d6e378f

Please sign in to comment.