supla-device
Loading...
Searching...
No Matches
roller_shutter_interface.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_CONTROL_ROLLER_SHUTTER_INTERFACE_H_
20#define SRC_SUPLA_CONTROL_ROLLER_SHUTTER_INTERFACE_H_
21
22#include "../action_handler.h"
23#include "../channel_element.h"
24
25#define UNKNOWN_POSITION -1
26#define STOP_POSITION -2
27#define MOVE_UP_POSITION -3
28#define MOVE_DOWN_POSITION -4
29#define RS_DEFAULT_OPERATION_TIMEOUT_MS 60000
30
31namespace Supla {
32
33namespace Control {
34
35class Button;
36
37enum class Directions : uint8_t { STOP_DIR = 0, DOWN_DIR = 1, UP_DIR = 2 };
38
39#pragma pack(push, 1)
41 uint8_t motorUpsideDown = 1; // 0 - not set/not used, 1 - false, 2 - true
42 uint8_t buttonsUpsideDown = 1; // 0 - not set/not used, 1 - false, 2 - true
43 int8_t timeMargin = -1; // -1 default (device specific), 0 - not set/not used
44 // 1 - no margin,
45 // > 1 - 51% of opening/closing time added on extreme
46 // positions - value should be decremented by 1.
47 uint8_t visualizationType = 0; // 0 - default, other values depends on
48 // Cloud and App support
49};
50#pragma pack(pop)
51
52class RollerShutterInterface : public ChannelElement, public ActionHandler {
53 public:
54 RollerShutterInterface();
55
56 RollerShutterInterface(const RollerShutterInterface &) = delete;
57 RollerShutterInterface &operator=(const RollerShutterInterface &) = delete;
58
59 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
60 void handleAction(int event, int action) override;
62 uint8_t applyChannelConfig(TSD_ChannelConfig *result,
63 bool local = false) override;
64 void fillChannelConfig(void *channelConfig, int *size) override;
65
66 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
67 // value for specific channel type (i.e. to prefill durationMS field when
68 // required)
70
71 virtual void close(); // Sets target position to 100%
72 virtual void open(); // Sets target position to 0%
73 virtual void stop(); // Stop motor
74 virtual void moveUp(); // start opening roller shutter regardless of its
75 // position (keep motor going up)
76 virtual void moveDown(); // starts closing roller shutter regardless of its
77 // position (keep motor going down)
78
79 virtual void setTargetPosition(int newPosition);
80 void setCurrentPosition(int newPosition);
81 void setNotCalibrated();
82 // Sets calibration ongoing flag, by setting calibration timeout.
83 // calibrationTime = 1 is used to indicate ongoing calibration for
84 // devices without explicit calibration time setting
85 void setCalibrationOngoing(int calibrationTime = 1);
86 void setCalibrationFinished();
87
88 int getCurrentPosition() const;
89 int getTargetPosition() const;
90 // Get current roller shutter movement direction. Returns int value of
91 // enum Supla::Control::Directions
92 int getCurrentDirection() const;
93
94 void configComfortUpValue(uint8_t position);
95 void configComfortDownValue(uint8_t position);
96
97 void onInit() override;
98 void onLoadConfig(SuplaDeviceClass *sdc) override;
99 void saveConfig();
100 void onLoadState() override;
101 void onSaveState() override;
102 void purgeConfig() override;
103 void iterateAlways() override;
104
105 uint32_t getClosingTimeMs() const;
106 uint32_t getOpeningTimeMs() const;
107
108 void attach(Supla::Control::Button *up, Supla::Control::Button *down);
109
110 virtual void triggerCalibration();
111 void setCalibrationNeeded();
112 bool isCalibrationRequested() const;
113 bool isCalibrated() const;
114
115 void setRsConfigMotorUpsideDownEnabled(bool enable);
116 void setRsConfigButtonsUpsideDownEnabled(bool enable);
117 void setRsConfigTimeMarginEnabled(bool enable);
118
119 void setRsConfigMotorUpsideDownValue(uint8_t value);
120 void setRsConfigButtonsUpsideDownValue(uint8_t value);
121 void setRsConfigTimeMarginValue(int8_t value);
122
123 uint8_t getMotorUpsideDown() const;
124 uint8_t getButtonsUpsideDown() const;
125 int8_t getTimeMargin() const;
126
127 static void setRsStorageSaveDelay(int delayMs);
128
129 virtual bool inMove();
130 virtual bool isCalibrationInProgress() const;
131 bool isCalibrationFailed() const;
132 bool isCalibrationLost() const;
133 bool isMotorProblem() const;
134
135 bool isFunctionSupported(int32_t channelFunction) const;
136 bool isAutoCalibrationSupported() const;
137
138 void setOpenCloseTime(uint32_t newClosingTimeMs, uint32_t newOpeningTimeMs);
139
140 void setCalibrationFailed(bool value);
141 void setCalibrationLost(bool value);
142 void setMotorProblem(bool value);
143
144 protected:
145 bool lastDirectionWasOpen() const;
146 bool lastDirectionWasClose() const;
147
148 // returns true, when opening/closing time is setable from config
149 bool isTimeSettingAvailable() const;
150 bool getCalibrate() const;
151 void setCalibrate(bool value);
152
153 void printConfig() const;
154 void setupButtonActions();
155 uint32_t getTimeMarginValue(uint32_t fullTime) const;
156
157 uint8_t flags = 0;
158
159 uint8_t comfortDownValue = 20;
160 uint8_t comfortUpValue = 80;
161
162 Directions currentDirection = Directions::STOP_DIR; // stop, up, down
163 Directions lastDirection = Directions::STOP_DIR;
164
165 int8_t currentPosition = UNKNOWN_POSITION; // 0 - closed; 100 - opened
166 int8_t targetPosition = STOP_POSITION; // 0-100
167 int8_t lastPositionBeforeMovement = UNKNOWN_POSITION; // 0-100
168 bool newTargetPositionAvailable = false;
169
170 RollerShutterConfig rsConfig;
171
172 Supla::Control::Button *upButton = nullptr;
173 Supla::Control::Button *downButton = nullptr;
174
175 uint32_t closingTimeMs = 0;
176 uint32_t openingTimeMs = 0;
177 // Calibration time may be used also as indication of ongoing calibration,
178 // but without information about calibration lenght. In general:
179 // calibrationTime > 0 means that there is ongoing calibration
180 uint32_t calibrationTime = 0;
181 uint32_t lastUpdateTime = 0;
182
183 static int16_t rsStorageSaveDelay;
184};
185
186} // namespace Control
187} // namespace Supla
188
189
190#endif // SRC_SUPLA_CONTROL_ROLLER_SHUTTER_INTERFACE_H_
Definition SuplaDevice.h:93
Definition button.h:30
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition roller_shutter_interface.cpp:479
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition roller_shutter_interface.cpp:143
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition roller_shutter_interface.cpp:825
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition roller_shutter_interface.cpp:97
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition roller_shutter_interface.cpp:498
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition roller_shutter_interface.cpp:777
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition roller_shutter_interface.cpp:443
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition roller_shutter_interface.cpp:629
int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request) override
Handles CALCFG requests from server.
Definition roller_shutter_interface.cpp:533
Definition roller_shutter_interface.h:40
Definition proto.h:2912
Definition proto.h:2246
Definition proto.h:1167