bck
Alejandro Acuña
2024-11-11 f1cb4443aede6d4657bdc3396c8914d3a9f4fa93
libraries/server/src/art/servers/controller/ListenerImplementation.java
@@ -11,7 +11,6 @@
import art.library.model.devices.application.ApplicationRealtime;
import art.library.model.devices.user.User;
import art.library.model.devices.user.UserPermission;
import art.library.model.devices.user.UserStatus;
import art.library.model.devices.user.configuration.ConfigurationLockLogin;
import art.library.model.general.ModelFile;
import art.library.model.transactions.traces.Trace;
@@ -38,6 +37,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Stream;
public class ListenerImplementation 
@@ -156,6 +156,8 @@
        User user = null;
        // TODO: esto cada vez es muy ineficiente, revisar.......
        try
        {
            user = (User)Shared.model.getDeviceExternal(Licence.encrypt(username));
@@ -177,6 +179,9 @@
            }
        }
        // TODO : volver a poner
/*
        if (user.getDeviceInformation().password.equals(Licence.encrypt(password)) == false)
        {
            // Check blocked users
@@ -243,6 +248,7 @@
        status.unlockTimestamp = -1;
        status.lastLogin = System.currentTimeMillis();
        Shared.model.updateDevice(user, newuser);
*/
        HttpAuthentication authentication = new HttpAuthentication(username, password, address, user.getDeviceInformation().group);
        return authentication;
@@ -392,7 +398,6 @@
        }
        catch (Exception e)
        {
           Shared.printstack("Listener", e);
           StringWriter sw = new StringWriter();
           e.printStackTrace(new PrintWriter(sw));
           throw new SerializationException(Shared.getMessage(language, e.getMessage()), sw.toString()); 
@@ -516,11 +521,22 @@
        {
            String identifier = (String)parameters.getParameterValue("identifier");
            if (identifier == null) identifier = (parameters.hasParameter("device") == true) ? (String)parameters.getParameterValue("device") : null;
            String username = (String)parameters.getParameterValue("username");
            User user = null;
            if (username != null)
            {
                try{user = (User)Shared.model.getDeviceExternal(identifier);} catch (Exception e){};
                if (user == null)
                {
                    try{user = (User)Shared.model.getDeviceExternal(Licence.encrypt(identifier));} catch (Exception e){};
                }
            }
            if (parameters.hasParameter("timestamp"))
            {
                long timestamp = getTimestamp((String)parameters.getParameterValue("timestamp"));
                return new InteropResponse(Shared.model.getDevices(identifier, timestamp));
                Device[] devices = checkDevices(user, Shared.model.getDevices(identifier, timestamp));
                return new InteropResponse(devices);
            }
            else if (parameters.hasParameter("groups"))
            {
@@ -666,6 +682,7 @@
            Class clazzDevice = Class.forName(Shared.model.deviceClassName);
            bodyContent = (String)parameters.getParameterValue("body-content");
            Device device = (Device)Serialization.deserialize(clazzDevice, bodyContent);
            addDevices(language, bodyContent);
            Shared.model.updateDevice(new Device[]{device});
            return new InteropResponse(new Boolean(true));
        }
@@ -690,14 +707,22 @@
        
        try
        {
            System.out.println("1.AddDevices: " + Shared.model.deviceClassName);
            Class clazzDevice = Class.forName(Shared.model.deviceClassName);
            bodyContent = (String)parameters.getParameterValue("body-content");
            System.out.println("2.AddDevices: " + clazzDevice);
            // bodyContent = (String)parameters.getParameterValue("body-content");
            Class<?> clazzArray = (Class<?>)Array.newInstance(clazzDevice, 0).getClass();
            Device[] ldevice = (Device[])Serialization.deserialize(clazzArray, bodyContent);
            Device[] ldevice = parameters.getBodyContentValue(clazzArray);
            // System.out.println("3.AddDevices: " + bodyContent);
            // Device[] ldevice = (Device[])Serialization.deserialize(clazzArray, bodyContent);
            System.out.println("4.AddDevices: " + ldevice);
            if ((ldevice != null) && (ldevice.length > 0))
            {
                System.out.println("5.AddDevices: " + ldevice[0]);
                if (ldevice[0] != null)
                {
                    System.out.println("6.AddDevices: " + ldevice[0].getIdentifier());
                    Shared.model.addDevices(Stream.of(ldevice).map(dev -> dev.getDeviceInformation()).toArray(DeviceInformation[]::new));
                    Shared.model.updateDevice(ldevice);
                    return new InteropResponse(new Boolean(true));
                }
@@ -705,6 +730,7 @@
        }
        catch (ClassCastException exception)
        {
            exception.printStackTrace();
        }
        catch (Exception exception)
        {
@@ -788,6 +814,7 @@
            }
            
            Shared.model.deleteDevices(lidentifier);
            return new InteropResponse(new Boolean(true));
        }
        catch (ServerException exception)
@@ -829,14 +856,15 @@
    public InteropResponse getDevicesRealtime(InteropParameters parameters) throws SerializationException 
    {
        String language = (String)parameters.getParameterValue("language");
        String language = (String)parameters.getParameterValue("language");
        long timestamp = (parameters.hasParameter("timestamp") == true) ? Long.parseLong((String)parameters.getParameterValue("timestamp")) : 0;
        try
        {
            List<DeviceRealtime> lrealtime = new ArrayList<DeviceRealtime>();
            for (Device device : Shared.model.getDevices())
            Device[] ldevice = art.servers.Shared.model.getDevicesCopy();
            for (Device device : ldevice)
            {
                DeviceRealtime realtime = device.getDeviceRealtime();
                
@@ -845,13 +873,12 @@
                    if (realtime.lastTimestampUpdate > timestamp) 
                    {
                        realtime.identifier = device.getIdentifier();
                        lrealtime.add(device.getDeviceRealtime());
                        lrealtime.add(realtime);
                    }
                }
            }
            return new InteropResponse(lrealtime.toArray(new DeviceRealtime[lrealtime.size()]));
        }
        catch (Exception e)
        {
@@ -1651,7 +1678,31 @@
    
    
   // </editor-fold>
    private Device[] checkDevices (User user, Device[] devices)
    {
        try
        {
            if (user == null) return(devices);
            List<Device> list = new ArrayList<Device>();
            for (Device device : devices)
            {
                if ((user.getDeviceInformation().municipality != null) && (device.getDeviceInformation().municipality != null) &&
                    (user.getDeviceInformation().municipality.equalsIgnoreCase(device.getDeviceInformation().municipality)))
                {
                    list.add(device);
                }
            }
            return(list.toArray(new Device[0]));
        }
        catch (Exception e)
        {
            return(devices);
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="static">
    
@@ -1664,7 +1715,7 @@
    
    
    
    private static String[] getStringArray(InteropParameter parameter)
    protected static String[] getStringArray(InteropParameter parameter)
    {
        if (parameter.getValue() instanceof String[])
        {
@@ -1710,11 +1761,16 @@
            if ((timestamp.indexOf("-") == 4) && (timestamp.length() == 10)) return new SimpleDateFormat("yyyy-MM-dd").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 2) && (timestamp.length() == 10)) return new SimpleDateFormat("dd-MM-yyyy").parse(timestamp).getTime();
            
            if ((timestamp.indexOf("/") == 4) && (timestamp.length() == 16)) return new SimpleDateFormat("yyyy/MM/dd HH:mm").parse(timestamp).getTime();
            if ((timestamp.indexOf("/") == 2) && (timestamp.length() == 16)) return new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 4) && (timestamp.length() == 16)) return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 2) && (timestamp.length() == 16)) return new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(timestamp).getTime();
            if ((timestamp.indexOf("/") == 4) && (timestamp.length() == 19)) return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(timestamp).getTime(); 
            if ((timestamp.indexOf("/") == 2) && (timestamp.length() == 19)) return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 4) && (timestamp.length() == 19)) return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 2) && (timestamp.length() == 19)) return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(timestamp).getTime();
            if ((timestamp.indexOf("/") == 4) && (timestamp.length() == 23)) return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").parse(timestamp).getTime(); 
            if ((timestamp.indexOf("/") == 2) && (timestamp.length() == 23)) return new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS").parse(timestamp).getTime();
            if ((timestamp.indexOf("-") == 4) && (timestamp.length() == 23)) return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(timestamp).getTime();