C

Qt Quick Ultralite Thermostat Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

import QtQuick 2.15

QtObject {
    enum Status {
        Heating, Cooling, Off
    }

    enum FanLevel {
        FanOff, FanQuarter, FanHalf, FanThreeQuarters, FanFull
    }

    id: root
    property string name;
    property string floor;
    property int temperature : 69;
    property bool auto_
    property bool power: true
    property bool dryer
    property int fan
    property bool eco
    property bool streamer
    property int status

    property int leftHandleX: 10
    property int rightHandleX: 200

    function fanImage() : image {
        switch (fan) {
        case Room.FanOff:
            return "fan-off.png"
        case Room.FanQuarter:
            return "fan-1-on.png"
        case Room.FanHalf:
            return "fan-2-on.png"
        case Room.FanThreeQuarters:
            return "fan-3-on.png"
        case Room.FanFull:
            return "fan-4-on.png"
        default:
            return "fan-off.png"
        }
    }

    function fanOffImage() : image {
        return "fan-off.png"
    }

    function smallFanImage() : image {
        switch (fan) {
        case Room.FanOff:
            return "fan-off-small.png"
        case Room.FanQuarter:
            return "fan-1-on-small.png"
        case Room.FanHalf:
            return "fan-2-on-small.png"
        case Room.FanThreeQuarters:
            return "fan-3-on-small.png"
        case Room.FanFull:
            return "fan-4-on-small.png"
        default:
            return "fan-off-small.png"
        }
    }

    onPowerChanged: {
        if (!power) {
            status = Room.Off
        } else {
            status = Room.Heating
        }
    }

    onStatusChanged: {
        power = (status !== Room.Off)
    }

    property Timer statusChanger: Timer {
        running: true
        repeat: true
        interval: 5000
        onTriggered: {
            if (root.status == Room.Off) {
                return;
            }
            if (Math.random() < 0.3) {
                root.status = root.status == Room.Heating ? Room.Cooling : Room.Heating
            }
        }
    }
    property Timer tempChanger: Timer {
        running: true
        repeat: true
        interval: 3000
        onTriggered: {
            if (Math.random() < 0.3) {
                var min = Units.fahrenheitToTemperatureUnit(50);
                var max = Units.fahrenheitToTemperatureUnit(90);
                root.temperature += Math.random() < 0.5 ? -1 : 1
                if (root.temperature < min) { root.temperature = min }
                else if (root.temperature > max) { root.temperature = max }
            }
        }
    }
}