Alejandro Acuña
2024-12-18 44b33e24b644459038edd956cfce7345ce3236c1
libraries/server/src/art/servers/Model.java
@@ -1,5 +1,6 @@
package art.servers;
import art.library.gui.flat.FlatDialog;
import art.library.interop.serialization.Serialization;
import art.library.model.devices.Device;
import art.library.model.devices.DeviceAction;
@@ -10,6 +11,8 @@
import art.library.model.devices.DeviceRealtime;
import art.library.model.devices.DeviceSymbol;
import art.library.model.devices.application.Application;
import art.library.model.devices.colors.controller.RTZ32.types.eventstrolley.EventTrolley;
import art.library.model.devices.colors.controller.RTZ32.types.eventstrolley.EventTrolleyResult;
import art.library.model.transactions.traces.Trace;
import art.library.model.transactions.traces.TracePersistance;
import art.library.model.transactions.traces.TraceResult;
@@ -19,6 +22,8 @@
import art.servers.configuration.Configuration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
@@ -150,7 +155,11 @@
        }
        catch (Exception exception)
        {   
            Shared.println(Shared.getMessage("Model"), exception);
            exception.printStackTrace();
            StringWriter sw = new StringWriter();
            exception.printStackTrace(new PrintWriter(sw));
            FlatDialog.showDialog(null, Shared.getMessage("Error"), exception.getMessage() + "\n\n" + sw.toString(), true, FlatDialog.DIALOG_INFORMATION);
            System.exit(0);
        }
@@ -181,7 +190,10 @@
        catch (Exception exception)
        {
            exception.printStackTrace();
            Shared.println(Shared.getMessage("Model"), exception);
            StringWriter sw = new StringWriter();
            exception.printStackTrace(new PrintWriter(sw));
            FlatDialog.showDialog(null, Shared.getMessage("Error"), exception.getMessage() + "\n\n" + sw.toString(), true, FlatDialog.DIALOG_INFORMATION);
            System.exit(0);
        }       
        
        
@@ -459,6 +471,30 @@
            mutexListDevices.releaseRead();
        }
    }
    public Device[] getDevicesCopy()
    {
        mutexListDevices.lockRead();
        try
        {
            List<Device> result = new ArrayList<Device>();
            for (Device device : ldevice)
            {
                Device clone = Serialization.clone(device);
                clone.realtime = device.getDeviceRealtime();
                result.add(clone);
            }
            return result.toArray(new Device[result.size()]);
        }
        finally
        {
            mutexListDevices.releaseRead();
        }
    }
    
    
        
@@ -938,8 +974,10 @@
            }
            else
            {
                System.out.println("3.UpdateDevice: " + currentDevice);
                if (currentDevice.updateDevice(device) == true)
                {
                    System.out.println("4.UpdateDevice: " + currentDevice);
                    if (Shared.configuration.database != null)
                    {
                        Shared.controllerDatabase.timeless_updateOrAddObject(new DevicePersistenceTimeless(device));
@@ -1062,6 +1100,61 @@
    
    
    
    public void forceUpdateDeviceStatus(Device device, long timestamp)
    {
        try
        {
            device.forceLastTimestampStatusUpdate(timestamp);
            if (Shared.configuration.database != null)
            {
                try
                {
                    Shared.controllerDatabase.historical_updateOrAddObject(new DevicePersistenceHistorical(device));
                }
                catch (org.postgresql.util.PSQLException exception)
                {
                    if ((exception.getMessage().toUpperCase().indexOf("NO PARTITION OF RELATION") > -1) || (exception.getMessage().toUpperCase().indexOf("NO SE ENCONTRÓ UNA PARTICIÓN") > -1))
                    {
                        Calendar calendar1 = Calendar.getInstance();
                        calendar1.setTimeInMillis(device.getLastTimestampUpdate());
                        calendar1.set(Calendar.DATE, 1);
                        calendar1.set(Calendar.HOUR_OF_DAY, 0);
                        calendar1.set(Calendar.MINUTE, 0);
                        calendar1.set(Calendar.SECOND, 0);
                        calendar1.set(Calendar.MILLISECOND, 0);
                        SimpleDateFormat formato1 = new SimpleDateFormat("yyyy_MM");
                        String tablename = "devices_" + formato1.format(calendar1.getTimeInMillis());
                        long startdate = calendar1.getTimeInMillis();
                        calendar1.add(Calendar.MONTH, 1);
                        long enddate = calendar1.getTimeInMillis();
                        String query1 = "CREATE TABLE IF NOT EXISTS " + tablename + " PARTITION OF devices FOR VALUES FROM ('" + startdate + "') TO ('" + enddate + "')";
                        try{Shared.controllerDatabase.historical_update(query1);} catch (Exception e){};
                        executeAlterTableDevicesVacuum(true, tablename);
                        // Do again the insert
                        try
                        {
                            Shared.controllerDatabase.historical_updateOrAddObject(new DevicePersistenceHistorical(device));
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
        }
    }
    public void updateDevice(Device[] ldevice)
    {
        mutexListDevices.lockWrite();
@@ -1123,9 +1216,6 @@
        }
    }    
    
    public Device cloneDevice(String identifier)
    {
@@ -1278,6 +1368,158 @@
    }
    
    
    private String getFilterEventController(String filter)
    {
        /*
        */
        try
        {
            String result = filter;
            return(result);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return(filter);
    }
    public TraceResult getEventsController(String language, long timestampfrom, long timestampto, int type, int limit, int offset, String filter, boolean viewTrolleyEvents, boolean viewMasterSlaveEvents) throws Exception
    {
        ResultSet resultset = null;
        Persistence persistence = Shared.controllerDatabase.getHistoricalPersistance().get(0);
        TraceResult result = new TraceResult();
        result.timestampfrom = timestampfrom;
        result.timestampto = timestampto;
        result.limit = limit;
        result.offset = offset;
        result.type = type;
        result.traces = new ArrayList<Trace>();
        try
        {
            String command = "SELECT COUNT(*) FROM traces WHERE timestamp >= " + timestampfrom + " AND timestamp <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
            // if (filter != null) command = command + " AND " + getFilterEventController(filter.substring(0, filter.indexOf("ORDER")));
            resultset = persistence.executeQuery(command);
            if (resultset.next())
            {
                result.total = resultset.getInt(1);
            }
            try{resultset.close();} catch (Exception e){};
            command = "SELECT value FROM traces WHERE timestamp >= " + timestampfrom + " AND timestamp <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
//            if (order == 1) command = command + " ORDER BY timestamp ASC";
//            else if (order == -1) command = command + " ORDER BY timestamp DESC";
            command = command + " ORDER BY timestamp DESC";
            // if (filter != null) command = command + " AND " + getFilterEventController(filter);
            if (limit > 0) command = command + " LIMIT " + limit;
            if (offset > -1) command = command + " OFFSET " + offset;
            System.out.println(command);
            resultset = persistence.executeQuery(command);
            while (resultset.next())
            {
                TracePersistance tracePersistance = Serialization.deserialize(TracePersistance.class, resultset.getString(1));
                if (tracePersistance.trace != null)
                {
                    result.traces.add(tracePersistance.trace);
                }
                else
                {
                    Trace trace = Serialization.deserialize(Trace.class, resultset.getString(1));
                    if (trace != null)
                    {
                        result.traces.add(trace);
                    }
                }
            }
            return result;
        }
        finally
        {
            try{resultset.getStatement().close();} catch (Exception e){};
            try{resultset.close();} catch (Exception e){};
        }
    }
    public TraceResult getTraces(String language, long timestampfrom, long timestampto, int type, int limit, int offset, int order, boolean viewTrolleyEvents, boolean viewMasterSlaveEvents) throws Exception
    {
        ResultSet resultset = null;
        Persistence persistence = Shared.controllerDatabase.getHistoricalPersistance().get(0);
        TraceResult result = new TraceResult();
        result.timestampfrom = timestampfrom;
        result.timestampto = timestampto;
        result.limit = limit;
        result.offset = offset;
        result.type = type;
        result.traces = new ArrayList<Trace>();
        try
        {
            String command = "SELECT COUNT(*) FROM traces WHERE timestamp >= " + timestampfrom + " AND timestamp <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
            resultset = persistence.executeQuery(command);
            if (resultset.next())
            {
                result.total = resultset.getInt(1);
            }
            try{resultset.close();} catch (Exception e){};
            command = "SELECT value FROM traces WHERE timestamp >= " + timestampfrom + " AND timestamp <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
            if (order == 1) command = command + " ORDER BY timestamp ASC";
            else if (order == -1) command = command + " ORDER BY timestamp DESC";
            if (limit > 0) command = command + " LIMIT " + limit;
            if (offset > -1) command = command + " OFFSET " + offset;
            System.out.println(command);
            resultset = persistence.executeQuery(command);
            while (resultset.next())
            {
                TracePersistance tracePersistance = Serialization.deserialize(TracePersistance.class, resultset.getString(1));
                if (tracePersistance.trace != null)
                {
                    result.traces.add(tracePersistance.trace);
                }
                else
                {
                    Trace trace = Serialization.deserialize(Trace.class, resultset.getString(1));
                    if (trace != null)
                    {
                        result.traces.add(trace);
                    }
                }
            }
            return result;
        }
        finally
        {
            try{resultset.getStatement().close();} catch (Exception e){};
            try{resultset.close();} catch (Exception e){};
        }
    }
    
    
    public TraceResult getTraces(TraceResult traceresult) throws Exception
@@ -1364,7 +1606,61 @@
    
    // </editor-fold>      
    public EventTrolleyResult getEventsTrolley(String language, long timestampfrom, long timestampto, int type, int limit, int offset, int order) throws Exception
    {
        ResultSet resultset = null;
        Persistence persistence = Shared.controllerDatabase.getHistoricalPersistance().get(0);
        EventTrolleyResult result = new EventTrolleyResult();
        result.timestampfrom = timestampfrom;
        result.timestampto = timestampto;
        result.limit = limit;
        result.offset = offset;
        result.type = type;
        result.events = new ArrayList<EventTrolley>();
        try
        {
            String command = "SELECT COUNT(*) FROM eventstrolley WHERE activation >= " + timestampfrom + " AND activation <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
            resultset = persistence.executeQuery(command);
            if (resultset.next())
            {
                result.total = resultset.getInt(1);
            }
            try{resultset.close();} catch (Exception e){};
            command = "SELECT value FROM eventstrolley WHERE activation >= " + timestampfrom + " AND activation <  " + timestampto;
            if (type > -1) command = command + " AND type = " + type;
            if (order == 1) command = command + " ORDER BY activation ASC";
            else if (order == -1) command = command + " ORDER BY activation DESC";
            if (limit > 0) command = command + " LIMIT " + limit;
            if (offset > -1) command = command + " OFFSET " + offset;
            System.out.println(command);
            resultset = persistence.executeQuery(command);
            while (resultset.next())
            {
                result.events.add(((EventTrolley)Serialization.deserialize(EventTrolley.class, resultset.getString(1))));
            }
            return result;
        }
        finally
        {
            try{resultset.getStatement().close();} catch (Exception e){};
            try{resultset.close();} catch (Exception e){};
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="Symbols">
    
@@ -1374,7 +1670,8 @@
                
        try
        {
            for (int i=0; i<32; i++)
            // for (int i=0; i<32; i++)
            for (float i=0; i<32; i+=0.5f)
            {
                for (String symbolName : Shared.configuration.symbols)
                {
@@ -1382,8 +1679,14 @@
                    symbol.type = deviceClassName;
                    symbol.name = symbolName;
                    symbol.zoom = i;
                    int zoom = (int)i;
                    String resourceName = "data/" + Shared.getApplicationName() + "/symbols/" + i + "/" + symbolName + ".svg";
                    if (zoom == i)
                    {
                        resourceName = "data/" + Shared.getApplicationName() + "/symbols/" + zoom + "/" + symbolName + ".svg";
                    }
                    // String resourceName = "data/" + Shared.getApplicationName() + "/symbols/" + i + "/" + symbolName + ".svg";
                    File file = new File(resourceName);
                    
                    if (file.exists() == true)