Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
import com.google.protobuf.Descriptors.FieldDescriptor.Type;
Expand All @@ -46,6 +47,7 @@
import org.openbase.type.domotic.action.ActionDescriptionType.ActionDescription;
import org.openbase.type.domotic.service.ServiceCommunicationTypeType.ServiceCommunicationType.CommunicationType;
import org.openbase.type.domotic.service.ServiceDescriptionType.ServiceDescription;
import org.openbase.type.domotic.service.ServiceStateDescriptionType.ServiceStateDescription;
import org.openbase.type.domotic.service.ServiceTemplateType.ServiceTemplate;
import org.openbase.type.domotic.service.ServiceTemplateType.ServiceTemplate.ServicePattern;
import org.openbase.type.domotic.service.ServiceTemplateType.ServiceTemplate.ServiceType;
Expand All @@ -54,7 +56,6 @@
import org.openbase.type.domotic.state.ActivationStateType.ActivationStateOrBuilder;
import org.openbase.type.domotic.state.BatteryStateType.BatteryState;
import org.openbase.type.domotic.state.BatteryStateType.BatteryStateOrBuilder;
import org.openbase.type.domotic.state.BrightnessStateType;
import org.openbase.type.domotic.state.BrightnessStateType.BrightnessState;
import org.openbase.type.domotic.state.BrightnessStateType.BrightnessStateOrBuilder;
import org.openbase.type.domotic.state.ButtonStateType.ButtonState;
Expand All @@ -70,9 +71,7 @@
import org.openbase.type.domotic.state.MotionStateType.MotionState;
import org.openbase.type.domotic.state.MotionStateType.MotionStateOrBuilder;
import org.openbase.type.domotic.state.PowerStateType.PowerState;
import org.openbase.type.domotic.state.PowerStateType.PowerState.State;
import org.openbase.type.domotic.state.PowerStateType.PowerStateOrBuilder;
import org.openbase.type.domotic.state.PresenceStateType;
import org.openbase.type.domotic.state.PresenceStateType.PresenceState;
import org.openbase.type.domotic.state.PresenceStateType.PresenceStateOrBuilder;
import org.openbase.type.domotic.state.TamperStateType.TamperState;
Expand Down Expand Up @@ -325,6 +324,22 @@ public static <SC extends Message.Builder, SV extends ProtocolMessageEnum> SC ge
}
}

/**
* Method generates a new service state builder related to the given {@code serviceType} and initializes this instance with the {@code stateValue} derived from the given {@code stateValueString}.
*
* @param serviceType the service type of the service state.
* @param stateValueString a string representation of a compatible state value related to the given service state.
* @return a new service state initialized with the state value.
* @throws CouldNotPerformException is thrown in case the given arguments are not compatible with each other or something else went wrong during the build.
*/
public static Message.Builder generateServiceStateBuilder(final ServiceType serviceType, String stateValueString) throws CouldNotPerformException {
final Message.Builder serviceStateBuilder = generateServiceStateBuilder(serviceType);
final FieldDescriptor serviceStateValueFieldDescriptor = ProtoBufFieldProcessor.getFieldDescriptor(serviceStateBuilder, "value");
final EnumValueDescriptor serviceStateValue = serviceStateValueFieldDescriptor.getEnumType().findValueByName(stateValueString);
serviceStateBuilder.setField(serviceStateValueFieldDescriptor, serviceStateValue);
return serviceStateBuilder;
}

/**
* Method generates a new service state builder related to the given {@code serviceType} and initializes this instance with the given {@code stateValue}.
*
Expand Down Expand Up @@ -394,23 +409,40 @@ public static Class<? extends Message> getServiceStateClass(final ServiceType se
}

/**
* Method detects and returns the service state class.
* Method detects and returns the service state enum class.
*
* @param communicationType the communication type to resolve the service state class.
* @param serviceType the given service type to resolve the enum class.
* @return the service state class.
* @throws NotAvailableException is thrown in case the class could not be detected.
*/
public static Class<? extends Message> getServiceStateClass(final CommunicationType communicationType) throws NotAvailableException {
String serviceStateName;
public static Class<? extends Enum> getServiceStateEnumClass(final ServiceType serviceType) throws NotAvailableException {
try {
return getServiceStateEnumClass(Registries.getTemplateRegistry().getServiceTemplateByType(serviceType).getCommunicationType());
} catch (CouldNotPerformException ex) {
throw new NotAvailableException("CommunicationType for ServiceType[" + serviceType + "]", ex);
}
}

public static String getServiceStateName(final CommunicationType communicationType) throws NotAvailableException {
try {
if (communicationType == CommunicationType.UNKNOWN) {
throw new InvalidStateException("CommunicationType is not configured in ServiceTemplate!");
}
serviceStateName = StringProcessor.transformUpperCaseToPascalCase(communicationType.name());
return StringProcessor.transformUpperCaseToPascalCase(communicationType.name());
} catch (CouldNotPerformException ex) {
throw new NotAvailableException("CommunicationType", communicationType.name(), ex);
}
}

/**
* Method detects and returns the service state class.
*
* @param communicationType the communication type to resolve the service state class.
* @return the service state class.
* @throws NotAvailableException is thrown in case the class could not be detected.
*/
public static Class<? extends Message> getServiceStateClass(final CommunicationType communicationType) throws NotAvailableException {
final String serviceStateName = getServiceStateName(communicationType);
final String serviceClassName = SERVICE_STATE_PACKAGE.getName() + "." + serviceStateName + "Type$" + serviceStateName;
try {
return (Class<? extends Message>) Class.forName(serviceClassName);
Expand All @@ -419,6 +451,27 @@ public static Class<? extends Message> getServiceStateClass(final CommunicationT
}
}

/**
* Method detects and returns the service state value enum class.
*
* @param communicationType the communication type to resolve the service state enum class.
* @return the class of the service state enum.
* @throws NotAvailableException is thrown in case the enum could not be detected.
*/
public static Class<? extends Enum<?>> getServiceStateEnumClass(final CommunicationType communicationType) throws NotAvailableException {
final String serviceStateName = getServiceStateName(communicationType);
final String serviceEnumClassName = SERVICE_STATE_PACKAGE.getName() + "." + serviceStateName + "Type$" + serviceStateName + "$State";
try {
final Class<?> clazz = Class.forName(serviceEnumClassName);
if (!clazz.isEnum()) {
throw new VerificationFailedException("Given Class["+clazz.getName()+"] is not an enum!");
}
return (Class<? extends Enum<?>>) clazz;
} catch (NullPointerException | ClassNotFoundException | ClassCastException | CouldNotPerformException ex) {
throw new NotAvailableException("ServiceStateClass", serviceEnumClassName, new CouldNotPerformException("Could not detect enum class!", ex));
}
}

public static Method detectServiceMethod(final ServiceType serviceType, final ServicePattern servicePattern, final Class instanceClass, final Class... argumentClasses) throws CouldNotPerformException {
return detectServiceMethod(serviceType, servicePattern, ServiceTempus.CURRENT, instanceClass, argumentClasses);
}
Expand Down Expand Up @@ -871,7 +924,26 @@ public static List<String> getFieldDataTypes(final Message messagePrototype) thr
return dataTypes;
}

public static Label generateServiceStateLabel(ServiceStateDescription serviceStateDescription) throws CouldNotPerformException {
return generateServiceStateLabel(
deserializeServiceState(serviceStateDescription),
serviceStateDescription.getServiceType()
);
}

public static Label generateServiceStateLabel(ServiceStateDescription serviceStateDescription, boolean discreteOnly) throws CouldNotPerformException {
return generateServiceStateLabel(
deserializeServiceState(serviceStateDescription),
serviceStateDescription.getServiceType(),
discreteOnly
);
}

public static Label generateServiceStateLabel(MessageOrBuilder serviceStateOrBuilder, ServiceType serviceType) {
return generateServiceStateLabel(serviceStateOrBuilder, serviceType, false);
}

public static Label generateServiceStateLabel(MessageOrBuilder serviceStateOrBuilder, ServiceType serviceType, boolean discreteOnly) {
try {
final Label.Builder labelBuilder = Label.newBuilder();
switch (serviceType) {
Expand Down Expand Up @@ -954,40 +1026,39 @@ public static Label generateServiceStateLabel(MessageOrBuilder serviceStateOrBui
LabelProcessor.addLabel(labelBuilder, Locale.ENGLISH, brightness).build();
LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, brightness).build();
return labelBuilder.build();

case COLOR_STATE_SERVICE:
final Color color = ((ColorStateOrBuilder) serviceStateOrBuilder).getColor();
return ColorStateToLabelTransformer.computeColorLabelFromColor(color);
case ILLUMINANCE_STATE_SERVICE:
final IlluminanceState.State illuminanceStateValue = ((IlluminanceStateOrBuilder) serviceStateOrBuilder).getValue();
final String illuminance = ((int) (((IlluminanceStateOrBuilder) serviceStateOrBuilder).getIlluminance())) + " Lux";
LabelProcessor.addLabel(labelBuilder, Locale.ENGLISH, illuminanceStateValue.name().toLowerCase() + " " + illuminance);
final String illuminance = discreteOnly ? "" : " (" + ((int) (((IlluminanceStateOrBuilder) serviceStateOrBuilder).getIlluminance())) + " Lux)";
LabelProcessor.addLabel(labelBuilder, Locale.ENGLISH, illuminanceStateValue.name().toLowerCase() + illuminance);
switch (illuminanceStateValue) {
case DARK:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "dunkel " + illuminance).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "dunkel" + illuminance).build();
case DUSKY:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "dämmernd " + illuminance).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "dämmernd" + illuminance).build();
case SHADY:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "schattig " + illuminance).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "schattig" + illuminance).build();
case SUNNY:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "sonnig " + illuminance).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "sonnig" + illuminance).build();
case UNKNOWN:
default:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "unbekannt " + illuminance).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "unbekannt" + illuminance).build();
}
case BATTERY_STATE_SERVICE:
final BatteryState.State batteryStateValue = ((BatteryStateOrBuilder) serviceStateOrBuilder).getValue();
final String level = ((int) (((BatteryStateOrBuilder) serviceStateOrBuilder).getLevel() * 100d)) + " %";
LabelProcessor.addLabel(labelBuilder, Locale.ENGLISH, batteryStateValue.name().toLowerCase() + " " + level);
final String level = discreteOnly ? "" : " (" + ((int) (((BatteryStateOrBuilder) serviceStateOrBuilder).getLevel() * 100d)) + " %)";
LabelProcessor.addLabel(labelBuilder, Locale.ENGLISH, batteryStateValue.name().toLowerCase() + level);
switch (batteryStateValue) {
case INSUFFICIENT:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "unzureichend " + level).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "unzureichend" + level).build();
case CRITICAL:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "kritisch " + level).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "kritisch" + level).build();
case LOW:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "gering " + level).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "gering" + level).build();
case OK:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "ok " + level).build();
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "ok" + level).build();
case UNKNOWN:
default:
return LabelProcessor.addLabel(labelBuilder, Locale.GERMAN, "unbekannt " + level).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void print(Unit<?> unit, Message data) {

private void print(Unit<?> unit, ServiceType serviceType, Message serviceState) {

// print human readable format
// print human-readable format
if (config.format == LogFormat.HUMAN_READABLE) {
try {
submit(MultiLanguageTextProcessor.getBestMatch(ServiceStateProcessor.getActionDescription(serviceState)));
Expand Down