Skip to content

Commit

Permalink
[resol] correct handling of system date/time field (#10811)
Browse files Browse the repository at this point in the history
* correct handling of system date/time field to close #10684

Signed-off-by: Raphael Mack <ramack@raphael-mack.de>

* further fix

Signed-off-by: Raphael Mack <ramack@raphael-mack.de>
  • Loading branch information
ramack committed Jun 12, 2021
1 parent 10f5ca8 commit 72e3729
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,20 @@ public class ResolThingHandler extends ResolBaseThingHandler {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
DateTimeType.DATE_PATTERN_WITH_TZ_AND_MS_GENERAL);

private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");

private static final SimpleDateFormat WEEK_FORMAT = new SimpleDateFormat("E, HH:mm");

static {
synchronized (DATE_FORMAT) {
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
synchronized (TIME_FORMAT) {
TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
synchronized (WEEK_FORMAT) {
WEEK_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
}

public ResolThingHandler(Thing thing, ResolStateDescriptionOptionProvider stateDescriptionProvider) {
Expand Down Expand Up @@ -134,7 +144,11 @@ protected void packetReceived(Specification spec, Language lang, Packet packet)
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID,
pfv.getPacketFieldSpec().getUnit().getUnitCodeText());
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.DateTime) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "DateTime");
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "datetime");
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.WeekTime) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "weektime");
} else if (pfv.getPacketFieldSpec().getType() == SpecificationFile.Type.Time) {
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "time");
} else {
/* used for enums and the numeric types without unit */
channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, "None");
Expand All @@ -144,10 +158,10 @@ protected void packetReceived(Specification spec, Language lang, Packet packet)

Thing thing = getThing();
switch (pfv.getPacketFieldSpec().getType()) {
case WeekTime:
case DateTime:
acceptedItemType = "DateTime";
break;
case WeekTime:
case Number:
acceptedItemType = ResolChannelTypeProvider.itemTypeForUnit(pfv.getPacketFieldSpec().getUnit());
break;
Expand Down Expand Up @@ -178,13 +192,35 @@ protected void packetReceived(Specification spec, Language lang, Packet packet)
Channel channel = ChannelBuilder.create(channelUID, "Number").withType(channelTypeUID)
.withLabel(pfv.getName(lang)).build();

thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else if ("DateTime".equals(acceptedItemType)) {
/* a date channel */
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
.withLabel(pfv.getName(lang)).build();

thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());

} else if ("String".equals(acceptedItemType)) {
/* a string channel */
Channel channel = ChannelBuilder.create(channelUID, "String").withType(channelTypeUID)
.withLabel(pfv.getName(lang)).build();

thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else if (pfv.getRawValueDouble() != null) {
/* a number channel */
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
.withLabel(pfv.getName(lang)).build();

thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
} else {
/* a string channel */
Channel channel = ChannelBuilder.create(channelUID, "String").withType(channelTypeUID)
.withLabel(pfv.getName(lang)).build();

thingBuilder.withChannel(channel).withLabel(thing.getLabel());
updateThing(thingBuilder.build());
}
Expand Down Expand Up @@ -229,19 +265,33 @@ protected void packetReceived(Specification spec, Language lang, Packet packet)
* }
*/
break;
case Time:
synchronized (TIME_FORMAT) {
this.updateState(channelId, new StringType(TIME_FORMAT.format(pfv.getRawValueDate())));
}
break;
case WeekTime:
synchronized (WEEK_FORMAT) {
DateTimeType d = new DateTimeType(WEEK_FORMAT.format(pfv.getRawValueDate()));
this.updateState(channelId, d);
}
break;
case DateTime:
synchronized (DATE_FORMAT) {
DateTimeType d = new DateTimeType(DATE_FORMAT.format(pfv.getRawValueDate()));
this.updateState(channelId, d);
}
break;
case WeekTime:
case Time:
default:
Bridge b = getBridge();
if (b != null) {
String value = pfv.formatTextValue(pfv.getPacketFieldSpec().getUnit(),
((ResolBridgeHandler) b).getLocale());
ResolBridgeHandler handler = (ResolBridgeHandler) b.getHandler();
String value;
if (handler != null) {
value = pfv.formatTextValue(pfv.getPacketFieldSpec().getUnit(), handler.getLocale());
} else {
value = pfv.formatTextValue(pfv.getPacketFieldSpec().getUnit(), Locale.getDefault());
}
try {
QuantityType<?> q = new QuantityType<>(value);
this.updateState(channelId, q);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@

</thing-type>

<channel-type id="weektime">
<item-type>DateTime</item-type>
<label>Time</label>
<description>Time and day of week.</description>
<category>Time</category>
<state readOnly="true"/>
</channel-type>

<channel-type id="time">
<item-type>String</item-type>
<label>Time</label>
<category>Time</category>
<state readOnly="true"/>
</channel-type>

<channel-type id="datetime">
<item-type>DateTime</item-type>
<label>Time</label>
<description>Time and date.</description>
<category>Time</category>
<state readOnly="true"/>
</channel-type>

<channel-type id="None">
<item-type>Number</item-type>
<label>Any</label>
Expand Down

0 comments on commit 72e3729

Please sign in to comment.