Search in sources :

Example 1 with JsonBuilder

use of com.giua.utils.JsonBuilder in project Giua-App by Giua-app.

the class CheckNewsReceiver method createJsonNotificationFile.

private void createJsonNotificationFile(File f, AlertsPage alertsPage) throws IOException {
    FileWriter fileWriter = new FileWriter(f);
    fileWriter.write("");
    fileWriter.close();
    JsonBuilder jsonBuilder = new JsonBuilder(context.getCacheDir() + "/alertsToNotify.json", gS);
    jsonBuilder.writeAlerts(alertsPage.getAllAlertsWithFilters(false, "per la materia"));
    jsonBuilder.saveJson();
}
Also used : JsonBuilder(com.giua.utils.JsonBuilder) FileWriter(java.io.FileWriter)

Example 2 with JsonBuilder

use of com.giua.utils.JsonBuilder in project Giua-App by Giua-app.

the class CheckNewsReceiver method checkAndSendNotificationForAgenda.

/**
 * Controlla e invia le notifiche riguardanti compiti e verifiche.
 *
 * @return La somma dei compiti e delle verifiche notificate. Serve a non far notificare anche gli avvisi.
 */
private int checkAndSendNotificationForAgenda() throws IOException {
    loggerManager.d("Inizio a controllare gli avvisi per le notifiche di compiti e verifiche");
    com.giua.utils.JsonParser jsonParser = new JsonParser();
    boolean canSendHomeworkNotification = SettingsData.getSettingBoolean(context, SettingKey.HOMEWORKS_NOTIFICATION);
    boolean canSendTestNotification = SettingsData.getSettingBoolean(context, SettingKey.TESTS_NOTIFICATION);
    if (!canSendTestNotification && !canSendHomeworkNotification)
        // Se non puo inviare nessuna notifica lo blocco
        return 0;
    loggerManager.d("Leggo il json per vedere gli avvisi dei compiti e delle verifiche già notificati");
    AlertsPage alertsPage = gS.getAlertsPage(false);
    File f = new File(context.getCacheDir() + "/alertsToNotify.json");
    if (!f.exists()) {
        createJsonNotificationFile(f, alertsPage);
        // Return perchè andrebbe a notificare tutti i vecchi compiti
        return 0;
    }
    BufferedReader file = new BufferedReader(new FileReader(context.getCacheDir() + "/alertsToNotify.json"));
    StringBuilder oldAlertsString = new StringBuilder();
    String read = file.readLine();
    if (read == null) {
        createJsonNotificationFile(f, alertsPage);
        return 0;
    }
    while (read != null) {
        oldAlertsString.append(read);
        read = file.readLine();
    }
    file.close();
    loggerManager.d("Faccio il parsing del json");
    // Lista con gli avvisi già notificati
    List<Alert> oldAlerts;
    if (oldAlertsString.toString().equals("")) {
        loggerManager.w("oldAlertsString è vuoto");
        oldAlerts = new Vector<>();
    } else
        oldAlerts = jsonParser.parseJsonForAlerts(oldAlertsString.toString());
    // Lista degli avvisi da notificare
    List<Alert> alertsToNotify = alertsPage.getAlertsToNotify(oldAlerts);
    // Salva gli avvisi (compresi i nuovi) nel json
    JsonBuilder jsonBuilder = new JsonBuilder(context.getCacheDir() + "/alertsToNotify.json", gS);
    jsonBuilder.writeAlerts(alertsPage.getAllAlertsWithFilters(false, "per la materia"));
    jsonBuilder.saveJson();
    loggerManager.d("Conto i compiti e le verifiche da notificare");
    // Conta i compiti da notificare
    int homeworkCounter = 0;
    // Conta le verifiche da notificare
    int testCounter = 0;
    // Lista in cui ci sono tutte le date dei compiti da notificare
    List<String> homeworkDates = new Vector<>(40);
    // Lista in cui ci sono tutte le date delle verifiche da notificare
    List<String> testDates = new Vector<>(40);
    // Lista in cui ci sono tutte le materie dei compiti da notificare
    List<String> homeworkSubjects = new Vector<>(40);
    // Lista in cui ci sono tutte le materie delle verifiche da notificare
    List<String> testSubjects = new Vector<>(40);
    for (Alert alert : alertsToNotify) {
        if (alert.object.startsWith("C")) {
            homeworkDates.add(alert.date);
            homeworkSubjects.add(alert.object.split(" per la materia ")[1]);
            homeworkCounter++;
        } else if (alert.object.startsWith("V")) {
            testDates.add(alert.date);
            testSubjects.add(alert.object.split(" per la materia ")[1]);
            testCounter++;
        }
    }
    loggerManager.d("Preparo le notifiche");
    StringBuilder homeworkNotificationText;
    StringBuilder testNotificationText;
    Notification homeworkNotification = null;
    Notification testNotification = null;
    if (canSendHomeworkNotification && homeworkCounter > 0) {
        String contentText;
        if (homeworkCounter == 1) {
            contentText = "Clicca per andare all' agenda";
            homeworkNotificationText = new StringBuilder("È stato programmato un nuovo compito di " + homeworkSubjects.get(0) + " per il giorno " + homeworkDates.get(0));
        } else {
            contentText = "Clicca per andare all' agenda";
            homeworkNotificationText = new StringBuilder("Sono stati programmati nuovi compiti:\n");
            for (int i = 0; i < homeworkCounter; i++) {
                homeworkNotificationText.append(homeworkSubjects.get(i));
                homeworkNotificationText.append(" - ");
                homeworkNotificationText.append(homeworkDates.get(i));
                if (i != homeworkCounter - 1)
                    homeworkNotificationText.append("\n");
            }
        }
        homeworkNotification = createNotificationForAgenda("Nuovi compiti", contentText, homeworkNotificationText.toString());
    }
    if (canSendTestNotification && testCounter > 0) {
        String contentText;
        if (testCounter == 1) {
            contentText = "Clicca per andare all' agenda";
            testNotificationText = new StringBuilder("È stata programmata una nuova verifica di " + testSubjects.get(0) + " per il giorno " + testDates.get(0));
        } else {
            contentText = "Clicca per andare all' agenda";
            testNotificationText = new StringBuilder("Sono state programmate nuove verifiche:\n");
            for (int i = 0; i < testCounter; i++) {
                testNotificationText.append(testSubjects.get(i));
                testNotificationText.append(" - ");
                testNotificationText.append(testDates.get(i));
                if (i != testCounter - 1)
                    testNotificationText.append("\n");
            }
        }
        testNotification = createNotificationForAgenda("Nuove verifiche", contentText, testNotificationText.toString());
    }
    loggerManager.d("Invio le notifiche");
    if (canSendHomeworkNotification && homeworkNotification != null)
        notificationManager.notify(13, homeworkNotification);
    if (canSendTestNotification && testNotification != null)
        notificationManager.notify(14, testNotification);
    return testCounter + homeworkCounter;
}
Also used : AlertsPage(com.giua.pages.AlertsPage) Notification(android.app.Notification) JsonBuilder(com.giua.utils.JsonBuilder) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Alert(com.giua.objects.Alert) JsonParser(com.giua.utils.JsonParser) File(java.io.File) Vector(java.util.Vector) JsonParser(com.giua.utils.JsonParser)

Example 3 with JsonBuilder

use of com.giua.utils.JsonBuilder in project Giua-Webscraper by Giua-app.

the class TestClasses method testSpeed.

// endregion
private static void testSpeed() {
    logEnabled = false;
    List<Long> tSite = new Vector<>();
    List<Long> tInternet = new Vector<>();
    List<Long> tPhase1 = new Vector<>();
    List<Long> tPhase2 = new Vector<>();
    List<Long> tPhase3 = new Vector<>();
    List<Long> tPhasesTot = new Vector<>();
    List<Long> tJsonBuilder = new Vector<>();
    List<Long> tJsonParser = new Vector<>();
    int errors = 0;
    LoggerManager loggerManager = new LoggerManager("GiuaScraper-silent");
    System.out.println("----  AVVIO SPEEDTEST PER SCRAPER ----");
    System.out.println("Legenda:  |   LOGIN    |  CACHE   | SESSIONE |");
    System.out.println("          [############|##########|##########]");
    int i = 0;
    while (i < speedTestAmount) {
        long t1;
        long t2;
        System.out.println("Test " + (i + 1) + "/" + speedTestAmount);
        System.out.print("Progress: [");
        try {
            t1 = nanoTime();
            GiuaScraper.isMyInternetWorking();
            System.out.print("#");
            t2 = nanoTime();
            tInternet.add((t2 / 1000000) - (t1 / 1000000));
            t1 = nanoTime();
            GiuaScraper.isSiteWorking();
            System.out.print("#");
            t2 = nanoTime();
            tSite.add((t2 / 1000000) - (t1 / 1000000));
            // ///////////////////////////////////////////////////////////////////
            // NO CACHE
            // In questa prima parte vengono generate tutte le cose mentre nella seconda viene usata la cache
            t1 = nanoTime();
            startLogin(loggerManager);
            testNews(true);
            System.out.print("#");
            testVotes(true);
            System.out.print("#");
            testAlerts(true);
            System.out.print("#");
            testAgendaPage(true);
            System.out.print("#");
            testNewsletters(true);
            System.out.print("#");
            testLessons(true);
            System.out.print("#");
            testReportCard(true);
            System.out.print("#");
            testNotes(true);
            System.out.print("#");
            testAbsences(true);
            System.out.print("#");
            t2 = nanoTime();
            tPhase1.add((t2 / 1000000) - (t1 / 1000000));
            // //////////////////////////////////////////////////////////
            // CACHE
            t1 = nanoTime();
            System.out.print("|");
            testNews(false);
            System.out.print("#");
            testVotes(false);
            System.out.print("#");
            testAlerts(false);
            System.out.print("#");
            testAgendaPage(false);
            System.out.print("#");
            testNewsletters(false);
            System.out.print("#");
            testLessons(false);
            System.out.print("#");
            testReportCard(false);
            System.out.print("#");
            testNotes(false);
            System.out.print("#");
            testAbsences(false);
            System.out.print("#");
            t2 = nanoTime();
            tPhase2.add((t2 / 1000000) - (t1 / 1000000));
            t1 = nanoTime();
            String phpsessid = gS.getCookie();
            gS = new GiuaScraper(user, password, phpsessid, true, loggerManager);
            gS.login();
            System.out.print("|");
            testNews(true);
            System.out.print("#");
            testVotes(true);
            System.out.print("#");
            testAlerts(true);
            System.out.print("#");
            testAgendaPage(true);
            System.out.print("#");
            testNewsletters(true);
            System.out.print("#");
            testLessons(true);
            System.out.print("#");
            testReportCard(true);
            System.out.print("#");
            testNotes(true);
            System.out.print("#");
            testAbsences(true);
            System.out.print("#");
            t2 = nanoTime();
            tPhase3.add((t2 / 1000000) - (t1 / 1000000));
            tPhasesTot.add(tPhase1.get(i) + tPhase2.get(i) + tPhase3.get(i));
            System.out.println("]");
        } catch (Exception e) {
            System.out.println("/!\\]");
            System.out.println("Error: " + e.getMessage());
            tInternet.add(-1L);
            tSite.add(-1L);
            tPhase1.add(-1L);
            tPhase2.add(-1L);
            tPhase3.add(-1L);
            tPhasesTot.add(-1L);
            errors += 1;
        }
        i++;
    }
    i = 0;
    System.out.println("----  AVVIO SPEEDTEST PER JSON BUILDER ----");
    System.out.println("Legenda:  | Scrittura |");
    System.out.println("          [###########]");
    while (i < speedTestAmount) {
        long t1;
        long t2;
        System.out.println("Test " + (i + 1) + "/" + speedTestAmount);
        System.out.print("Progress: [");
        try {
            JsonBuilder jb = new JsonBuilder("test.json", gS);
            t1 = nanoTime();
            startLogin(loggerManager);
            jb.writeDocuments(gS.getDocumentsPage(false).getDocuments());
            System.out.print("#");
            jb.writeObservations(gS.getObservationsPage(false).getAllObservations());
            System.out.print("#");
            jb.writeAlerts(gS.getAlertsPage(false).getAllAlerts(1));
            System.out.print("#");
            jb.writeAgendaObjects(gS.getAgendaPage(false).getAllAgendaObjectsWithoutDetails(null));
            System.out.print("#");
            jb.writeNews(gS.getHomePage(false).getAllNewsFromHome());
            System.out.print("#");
            jb.writeMaintenance(gS.getMaintenanceInfo());
            System.out.print("#");
            jb.writeLessons(gS.getLessonsPage(false).getAllLessonsFromDate(new Date()));
            System.out.print("#");
            jb.writeAbsences(gS.getAbsencesPage(false).getAllAbsences());
            System.out.print("#");
            jb.writeDisciplinaryNotices(gS.getDisciplinaryNotesPage(false).getAllDisciplinaryNotices());
            System.out.print("#");
            jb.writeDocuments(gS.getDocumentsPage(false).getDocuments());
            System.out.print("#");
            t2 = nanoTime();
            tJsonBuilder.add((t2 / 1000000) - (t1 / 1000000));
            System.out.println("]");
        } catch (Exception e) {
            System.out.println("/!\\]");
            System.out.println("Error: " + e.getMessage());
            tInternet.add(-1L);
            tSite.add(-1L);
            tPhase1.add(-1L);
            tPhase2.add(-1L);
            tPhase3.add(-1L);
            tPhasesTot.add(-1L);
            errors += 1;
        }
        i++;
    }
    System.out.println("\n\n/---------------------LISTS----------------------------");
    Long tInternetAdd = 0L;
    System.out.print("|    Check Internet:                 ");
    for (Long value : tInternet) {
        tInternetAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tSiteAdd = 0L;
    System.out.print("|    Check Site:                     ");
    for (Long value : tSite) {
        tSiteAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase1Add = 0L;
    System.out.print("|    Fase 1 (login iniziale):        ");
    for (Long value : tPhase1) {
        tPhase1Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase2Add = 0L;
    System.out.print("|    Fase 2 (cache):                 ");
    for (Long value : tPhase2) {
        tPhase2Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase3Add = 0L;
    System.out.print("|    Fase 3 (riutilizzo sessione):   ");
    for (Long value : tPhase3) {
        tPhase3Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tJsonBuilder3Add = 0L;
    System.out.print("|    Json Builder (scrittura):       ");
    for (Long value : tJsonBuilder) {
        tJsonBuilder3Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhasesTotAdd = 0L;
    System.out.print("|    Tempo Totale impiegato:         ");
    for (Long value : tPhasesTot) {
        tPhasesTotAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    System.out.println("\\--------------------------------------------------------");
    System.out.println("\n\n");
    System.out.println("/---------------------FINAL RESULTS " + speedTestAmount + " TESTS-----------------------");
    System.out.println("|    Check Internet:                 " + tInternetAdd / speedTestAmount + "ms");
    System.out.println("|    Check Site:                     " + tSiteAdd / speedTestAmount + "ms");
    System.out.println("|    Fase 1 (login iniziale):        " + tPhase1Add / speedTestAmount + "ms");
    System.out.println("|    Fase 2 (cache):                 " + tPhase2Add / speedTestAmount + "ms");
    System.out.println("|    Fase 3 (riutilizzo sessione):   " + tPhase3Add / speedTestAmount + "ms");
    System.out.println("|");
    System.out.println("|    Tempo impiegato mediamente:     " + tPhasesTotAdd / speedTestAmount + "ms");
    System.out.println("|    Tempo impiegato totalmente:     " + tPhasesTotAdd / 1000 + "s");
    System.out.println("\\--------------------------------------------------------");
    if (errors != 0) {
        System.err.println("Attenzione! " + errors + " su " + speedTestAmount + " hanno fallito con errore");
    }
}
Also used : JsonBuilder(com.giua.utils.JsonBuilder) GiuaScraper(com.giua.webscraper.GiuaScraper)

Aggregations

JsonBuilder (com.giua.utils.JsonBuilder)3 Notification (android.app.Notification)1 Alert (com.giua.objects.Alert)1 AlertsPage (com.giua.pages.AlertsPage)1 JsonParser (com.giua.utils.JsonParser)1 GiuaScraper (com.giua.webscraper.GiuaScraper)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Vector (java.util.Vector)1