#!/bin/bash
# additional dependencies: graphviz imagemagick

# put temporal files in ram filesystem
file="/dev/shm/networkmap"
fechahora=$(date '+%F-%H:%M')
ROOT="0x00000000000000"  # put your coordinator here
mqtthost="xxx.xxx.xxx.xxx" # change if your mosquitto is on another host
user="xxxxxxxxxxx"
pass="xxxxxxxxxxx"
echo $fechahora

mosquitto_sub -h $mqtthost -u $user -P $pass -t zigbee2mqtt/bridge/networkmap/graphviz -C 1 > ${file}routes.dot &
mosquitto_pub -h $mqtthost -u $user -P $pass -t zigbee2mqtt/bridge/networkmap/routes -m graphviz

# wait until mosquitto_sub ends
wait

cat ${file}routes.dot| dot -Kneato -Groot=$ROOT -Goverlap=false -Gnodesep=1 -Granksep=1 -Tsvg > ${fechahora}routes.svg

# display with imageMagick command
display ${fechahora}routes.svg &

#include "FastLED.h"

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

/* Put your SSID & Password */
const char* ssid = "IlariMCU";  // Enter SSID here
const char* password = "";  //Enter Password here

/* Put IP Address details */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

ESP8266WebServer server(80);

bool LED1status = LOW;
bool LED2status = LOW;

#define NUM_LEDS 4
#define DATA_PIN 4

// #define FASTLED_ALLOW_INTERRUPTS 0

#define FASTLED_INTERRUPT_RETRY_COUNT 1


CRGB leds[NUM_LEDS];


void setPixel(int Pixel, byte red, byte green, byte blue) {
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
}


void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(int i = 0; i < NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      FastLED.show();
      delay(SpeedDelay);
  }
}


void setup() {  

    FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
    FastLED.clear();
     
    FastLED.setBrightness(255);

    colorWipe(0xFF,0x00,0x00, 300);     
    colorWipe(0x00,0x00,0x00, 300);

    WiFi.softAP(ssid, password);
    WiFi.softAPConfig(local_ip, gateway, subnet);
    delay(100);
 
    server.on("/", handle_OnConnect);
    server.on("/led1on", handle_led1on);
    server.on("/led1off", handle_led1off);
    server.on("/led2on", handle_led2on);
    server.on("/led2off", handle_led2off);
    server.onNotFound(handle_NotFound);
 
    server.begin();
    
    fill_solid(leds, NUM_LEDS, CRGB(0,0,255));
    FastLED.show();
    delay(200);
    fill_solid(leds, NUM_LEDS, CRGB(0,0,0));
    FastLED.show();
    delay(200);   
    fill_solid(leds, NUM_LEDS, CRGB(0,0,255));
    FastLED.show();
    delay(200);
    fill_solid(leds, NUM_LEDS, CRGB(0,0,0));
    FastLED.show();
    delay(200);     
    fill_solid(leds, NUM_LEDS, CRGB(0,0,255));
    FastLED.show();
    delay(200);
    fill_solid(leds, NUM_LEDS, CRGB(0,0,0));
    FastLED.show();
}


void loop() {

  server.handleClient();
 
  if(LED1status)
  {
    colorWipe(0xFF,0xFF,0xFF, 300);
    colorWipe(0x00,0x00,0x00, 300);    
  }
  else
  {colorWipe(0x00,0x00,0x00, 1);}
 
  if(LED2status)
  {
   colorWipe(0x00,0xFF,0x00, 300);
   colorWipe(0xFF,0x00,0x00, 300);       
   colorWipe(0x00,0x00,0xFF, 300);
   colorWipe(0x00,0x00,0x00, 300);
  }
  else
  {colorWipe(0x00,0x00,0x00, 1);}

}

void handle_OnConnect() {
  LED1status = LOW;
  LED2status = LOW;
  server.send(200, "text/html", SendHTML(LED1status,LED2status));
}

void handle_led1on() {
  LED1status = HIGH;
  server.send(200, "text/html", SendHTML(true,LED2status));
}

void handle_led1off() {
  LED1status = LOW;
  server.send(200, "text/html", SendHTML(false,LED2status));
}

void handle_led2on() {
  LED2status = HIGH;
  server.send(200, "text/html", SendHTML(LED1status,true));
}

void handle_led2off() {
  LED2status = LOW;
  server.send(200, "text/html", SendHTML(LED1status,false));
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(uint8_t led1stat,uint8_t led2stat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>Ilari's LED Control</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button-on {background-color: #1abc9c;}\n";
  ptr +=".button-on:active {background-color: #16a085;}\n";
  ptr +=".button-off {background-color: #34495e;}\n";
  ptr +=".button-off:active {background-color: #2c3e50;}\n";
  ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<h1>Ilari's Web Server</h1>\n";
  ptr +="<h3>Using Access Point(AP) Mode<br> to control WS2812B</h3>\n";
 
   if(led1stat)
  {ptr +="<p>White LED wipe: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
  else
  {ptr +="<p>White LED wipe: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}

  if(led2stat)
  {ptr +="<p>RGB LED wipe: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
  else
  {ptr +="<p>RGB LED wipe: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}

  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}