Exemplo n.º 1
0
void debugSPIFFS() {
    Dir dir = SPIFFS.openDir("/");
    while (dir.next()) {
        String fileName = dir.fileName();
        Serial.printf("FS File: %s\n", fileName.c_str());
    }

}
Exemplo n.º 2
0
/* ======================================================================
Function: getSpiffsJSONData 
Purpose : Return JSON string containing list of SPIFFS files
Input   : Response String
Output  : - 
Comments: -
====================================================================== */
void getSpiffsJSONData(String & response)
{
  char buffer[32];
  bool first_item = true;

  // Json start
  response = FPSTR(FP_JSON_START);

  // Files Array  
  response += F("\"files\":[\r\n");

  // Loop trough all files
  Dir dir = SPIFFS.openDir("/");
  while (dir.next()) {    
    String fileName = dir.fileName();
    size_t fileSize = dir.fileSize();
    if (first_item)  
      first_item=false;
    else
      response += ",";

    response += F("{\"na\":\"");
    response += fileName.c_str();
    response += F("\",\"va\":\"");
    response += fileSize;
    response += F("\"}\r\n");
  }
  response += F("],\r\n");


  // SPIFFS File system array
  response += F("\"spiffs\":[\r\n{");
  
  // Get SPIFFS File system informations
  FSInfo info;
  SPIFFS.info(info);
  response += F("\"Total\":");
  response += info.totalBytes ;
  response += F(", \"Used\":");
  response += info.usedBytes ;
  response += F(", \"ram\":");
  response += system_get_free_heap_size() ;
  response += F("}\r\n]"); 

  // Json end
  response += FPSTR(FP_JSON_END);
}
Exemplo n.º 3
0
void RRFs::handleFileList() {
  if(!server->hasArg("dir")) {server->send(500, "text/plain", "BAD ARGS"); return;}
  
  String path = server->arg("dir");
  DEBUGPRINT.println("handleFileList: " + path);
  Dir dir = SPIFFS.openDir(path);
  path = String();

  String output = "[";
  while(dir.next()){
    if (output != "[") output += ',';
   output += "{";
   //output+="\"type\":\"";
   // output += (dir.isDir())?"dir":"file";
    output += "\"name\":\"";
    output += String(dir.fileName()).substring(1);
    output += "\",\"size\":\"";
    output += formatBytes(dir.fileSize());
    output += "\"}";
  }
  
  output += "]";
  server->send(200, "text/json", output);
}
Exemplo n.º 4
0
void parseTextMessage(AsyncWebSocketClient * client, uint8_t * data, size_t len)
{
  DynamicJsonBuffer jsonBuffer;
  
  // parse possible json files
  if( data[0] =='{' && data[len - 1] == '}' )
  {
     // let's parse the json file
     Serial.println("JSON STR RECEIVED!");

     char tmp[len + 1];
     memcpy(tmp, (char *)data, len);
     tmp[len] = '\0';
     JsonObject& root = jsonBuffer.parseObject(tmp);

     // ########################
     // Wifi configuration file
     // ########################
     if ( root.containsKey("mode") )
     {
  
       if ( strcmp((const char*)root["mode"], "ap") == 0 )
       {
         Serial.println("AP");
         storage.wifisett.setSSID((const char*)root["ssid"]);
         storage.wifisett.setPwd((const char*)root["pwd"]);

         storage.wifisett.enableSoftAP();
       }
       else if ( strcmp((const char*)root["mode"], "sta") == 0 )
       {
         Serial.println("STA");
         storage.wifisett.setClientSSID((const char*)root["ssid"]);
         storage.wifisett.setClientPwd((const char*)root["pwd"]);
  
         // set mode to STA
         storage.wifisett.enableClient();
       }
       
     }



     // #########################
     // Set Clock
     // #########################
     if (root.containsKey("setclock") )
     {
       JsonObject& rootsetclock = root.get("setclock");
      
       if ( rootsetclock.containsKey("type") && ( strcmp((const char*)rootsetclock["type"], "ntp") == 0 ) )
       {
         // get ntp time
         opaq_controller.syncClock();
       }
       else
       {
         // get values from json
         RtcDateTime tmp = RtcDateTime( rootsetclock["year"], rootsetclock["month"], rootsetclock["day"], rootsetclock["hour"], rootsetclock["minute"], rootsetclock["second"]);
         opaq_controller.getCom().setClock(tmp);
       }
     }

     // #########################
     // Get Clock
     // #########################
     if (root.containsKey("getclock") )
     {
       // let's send the clock values
       JsonObject& tmp_root = jsonBuffer.createObject();

       JsonObject& conf = tmp_root.createNestedObject("realtimeclock");

       RtcDateTime date;
       opaq_controller.getCom().getClock(date); // [TODO] this can fail
       
       conf["second"] = date.Second();
       conf["minute"] = date.Minute();
       conf["hour"]   = date.Hour();
       conf["day"]    = date.Day();
       conf["month"]  = date.Month();
       conf["year"]   = date.Year();

       String tmp = "";
       tmp_root.printTo(tmp);

       client->text(tmp);
     }


     
     // ################
     // Filename getter
     // ################
     if (root.containsKey("filename") )
     {
       String content = "";
       
       sendFile(SPIFFS, root["filename"], content);
       
       client->text(content);
     }

     // ################################
     // Light device full dimmer setter
     // ###############################
     if (root.containsKey("adimid") )
     {
       // let's write the file configuration
      
       storage.faqdim.save(root["adimid"], data, len);
      
       client->text("{\"success\":\"\"}");
     }

     // ################################
     // Light device full dimmer getter
     // ################################
     if (root.containsKey("adim") )
     {
       String content = "", dirname = "";
       
       // list devices and send it
       // {"adim":["filea.json","fileb.json"]}
       
       content += F("{\"adim\":[\"\"");

       storage.faqdim.getDir(dirname);
       
       // for each file in /sett/adim directory do
       Dir directory = SPIFFS.openDir(dirname.c_str());
       
       while ( directory.next() )
       {
         content += F(", \"");
         content += directory.fileName();
         content += F("\" ");
       }

       content += F(" ]}");
   
       client->text(content);
    }

    // ##################################
    // Light device full dimmer remover
    // #################################
    if (root.containsKey("adimremove") )
    {
      storage.faqdim.remove(root["adimremove"]);
     
      client->text("{\"success\":\"\"}");
    }

    // ################################
    // Light device full dimmer setter
    // ################################
    if (root.containsKey("adimadd") )
    {
      storage.faqdim.add();

      client->text("{\"success\":\"\"}");
          
    }

    // ####################################
    // Light device full dimmer bind state
    // ####################################
    if (root.containsKey("adimbind") )
    {
      //root["adimbind"]
    }




    // ################################
    // Power device getter
    // ################################
    if (root.containsKey("pdev") )
    {
      String content = "", dirname = "";
       
      // list devices and send it
      // {"pdev":["filea.json","fileb.json"]}
       
      content += F("{\"pdev\":[\"\"");

      storage.pwdevice.getDir(dirname);
       
      // for each file in /sett/pdev directory do
      Dir directory = SPIFFS.openDir(dirname.c_str());
       
      while ( directory.next() )
      {
        content += F(", \"");
        content += directory.fileName();
        content += F("\" ");
      }

      content += F(" ]}");
   
      client->text(content);
    }

    // ################################
    // Power device setter
    // ################################
    if (root.containsKey("pdevid") )
    {
      // let's write the file configuration
      
      storage.pwdevice.save(root["pdevid"], data, len);
      
      client->text("{\"success\":\"\"}");
    }

    // ################################
    // Power device add setter
    // ################################
    if (root.containsKey("pdevadd") )
    {
      storage.pwdevice.add();

      client->text("{\"success\":\"\"}");
          
    }

    // ##################################
    // Power device remover
    // #################################
    if (root.containsKey("pdevremove") )
    {
      storage.pwdevice.remove(root["pdevremove"]);
     
      client->text("{\"success\":\"\"}");
    }



    // ################################
    // Update filesystem
    // ###############################
    if (root.containsKey("updatefilesystem") )
    {
      // let's write the file configuration
      
      storage.setUpdate(true);
      
      client->text("{\"success\":\"\"}");
    }
     
  }
  else if( strcmp((char*)data, "GET_OPAQ_WIFISETTINGS") == 0 )
  {
    // let's send the wifisettings
    JsonObject& root = jsonBuffer.createObject();

    JsonObject& conf = root.createNestedObject("wifisettings");

    String wssid, wpwd, wclientssid, wclientpwd, wmode;
    storage.wifisett.getSSID(wssid);
    storage.wifisett.getPwd(wpwd);
    storage.wifisett.getClientSSID(wclientssid);
    storage.wifisett.getClientPwd(wclientpwd);
    storage.wifisett.getMode(wmode);
    
    conf["wssid"] = wssid.c_str();
    conf["wpwd"] = wpwd.c_str();
    conf["wchan"] = 6; // [TODO]
    
    conf["wssidsta"] = wclientssid.c_str();
    conf["wpwdsta"] = wclientpwd.c_str();
    conf["wmode"] = wmode.c_str();

    String tmp = "";
    root.printTo(tmp);

    client->text(tmp);
  }
  else if( strcmp((char*)data, "GET_OPAQ_SUMMARY") == 0 )
  {  
    String wssid;
    storage.wifisett.getSSID(wssid);
    
    JsonObject& root = jsonBuffer.createObject();
    root["version"] = OPAQ_VERSION;
    root["id"]      = ESP.getFlashChipId();
    root["status"]  = "Running without errors"; // [TODO]
    root["wstatus"] = "Radio is On"; // [TODO]
    root["wmode"]   = (storage.wifisett.getModeOperation())? "softAP" : "client";
    root["wssid"]   = wssid.c_str();
    root["wchan"]   = WiFi.channel();
    root["wdhcp"]   = "Enabled"; // [TODO]
    root["wmac"]    = WiFi.softAPmacAddress();
    root["wip"]     = (storage.wifisett.getModeOperation())? WiFi.softAPIP().toString() : WiFi.localIP().toString();

    String tmp = "";
    root.printTo(tmp);

    client->text(tmp);
  }
  else
  {
    client->text("{\"msg\":\"I got your text message\"}");
  }
  
}