Skip to content
Pablo Mejías edited this page Nov 28, 2023 · 1 revision

En el nostre Server tractem tenim 3 tipus de missatges que pot rebre.

type: login

if("login".equals(jsonObject.getString("type"))){
                if(userPasswords.containsKey(jsonObject.getString("user"))){
                if(userPasswords.get(jsonObject.getString("user")).equals(jsonObject.getString("pass"))){
                    conn.send("{\"type\": \"login\", \"valid\":\"true\"}");
                }else{
                    conn.send("{\"type\": \"login\", \"valid\":\"false\"}");
                }
            }else{
                conn.send("{\"type\": \"login\", \"valid\":\"false\"}");
            }
        }

Aquí el servidor verifica l'usuari i contrasenya, i respon amb el missatge apropiat.

type: mssg

else if("mssg".equals(jsonObject.getString("type"))){
            args2 = new String[] {
                "/home/ieti/bin/text-scroller",
                "-f", "/home/ieti/dev/bitmap-fonts/bitmap/cherry/cherry-10-b.bdf",
                "--led-cols=64", "--led-rows=64",
                "--led-slowdown-gpio=4", "--led-no-hardware-pulse",
                jsonObject.getString("text")
            };

En aquest cas executa la comanda apropiada per poder mostrar el text que conté le JSON

type: img

else if("img".equals(jsonObject.getString("type"))){
            try{
                String outputPath = "./imagen."+jsonObject.getString("ext");

                byte[] decodedBytes = Base64.getDecoder().decode(jsonObject.getString("image"));

                Path filePath = Path.of(outputPath);
                Files.write(filePath, decodedBytes, StandardOpenOption.CREATE);

                args2 = new String[]{
                    "/home/ieti/bin/led-image-viewer",
                    "-C", "--led-cols=64", "--led-rows=64",
                    "--led-slowdown-gpio=4", "--led-no-hardware-pulse",
                    filePath.toString()
                };
            } catch (IOException e) {
                // Manejar la excepción aquí
                e.printStackTrace(); // O cualquier otro tratamiento que desees
            }
        }

Per últim, temin el cas de les imatges, el que fem després de descodificar l'String és guardar la imatge en el servidor i posteriorment enviem aquest path a la pantalla.

Clone this wiki locally