Skip to content

Commit

Permalink
EspHap integrated plus crone based schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurik72 committed Jan 27, 2020
1 parent c3d8989 commit c47fe43
Show file tree
Hide file tree
Showing 26 changed files with 1,982 additions and 56 deletions.
108 changes: 107 additions & 1 deletion HomeController/BME280Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ BME280Controller::BME280Controller() {
this->isinit = false;
this->temp_corr = 0.0;
this->hum_corr = 0.0;
#ifdef ENABLE_NATIVE_HAP
this->ishap=true;
this->hapservice_temp=NULL;
this->hapservice_hum=NULL;
this->hapservice_press=NULL;

this->hap_temp=NULL;
this->hap_hum=NULL;
this->hap_press=NULL;
#endif
}
String BME280Controller::serializestate() {

Expand Down Expand Up @@ -363,4 +373,100 @@ void BME280Controller::directmeassure(BMEState& state) {
state.hum =round(constrain( humidity,0.0,100.0)*100)/100;


}
}



#ifdef ENABLE_NATIVE_HAP

void BME280Controller::setup_hap_service(){

#ifdef BMECONTROLLER_DEBUG
DBG_OUTPUT_PORT.println("BME280Controller::setup_hap_service()");
#endif

if(!ishap)
return;
if(this->accessory_type>1){
#ifdef BMECONTROLLER_DEBUG
DBG_OUTPUT_PORT.println("BME280Controller adding as new accessory");
#endif
hap_add_temp_hum_as_accessory(this->accessory_type,this->get_name(),&this->hapservice_temp,&this->hapservice_hum);

}
else{
this->hapservice_temp=hap_add_temperature_service(this->get_name());
this->hapservice_hum=hap_add_humidity_service(this->get_name());
}
if(this->hapservice_temp)
this->hap_temp=homekit_service_characteristic_by_type(this->hapservice_temp, HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE);
if(this->hapservice_hum)
this->hap_hum=homekit_service_characteristic_by_type(this->hapservice_hum, HOMEKIT_CHARACTERISTIC_CURRENT_RELATIVE_HUMIDITY);




}
void BME280Controller::notify_hap(){
if(this->ishap && this->hapservice_temp && this->hap_temp){
BMEState newState=this->get_state();
if(this->hap_temp->value.float_value!=newState.temp){
this->hap_temp->value.float_value=newState.temp;
homekit_characteristic_notify(this->hap_temp,this->hap_temp->value);
}
}
if(this->ishap && this->hapservice_hum && this->hap_hum){
BMEState newState=this->get_state();
if(this->hap_hum->value.float_value!=newState.hum){
this->hap_hum->value.float_value=newState.hum;
homekit_characteristic_notify(this->hap_hum,this->hap_hum->value);
}
}

}


void BME280Controller::hap_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context){
DBG_OUTPUT_PORT.println("RGBStripController::hap_callback");

if(!context){
return;
};
/*
RGBStripController* ctl= (RGBStripController*)context;
RGBState newState=ctl->get_state();
RGBCMD cmd = On;
bool isSet=false;
if(ch==ctl->hap_on && ch->value.bool_value!=newState.isOn){
newState.isOn=ch->value.bool_value;
cmd =newState.isOn?On:Off;
isSet=true;
}
if(ch==ctl->hap_br && ch->value.int_value!=newState.get_br_100()){
newState.set_br_100(ch->value.int_value);
cmd=SetBrigthness;
isSet=true;
}
if(ch==ctl->hap_hue && ch->value.float_value!=ctl->mqtt_hue){
ctl->mqtt_hue=ch->value.float_value;
newState.color = HSVColor(ctl->mqtt_hue, ctl->mqtt_saturation, newState.brightness);
cmd=SetColor;
isSet=true;
DBG_OUTPUT_PORT.println("HUE");
DBG_OUTPUT_PORT.println(ch->value.float_value);
}
if(ch==ctl->hap_saturation && ch->value.float_value!=ctl->mqtt_saturation){
ctl->mqtt_saturation=ch->value.float_value;
newState.color = HSVColor(ctl->mqtt_hue, ctl->mqtt_saturation, newState.brightness);
cmd=SetColor;
isSet=true;
DBG_OUTPUT_PORT.println("Saturation");
DBG_OUTPUT_PORT.println(ch->value.float_value);
}
// newState.isOn=value.bool_value;
if(isSet)
ctl->AddCommand(newState, cmd, srcHAP);
*/
}
#endif

24 changes: 23 additions & 1 deletion HomeController/BME280Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#include "BaseController.h"
#ifdef ENABLE_NATIVE_HAP
extern "C"{
#include "homeintegration.h"
}
#endif

class Adafruit_BME280;
struct BMEState
Expand Down Expand Up @@ -34,6 +39,13 @@ class BME280Controller : public BME

virtual bool onpublishmqtt(String& endkey, String& payload);
virtual void onmqqtmessage(String topic, String payload);
#ifdef ENABLE_NATIVE_HAP
virtual void setup_hap_service();
static void hap_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context);

virtual void notify_hap();

#endif
protected:
void meassure(BMEState& state);
void directmeassure(BMEState& state);
Expand All @@ -44,7 +56,17 @@ class BME280Controller : public BME
double temp_corr;
double hum_corr;
Adafruit_BME280* pbme;
#ifdef ENABLE_NATIVE_HAP
homekit_service_t* hapservice_temp;
homekit_characteristic_t * hap_temp;

homekit_service_t* hapservice_hum;
homekit_characteristic_t * hap_hum;

homekit_service_t* hapservice_press;
homekit_characteristic_t * hap_press;
#endif
};
DEFINE_CONTROLLER_FACTORY(BME280Controller)

#endif
#endif
6 changes: 6 additions & 0 deletions HomeController/BaseController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ CBaseController::CBaseController() {
this->priority = 1;
this->isforcedinterval = false;
this->statemon = false;
this->accessory_type=1;
this->ishap=false;

#if defined(ESP8266)
this->pTicker = NULL;;
Expand Down Expand Up @@ -274,7 +276,11 @@ void CBaseController::loadconfigbase(JsonObject& json) {
enabled = json["enabled"];
statemon= json["statemon"];
loadif(repch, json, "repch");
loadif(ishap,json,"ishap");
#ifdef ENABLE_NATIVE_HAP
loadif(accessory_type,json,"acctype");

#endif
interval= json["interval"].as<unsigned long>();
this->loadconfig(json);
}
Expand Down
18 changes: 16 additions & 2 deletions HomeController/BaseController.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ enum CmdSource
srcRestore=4,
srcPowerOn=5,
srcSmooth=6,
srcUserAction=7
srcUserAction=7,
srcHAP=8
};
enum BaseCMD :uint {
BaseOn=1,
Expand All @@ -189,6 +190,7 @@ class CBaseController
{
public:
CBaseController();
virtual ~CBaseController(){};
friend class Controllers;
virtual String serializestate() = 0;
virtual bool deserializestate(String jsonstate, CmdSource src= srcState)=0;
Expand Down Expand Up @@ -238,12 +240,18 @@ class CBaseController
CoreMode get_coremode() { return coreMode; };

short get_core() { return core; };
bool get_ishap() { return ishap; };
short get_priority() { return priority; };
virtual bool ispersiststate() { return false; }
virtual void savestate() ;
virtual bool loadstate()=0;
String get_filename_state();
virtual void set_power_on() {};
#ifdef ENABLE_NATIVE_HAP
virtual void setup_hap_service(){};
virtual void notify_hap(){};

#endif
#if defined(ESP8266)
static void callback(CBaseController* self);
void oncallback();
Expand All @@ -256,6 +264,10 @@ class CBaseController
short core;
short priority;
int repch = -1;
bool ishap=false;
#ifdef ENABLE_NATIVE_HAP
int accessory_type;
#endif
#if defined(ESP8266)
Ticker* pTicker;
#endif
Expand Down Expand Up @@ -311,7 +323,7 @@ class CController:public CBaseController
return commands.GetSize();
}
virtual bool baseprocesscommands(command cmd) {
if (cmd.mode == BaseSaveState) {
if ((int)cmd.mode ==(int) BaseSaveState) {

this->savestate();
return true;
Expand Down Expand Up @@ -348,6 +360,8 @@ class CController:public CBaseController
}
void set_handler_statechange(func_onstatechange f) { handler_statechange = f; };
void add_eventshandler_statechange(func_onstatechange f) { events.Add(f); };


protected:
virtual int get_maxcommands() { return CONTROLLER_MAX_COMMANDS; }
void storestate(P state) { this->state = state; };
Expand Down
73 changes: 70 additions & 3 deletions HomeController/Controllers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Ticker mqttReconnectTimer;
#endif
#endif

#ifdef ENABLE_NATIVE_HAP
extern "C"{
#include "homeintegration.h"
}
#endif

static Controllers* _instance=NULL;

//REGISTER_CONTROLLER(RFController)
Expand Down Expand Up @@ -67,6 +73,12 @@ void Controllers::setup() {
DBG_OUTPUT_PORT.println("Controllers::setup");
// Factories::registerController("-", &global_LDRControllerFactory);
//DBG_OUTPUT_PORT.println(globlog);
#ifdef ENABLE_NATIVE_HAP
init_hap_storage();
set_callback_storage_change(storage_changed);
hap_setbase_accessorytype(accessory_type);
hap_initbase_accessory_service(HOSTNAME,"Yurik72","0","EspHapCtl",VERSION);
#endif
this->loadconfig();
connectmqtt();
for (int i = 0; i < this->GetSize(); i++) {
Expand All @@ -79,6 +91,11 @@ void Controllers::setup() {
}
ctl->set_power_on();
}
DBG_OUTPUT_PORT.println("Load services done ");
#ifdef ENABLE_NATIVE_HAP
DBG_OUTPUT_PORT.println("starting hap_init_homekit_server ");
hap_init_homekit_server();
#endif
}
void Controllers::loadconfig() {
String filename = "/services.json";
Expand Down Expand Up @@ -115,7 +132,9 @@ void Controllers::loadconfig() {
if (controller->statemon) {
this->pMonitor = controller;
}

#ifdef ENABLE_NATIVE_HAP
controller->setup_hap_service();
#endif

DBG_OUTPUT_PORT.print("Controllers added:");
DBG_OUTPUT_PORT.println(name);
Expand Down Expand Up @@ -461,16 +480,21 @@ void Controllers::checkandreconnectWifi() {
isConnectingMode = false;

//DBG_OUTPUT_PORT.println("Wifi Restored connection");
#ifdef ENABLE_HOMEBRIDGE
if (strlen(mqtt_host) > 0 && atoi(mqtt_port) > 0)
mqttReconnectTimer.attach(2, realconnectToMqtt);
#endif
}
}
}
}
void onstatechanged(CBaseController * ctl)
{


#ifdef ENABLE_NATIVE_HAP
if(ctl->get_ishap())
ctl-> notify_hap();
#endif
#ifdef ENABLE_HOMEBRIDGE
String endkey;
String payload;
Expand Down Expand Up @@ -579,4 +603,47 @@ void realconnectToMqtt() {

void Controllers::onWifiDisconnect() {
this->isWifiConnected = false;
}
}

#ifdef ENABLE_NATIVE_HAP
void init_hap_storage(){
DBG_OUTPUT_PORT.print("init_hap_storage");
String fname="/pair.dat";
File fsDAT=SPIFFS.open(fname, "r");
if(!fsDAT){
DBG_OUTPUT_PORT.println("Failed to read pair.dat");
return;
}
int size=hap_get_storage_size_ex();
char* buf=new char[size];
memset(buf,0xff,size);
int readed=fsDAT.readBytes(buf,size);
DBG_OUTPUT_PORT.print("Readed bytes ->");
DBG_OUTPUT_PORT.println(readed);
hap_init_storage_ex(buf,size);
fsDAT.close();
delete []buf;

}
void storage_changed(char * szstorage,int size){
//DBG_OUTPUT_PORT.println("storage changed");
//DBG_OUTPUT_PORT.println(szstorage);
//DBG_OUTPUT_PORT.println(size);
//DBG_OUTPUT_PORT.println(szstorage+7);
String fname="/pair.dat";
///char* x=new char[size];
//memset(x,0xff,size);
//memset(x+5,0x00,5);
//DBG_OUTPUT_PORT.println(x);
SPIFFS.remove(fname);
File fsDAT=SPIFFS.open(fname, "w+");
if(!fsDAT){
DBG_OUTPUT_PORT.println("Failed to open pair.dat");
return;
}
fsDAT.write((uint8_t*)szstorage, size);
//fsDAT.write((uint8_t*)x, size);
fsDAT.close();
}

#endif
5 changes: 4 additions & 1 deletion HomeController/Controllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

class Triggers;
class CBaseController;

#ifdef ENABLE_NATIVE_HAP
void init_hap_storage();
void storage_changed(char * szstorage,int size);
#endif
class Controllers :public CSimpleArray< CBaseController*>
{
public:
Expand Down
Loading

0 comments on commit c47fe43

Please sign in to comment.