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



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


AsyncWebServer server(80);

ESP8266React    esp8266React(&server);
FanStateService fanStateService = FanStateService(&server,esp8266React.getFS(),esp8266React.getSecurityManager());


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();
*/



  // pinMode(14,INPUT); // 14 = D5
  // attachInterrupt(digitalPinToInterrupt(14), handleInterrupt, RISING);

  Serial.println("================= SETUP ================== ");
  Serial.println("Configure pin " + (String)fanStateService.getTachGPIO());
  pinMode(fanStateService.getTachGPIO(),INPUT);
  attachInterrupt(fanStateService.getTachGPIO(),handleInterrupt,RISING);

  Serial.println("================= END ================== ");


  // 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();
}
