]> git.basschouten.com Git - openhab-addons.git/blob
e1420d86bfbb35fafb1f6191828b6b1404e42d15
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.openwebnet.internal.handler;
14
15 import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.*;
16
17 import java.util.Set;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.UnDefType;
36 import org.openwebnet4j.communication.OWNException;
37 import org.openwebnet4j.communication.Response;
38 import org.openwebnet4j.message.BaseOpenMessage;
39 import org.openwebnet4j.message.FrameException;
40 import org.openwebnet4j.message.MalformedFrameException;
41 import org.openwebnet4j.message.Thermoregulation;
42 import org.openwebnet4j.message.Thermoregulation.DimThermo;
43 import org.openwebnet4j.message.Thermoregulation.Function;
44 import org.openwebnet4j.message.Thermoregulation.OperationMode;
45 import org.openwebnet4j.message.Thermoregulation.WhatThermo;
46 import org.openwebnet4j.message.Thermoregulation.WhatThermoType;
47 import org.openwebnet4j.message.Where;
48 import org.openwebnet4j.message.WhereThermo;
49 import org.openwebnet4j.message.Who;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * The {@link OpenWebNetThermoregulationHandler} is responsible for handling
55  * commands/messages for Thermoregulation Things. It extends the abstract
56  * {@link OpenWebNetThingHandler}.
57  *
58  * @author Massimo Valla - Initial contribution. Added support for 4-zones CU.
59  *         Rafactoring and fixed CU state channels updates. Completed support
60  *         for HOLIDAY/VACATION modes and refactored mode handling.
61  * @author Gilberto Cocchi - Initial contribution.
62  * @author Andrea Conte - Added support for 99-zone CU and CU state channels.
63  */
64 @NonNullByDefault
65 public class OpenWebNetThermoregulationHandler extends OpenWebNetThingHandler {
66
67     private final Logger logger = LoggerFactory.getLogger(OpenWebNetThermoregulationHandler.class);
68
69     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.THERMOREGULATION_SUPPORTED_THING_TYPES;
70
71     private double currentSetPointTemp = 20.0d;
72
73     private Thermoregulation.@Nullable Function currentFunction = Function.HEATING;
74     private Thermoregulation.@Nullable OperationMode currentMode = OperationMode.PROTECTION;
75     private int currentWeeklyPrgNum = 1;
76     private int currentScenarioPrgNum = 1;
77     private int currentVacationDays = 1;
78
79     private boolean isStandAlone = true; // true if zone is not associated to a CU
80     private boolean isCentralUnit = false;
81
82     private boolean cuAtLeastOneProbeOff = false;
83     private boolean cuAtLeastOneProbeProtection = false;
84     private boolean cuAtLeastOneProbeManual = false;
85     private String cuBatteryStatus = CU_BATTERY_OK;
86     private boolean cuFailureDiscovered = false;
87
88     private @Nullable ScheduledFuture<?> cuStateChannelsUpdateSchedule;
89
90     public static final int CU_STATE_CHANNELS_UPDATE_DELAY = 1500; // msec
91
92     private static final String CU_REMOTE_CONTROL_ENABLED = "ENABLED";
93     private static final String CU_REMOTE_CONTROL_DISABLED = "DISABLED";
94     private static final String CU_BATTERY_OK = "OK";
95     private static final String CU_BATTERY_KO = "KO";
96     private static final String MODE_WEEKLY = "WEEKLY";
97
98     public OpenWebNetThermoregulationHandler(Thing thing) {
99         super(thing);
100     }
101
102     @Override
103     public void initialize() {
104         super.initialize();
105         ThingTypeUID thingType = thing.getThingTypeUID();
106         isCentralUnit = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_CU.equals(thingType);
107         if (!isCentralUnit) {
108             if (!((WhereThermo) deviceWhere).isProbe()) {
109                 Object standAloneConfig = getConfig().get(OpenWebNetBindingConstants.CONFIG_PROPERTY_STANDALONE);
110                 if (standAloneConfig != null) {
111                     isStandAlone = Boolean.parseBoolean(standAloneConfig.toString());
112                 }
113                 logger.debug("@@@@  THERMO ZONE INITIALIZE isStandAlone={}", isStandAlone);
114             }
115         } else {
116             // central unit must have WHERE=#0 or WHERE=0 or WHERE=#0#n
117             String w = deviceWhere.value();
118             if (w == null || !("0".equals(w) || "#0".equals(w) || w.startsWith("#0#"))) {
119                 logger.warn("initialize() Invalid WHERE={} for Central Unit.", deviceWhere.value());
120                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
121                         "@text/offline.conf-error-where");
122                 return;
123             }
124         }
125     }
126
127     @Override
128     protected void handleChannelCommand(ChannelUID channel, Command command) {
129         switch (channel.getId()) {
130             case CHANNEL_TEMP_SETPOINT:
131                 handleSetpoint(command);
132                 break;
133             case CHANNEL_FUNCTION:
134                 handleFunction(command);
135                 break;
136             case CHANNEL_MODE:
137                 handleMode(command);
138                 break;
139             case CHANNEL_FAN_SPEED:
140                 handleSetFanSpeed(command);
141                 break;
142             case CHANNEL_CU_WEEKLY_PROGRAM_NUMBER:
143             case CHANNEL_CU_SCENARIO_PROGRAM_NUMBER:
144                 handleSetProgramNumber(channel, command);
145                 break;
146             case CHANNEL_CU_VACATION_DAYS:
147                 handleVacationDays(channel, command);
148                 break;
149             default: {
150                 logger.warn("handleChannelCommand() Unsupported ChannelUID {}", channel.getId());
151             }
152         }
153     }
154
155     @Override
156     protected void requestChannelState(ChannelUID channel) {
157         super.requestChannelState(channel);
158         refreshDevice(false);
159     }
160
161     @Override
162     protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
163         return new WhereThermo(wStr);
164     }
165
166     @Override
167     protected String ownIdPrefix() {
168         return Who.THERMOREGULATION.value().toString();
169     }
170
171     private void handleSetFanSpeed(Command command) {
172         if (command instanceof StringType) {
173             Where w = deviceWhere;
174             if (w == null) {
175                 logger.warn("handleSetFanSpeed() Where is null for thing {}", getThing().getUID());
176                 return;
177             }
178             try {
179                 Thermoregulation.FanCoilSpeed speed = Thermoregulation.FanCoilSpeed.valueOf(command.toString());
180                 send(Thermoregulation.requestWriteFanCoilSpeed(w.value(), speed));
181             } catch (OWNException e) {
182                 logger.warn("handleSetFanSpeed() {}", e.getMessage());
183             } catch (IllegalArgumentException e) {
184                 logger.warn("handleSetFanSpeed() Unsupported command {} for thing {}", command, getThing().getUID());
185                 return;
186             }
187         } else {
188             logger.warn("handleSetFanSpeed() Unsupported command {} for thing {}", command, getThing().getUID());
189         }
190     }
191
192     private void handleSetProgramNumber(ChannelUID channel, Command command) {
193         if (command instanceof DecimalType) {
194             if (!isCentralUnit) {
195                 logger.warn("handleSetProgramNumber() This command can be sent only for a Central Unit.");
196                 return;
197             }
198             Where w = deviceWhere;
199             if (w == null) {
200                 logger.warn("handleSetProgramNumber() Where is null for thing {}", getThing().getUID());
201                 return;
202             }
203
204             int programNumber = ((DecimalType) command).intValue();
205             boolean updateOpMode = false;
206
207             if (CHANNEL_CU_WEEKLY_PROGRAM_NUMBER.equals(channel.getId())) {
208                 if (programNumber < 1 || programNumber > 3) {
209                     logger.warn("handleSetProgramNumber() Invalid program number {} for thing {}", command,
210                             getThing().getUID());
211                     return;
212                 }
213                 updateOpMode = (currentMode == Thermoregulation.OperationMode.WEEKLY);
214                 currentWeeklyPrgNum = programNumber;
215                 logger.debug("handleSetProgramNumber() currentWeeklyPrgNum changed to: {}", programNumber);
216             } else {
217                 if (programNumber < 1 || programNumber > 16) {
218                     logger.warn("handleSetProgramNumber() Invalid program number {} for thing {}", command,
219                             getThing().getUID());
220                     return;
221                 }
222                 updateOpMode = (currentMode == Thermoregulation.OperationMode.SCENARIO);
223                 currentScenarioPrgNum = programNumber;
224                 logger.debug("handleSetProgramNumber() currentScenarioPrgNum changed to: {}", programNumber);
225             }
226
227             // force OperationMode update if we are already in SCENARIO or WEEKLY mode
228             if (updateOpMode) {
229                 try {
230                     send(Thermoregulation.requestWriteWeeklyScenarioMode(getWhere(w.value()), currentMode,
231                             currentFunction, programNumber));
232                 } catch (OWNException e) {
233                     logger.warn("handleSetProgramNumber() {}", e.getMessage());
234                 }
235             } else { // just update channel
236                 updateState(channel, new DecimalType(programNumber));
237             }
238         } else {
239             logger.warn("handleSetProgramNumber() Unsupported command {} for thing {}", command, getThing().getUID());
240         }
241     }
242
243     private void handleVacationDays(ChannelUID channel, Command command) {
244         if (command instanceof DecimalType) {
245             Where w = deviceWhere;
246             if (w == null) {
247                 logger.warn("handleVacationDays() Where is null for thing {}", getThing().getUID());
248                 return;
249             }
250             if (!isCentralUnit) {
251                 logger.warn("handleVacationDays() This command can be sent only for a Central Unit.");
252                 return;
253             }
254             int vacationDays = ((DecimalType) command).intValue();
255
256             if (vacationDays < 1 || vacationDays > 255) {
257                 logger.warn("handleVacationDays() vacation days must be between 1 and 255");
258                 return;
259             }
260
261             currentVacationDays = vacationDays;
262             logger.debug("handleVacationDays() currentVacationDays changed to: {}", currentVacationDays);
263
264             // force OperationMode update if we are already in VACATION mode
265             if (currentMode == Thermoregulation.OperationMode.VACATION) {
266                 try {
267                     send(Thermoregulation.requestWriteVacationMode(getWhere(w.value()), currentFunction,
268                             currentVacationDays, currentWeeklyPrgNum));
269                 } catch (OWNException e) {
270                     logger.warn("handleVacationDays() {}", e.getMessage());
271                 } catch (IllegalArgumentException e) {
272                     logger.warn("handleVacationDays() Unsupported command {} for thing {}", command,
273                             getThing().getUID());
274                 }
275             } else { // just update channel
276                 updateState(channel, new DecimalType(currentVacationDays));
277             }
278         } else {
279             logger.warn("handleVacationDays() Unsupported command {} for thing {}", command, getThing().getUID());
280         }
281     }
282
283     private void handleSetpoint(Command command) {
284         if (command instanceof QuantityType || command instanceof DecimalType) {
285             Where w = deviceWhere;
286             if (w == null) {
287                 logger.warn("handleSetpoint() Where is null for thing {}", getThing().getUID());
288                 return;
289             }
290             double newTemp = 0;
291             if (command instanceof QuantityType) {
292                 QuantityType<?> tempCelsius = ((QuantityType<?>) command).toUnit(SIUnits.CELSIUS);
293                 if (tempCelsius != null) {
294                     newTemp = tempCelsius.doubleValue();
295                 }
296             } else {
297                 newTemp = ((DecimalType) command).doubleValue();
298             }
299             if (newTemp >= 5.0 && newTemp <= 40.0) {
300                 try {
301                     Response res;
302                     res = send(Thermoregulation.requestWriteSetpointTemperature(getWhere(w.value()), newTemp,
303                             currentFunction));
304                     // For zones no setPoint temperature confirmation message is returned from gw,
305                     // so we update channel with current requested temp on successful response.
306                     if (!isCentralUnit && res != null && res.isSuccess()) {
307                         updateState(CHANNEL_TEMP_SETPOINT, getAsQuantityTypeOrNull(newTemp, SIUnits.CELSIUS));
308                         currentSetPointTemp = newTemp;
309                     }
310                 } catch (MalformedFrameException | OWNException e) {
311                     logger.warn("handleSetpoint() {}", e.getMessage());
312
313                 }
314             } else {
315                 logger.info("handleSetpoint() Setpoint temperature must be between 5°C and 40°C for thing {}",
316                         getThing().getUID());
317             }
318         } else {
319             logger.warn("handleSetpoint() Unsupported command {} for thing {}", command, getThing().getUID());
320         }
321     }
322
323     private void handleMode(Command command) {
324         if (command instanceof StringType) {
325             Where w = deviceWhere;
326             if (w == null) {
327                 logger.warn("handleMode() Where is null for thing {}", getThing().getUID());
328                 return;
329             }
330
331             String whStr = getWhere(w.value());
332             String cmdStr = command.toString().toUpperCase();
333             OperationMode newMode;
334             try {
335                 newMode = OperationMode.valueOf(cmdStr);
336             } catch (IllegalArgumentException e) {
337                 logger.warn("handleMode() Unsupported command {} for thing {}", command, getThing().getUID());
338                 return;
339             }
340             Thermoregulation tMsg = null;
341             if (isCentralUnit) { // CU accepted modes: MANUAL, OFF, PROTECTION, WEEKLY, SCENARIO, HOLIDAY,
342                                  // VACATION
343                 switch (newMode) {
344                     case MANUAL:
345                     case OFF:
346                     case PROTECTION:
347                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, currentFunction, currentSetPointTemp);
348                         break;
349                     case WEEKLY:
350                     case SCENARIO:
351                         int programNumber = (MODE_WEEKLY.equals(cmdStr) ? currentWeeklyPrgNum : currentScenarioPrgNum);
352                         tMsg = Thermoregulation.requestWriteWeeklyScenarioMode(whStr, newMode, currentFunction,
353                                 programNumber);
354                         break;
355                     case HOLIDAY:
356                         tMsg = Thermoregulation.requestWriteHolidayMode(whStr, currentFunction, currentWeeklyPrgNum);
357                         break;
358                     case VACATION:
359                         tMsg = Thermoregulation.requestWriteVacationMode(whStr, currentFunction, currentVacationDays,
360                                 currentWeeklyPrgNum);
361                         break;
362                     default:
363                         logger.warn("handleMode() Unsupported command {} for CU thing {}", command,
364                                 getThing().getUID());
365                 }
366             } else {// Zone accepted modes: MANUAL, OFF, PROTECTION, AUTO
367                 switch (newMode) {
368                     case MANUAL:
369                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, currentFunction, currentSetPointTemp);
370                         break;
371                     case OFF:
372                     case PROTECTION:
373                     case AUTO:
374                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, Function.GENERIC, currentSetPointTemp);
375                         break;
376                     default:
377                         logger.warn("handleMode() Unsupported command {} for ZONE thing {}", command,
378                                 getThing().getUID());
379                 }
380             }
381
382             if (tMsg != null) {
383                 try {
384                     send(tMsg);
385                 } catch (OWNException e) {
386                     logger.warn("handleMode() {}", e.getMessage());
387                 }
388             } else {
389                 logger.warn("handleMode() Unsupported command {} for thing {}", command, getThing().getUID());
390             }
391         }
392     }
393
394     private String getWhere(String where) {
395         if (isCentralUnit) {
396             if (where.charAt(0) == '#') {
397                 return where;
398             } else { // to support old configurations for CU with where="0"
399                 return "#" + where;
400             }
401         } else {
402             return isStandAlone ? where : "#" + where;
403         }
404     }
405
406     private void handleFunction(Command command) {
407         if (command instanceof StringType) {
408             Where w = deviceWhere;
409             if (w == null) {
410                 logger.warn("handleFunction() Where is null for thing {}", getThing().getUID());
411                 return;
412             }
413             try {
414                 Thermoregulation.Function function = Thermoregulation.Function.valueOf(command.toString());
415                 send(Thermoregulation.requestWriteFunction(w.value(), function));
416             } catch (OWNException e) {
417                 logger.warn("handleFunction() {}", e.getMessage());
418             } catch (IllegalArgumentException e) {
419                 logger.warn("handleFunction() Unsupported command {} for thing {}", command, getThing().getUID());
420                 return;
421             }
422         } else {
423             logger.warn("handleFunction() Unsupported command {} for thing {}", command, getThing().getUID());
424         }
425     }
426
427     @Override
428     protected void handleMessage(BaseOpenMessage msg) {
429         super.handleMessage(msg);
430         logger.debug("@@@@ Thermo.handleMessage(): {}", msg.toStringVerbose());
431         Thermoregulation tmsg = (Thermoregulation) msg;
432         if (isCentralUnit) {
433             WhatThermo tWhat = (WhatThermo) msg.getWhat();
434             if (tWhat == null) {
435                 logger.debug("handleMessage() Ignoring unsupported WHAT {}. Frame={}", tWhat, msg);
436                 return;
437             }
438             if (tWhat.value() > 40) {
439                 // it's a CU mode event, CU state events will follow shortly, so let's reset
440                 // their values
441                 resetCUState();
442             }
443             switch (tWhat.getType()) {
444                 case AT_LEAST_ONE_PROBE_ANTIFREEZE:
445                     cuAtLeastOneProbeProtection = true;
446                     break;
447                 case AT_LEAST_ONE_PROBE_MANUAL:
448                     cuAtLeastOneProbeManual = true;
449                     break;
450                 case AT_LEAST_ONE_PROBE_OFF:
451                     cuAtLeastOneProbeOff = true;
452                     break;
453                 case BATTERY_KO:
454                     cuBatteryStatus = CU_BATTERY_KO;
455                     break;
456                 case FAILURE_DISCOVERED:
457                     cuFailureDiscovered = true;
458                     break;
459                 case RELEASE_SENSOR_LOCAL_ADJUST:
460                     logger.debug("handleMessage(): Ignoring unsupported WHAT {}. Frame={}", tWhat, msg);
461                     break;
462                 case REMOTE_CONTROL_DISABLED:
463                     updateCURemoteControlStatus(CU_REMOTE_CONTROL_DISABLED);
464                     break;
465                 case REMOTE_CONTROL_ENABLED:
466                     updateCURemoteControlStatus(CU_REMOTE_CONTROL_ENABLED);
467                     break;
468                 default:
469                     // check and update values of other channels (mode, function, temp)
470                     updateModeAndFunction(tmsg);
471                     updateSetpoint(tmsg);
472                     break;
473             }
474             return;
475         }
476
477         if (tmsg.isCommand()) {
478             updateModeAndFunction(tmsg);
479         } else {
480             DimThermo dim = (DimThermo) tmsg.getDim();
481             switch (dim) {
482                 case TEMP_SETPOINT:
483                     updateSetpoint(tmsg);
484                     break;
485                 case COMPLETE_PROBE_STATUS:
486                     break;
487                 case PROBE_TEMPERATURE:
488                 case TEMPERATURE:
489                     updateTemperature(tmsg);
490                     break;
491                 case ACTUATOR_STATUS:
492                     updateActuatorStatus(tmsg);
493                     break;
494                 case FAN_COIL_SPEED:
495                     updateFanCoilSpeed(tmsg);
496                     break;
497                 case OFFSET:
498                     updateLocalOffset(tmsg);
499                     break;
500                 case VALVES_STATUS:
501                     updateValveStatus(tmsg);
502                     break;
503                 default:
504                     logger.debug("handleMessage() Ignoring unsupported DIM {} for thing {}. Frame={}", tmsg.getDim(),
505                             getThing().getUID(), tmsg);
506                     break;
507             }
508         }
509     }
510
511     private void updateModeAndFunction(Thermoregulation tmsg) {
512         if (tmsg.getWhat() == null) {
513             logger.warn("updateModeAndFunction() Could not parse What {} (WHAT is null)", tmsg.getFrameValue());
514             return;
515         }
516         Thermoregulation.WhatThermo what = tmsg.new WhatThermo(tmsg.getWhat().value());
517         if (what.getMode() == null) {
518             logger.warn("updateModeAndFunction() Could not parse Mode from: {}", tmsg.getFrameValue());
519             return;
520         }
521         if (what.getFunction() == null) {
522             logger.warn("updateModeAndFunction() Could not parse Function from: {}", tmsg.getFrameValue());
523             return;
524         }
525
526         // update Function if it's not GENERIC
527         Thermoregulation.Function function = what.getFunction();
528         if (function != Function.GENERIC) {
529             updateState(CHANNEL_FUNCTION, new StringType(function.toString()));
530             currentFunction = function;
531         }
532
533         // then update Mode
534         Thermoregulation.OperationMode operationMode = null;
535         if (what.getType() != WhatThermoType.HEATING && what.getType() != WhatThermoType.CONDITIONING) {
536             // *4*1*z## and *4*0*z## do not tell us which mode is the zone now
537             operationMode = what.getMode();
538         }
539
540         // set ProgramNumber/vacationDays channels when necessary
541         if (operationMode != null) {
542             switch (operationMode) {
543                 case VACATION:
544                     updateVacationDays(tmsg);
545                     break;
546                 case WEEKLY:
547                 case SCENARIO:
548                     updateProgramNumber(tmsg);
549                     break;
550                 default:
551                     break;
552             }
553             if (!isCentralUnit && !(operationMode == OperationMode.AUTO || operationMode == OperationMode.OFF
554                     || operationMode == OperationMode.PROTECTION || operationMode == OperationMode.MANUAL)) {
555                 logger.warn("updateModeAndFunction() Unsupported mode for zone {} from message: {}",
556                         getThing().getUID(), tmsg.getFrameValue());
557                 return;
558             } else {
559                 updateState(CHANNEL_MODE, new StringType(operationMode.name()));
560                 currentMode = operationMode;
561             }
562         } else {
563             logger.debug("updateModeAndFunction() Unrecognized mode from message: {}", tmsg.getFrameValue());
564         }
565     }
566
567     private void updateVacationDays(Thermoregulation tmsg) {
568         @Nullable
569         Integer vDays = ((WhatThermo) tmsg.getWhat()).vacationDays();
570         if (vDays != null) {
571             logger.debug("{} - updateVacationDays() set VACATION DAYS to: {}", getThing().getUID(), vDays);
572             updateState(CHANNEL_CU_VACATION_DAYS, new DecimalType(vDays));
573             currentVacationDays = vDays;
574         }
575     }
576
577     private void updateProgramNumber(Thermoregulation tmsg) {
578         @Nullable
579         WhatThermo wt = (WhatThermo) tmsg.getWhat();
580         if (wt == null) {
581             return;
582         }
583         Integer prNum = wt.programNumber();
584         if (prNum != null) {
585             if (wt.getMode() == OperationMode.SCENARIO) {
586                 logger.debug("{} - updateProgramNumber() set SCENARIO program to: {}", getThing().getUID(), prNum);
587                 updateState(CHANNEL_CU_SCENARIO_PROGRAM_NUMBER, new DecimalType(prNum));
588                 currentScenarioPrgNum = prNum;
589             } else if (wt.getMode() == OperationMode.WEEKLY) {
590                 logger.debug("{} - updateProgramNumber() set WEEKLY program to: {}", getThing().getUID(), prNum);
591                 updateState(CHANNEL_CU_WEEKLY_PROGRAM_NUMBER, new DecimalType(prNum));
592                 currentWeeklyPrgNum = prNum;
593             }
594         }
595     }
596
597     private void updateTemperature(Thermoregulation tmsg) {
598         try {
599             double temp = Thermoregulation.parseTemperature(tmsg);
600             updateState(CHANNEL_TEMPERATURE, getAsQuantityTypeOrNull(temp, SIUnits.CELSIUS));
601         } catch (FrameException e) {
602             logger.warn("updateTemperature() FrameException on frame {}: {}", tmsg, e.getMessage());
603             updateState(CHANNEL_TEMPERATURE, UnDefType.UNDEF);
604         }
605     }
606
607     private void updateSetpoint(Thermoregulation tmsg) {
608         try {
609             double newTemp = -1;
610             if (isCentralUnit) {
611                 WhatThermo tw = (WhatThermo) tmsg.getWhat();
612                 if (tw == null) {
613                     logger.warn("updateSetpoint() Could not parse what from {} (what is null)", tmsg.getFrameValue());
614                     return;
615                 }
616                 String[] parameters = tmsg.getWhatParams();
617                 if (parameters.length > 0 && tw.getType() == WhatThermoType.MANUAL) {
618                     // manual setpoint frame should be like *4*110#TTTT*#0##
619                     newTemp = Thermoregulation.decodeTemperature(parameters[0]);
620                     logger.debug("updateSetpoint() parsed temperature from {}: {} ---> {}", tmsg.toStringVerbose(),
621                             parameters[0], newTemp);
622                 }
623             } else {
624                 newTemp = Thermoregulation.parseTemperature(tmsg);
625             }
626             if (newTemp > 0) {
627                 updateState(CHANNEL_TEMP_SETPOINT, getAsQuantityTypeOrNull(newTemp, SIUnits.CELSIUS));
628                 currentSetPointTemp = newTemp;
629             }
630         } catch (NumberFormatException e) {
631             logger.warn("updateSetpoint() NumberFormatException on frame {}: {}", tmsg, e.getMessage());
632             updateState(CHANNEL_TEMP_SETPOINT, UnDefType.UNDEF);
633         } catch (FrameException e) {
634             logger.warn("updateSetpoint() FrameException on frame {}: {}", tmsg, e.getMessage());
635             updateState(CHANNEL_TEMP_SETPOINT, UnDefType.UNDEF);
636         }
637     }
638
639     private void updateFanCoilSpeed(Thermoregulation tmsg) {
640         try {
641             Thermoregulation.FanCoilSpeed speed = Thermoregulation.parseFanCoilSpeed(tmsg);
642             updateState(CHANNEL_FAN_SPEED, new StringType(speed.toString()));
643         } catch (NumberFormatException e) {
644             logger.warn("updateFanCoilSpeed() NumberFormatException on frame {}: {}", tmsg, e.getMessage());
645             updateState(CHANNEL_FAN_SPEED, UnDefType.UNDEF);
646         } catch (FrameException e) {
647             logger.warn("updateFanCoilSpeed() FrameException on frame {}: {}", tmsg, e.getMessage());
648             updateState(CHANNEL_FAN_SPEED, UnDefType.UNDEF);
649         }
650     }
651
652     private void updateValveStatus(Thermoregulation tmsg) {
653         try {
654             Thermoregulation.ValveOrActuatorStatus cv = Thermoregulation.parseValveStatus(tmsg,
655                     Thermoregulation.WhatThermoType.CONDITIONING);
656             updateState(CHANNEL_CONDITIONING_VALVES, new StringType(cv.toString()));
657
658             Thermoregulation.ValveOrActuatorStatus hv = Thermoregulation.parseValveStatus(tmsg,
659                     Thermoregulation.WhatThermoType.HEATING);
660             updateState(CHANNEL_HEATING_VALVES, new StringType(hv.toString()));
661         } catch (FrameException e) {
662             logger.warn("updateValveStatus() FrameException on frame {}: {}", tmsg, e.getMessage());
663             updateState(CHANNEL_CONDITIONING_VALVES, UnDefType.UNDEF);
664             updateState(CHANNEL_HEATING_VALVES, UnDefType.UNDEF);
665         }
666     }
667
668     private void updateActuatorStatus(Thermoregulation tmsg) {
669         try {
670             Thermoregulation.ValveOrActuatorStatus hv = Thermoregulation.parseActuatorStatus(tmsg);
671             updateState(CHANNEL_ACTUATORS, new StringType(hv.toString()));
672         } catch (FrameException e) {
673             logger.warn("updateActuatorStatus() FrameException on frame {}: {}", tmsg, e.getMessage());
674             updateState(CHANNEL_ACTUATORS, UnDefType.UNDEF);
675         }
676     }
677
678     private void updateLocalOffset(Thermoregulation tmsg) {
679         try {
680             Thermoregulation.LocalOffset offset = Thermoregulation.parseLocalOffset(tmsg);
681             updateState(CHANNEL_LOCAL_OFFSET, new StringType(offset.toString()));
682             logger.debug("updateLocalOffset() {}: {}", tmsg, offset.toString());
683
684         } catch (FrameException e) {
685             logger.warn("updateLocalOffset() FrameException on frame {}: {}", tmsg, e.getMessage());
686             updateState(CHANNEL_LOCAL_OFFSET, UnDefType.UNDEF);
687         }
688     }
689
690     private void updateCURemoteControlStatus(String status) {
691         updateState(CHANNEL_CU_REMOTE_CONTROL, new StringType(status));
692         logger.debug("updateCURemoteControlStatus(): {}", status);
693     }
694
695     private void resetCUState() {
696         logger.debug("########### resetting CU state");
697         cuAtLeastOneProbeOff = false;
698         cuAtLeastOneProbeProtection = false;
699         cuAtLeastOneProbeManual = false;
700         cuBatteryStatus = CU_BATTERY_OK;
701         cuFailureDiscovered = false;
702
703         cuStateChannelsUpdateSchedule = scheduler.schedule(() -> {
704             updateCUStateChannels();
705         }, CU_STATE_CHANNELS_UPDATE_DELAY, TimeUnit.MILLISECONDS);
706     }
707
708     private void updateCUStateChannels() {
709         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_OFF, OnOffType.from(cuAtLeastOneProbeOff));
710         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_PROTECTION, OnOffType.from(cuAtLeastOneProbeProtection));
711         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_MANUAL, OnOffType.from(cuAtLeastOneProbeManual));
712         updateState(CHANNEL_CU_BATTERY_STATUS, new StringType(cuBatteryStatus));
713         updateState(CHANNEL_CU_FAILURE_DISCOVERED, OnOffType.from(cuFailureDiscovered));
714     }
715
716     private Boolean channelExists(String channelID) {
717         return thing.getChannel(channelID) != null;
718     }
719
720     @Override
721     protected void refreshDevice(boolean refreshAll) {
722         logger.debug("--- refreshDevice() : refreshing SINGLE... ({})", thing.getUID());
723
724         Where w = deviceWhere;
725         if (w == null) {
726             logger.warn("refreshDevice() Where is null for thing {}", getThing().getUID());
727             return;
728         }
729
730         String whereStr = w.value();
731
732         if (isCentralUnit) {
733             try {
734                 send(Thermoregulation.requestStatus(getWhere(whereStr)));
735             } catch (OWNException e) {
736                 logger.warn("refreshDevice() central unit returned OWNException {}", e.getMessage());
737             }
738             return;
739         }
740
741         try {
742             send(Thermoregulation.requestTemperature(whereStr));
743
744             if (!((WhereThermo) w).isProbe()) {
745                 // for bus_thermo_zone request also other single channels updates
746                 send(Thermoregulation.requestSetPointTemperature(whereStr));
747                 send(Thermoregulation.requestMode(whereStr));
748
749                 // refresh ONLY subscribed channels
750                 if (channelExists(CHANNEL_FAN_SPEED)) {
751                     send(Thermoregulation.requestFanCoilSpeed(whereStr));
752                 }
753                 if (channelExists(CHANNEL_CONDITIONING_VALVES) || channelExists(CHANNEL_HEATING_VALVES)) {
754                     send(Thermoregulation.requestValvesStatus(whereStr));
755                 }
756                 if (channelExists(CHANNEL_ACTUATORS)) {
757                     send(Thermoregulation.requestActuatorsStatus(whereStr));
758                 }
759                 if (channelExists(CHANNEL_LOCAL_OFFSET)) {
760                     send(Thermoregulation.requestLocalOffset(whereStr));
761                 }
762             }
763         } catch (OWNException e) {
764             logger.warn("refreshDevice() where='{}' returned OWNException {}", whereStr, e.getMessage());
765         }
766     }
767
768     @Override
769     public void dispose() {
770         ScheduledFuture<?> s = cuStateChannelsUpdateSchedule;
771         if (s != null) {
772             s.cancel(false);
773             logger.debug("dispose() - scheduler stopped.");
774         }
775         super.dispose();
776     }
777 }