#include <ESP8266React.h>
/*
#include <LightMqttSettingsService.h>
#include <LightStateService.h>
*/
#include <FanStateService.h>
#include <FanMqttSettingsService.h>


#define ONE_WIRE_BUS              14
#define SERIAL_BAUD_RATE          115200
#define PWM_PROBE_MS              5000


AsyncWebServer server(80);

ESP8266React    esp8266React(&server);

FanMqttSettingsService fanMqttSettingsService = FanMqttSettingsService(
                                                    &server, 
                                                    esp8266React.getFS(), 
                                                    esp8266React.getSecurityManager()
                                                    );
FanStateService        fanStateService        = FanStateService       (
                                                    &server, 
                                                    esp8266React.getFS(), 
                                                    esp8266React.getSecurityManager(),
                                                    esp8266React.getMqttClient(),
                                                    &fanMqttSettingsService
                                                    );



void ICACHE_RAM_ATTR handleInterrupt() {
  fanStateService.RPM++;
}




void setup() {
  // start serial and filesystem
  Serial.begin(SERIAL_BAUD_RATE);

  // start the framework and demo project
  esp8266React.begin();

  // load the initial light settings
  fanStateService.begin();

//  Serial.println(" FAN GPIO = " + fanStateService.fan);

/*
  // start the light service
  lightMqttSettingsService.begin();
*/
  fanMqttSettingsService.begin();

  pinMode(fanStateService.getTachGPIO(),INPUT);
  attachInterrupt(fanStateService.getTachGPIO(),handleInterrupt,RISING);

  // start the server
  server.begin();
}


void loop() {
  // run the framework's loop function

  if( ( millis() - fanStateService.lastpoll ) > PWM_PROBE_MS ) {
    fanStateService.compute();    
    fanStateService.SetLastPoll(millis());
    }

  esp8266React.loop();
}
