1 #pragma once 2 3 #include "RFXtrx.h" 4 #include "../hardware/DomoticzHardware.h" 5 #include "Scheduler.h" 6 #include "EventSystem.h" 7 #include "NotificationSystem.h" 8 #include "Camera.h" 9 #include <deque> 10 #include "WindCalculation.h" 11 #include "TrendCalculator.h" 12 #include "StoppableTask.h" 13 #include "../tcpserver/TCPServer.h" 14 #include "concurrent_queue.h" 15 #include "../webserver/server_settings.hpp" 16 #ifdef ENABLE_PYTHON 17 # include "../hardware/plugins/PluginManager.h" 18 #endif 19 20 class MainWorker : public StoppableTask 21 { 22 public: 23 MainWorker(); 24 ~MainWorker(); 25 26 bool Start(); 27 bool Stop(); 28 29 void AddAllDomoticzHardware(); 30 void StopDomoticzHardware(); 31 void StartDomoticzHardware(); 32 void AddDomoticzHardware(CDomoticzHardwareBase *pHardware); 33 void RemoveDomoticzHardware(CDomoticzHardwareBase *pHardware); 34 void RemoveDomoticzHardware(int HwdId); 35 int FindDomoticzHardware(int HwdId); 36 int FindDomoticzHardwareByType(const _eHardwareTypes HWType); 37 CDomoticzHardwareBase* GetHardware(int HwdId); 38 CDomoticzHardwareBase* GetHardwareByIDType(const std::string &HwdId, const _eHardwareTypes HWType); 39 CDomoticzHardwareBase* GetHardwareByType(const _eHardwareTypes HWType); 40 41 void HeartbeatUpdate(const std::string &component, bool critical = true); 42 void HeartbeatRemove(const std::string &component); 43 void HeartbeatCheck(); 44 45 void SetWebserverSettings(const http::server::server_settings & settings); 46 std::string GetWebserverAddress(); 47 std::string GetWebserverPort(); 48 #ifdef WWW_ENABLE_SSL 49 void SetSecureWebserverSettings(const http::server::ssl_server_settings & ssl_settings); 50 std::string GetSecureWebserverPort(); 51 #endif 52 void DecodeRXMessage(const CDomoticzHardwareBase *pHardware, const uint8_t *pRXCommand, const char *defaultName, const int BatteryLevel); 53 void PushAndWaitRxMessage(const CDomoticzHardwareBase *pHardware, const uint8_t *pRXCommand, const char *defaultName, const int BatteryLevel); 54 55 bool SwitchLight(const std::string &idx, const std::string &switchcmd, const std::string &level, const std::string &color, const std::string &ooc, const int ExtraDelay, const std::string& User); 56 bool SwitchLight(const uint64_t idx, const std::string &switchcmd, const int level, const _tColor color, const bool ooc, const int ExtraDelay, const std::string& User); 57 bool SwitchLightInt(const std::vector<std::string> &sd, std::string switchcmd, int level, const _tColor color, const bool IsTesting, const std::string &User); 58 59 bool SwitchScene(const std::string &idx, const std::string &switchcmd, const std::string& User); 60 bool SwitchScene(const uint64_t idx, std::string switchcmd, const std::string& User); 61 void CheckSceneCode(const uint64_t DevRowIdx, const uint8_t dType, const uint8_t dSubType, const int nValue, const char* sValue, const std::string& User); 62 bool DoesDeviceActiveAScene(const uint64_t DevRowIdx, const int Cmnd); 63 64 bool SetSetPoint(const std::string &idx, const float TempValue); 65 bool SetSetPoint(const std::string &idx, const float TempValue, const std::string &newMode, const std::string &until); 66 bool SetSetPointInt(const std::vector<std::string> &sd, const float TempValue); 67 bool SetThermostatState(const std::string &idx, const int newState); 68 bool SetClock(const std::string &idx, const std::string &clockstr); 69 bool SetClockInt(const std::vector<std::string> &sd, const std::string &clockstr); 70 bool SetZWaveThermostatMode(const std::string &idx, const int tMode); 71 bool SetZWaveThermostatFanMode(const std::string &idx, const int fMode); 72 bool SetZWaveThermostatModeInt(const std::vector<std::string> &sd, const int tMode); 73 bool SetZWaveThermostatFanModeInt(const std::vector<std::string> &sd, const int fMode); 74 75 bool SwitchModal(const std::string &idx, const std::string &status, const std::string &action, const std::string &ooc, const std::string &until); 76 77 bool GetSunSettings(); 78 void LoadSharedUsers(); 79 80 void ForceLogNotificationCheck(); 81 82 bool RestartHardware(const std::string &idx); 83 84 bool AddHardwareFromParams( 85 const int ID, 86 const std::string &Name, 87 const bool Enabled, 88 const _eHardwareTypes Type, 89 const std::string &Address, const uint16_t Port, const std::string &SerialPort, 90 const std::string &Username, const std::string &Password, 91 const std::string &Extra, 92 const int Mode1, 93 const int Mode2, 94 const int Mode3, 95 const int Mode4, 96 const int Mode5, 97 const int Mode6, 98 const int DataTimeout, 99 const bool bDoStart 100 ); 101 102 void UpdateDomoticzSecurityStatus(const int iSecStatus); 103 void SetInternalSecStatus(); 104 bool GetSensorData(const uint64_t idx, int &nValue, std::string &sValue); 105 106 bool UpdateDevice(const int DevIdx, int nValue, std::string& sValue, const int signallevel = 12, const int batterylevel = 255, const bool parseTrigger = true); 107 bool UpdateDevice(const int HardwareID, const std::string &DeviceID, const int unit, const int devType, const int subType, int nValue, std::string &sValue, const int signallevel = 12, const int batterylevel = 255, const bool parseTrigger = true); 108 109 boost::signals2::signal<void(const int m_HwdID, const uint64_t DeviceRowIdx, const std::string &DeviceName, const uint8_t *pRXCommand)> sOnDeviceReceived; 110 boost::signals2::signal<void(const uint64_t SceneIdx, const std::string &SceneName)> sOnSwitchScene; 111 112 CScheduler m_scheduler; 113 CEventSystem m_eventsystem; 114 CNotificationSystem m_notificationsystem; 115 #ifdef ENABLE_PYTHON 116 Plugins::CPluginSystem m_pluginsystem; 117 #endif 118 CCameraHandler m_cameras; 119 bool m_bIgnoreUsernamePassword; 120 bool m_bHaveUpdate; 121 int m_iRevision; 122 123 std::string m_szSystemName; 124 std::string m_szDomoticzUpdateURL; 125 126 bool IsUpdateAvailable(const bool bIsForced = false); 127 bool StartDownloadUpdate(); 128 bool m_bHaveDownloadedDomoticzUpdate; 129 bool m_bHaveDownloadedDomoticzUpdateSuccessFull; 130 std::string m_UpdateStatusMessage; 131 132 void GetAvailableWebThemes(); 133 134 tcp::server::CTCPServer m_sharedserver; 135 std::string m_LastSunriseSet; 136 std::vector<int> m_SunRiseSetMins; 137 std::string m_DayLength; 138 std::vector<std::string> m_webthemes; 139 std::map<uint16_t, _tWindCalculator> m_wind_calculator; 140 std::map<uint64_t, _tTrendCalculator> m_trend_calculator; 141 142 time_t m_LastHeartbeat = 0; 143 std::string m_szLastSwitchUser; 144 private: 145 void HandleAutomaticBackups(); 146 uint64_t PerformRealActionFromDomoticzClient(const uint8_t *pRXCommand, CDomoticzHardwareBase **pOriginalHardware); 147 void HandleLogNotifications(); 148 std::map<std::string, std::pair<time_t, bool> > m_componentheartbeats; 149 std::mutex m_heartbeatmutex; 150 151 std::mutex m_decodeRXMessageMutex; 152 153 std::vector<int> m_devicestorestart; 154 155 bool m_bForceLogNotificationCheck; 156 157 int m_SecCountdown; 158 int m_SecStatus; 159 160 int m_ScheduleLastMinute; 161 int m_ScheduleLastHour; 162 //fix for hardware clock that sets time back/ford 163 time_t m_ScheduleLastMinuteTime; 164 time_t m_ScheduleLastHourTime; 165 time_t m_ScheduleLastDayTime; 166 167 168 std::mutex m_devicemutex; 169 170 std::string m_szDomoticzUpdateChecksumURL; 171 bool m_bDoDownloadDomoticzUpdate; 172 bool m_bStartHardware; 173 uint8_t m_hardwareStartCounter; 174 175 std::vector<CDomoticzHardwareBase*> m_hardwaredevices; 176 http::server::server_settings m_webserver_settings; 177 #ifdef WWW_ENABLE_SSL 178 http::server::ssl_server_settings m_secure_webserver_settings; 179 #endif 180 std::shared_ptr<std::thread> m_thread; 181 std::mutex m_mutex; 182 183 time_t m_LastUpdateCheck; 184 185 void Do_Work(); 186 void Heartbeat(); 187 void ParseRFXLogFile(); 188 bool WriteToHardware(const int HwdID, const char *pdata, const uint8_t length); 189 190 void OnHardwareConnected(CDomoticzHardwareBase *pHardware); 191 192 void WriteMessageStart(); 193 void WriteMessage(const char *szMessage); 194 void WriteMessage(const char *szMessage, bool linefeed); 195 void WriteMessageEnd(); 196 197 //message decoders 198 void decode_BateryLevel(bool bIsInPercentage, uint8_t level); 199 uint8_t get_BateryLevel(const _eHardwareTypes HwdType, bool bIsInPercentage, uint8_t level); 200 201 // RxMessage queue resources 202 volatile unsigned long m_rxMessageIdx; 203 std::shared_ptr<std::thread> m_rxMessageThread; 204 StoppableTask m_TaskRXMessage; 205 void Do_Work_On_Rx_Messages(); 206 struct _tRxQueueItem { 207 std::string Name; 208 int BatteryLevel; 209 unsigned long rxMessageIdx; 210 int hardwareId; 211 std::vector<uint8_t> vrxCommand; 212 boost::uint16_t crc; 213 queue_element_trigger* trigger; 214 }; 215 concurrent_queue<_tRxQueueItem> m_rxMessageQueue; 216 void UnlockRxMessageQueue(); 217 void PushRxMessage(const CDomoticzHardwareBase *pHardware, const uint8_t *pRXCommand, const char *defaultName, const int BatteryLevel); 218 void CheckAndPushRxMessage(const CDomoticzHardwareBase *pHardware, const uint8_t *pRXCommand, const char *defaultName, const int BatteryLevel, const bool wait); 219 void ProcessRXMessage(const CDomoticzHardwareBase *pHardware, const uint8_t *pRXCommand, const char *defaultName, const int BatteryLevel); //battery level: 0-100, 255=no battery, -1 = don't set 220 221 struct _tRxMessageProcessingResult { 222 std::string DeviceName; 223 uint64_t DeviceRowIdx; 224 bool bProcessBatteryValue; 225 }; 226 227 //(RFX) Message decoders 228 void decode_InterfaceMessage(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 229 void decode_InterfaceControl(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 230 void decode_UNDECODED(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 231 void decode_RecXmitMessage(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 232 void decode_Rain(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 233 void decode_Wind(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 234 void decode_Temp(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 235 void decode_Hum(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 236 void decode_TempHum(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 237 void decode_TempRain(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 238 void decode_UV(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 239 void decode_Lighting1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 240 void decode_Lighting2(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 241 void decode_Lighting3(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 242 void decode_Lighting4(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 243 void decode_Lighting5(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 244 void decode_Lighting6(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 245 void decode_Fan(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 246 void decode_Curtain(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 247 void decode_BLINDS1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 248 void decode_RFY(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 249 void decode_Security1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 250 void decode_Security2(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 251 void decode_Camera1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 252 void decode_Remote(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 253 void decode_Thermostat1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 254 void decode_Thermostat2(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 255 void decode_Thermostat3(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 256 void decode_Thermostat4(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 257 void decode_Radiator1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 258 void decode_Baro(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 259 void decode_TempHumBaro(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 260 void decode_TempBaro(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 261 void decode_DateTime(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 262 void decode_Current(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 263 void decode_Energy(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 264 void decode_Current_Energy(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 265 void decode_Gas(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 266 void decode_Water(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 267 void decode_Weight(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 268 void decode_RFXSensor(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 269 void decode_RFXMeter(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 270 void decode_P1MeterPower(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 271 void decode_P1MeterGas(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 272 void decode_YouLessMeter(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 273 void decode_AirQuality(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 274 void decode_FS20(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 275 void decode_Rego6XXTemp(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 276 void decode_Rego6XXValue(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 277 void decode_Usage(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 278 void decode_Lux(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 279 void decode_General(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult, const uint8_t SignalLevel = 12, const uint8_t BatteryLevel = 255); 280 void decode_GeneralSwitch(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 281 void decode_HomeConfort(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 282 void decode_Thermostat(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 283 void decode_Chime(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 284 void decode_BBQ(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 285 void decode_Power(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 286 void decode_ColorSwitch(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 287 void decode_evohome1(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 288 void decode_evohome2(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 289 void decode_evohome3(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 290 void decode_Cartelectronic(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 291 void decode_CartelectronicTIC(const CDomoticzHardwareBase* pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 292 void decode_CartelectronicEncoder(const CDomoticzHardwareBase* pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 293 void decode_ASyncPort(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 294 void decode_ASyncData(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 295 void decode_Weather(const CDomoticzHardwareBase *pHardware, const tRBUF *pResponse, _tRxMessageProcessingResult & procResult); 296 void decode_Solar(const CDomoticzHardwareBase *pHardware, const tRBUF* pResponse, _tRxMessageProcessingResult& procResult); 297 void decode_Hunter(const CDomoticzHardwareBase *pHardware, const tRBUF* pResponse, _tRxMessageProcessingResult& procResult); 298 }; 299 300 extern MainWorker m_mainworker; 301