From b43267bdedd0b0cb352a7e2f4940b04e03598698 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 28 May 2017 16:47:11 +0300 Subject: [PATCH] Numass control room global update --- numass-control/control-room/build.gradle | 24 ++++++++ .../inr/numass/control/BoardController.kt | 60 +++++++++++++++---- .../kotlin/inr/numass/control/BoardView.kt | 24 ++++---- .../inr/numass/control/ControlFXExtensions.kt | 6 +- .../kotlin/inr/numass/control/ServerApp.kt | 22 +------ 5 files changed, 94 insertions(+), 42 deletions(-) diff --git a/numass-control/control-room/build.gradle b/numass-control/control-room/build.gradle index c8b78640..7b02c750 100644 --- a/numass-control/control-room/build.gradle +++ b/numass-control/control-room/build.gradle @@ -49,4 +49,28 @@ task debugWithDevice(dependsOn: classes, type: JavaExec) { group "debug" } +task startScriptWithDevices(type: CreateStartScripts){ + applicationName = "control-room-devices" + mainClassName = mainClass + outputDir = new File(project.buildDir, 'scripts') + classpath = jar.outputs.files + project.configurations.devices +} + +distributions{ + devices{ + contents { + into("lib") { + from jar + from configurations.devices + } + into("bin"){ + from startScriptWithDevices + } + } + } +} + + + + diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardController.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardController.kt index adc976cf..117d5d82 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardController.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardController.kt @@ -1,6 +1,7 @@ package inr.numass.control import hep.dataforge.context.Context +import hep.dataforge.context.Global import hep.dataforge.control.connections.Roles import hep.dataforge.control.connections.StorageConnection import hep.dataforge.meta.Meta @@ -9,51 +10,81 @@ import hep.dataforge.storage.api.Storage import hep.dataforge.storage.commons.StorageFactory import inr.numass.client.ClientUtils import inr.numass.server.NumassStorageServerObject +import javafx.application.Application +import javafx.application.Platform import javafx.beans.property.SimpleObjectProperty import javafx.collections.FXCollections import javafx.collections.ObservableList import tornadofx.* +import java.io.File /** * Created by darksnake on 12-May-17. */ -class BoardController() : Controller() { +class BoardController() : Controller(), AutoCloseable { val devices: ObservableList> = FXCollections.observableArrayList>(); + val contextProperty = SimpleObjectProperty(Global.instance()) + var context: Context by contextProperty + private set + val storageProperty = SimpleObjectProperty() - var storage by storageProperty + var storage: Storage? by storageProperty private set val serverManagerProperty = SimpleObjectProperty() - var serverManager: ServerManager by serverManagerProperty + var serverManager: ServerManager? by serverManagerProperty private set - fun load(context: Context, meta: Meta) { + fun load(app: Application) { + runAsync { + NumassControlUtils.getConfig(app).ifPresent { + val libDir = File(app.parameters.named.getOrDefault("libPath", "../lib")); + val contextBuilder = Context + .builder("NUMASS-SERVER"); + if (libDir.exists()) { + Global.logger().info("Found library directory {}. Loading it into server context", libDir) + contextBuilder.classPath(libDir.listFiles { _, name -> name.endsWith(".jar") }.map { it.toURI().toURL() }) + } + context = contextBuilder.build(); + load(context, it); + } + } + + } + + private fun load(context: Context, meta: Meta) { + this.context = context; devices.clear(); meta.getMetaList("device").forEach { try { - devices.add(buildDeviceView(context, it)); + Platform.runLater { devices.add(buildDeviceView(context, it)) }; } catch (ex: Exception) { context.logger.error("Can't build device view", ex); } } if (meta.hasMeta("storage")) { - storage = buildStorage(context, meta); + val st = buildStorage(context, meta); val storageConnection = StorageConnection(storage); devices.forEach { if (it.device.acceptsRole(Roles.STORAGE_ROLE)) { it.device.connect(storageConnection, Roles.STORAGE_ROLE); } } + Platform.runLater { + storage = st + meta.optMeta("server").ifPresent { serverMeta -> + val sm = context.pluginManager().getOrLoad(ServerManager::class.java); + sm.configure(serverMeta) + + sm.bind(NumassStorageServerObject(serverManager, storage, "numass-storage")); + serverManager = sm + } + } } - meta.optMeta("server").ifPresent { serverMeta -> - serverManager = context.pluginManager().getOrLoad(ServerManager::class.java); - serverManager.configure(serverMeta) - serverManager.bind(NumassStorageServerObject(serverManager, storage, "numass-storage")); - } } private fun buildDeviceView(context: Context, deviceMeta: Meta): DeviceViewConnection<*> { @@ -88,4 +119,11 @@ class BoardController() : Controller() { } return storage; } + + override fun close() { + devices.forEach { + it.close() + } + context.close(); + } } \ No newline at end of file diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt index 1099b444..faff1994 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/BoardView.kt @@ -38,11 +38,11 @@ class BoardView : View("Numass control board", ImageView(getDFIcon())) { action { if (isSelected) { text = "Stop" - controller.serverManager.startServer() - serverLabel.text = controller.serverManager.link; + controller.serverManager?.startServer() + serverLabel.text = controller.serverManager?.link; } else { text = "Start" - controller.serverManager.stopServer() + controller.serverManager?.stopServer() serverLabel.text = "" } } @@ -51,13 +51,13 @@ class BoardView : View("Numass control board", ImageView(getDFIcon())) { paddingHorizontal = 5 } indicator { - bind(controller.serverManager.isStarted) + bind(controller.serverManagerProperty.select { it.isStarted }) } separator(Orientation.VERTICAL) text("Address: ") serverLabel = hyperlink { action { - hostServices.showDocument(controller.serverManager.link); + hostServices.showDocument(controller.serverManager?.link); } } } @@ -67,12 +67,16 @@ class BoardView : View("Numass control board", ImageView(getDFIcon())) { hbox { alignment = Pos.CENTER_LEFT prefHeight = 40.0 - label(stringBinding(controller.storage) { + label(stringBinding(controller.storageProperty) { val storage = controller.storage - if (storage is FileStorage) { - "Path: " + storage.dataDir; + if (storage == null) { + "Storage not initialized" } else { - "Name: " + controller.storage.fullPath + if (storage is FileStorage) { + "Path: " + storage.dataDir; + } else { + "Name: " + storage.fullPath + } } }) } @@ -87,7 +91,7 @@ class BoardView : View("Numass control board", ImageView(getDFIcon())) { hbox { alignment = Pos.CENTER_LEFT vgrow = Priority.ALWAYS; - deviceStateIndicator(connection,Device.INITIALIZED_STATE) + deviceStateIndicator(connection, Device.INITIALIZED_STATE) deviceStateIndicator(connection, PortSensor.CONNECTED_STATE) deviceStateIndicator(connection, Sensor.MEASURING_STATE) deviceStateIndicator(connection, "storing") diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/ControlFXExtensions.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/ControlFXExtensions.kt index cae2bd12..9627e4a8 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/ControlFXExtensions.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/ControlFXExtensions.kt @@ -40,9 +40,11 @@ class Indicator(radius: Double = 10.0) : Circle(radius, Color.GRAY) { /** * bind indicator to the boolean value using default colours */ - fun bind(booleanValue: ObservableValue) { + fun bind(booleanValue: ObservableValue) { bind(booleanValue) { - if (it) { + if (it == null) { + Color.GRAY + } else if (it) { Color.GREEN; } else { Color.RED; diff --git a/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerApp.kt b/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerApp.kt index 812ab2e0..7e07eb56 100644 --- a/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerApp.kt +++ b/numass-control/control-room/src/main/kotlin/inr/numass/control/ServerApp.kt @@ -1,39 +1,23 @@ package inr.numass.control -import hep.dataforge.context.Context -import hep.dataforge.context.Global import javafx.stage.Stage import tornadofx.* -import java.io.File /** * Created by darksnake on 19-May-17. */ class ServerApp : App(BoardView::class) { val controller: BoardController by inject(); - var context: Context by singleAssign(); + override fun start(stage: Stage) { - NumassControlUtils.getConfig(this).ifPresent { - val libDir = File(parameters.named.getOrDefault("libPath", "../lib")); - val contextBuilder = Context - .builder("NUMASS-SERVER"); - if (libDir.exists()) { - Global.logger().info("Found library directory {}. Loading it into server context", libDir) - contextBuilder.classPath(libDir.listFiles { _, name -> name.endsWith(".jar") }.map { it.toURI().toURL() }) - } - context = contextBuilder.build(); - controller.load(context, it); - } + controller.load(this) super.start(stage) NumassControlUtils.setDFStageIcon(stage) } override fun stop() { - controller.devices.forEach { - it.device.shutdown() - } + controller.close() super.stop() - context.close(); } } \ No newline at end of file