A lot of minor updates in Control

This commit is contained in:
Alexander Nozik 2017-11-06 15:00:24 +03:00
parent fd5b177b30
commit b175c3af74
9 changed files with 129 additions and 66 deletions

View File

@ -139,7 +139,7 @@ public class MspDevice extends Sensor<Values> implements PortHandler.PortControl
} }
@Override @Override
public String type() { public String getType() {
return "MKS E-Vision"; return "MKS E-Vision";
} }

View File

@ -47,7 +47,7 @@ public class CM32Device extends PortSensor<Double> {
} }
@Override @Override
public String type() { public String getType() {
return meta().getString("type", "Leibold CM32"); return meta().getString("type", "Leibold CM32");
} }
@ -61,19 +61,19 @@ public class CM32Device extends PortSensor<Double> {
String answer = sendAndWait(CM32_QUERY, timeout()); String answer = sendAndWait(CM32_QUERY, timeout());
if (answer.isEmpty()) { if (answer.isEmpty()) {
this.progressUpdate("No signal"); this.updateMessage("No signal");
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
return null; return null;
} else if (!answer.contains("PM1:mbar")) { } else if (!answer.contains("PM1:mbar")) {
this.progressUpdate("Wrong answer: " + answer); this.updateMessage("Wrong answer: " + answer);
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
return null; return null;
} else if (answer.substring(14, 17).equals("OFF")) { } else if (answer.substring(14, 17).equals("OFF")) {
this.progressUpdate("Off"); this.updateMessage("Off");
updateState(CONNECTED_STATE, true); updateState(CONNECTED_STATE, true);
return null; return null;
} else { } else {
this.progressUpdate("OK"); this.updateMessage("OK");
updateState(CONNECTED_STATE, true); updateState(CONNECTED_STATE, true);
return Double.parseDouble(answer.substring(14, 17) + answer.substring(19, 23)); return Double.parseDouble(answer.substring(14, 17) + answer.substring(19, 23));
} }

View File

@ -36,7 +36,7 @@ public class MKSBaratronDevice extends PortSensor<Double> {
} }
@Override @Override
public String type() { public String getType() {
return meta().getString("type", "MKS baratron"); return meta().getString("type", "MKS baratron");
} }
@ -64,18 +64,18 @@ public class MKSBaratronDevice extends PortSensor<Double> {
if (answer == null || answer.isEmpty()) { if (answer == null || answer.isEmpty()) {
// invalidateState("connection"); // invalidateState("connection");
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
this.progressUpdate("No connection"); this.updateMessage("No connection");
return null; return null;
} else { } else {
updateState(CONNECTED_STATE, true); updateState(CONNECTED_STATE, true);
} }
double res = Double.parseDouble(answer); double res = Double.parseDouble(answer);
if (res <= 0) { if (res <= 0) {
this.progressUpdate("Non positive"); this.updateMessage("Non positive");
// invalidateState("power"); // invalidateState("power");
return null; return null;
} else { } else {
this.progressUpdate("OK"); this.updateMessage("OK");
return res; return res;
} }
} }

View File

@ -138,7 +138,7 @@ public class MKSVacDevice extends PortSensor<Double> {
} }
@Override @Override
public String type() { public String getType() {
return meta().getString("type", "MKS vacuumeter"); return meta().getString("type", "MKS vacuumeter");
} }
@ -154,16 +154,16 @@ public class MKSVacDevice extends PortSensor<Double> {
String answer = talk("PR" + getChannel() + "?"); String answer = talk("PR" + getChannel() + "?");
if (answer == null || answer.isEmpty()) { if (answer == null || answer.isEmpty()) {
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
this.progressUpdate("No connection"); this.updateMessage("No connection");
return null; return null;
} }
double res = Double.parseDouble(answer); double res = Double.parseDouble(answer);
if (res <= 0) { if (res <= 0) {
this.progressUpdate("No power"); this.updateMessage("No power");
invalidateState("power"); invalidateState("power");
return null; return null;
} else { } else {
this.progressUpdate("OK"); this.updateMessage("OK");
return res; return res;
} }
} }

View File

@ -51,7 +51,7 @@ public class MeradatVacDevice extends PortSensor<Double> {
} }
@Override @Override
public String type() { public String getType() {
return meta().getString("type", "Vit vacuumeter"); return meta().getString("type", "Vit vacuumeter");
} }
@ -93,7 +93,7 @@ public class MeradatVacDevice extends PortSensor<Double> {
String answer = sendAndWait(query, timeout(), phrase -> phrase.startsWith(base)); String answer = sendAndWait(query, timeout(), phrase -> phrase.startsWith(base));
if (answer.isEmpty()) { if (answer.isEmpty()) {
this.progressUpdate("No signal"); this.updateMessage("No signal");
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
return null; return null;
} else { } else {
@ -107,11 +107,11 @@ public class MeradatVacDevice extends PortSensor<Double> {
} }
BigDecimal res = BigDecimal.valueOf(base * Math.pow(10, exp)); BigDecimal res = BigDecimal.valueOf(base * Math.pow(10, exp));
res = res.setScale(4, RoundingMode.CEILING); res = res.setScale(4, RoundingMode.CEILING);
this.progressUpdate("OK"); this.updateMessage("OK");
updateState(CONNECTED_STATE, true); updateState(CONNECTED_STATE, true);
return res.doubleValue(); return res.doubleValue();
} else { } else {
this.progressUpdate("Wrong answer: " + answer); this.updateMessage("Wrong answer: " + answer);
updateState(CONNECTED_STATE, false); updateState(CONNECTED_STATE, false);
return null; return null;
} }

View File

@ -92,7 +92,7 @@ public class VacCollectorDevice extends Sensor<Values> {
} }
@Override @Override
public String type() { public String getType() {
return "Numass vacuum"; return "Numass vacuum";
} }

View File

@ -11,6 +11,7 @@ description = 'kodex/ktor based server'
dependencies { dependencies {
compile "hep.dataforge:kodex-server" compile "hep.dataforge:kodex-server"
compile "hep.dataforge:dataforge-storage" compile "hep.dataforge:dataforge-storage"
compile "hep.dataforge:dataforge-control"
compile project(":numass-core") compile project(":numass-core")
} }

View File

@ -1,7 +1,6 @@
package inr.numass.server package inr.numass.server
import hep.dataforge.context.Context import hep.dataforge.control.DeviceManager
import hep.dataforge.meta.Meta
import hep.dataforge.providers.Path import hep.dataforge.providers.Path
import hep.dataforge.server.* import hep.dataforge.server.*
import hep.dataforge.storage.api.TableLoader import hep.dataforge.storage.api.TableLoader
@ -32,12 +31,10 @@ private suspend fun ApplicationCall.json(json: suspend JsonObjectBuilder.() -> U
} }
} }
val storageInterceptor = InterceptorFactory { context, meta ->
class StorageInterceptorBuilder : InterceptorBuilder {
override fun build(context: Context, meta: Meta): ServerInterceptor {
val storageManager = context.getFeature(StorageManager::class.java); val storageManager = context.getFeature(StorageManager::class.java);
val storage = storageManager.buildStorage(meta); val storage = storageManager.buildStorage(meta);
return ServerInterceptor("storage") { ServerInterceptor("storage") {
get("listStorage") { get("listStorage") {
val path = call.request.queryParameters["path"] ?: "" val path = call.request.queryParameters["path"] ?: ""
val shelf = storage.optShelf(path) val shelf = storage.optShelf(path)
@ -69,7 +66,7 @@ class StorageInterceptorBuilder : InterceptorBuilder {
if (loader is TableLoader) { if (loader is TableLoader) {
val from = Value.of(call.request.queryParameters["from"] ?: "") val from = Value.of(call.request.queryParameters["from"] ?: "")
val to = Value.of(call.request.queryParameters["to"] ?: "") val to = Value.of(call.request.queryParameters["to"] ?: "")
val maxItems = (call.request.queryParameters.get("maxItems") ?: "1000").toInt() val maxItems = (call.request.queryParameters["maxItems"] ?: "1000").toInt()
call.json { call.json {
add("path", loader.path.toString()) add("path", loader.path.toString())
val data = jsonArray() val data = jsonArray()
@ -86,6 +83,47 @@ class StorageInterceptorBuilder : InterceptorBuilder {
} }
} }
} }
}
} }
val controlInterceptor = InterceptorFactory { context, meta ->
val deviceManager = context.getFeature(DeviceManager::class.java);
ServerInterceptor("devices") {
get("listDevices") {
call.json {
val devices = jsonArray();
for (name in deviceManager.deviceNames()) {
val device = deviceManager.optDevice(name).get();
devices.add(jsonObject {
add("name", name.toUnescaped())
add("type", device.getType())
add("meta", device.meta.asJson())
})
}
}
}
get("getDeviceState") {
val deviceName = call.request.queryParameters["name"] ?: "";
if (deviceName.isEmpty()) {
call.error("devices.missingParameter", "Parameter 'name' is required")
} else {
val deviceOpt = deviceManager.optDevice(deviceName);
if (deviceOpt.isPresent) {
val device = deviceOpt.get();
call.json {
add("name", deviceName)
add("type", device.type)
add("meta", device.meta.asJson())
add("state", jsonObject {
for (state in device.listStates()) {
add(state, device.getState(state).toString())
}
})
}
} else {
call.error("devices.deviceNotFound", "Device with name: $deviceName not found in the system.")
}
}
}
}
}

View File

@ -0,0 +1,24 @@
package inr.numass.server
import hep.dataforge.context.Context
import hep.dataforge.context.Global
import hep.dataforge.control.DeviceManager
import hep.dataforge.meta.Meta
import hep.dataforge.server.KodexServer
import hep.dataforge.storage.commons.StorageManager
fun main(args: Array<String>) {
val meta = Meta.empty();
val context = Context.build("SERVER", Global.instance(), meta)
val server = KodexServer(context, meta)
context.optFeature(StorageManager::class.java).ifPresent{
server.intercept(storageInterceptor)
}
context.optFeature(DeviceManager::class.java).ifPresent{
}
server.start()
}