numass-framework/numass-control/src/main/kotlin/inr/numass/control/NumassControlApplication.kt

82 lines
2.3 KiB
Kotlin
Raw Normal View History

2017-05-31 09:53:21 +03:00
package inr.numass.control
import ch.qos.logback.classic.Level
import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.DeviceFactory
2017-05-31 09:53:21 +03:00
import hep.dataforge.exceptions.ControlException
2018-03-19 16:57:07 +03:00
import hep.dataforge.kodex.optional
2017-05-31 09:53:21 +03:00
import hep.dataforge.meta.Meta
import javafx.scene.Scene
import javafx.stage.Stage
import org.slf4j.LoggerFactory
import tornadofx.*
import java.util.*
/**
* Created by darksnake on 14-May-17.
*/
2017-11-13 16:56:34 +03:00
abstract class NumassControlApplication<in D : Device> : App() {
2018-04-05 17:03:05 +03:00
private var device: D? = null
2017-05-31 09:53:21 +03:00
override fun start(stage: Stage) {
Locale.setDefault(Locale.US)// чтобы отделение десятичных знаков было точкой
val rootLogger = LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger
rootLogger.level = Level.INFO
2018-04-05 17:03:05 +03:00
device = setupDevice().also {
val controller = it.getDisplay()
val scene = Scene(controller.view?.root ?: controller.getBoardView())
stage.scene = scene
2018-03-19 16:57:07 +03:00
2018-04-05 17:03:05 +03:00
stage.show()
setupStage(stage, it)
setDFStageIcon(stage)
}
2017-05-31 09:53:21 +03:00
}
/**
* Get a device factory for given device
* @return
*/
protected abstract val deviceFactory: DeviceFactory
2017-05-31 09:53:21 +03:00
protected abstract fun setupStage(stage: Stage, device: D)
2018-04-05 17:03:05 +03:00
abstract fun getDeviceMeta(config: Meta): Meta
2017-05-31 09:53:21 +03:00
private fun setupDevice(): D {
2018-04-11 12:49:47 +03:00
val config = getConfig(this).optional.orElseGet { readResourceMeta("config/devices.xml") }
2017-05-31 09:53:21 +03:00
val ctx = setupContext(config)
2018-04-05 17:03:05 +03:00
val deviceConfig = getDeviceMeta(config)
2017-05-31 09:53:21 +03:00
try {
2017-05-31 16:53:51 +03:00
@Suppress("UNCHECKED_CAST")
2017-05-31 09:53:21 +03:00
val d = deviceFactory.build(ctx, deviceConfig) as D
d.init()
connectStorage(d, config)
return d
} catch (e: ControlException) {
throw RuntimeException("Failed to build device", e)
}
}
override fun stop() {
2017-05-31 16:53:51 +03:00
try {
2018-04-05 17:03:05 +03:00
device?.shutdown()
2017-05-31 16:53:51 +03:00
} catch (ex: Exception) {
2018-04-08 11:01:58 +03:00
LoggerFactory.getLogger(javaClass).error("Failed to properly shutdown application", ex);
2018-04-06 10:02:28 +03:00
device?.context?.close()
2017-05-31 16:53:51 +03:00
} finally {
super.stop()
}
2017-05-31 09:53:21 +03:00
}
}