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 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
63 bool local = false) override;
64 void fillChannelConfig(void *channelConfig,
65 int *size,
66 uint8_t configType) override;
67
68 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
69 // value for specific channel type (i.e. to prefill durationMS field when
70 // required)
72
73 virtual void close(); // Sets target position to 100%
74 virtual void open(); // Sets target position to 0%
75 virtual void stop(); // Stop motor
76 virtual void moveUp(); // start opening roller shutter regardless of its
77 // position (keep motor going up)
78 virtual void moveDown(); // starts closing roller shutter regardless of its
79 // position (keep motor going down)
80
81 virtual void setTargetPosition(int newPosition);
82 void setCurrentPosition(int newPosition);
83 void setNotCalibrated();
84 // Sets calibration ongoing flag, by setting calibration timeout.
85 // calibrationTime = 1 is used to indicate ongoing calibration for
86 // devices without explicit calibration time setting
87 void setCalibrationOngoing(int calibrationTime = 1);
88 void setCalibrationFinished();
89
90 int getCurrentPosition() const;
91 int getTargetPosition() const;
92 // Get current roller shutter movement direction. Returns int value of
93 // enum Supla::Control::Directions
94 int getCurrentDirection() const;
95
96 void configComfortUpValue(uint8_t position);
97 void configComfortDownValue(uint8_t position);
98
99 void onInit() override;
100 void onLoadConfig(SuplaDeviceClass *sdc) override;
101 void saveConfig();
102 void onLoadState() override;
103 void onSaveState() override;
104 void purgeConfig() override;
105 void iterateAlways() override;
106
107 uint32_t getClosingTimeMs() const;
108 uint32_t getOpeningTimeMs() const;
109
110 void attach(Supla::Control::Button *up, Supla::Control::Button *down);
111
112 virtual void triggerCalibration();
113 void setCalibrationNeeded();
114 bool isCalibrationRequested() const;
115 bool isCalibrated() const;
116
117 void setRsConfigMotorUpsideDownEnabled(bool enable);
118 void setRsConfigButtonsUpsideDownEnabled(bool enable);
119 void setRsConfigTimeMarginEnabled(bool enable);
120
121 void setRsConfigMotorUpsideDownValue(uint8_t value);
122 void setRsConfigButtonsUpsideDownValue(uint8_t value);
123 void setRsConfigTimeMarginValue(int8_t value);
124
125 uint8_t getMotorUpsideDown() const;
126 uint8_t getButtonsUpsideDown() const;
127 int8_t getTimeMargin() const;
128
129 static void setRsStorageSaveDelay(int delayMs);
130
131 virtual bool inMove();
132 virtual bool isCalibrationInProgress() const;
133 bool isCalibrationFailed() const;
134 bool isCalibrationLost() const;
135 bool isMotorProblem() const;
136
137 bool isFunctionSupported(int32_t channelFunction) const;
138 bool isAutoCalibrationSupported() const;
139
140 void setOpenCloseTime(uint32_t newClosingTimeMs, uint32_t newOpeningTimeMs);
141
142 void setCalibrationFailed(bool value);
143 void setCalibrationLost(bool value);
144 void setMotorProblem(bool value);
145
146 protected:
147 bool lastDirectionWasOpen() const;
148 bool lastDirectionWasClose() const;
149
150 // returns true, when opening/closing time is setable from config
151 bool isTimeSettingAvailable() const;
152 bool getCalibrate() const;
153 void setCalibrate(bool value);
154
155 void printConfig() const;
156 void setupButtonActions();
157 uint32_t getTimeMarginValue(uint32_t fullTime) const;
158
159 uint8_t flags = 0;
160
161 uint8_t comfortDownValue = 20;
162 uint8_t comfortUpValue = 80;
163
164 Directions currentDirection = Directions::STOP_DIR; // stop, up, down
165 Directions lastDirection = Directions::STOP_DIR;
166
167 int8_t currentPosition = UNKNOWN_POSITION; // 0 - closed; 100 - opened
168 int8_t targetPosition = STOP_POSITION; // 0-100
169 int8_t lastPositionBeforeMovement = UNKNOWN_POSITION; // 0-100
170 bool newTargetPositionAvailable = false;
171
172 RollerShutterConfig rsConfig;
173
174 Supla::Control::Button *upButton = nullptr;
175 Supla::Control::Button *downButton = nullptr;
176
177 uint32_t closingTimeMs = 0;
178 uint32_t openingTimeMs = 0;
179 // Calibration time may be used also as indication of ongoing calibration,
180 // but without information about calibration lenght. In general:
181 // calibrationTime > 0 means that there is ongoing calibration
182 uint32_t calibrationTime = 0;
183 uint32_t lastUpdateTime = 0;
184
185 static int16_t rsStorageSaveDelay;
186};
187
188} // namespace Control
189} // namespace Supla
190
191
192#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:480
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition roller_shutter_interface.cpp:144
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition roller_shutter_interface.cpp:835
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition roller_shutter_interface.cpp:98
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition roller_shutter_interface.cpp:499
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition roller_shutter_interface.cpp:787
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition roller_shutter_interface.cpp:444
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition roller_shutter_interface.cpp:632
int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request) override
Handles CALCFG requests from server.
Definition roller_shutter_interface.cpp:534
Definition roller_shutter_interface.h:40
Definition proto.h:2987
Definition proto.h:2248
Definition proto.h:1169