<commit_before>#include <Arduino.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <ESP8266Wifi.h>
#include <SPI.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <RCSwitch.h>

#include "constants.h"

WiFiUDP ntpUDP;
NTPClient ntpClient(ntpUDP, NTP_SERVER, NTP_TIME_OFFSET, NTP_DEFAULT_SYNC);

RCSwitch rcswitch = RCSwitch();

typedef struct {
  char family;
  int group;
  int number;
} socketmapping_t;

socketmapping_t sockets[] = {
  socketmapping_t{'a', 1, 1},
  socketmapping_t{'a', 1, 2},
  socketmapping_t{'a', 1, 3}
};

bool socket1 = true;
bool ledOn = true;

time_t updateTimeFromNTP() {
  Serial.print("Refresh clock from ntp: ");
  Serial.print(ntpClient.getFormattedTime());

  Serial.print(", epoch time is: ");
  Serial.println(ntpClient.getEpochTime());

  return ntpClient.getEpochTime();
}

void pulse() {
  digitalWrite(D0, ledOn ? HIGH : LOW);
  ledOn = !ledOn;
}

void heartbeat() {
  Serial.print("Heartbeat @ ");
  Serial.print(ntpClient.getFormattedTime());
  Serial.print(", millis = ");
  Serial.println(millis());
}

void socketN(int n, bool on) {
  Serial.print("Turning socket ");
  Serial.print(n);
  Serial.print(on ? " on" : " off");

  if (on) {
    rcswitch.switchOn(sockets[n].family, sockets[n].group, sockets[n].number);
  } else {
    rcswitch.switchOff(sockets[n].family, sockets[n].group, sockets[n].number);
  }

  Serial.println(" ... done.");
}

void socket1PowerOn() { socketN(0, true); }
void socket2PowerOn() { socketN(1, true); }
void socket3PowerOn() { socketN(2, true); }
void socket1PowerOff() { socketN(0, false); }
void socket2PowerOff() { socketN(1, false); }
void socket3PowerOff() { socketN(2, false); }

void socket2Alternate() {
  Serial.print("Alternation: ");
  socketN(1, socket1);
  socket1 = !socket1;
}

void setup() {
  Serial.begin(9600);
  Serial.println();

  pinMode(D0, OUTPUT);

  rcswitch.enableTransmit(D2);
  delay(250);

  Serial.print("Attempting to connect to SSID ");
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();

  Serial.print("Connected to SSID ");
  Serial.print(WLAN_SSID);
  Serial.print(" @ ");
  Serial.println(WiFi.localIP());

  ntpClient.begin();
  ntpClient.update();

  Serial.println("Registering NTP update service ...");
  setSyncProvider(updateTimeFromNTP);
  setSyncInterval(60);

  Serial.println("Registering alarms ...");
  Alarm.timerRepeat(3, pulse);
  Alarm.timerRepeat(15, heartbeat);

  // Register triggers for babyphone station
  Alarm.alarmRepeat(12, 15, 0, socket1PowerOn);
  Alarm.alarmRepeat(18, 30, 0, socket1PowerOn);
  Alarm.alarmRepeat(10, 30, 0, socket1PowerOff);

  // Register triggers for night illumination socket
  Alarm.alarmRepeat(17, 30, 0, socket2PowerOn);
  Alarm.alarmRepeat(9, 0, 0, socket2PowerOff);

  // Register triggers for living room lamp
  Alarm.alarmRepeat(17, 30, 0, socket3PowerOn);
  Alarm.alarmRepeat(0, 30, 0, socket3PowerOff);<commit_msg>Refactor: introduce programmatic configuration<commit_after>#include <Arduino.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <ESP8266Wifi.h>
#include <SPI.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <RCSwitch.h>

#include "constants.h"

WiFiUDP ntpUDP;
NTPClient ntpClient(ntpUDP, NTP_SERVER, NTP_TIME_OFFSET, NTP_DEFAULT_SYNC);

RCSwitch rcswitch = RCSwitch();

typedef struct {
  char family;
  int group;
  int number;
} socket_t;

socket_t sockets[] = {
  socket_t{'a', 1, 1},
  socket_t{'a', 1, 2},
  socket_t{'a', 1, 3}
};

typedef struct {
  AlarmID_t alarm_id;
  String name;
  bool on;
  socket_t *socket;
  int hour;
  int minute;
  int second;
} socket_timer_t;

bool socket1 = true;
bool ledOn = true;
const int ILED = D4;
const int RCSWITCH_PIN = D3;

socket_timer_t timers[] = {
  socket_timer_t{dtINVALID_ALARM_ID, "Livingroom lamp ON", true, &sockets[0], 17, 30, 0},
  socket_timer_t{dtINVALID_ALARM_ID, "Livingroom lamp OFF", false, &sockets[0], 0, 1, 0},
  socket_timer_t{dtINVALID_ALARM_ID, "Night illumination ON", true, &sockets[1], 17, 30, 0},
  socket_timer_t{dtINVALID_ALARM_ID, "Night illumination OFF", false, &sockets[1], 9, 0, 0},
  socket_timer_t{dtINVALID_ALARM_ID, "Babyphone Station Power ON", true, &sockets[2], 18, 30, 0},
  socket_timer_t{dtINVALID_ALARM_ID, "Babyphone Station Power OFF", false, &sockets[2], 10, 0, 0},
};

time_t updateTimeFromNTP() {
  Serial.print("Refresh clock from ntp: ");
  Serial.print(ntpClient.getFormattedTime());

  Serial.print(", epoch time is: ");
  Serial.println(ntpClient.getEpochTime());

  return ntpClient.getEpochTime();
}

void pulse() {
  digitalWrite(ILED, LOW);
  Alarm.delay(50);
  digitalWrite(ILED, HIGH);
}

void execute_socket_timer(socket_timer_t *socket_timer) {
  Serial.print("Executing alarm '");
  Serial.print(socket_timer->name);
  Serial.print("' on socket ");
  Serial.print(socket_timer->socket->family);
  Serial.print(", ");
  Serial.print(socket_timer->socket->group);
  Serial.print(", ");
  Serial.println(socket_timer->socket->number);

  if (socket_timer->on) {
    rcswitch.switchOn(socket_timer->socket->family, socket_timer->socket->group, socket_timer->socket->number);
  } else {
    rcswitch.switchOff(socket_timer->socket->family, socket_timer->socket->group, socket_timer->socket->number);
  }

  Serial.println(" ... done.");
}

void socket_timer_callback() {
  AlarmID_t alarm_id = Alarm.getTriggeredAlarmId();

  Serial.print("Triggered alarm is ");
  Serial.println(alarm_id);

  for (int i = 0; i < sizeof(timers) / sizeof(socket_timer_t); i++) {
    if (timers[i].alarm_id == alarm_id) {
      execute_socket_timer(&timers[i]);
      return;
    }
  }

  Serial.print("Unable to resolve alarm w/ id ");
  Serial.println(alarm_id);
}

void register_socket_timer(socket_timer_t *socket_timer) {
  AlarmID_t alarm_id = Alarm.alarmRepeat(socket_timer->hour, socket_timer->minute, socket_timer->second, socket_timer_callback);
  socket_timer->alarm_id = alarm_id;

  Serial.print("Registered alarm '");
  Serial.print(socket_timer->name);
  Serial.print("' w/ id ");
  Serial.println(alarm_id);
}

void setup() {
  Serial.begin(9600);
  Serial.println();

  pinMode(ILED, OUTPUT);
  digitalWrite(ILED, HIGH);

  rcswitch.enableTransmit(RCSWITCH_PIN);
  delay(250);

  Serial.print("Attempting to connect to SSID ");
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();

  Serial.print("Connected to SSID ");
  Serial.print(WLAN_SSID);
  Serial.print(" @ ");
  Serial.println(WiFi.localIP());

  ntpClient.begin();
  ntpClient.update();

  Serial.println("Registering NTP update service ...");
  setSyncProvider(updateTimeFromNTP);
  setSyncInterval(60);

  Serial.println("Registering alarms ...");
  Alarm.timerRepeat(2, pulse);

  for (int i = 0; i < sizeof(timers) / sizeof(socket_timer_t); i++) {
    register_socket_timer(&timers[i]);
  }
{'fraction_non_alphanumeric': 0.15739155251141554, 'fraction_numerical': 0.020833333333333332, 'mean_word_length': 4.835970024979184, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6495059', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2595, 'n_words': 506}