queue.ino 3.4 KB
#include "queue.h"

void enqueue(int command, double start, double data) {
  Serial.println("enqueue");
  /* // no dupes, only create new if it is scheduled */
  /* while(ptr != NULL) { */
  /*   if (    command == T_PUMPLEVEL */
  /*        || command == T_PUMPDURATION */
  /*        || command == T_PUMPABORT */
  /*        && */
  /*           ptr->command == T_PUMPLEVEL */
  /*        || ptr->command == T_PUMPDURATION */
  /*        ) { */
  /*     break; */
  /*   } */
  /* } */

  struct action *ptr = (struct action*) malloc(sizeof(struct action));

  ptr->command = command;
  ptr->start = start;
  ptr->data = data;

  ptr->next = NULL;
  ptr->running = 0;

  if (head == NULL) {
    head = ptr;
  }
  if (tail != NULL) {
    tail->next = ptr;
  }
  tail = ptr;

  /* struct action *ptr = head; */
  /* if ( ptr == NULL ) { */
    // in an empty list, this is head
    // previous tail is not last anymore
    // this is now tail
    /*
  /* } */
  /* if ( command == T_PUMPABORT ) { */
  /*   ptr->end = 1; */
  /*   ptr->running = 1; */
  /* } else { */
  /* } */
}

int entry_start(struct action *ptr) {
  switch( ptr->command ) {
  case C_FILL_DURATION:
    return fill_start(ptr);
  case C_ROTATE_DURATION:
    return rotate_start(ptr);
  case C_FERTILIZE_DURATION:
    return fertilize_start(ptr);
  }
  return 0;
}

void entry_running(struct action *ptr) {
  unsigned int ut = time(NULL);
  String remainingStr(ptr->end - ut);
  delay(10); // Don't remove! mqttClient crashes otherwise
  switch( ptr->command ) {
  case C_FILL_DURATION:
    mqttClient.publish("home/compost/action/3/duration", remainingStr );
    break;
  case C_ROTATE_DURATION:
    mqttClient.publish("home/compost/action/1/duration", remainingStr );
    break;
  case C_FERTILIZE_DURATION:
    mqttClient.publish("home/compost/action/2/duration", remainingStr );
    break;
  }
}

/* void device_running(struct action *ptr) { */

/*   /\* int device = sequence_head->device; *\/ */
/*   /\* String pre = "home/compost/device/"; *\/ */
/*   /\* int device = 1; *\/ */
/*   /\* String state(pre + device + "state"); *\/ */
/*   /\* String duration(pre + device + "/duration"); *\/ */

/* } */

void entry_end(struct action *ptr) {
  String remainingStr(0);
  mqttClient.publish(PREAMBLE T_PUMPSTATE, remainingStr );
}

void entry_end(struct button *ptr) {
}

void entry_end(int source) {
}

void loop_queue() {
  struct action *ptr = head;
  struct action *next = NULL;
  struct action *prev = NULL;
  unsigned int ut = time(NULL);

  while(ptr != NULL) {
    next=ptr->next;

    if ( ptr->running == 0
         && ut >= ptr->start
         ) {
      if ( entry_start(ptr) ) { // pump is blocked ?
        ptr->end = ptr->data + ut;
        ptr->running = 1;
      } else {
        ptr->start++; // delay action;
      }
    }

    /////////////////////////   action
    if ( ptr->running == 1 ) {
      entry_running(ptr);
    }

    /////////////////////////   end action
    if ( ptr->running == 1
         && ut >= ptr->end
         ) {
      Serial.println("end");
      entry_end(ptr);
      Serial.println("end1");
      /* action is due to removal */

      if (prev != NULL) {
        prev->next = ptr->next;
      }

      if (ptr == head) {
        head = ptr->next;
      }
      Serial.println("end5");
      /* if (ptr == tail) { */
      /*   tail = prev; */
      /* } */
      free(ptr);
    } else {
      prev = ptr;
    }
    ptr = next;
  }
}

void setup_queue() {
}