]> git.basschouten.com Git - openhab-addons.git/blob
2984d571831c01ed0709432731e879b982a107f3
[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 = null;
74     private Thermoregulation.@Nullable OperationMode currentMode = null;
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             } else {
314                 logger.info("handleSetpoint() Setpoint temperature must be between 5°C and 40°C for thing {}",
315                         getThing().getUID());
316             }
317         } else {
318             logger.warn("handleSetpoint() Unsupported command {} for thing {}", command, getThing().getUID());
319         }
320     }
321
322     private void handleMode(Command command) {
323         if (command instanceof StringType) {
324             Where w = deviceWhere;
325             if (w == null) {
326                 logger.warn("handleMode() Where is null for thing {}", getThing().getUID());
327                 return;
328             }
329
330             String whStr = getWhere(w.value());
331             String cmdStr = command.toString().toUpperCase();
332             OperationMode newMode;
333             try {
334                 newMode = OperationMode.valueOf(cmdStr);
335             } catch (IllegalArgumentException e) {
336                 logger.warn("handleMode() Unsupported command {} for thing {}", command, getThing().getUID());
337                 return;
338             }
339             Thermoregulation tMsg = null;
340             if (isCentralUnit) { // CU accepted modes: MANUAL, OFF, PROTECTION, WEEKLY, SCENARIO, HOLIDAY,
341                                  // VACATION
342                 switch (newMode) {
343                     case MANUAL:
344                     case OFF:
345                     case PROTECTION:
346                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, currentFunction, currentSetPointTemp);
347                         break;
348                     case WEEKLY:
349                     case SCENARIO:
350                         int programNumber = (MODE_WEEKLY.equals(cmdStr) ? currentWeeklyPrgNum : currentScenarioPrgNum);
351                         tMsg = Thermoregulation.requestWriteWeeklyScenarioMode(whStr, newMode, currentFunction,
352                                 programNumber);
353                         break;
354                     case HOLIDAY:
355                         tMsg = Thermoregulation.requestWriteHolidayMode(whStr, currentFunction, currentWeeklyPrgNum);
356                         break;
357                     case VACATION:
358                         tMsg = Thermoregulation.requestWriteVacationMode(whStr, currentFunction, currentVacationDays,
359                                 currentWeeklyPrgNum);
360                         break;
361                     default:
362                         logger.warn("handleMode() Unsupported command {} for CU thing {}", command,
363                                 getThing().getUID());
364                 }
365             } else {// Zone accepted modes: MANUAL, OFF, PROTECTION, AUTO
366                 switch (newMode) {
367                     case MANUAL:
368                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, currentFunction, currentSetPointTemp);
369                         break;
370                     case OFF:
371                     case PROTECTION:
372                     case AUTO:
373                         tMsg = Thermoregulation.requestWriteMode(whStr, newMode, Function.GENERIC, currentSetPointTemp);
374                         break;
375                     default:
376                         logger.warn("handleMode() Unsupported command {} for ZONE thing {}", command,
377                                 getThing().getUID());
378                 }
379             }
380
381             if (tMsg != null) {
382                 try {
383                     send(tMsg);
384                 } catch (OWNException e) {
385                     logger.warn("handleMode() {}", e.getMessage());
386                 }
387             } else {
388                 logger.warn("handleMode() Unsupported command {} for thing {}", command, getThing().getUID());
389             }
390         }
391     }
392
393     private String getWhere(String where) {
394         if (isCentralUnit) {
395             if (where.charAt(0) == '#') {
396                 return where;
397             } else { // to support old configurations for CU with where="0"
398                 return "#" + where;
399             }
400         } else {
401             return isStandAlone ? where : "#" + where;
402         }
403     }
404
405     private void handleFunction(Command command) {
406         if (command instanceof StringType) {
407             Where w = deviceWhere;
408             if (w == null) {
409                 logger.warn("handleFunction() Where is null for thing {}", getThing().getUID());
410                 return;
411             }
412             try {
413                 Thermoregulation.Function function = Thermoregulation.Function.valueOf(command.toString());
414                 send(Thermoregulation.requestWriteFunction(w.value(), function));
415             } catch (OWNException e) {
416                 logger.warn("handleFunction() {}", e.getMessage());
417             } catch (IllegalArgumentException e) {
418                 logger.warn("handleFunction() Unsupported command {} for thing {}", command, getThing().getUID());
419                 return;
420             }
421         } else {
422             logger.warn("handleFunction() Unsupported command {} for thing {}", command, getThing().getUID());
423         }
424     }
425
426     @Override
427     protected void handleMessage(BaseOpenMessage msg) {
428         super.handleMessage(msg);
429         logger.debug("@@@@ Thermo.handleMessage(): {}", msg.toStringVerbose());
430         Thermoregulation tmsg = (Thermoregulation) msg;
431         if (isCentralUnit) {
432             WhatThermo tWhat = (WhatThermo) msg.getWhat();
433             if (tWhat == null) {
434                 logger.debug("handleMessage() Ignoring unsupported WHAT {}. Frame={}", tWhat, msg);
435                 return;
436             }
437             if (tWhat.value() > 40) {
438                 // it's a CU mode event, CU state events will follow shortly, so let's reset
439                 // their values
440                 resetCUState();
441             }
442             switch (tWhat.getType()) {
443                 case AT_LEAST_ONE_PROBE_ANTIFREEZE:
444                     cuAtLeastOneProbeProtection = true;
445                     break;
446                 case AT_LEAST_ONE_PROBE_MANUAL:
447                     cuAtLeastOneProbeManual = true;
448                     break;
449                 case AT_LEAST_ONE_PROBE_OFF:
450                     cuAtLeastOneProbeOff = true;
451                     break;
452                 case BATTERY_KO:
453                     cuBatteryStatus = CU_BATTERY_KO;
454                     break;
455                 case FAILURE_DISCOVERED:
456                     cuFailureDiscovered = true;
457                     break;
458                 case RELEASE_SENSOR_LOCAL_ADJUST:
459                     logger.debug("handleMessage(): Ignoring unsupported WHAT {}. Frame={}", tWhat, msg);
460                     break;
461                 case REMOTE_CONTROL_DISABLED:
462                     updateCURemoteControlStatus(CU_REMOTE_CONTROL_DISABLED);
463                     break;
464                 case REMOTE_CONTROL_ENABLED:
465                     updateCURemoteControlStatus(CU_REMOTE_CONTROL_ENABLED);
466                     break;
467                 default:
468                     // check and update values of other channels (mode, function, temp)
469                     updateModeAndFunction(tmsg);
470                     updateSetpoint(tmsg);
471                     break;
472             }
473             return;
474         }
475
476         if (tmsg.isCommand()) {
477             updateModeAndFunction(tmsg);
478         } else {
479             DimThermo dim = (DimThermo) tmsg.getDim();
480             switch (dim) {
481                 case TEMP_SETPOINT:
482                     updateSetpoint(tmsg);
483                     break;
484                 case COMPLETE_PROBE_STATUS:
485                     updateTargetTemperature(tmsg);
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
518         if (what.getFunction() == null) {
519             logger.warn("updateModeAndFunction() Could not parse Function from: {}", tmsg.getFrameValue());
520             return;
521         }
522         // update Function if it's not GENERIC
523         Thermoregulation.Function function = what.getFunction();
524         if (function != Function.GENERIC) {
525             if (currentFunction != function) {
526                 updateState(CHANNEL_FUNCTION, new StringType(function.toString()));
527                 currentFunction = function;
528             }
529         }
530         if (what.getType() == WhatThermoType.HEATING || what.getType() == WhatThermoType.CONDITIONING
531                 || what.getType() == WhatThermoType.GENERIC) {
532             // *4*1*z## and *4*0*z## do not tell us which mode is the zone now, so let's
533             // skip
534             return;
535         }
536
537         // update Mode
538         Thermoregulation.OperationMode operationMode = null;
539
540         operationMode = what.getMode();
541
542         if (operationMode != null) {
543             // set ProgramNumber/vacationDays channels when necessary
544             switch (operationMode) {
545                 case VACATION:
546                     updateVacationDays(tmsg);
547                     break;
548                 case WEEKLY:
549                 case SCENARIO:
550                     updateProgramNumber(tmsg);
551                     break;
552                 default:
553                     break;
554             }
555             if (!isCentralUnit && !(operationMode == OperationMode.AUTO || operationMode == OperationMode.OFF
556                     || operationMode == OperationMode.PROTECTION || operationMode == OperationMode.MANUAL)) {
557                 logger.warn("updateModeAndFunction() Unsupported mode for zone {} from message: {}",
558                         getThing().getUID(), tmsg.getFrameValue());
559                 return;
560             } else {
561                 if (currentMode != operationMode) {
562                     updateState(CHANNEL_MODE, new StringType(operationMode.name()));
563                     currentMode = operationMode;
564                 }
565             }
566         } else {
567             logger.warn("updateModeAndFunction() Unrecognized mode from message: {}", tmsg.getFrameValue());
568         }
569     }
570
571     private void updateVacationDays(Thermoregulation tmsg) {
572         @Nullable
573         Integer vDays = ((WhatThermo) tmsg.getWhat()).vacationDays();
574         if (vDays != null) {
575             logger.debug("{} - updateVacationDays() set VACATION DAYS to: {}", getThing().getUID(), vDays);
576             updateState(CHANNEL_CU_VACATION_DAYS, new DecimalType(vDays));
577             currentVacationDays = vDays;
578         }
579     }
580
581     private void updateProgramNumber(Thermoregulation tmsg) {
582         @Nullable
583         WhatThermo wt = (WhatThermo) tmsg.getWhat();
584         if (wt == null) {
585             return;
586         }
587         Integer prNum = wt.programNumber();
588         if (prNum != null) {
589             if (wt.getMode() == OperationMode.SCENARIO) {
590                 logger.debug("{} - updateProgramNumber() set SCENARIO program to: {}", getThing().getUID(), prNum);
591                 updateState(CHANNEL_CU_SCENARIO_PROGRAM_NUMBER, new DecimalType(prNum));
592                 currentScenarioPrgNum = prNum;
593             } else if (wt.getMode() == OperationMode.WEEKLY) {
594                 logger.debug("{} - updateProgramNumber() set WEEKLY program to: {}", getThing().getUID(), prNum);
595                 updateState(CHANNEL_CU_WEEKLY_PROGRAM_NUMBER, new DecimalType(prNum));
596                 currentWeeklyPrgNum = prNum;
597             }
598         }
599     }
600
601     private void updateTemperature(Thermoregulation tmsg) {
602         try {
603             double temp = Thermoregulation.parseTemperature(tmsg);
604             updateState(CHANNEL_TEMPERATURE, getAsQuantityTypeOrNull(temp, SIUnits.CELSIUS));
605         } catch (FrameException e) {
606             logger.warn("updateTemperature() FrameException on frame {}: {}", tmsg, e.getMessage());
607             updateState(CHANNEL_TEMPERATURE, UnDefType.UNDEF);
608         }
609     }
610
611     private void updateTargetTemperature(Thermoregulation tmsg) {
612         try {
613             double temp = Thermoregulation.parseTemperature(tmsg);
614             updateState(CHANNEL_TEMP_TARGET, getAsQuantityTypeOrNull(temp, SIUnits.CELSIUS));
615         } catch (FrameException e) {
616             logger.warn("updateTargetTemperature() FrameException on frame {}: {}", tmsg, e.getMessage());
617             updateState(CHANNEL_TEMP_TARGET, UnDefType.UNDEF);
618         }
619     }
620
621     private void updateSetpoint(Thermoregulation tmsg) {
622         try {
623             double newTemp = -1;
624             if (isCentralUnit) {
625                 WhatThermo tw = (WhatThermo) tmsg.getWhat();
626                 if (tw == null) {
627                     logger.warn("updateSetpoint() Could not parse what from {} (what is null)", tmsg.getFrameValue());
628                     return;
629                 }
630                 String[] parameters = tmsg.getWhatParams();
631                 if (parameters.length > 0 && tw.getType() == WhatThermoType.MANUAL) {
632                     // manual setpoint frame should be like *4*110#TTTT*#0##
633                     newTemp = Thermoregulation.decodeTemperature(parameters[0]);
634                     logger.debug("updateSetpoint() parsed temperature from {}: {} ---> {}", tmsg.toStringVerbose(),
635                             parameters[0], newTemp);
636                 }
637             } else {
638                 newTemp = Thermoregulation.parseTemperature(tmsg);
639             }
640             if (newTemp > 0) {
641                 updateState(CHANNEL_TEMP_SETPOINT, getAsQuantityTypeOrNull(newTemp, SIUnits.CELSIUS));
642                 currentSetPointTemp = newTemp;
643             }
644         } catch (NumberFormatException e) {
645             logger.warn("updateSetpoint() NumberFormatException on frame {}: {}", tmsg, e.getMessage());
646             updateState(CHANNEL_TEMP_SETPOINT, UnDefType.UNDEF);
647         } catch (FrameException e) {
648             logger.warn("updateSetpoint() FrameException on frame {}: {}", tmsg, e.getMessage());
649             updateState(CHANNEL_TEMP_SETPOINT, UnDefType.UNDEF);
650         }
651     }
652
653     private void updateFanCoilSpeed(Thermoregulation tmsg) {
654         try {
655             Thermoregulation.FanCoilSpeed speed = Thermoregulation.parseFanCoilSpeed(tmsg);
656             updateState(CHANNEL_FAN_SPEED, new StringType(speed.toString()));
657         } catch (NumberFormatException e) {
658             logger.warn("updateFanCoilSpeed() NumberFormatException on frame {}: {}", tmsg, e.getMessage());
659             updateState(CHANNEL_FAN_SPEED, UnDefType.UNDEF);
660         } catch (FrameException e) {
661             logger.warn("updateFanCoilSpeed() FrameException on frame {}: {}", tmsg, e.getMessage());
662             updateState(CHANNEL_FAN_SPEED, UnDefType.UNDEF);
663         }
664     }
665
666     private void updateValveStatus(Thermoregulation tmsg) {
667         try {
668             Thermoregulation.ValveOrActuatorStatus cv = Thermoregulation.parseValveStatus(tmsg,
669                     Thermoregulation.WhatThermoType.CONDITIONING);
670             updateState(CHANNEL_CONDITIONING_VALVES, new StringType(cv.toString()));
671             updateHeatingCooling(false, cv);
672
673             Thermoregulation.ValveOrActuatorStatus hv = Thermoregulation.parseValveStatus(tmsg,
674                     Thermoregulation.WhatThermoType.HEATING);
675             updateState(CHANNEL_HEATING_VALVES, new StringType(hv.toString()));
676             updateHeatingCooling(true, hv);
677
678         } catch (FrameException e) {
679             logger.warn("updateValveStatus() FrameException on frame {}: {}", tmsg, e.getMessage());
680             updateState(CHANNEL_CONDITIONING_VALVES, UnDefType.UNDEF);
681             updateState(CHANNEL_HEATING_VALVES, UnDefType.UNDEF);
682         }
683     }
684
685     private void updateHeatingCooling(boolean heating, Thermoregulation.ValveOrActuatorStatus v) {
686         boolean state = false;
687         switch (v) {
688             case STOP:
689             case CLOSED:
690             case OFF:
691             case OFF_FAN_COIL:
692             case OFF_SPEED_1:
693             case OFF_SPEED_2:
694             case OFF_SPEED_3:
695                 state = false;
696                 break;
697             case ON:
698             case ON_FAN_COIL:
699             case ON_SPEED_1:
700             case ON_SPEED_2:
701             case ON_SPEED_3:
702             case OPENED:
703                 state = true;
704                 break;
705         }
706
707         if (heating) {
708             updateState(CHANNEL_HEATING, OnOffType.from(state));
709         } else {
710             updateState(CHANNEL_COOLING, OnOffType.from(state));
711         }
712     }
713
714     private void updateActuatorStatus(Thermoregulation tmsg) {
715         try {
716             Thermoregulation.ValveOrActuatorStatus hv = Thermoregulation.parseActuatorStatus(tmsg);
717             updateState(CHANNEL_ACTUATORS, new StringType(hv.toString()));
718         } catch (FrameException e) {
719             logger.warn("updateActuatorStatus() FrameException on frame {}: {}", tmsg, e.getMessage());
720             updateState(CHANNEL_ACTUATORS, UnDefType.UNDEF);
721         }
722     }
723
724     private void updateLocalOffset(Thermoregulation tmsg) {
725         try {
726             Thermoregulation.LocalOffset offset = Thermoregulation.parseLocalOffset(tmsg);
727             updateState(CHANNEL_LOCAL_OFFSET, new StringType(offset.toString()));
728             logger.debug("updateLocalOffset() {}: {}", tmsg, offset.toString());
729
730         } catch (FrameException e) {
731             logger.warn("updateLocalOffset() FrameException on frame {}: {}", tmsg, e.getMessage());
732             updateState(CHANNEL_LOCAL_OFFSET, UnDefType.UNDEF);
733         }
734     }
735
736     private void updateCURemoteControlStatus(String status) {
737         updateState(CHANNEL_CU_REMOTE_CONTROL, new StringType(status));
738         logger.debug("updateCURemoteControlStatus(): {}", status);
739     }
740
741     private void resetCUState() {
742         logger.debug("########### resetting CU state");
743         cuAtLeastOneProbeOff = false;
744         cuAtLeastOneProbeProtection = false;
745         cuAtLeastOneProbeManual = false;
746         cuBatteryStatus = CU_BATTERY_OK;
747         cuFailureDiscovered = false;
748
749         cuStateChannelsUpdateSchedule = scheduler.schedule(() -> {
750             updateCUStateChannels();
751         }, CU_STATE_CHANNELS_UPDATE_DELAY, TimeUnit.MILLISECONDS);
752     }
753
754     private void updateCUStateChannels() {
755         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_OFF, OnOffType.from(cuAtLeastOneProbeOff));
756         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_PROTECTION, OnOffType.from(cuAtLeastOneProbeProtection));
757         updateState(CHANNEL_CU_AT_LEAST_ONE_PROBE_MANUAL, OnOffType.from(cuAtLeastOneProbeManual));
758         updateState(CHANNEL_CU_BATTERY_STATUS, new StringType(cuBatteryStatus));
759         updateState(CHANNEL_CU_FAILURE_DISCOVERED, OnOffType.from(cuFailureDiscovered));
760     }
761
762     private Boolean channelExists(String channelID) {
763         return thing.getChannel(channelID) != null;
764     }
765
766     @Override
767     protected void refreshDevice(boolean refreshAll) {
768         logger.debug("--- refreshDevice() : refreshing SINGLE... ({})", thing.getUID());
769
770         Where w = deviceWhere;
771         if (w == null) {
772             logger.warn("refreshDevice() Where is null for thing {}", getThing().getUID());
773             return;
774         }
775
776         String whereStr = w.value();
777
778         if (isCentralUnit) {
779             try {
780                 send(Thermoregulation.requestStatus(getWhere(whereStr)));
781             } catch (OWNException e) {
782                 logger.warn("refreshDevice() central unit returned OWNException {}", e.getMessage());
783             }
784             return;
785         }
786
787         try {
788             send(Thermoregulation.requestTemperature(whereStr));
789
790             if (!((WhereThermo) w).isProbe()) {
791                 // for bus_thermo_zone request also other single channels updates
792                 send(Thermoregulation.requestSetPointTemperature(whereStr));
793                 send(Thermoregulation.requestMode(whereStr));
794
795                 // refresh ONLY subscribed channels
796                 if (channelExists(CHANNEL_FAN_SPEED)) {
797                     send(Thermoregulation.requestFanCoilSpeed(whereStr));
798                 }
799                 if (channelExists(CHANNEL_CONDITIONING_VALVES) || channelExists(CHANNEL_HEATING_VALVES)) {
800                     send(Thermoregulation.requestValvesStatus(whereStr));
801                 }
802                 if (channelExists(CHANNEL_ACTUATORS)) {
803                     send(Thermoregulation.requestActuatorsStatus(whereStr));
804                 }
805                 if (channelExists(CHANNEL_LOCAL_OFFSET)) {
806                     send(Thermoregulation.requestLocalOffset(whereStr));
807                 }
808             }
809         } catch (OWNException e) {
810             logger.warn("refreshDevice() where='{}' returned OWNException {}", whereStr, e.getMessage());
811         }
812     }
813
814     @Override
815     public void dispose() {
816         ScheduledFuture<?> s = cuStateChannelsUpdateSchedule;
817         if (s != null) {
818             s.cancel(false);
819             logger.debug("dispose() - scheduler stopped.");
820         }
821         super.dispose();
822     }
823 }