package art.servers.rtzserver.controller; import art.library.interop.InteropParameters; import art.library.interop.serialization.Serialization; import art.library.interop.serialization.SerializationException; import art.library.model.devices.DeviceAction; import art.library.model.devices.colors.controller.RTZ32.RTZ32_Controller; import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerCommands; import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerConfiguration; import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerRealtime; import art.library.model.devices.colors.controller.RTZ32.RTZ32_ControllerStatus; import art.library.model.devices.colors.controller.RTZ32.commands.RTZ32_Commands_Configuration_Write; import art.library.model.devices.colors.controller.RTZ32.configuration.RTZ32_Configuration; import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime; import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime_Reading; import art.library.model.devices.colors.controller.RTZ32.realtime.RTZ32_Realtime_Writing; import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status; import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status_Centralized; import art.library.model.devices.colors.controller.RTZ32.status.RTZ32_Status_Realtime; import art.library.model.devices.colors.controller.RTZ32.types.RTZ32_Persistent_Configuration; import art.library.utils.synchro.Mutex; import art.servers.ServerException; import art.servers.Shared; import art.servers.rtzserver.controller.RTZ32.RTZ32_Talker; import art.servers.rtzserver.controller.RTZ32.RTZ32_Talker_Listener; import art.servers.rtzserver.gui.RTZ32_Window_Realtime; import java.awt.Dimension; import java.io.File; import static java.lang.Thread.sleep; import java.util.Date; public class Controller_RTZ32 extends Controller { public String name = null; protected RTZ32_Controller device_rtz32 = null; protected RTZ32_Talker talker = null; protected Mutex mutexUpdate = new Mutex(); private boolean forceUpdate = false; protected RTZ32_Status_Centralized centralized = null; protected Boolean realtime_junction = null; protected Boolean realtime_groups = null; protected Boolean realtime_phases = null; public Controller_RTZ32(RTZ32_Controller device_rtz32) { super(device_rtz32); this.device_rtz32 = device_rtz32; this.name = art.servers.Shared.getMessage("Controller") + " " + this.device_rtz32.getDeviceInformation().name; this.setName(this.name); try { this.device_rtz32.configuration = ((RTZ32_Controller)Serialization.deserialize(RTZ32_Controller.class, new File("controller-zaragoza-90020_v1.json"))).configuration; this.device_rtz32.status = new RTZ32_ControllerStatus(); this.device_rtz32.getDeviceStatus().rtz32 = new RTZ32_Status(); this.device_rtz32.getDeviceStatus().rtz32.realtime = new RTZ32_Status_Realtime(); this.device_rtz32.getDeviceStatus().rtz32.realtime.junction = true; // Controller_RTZ32_Factory_Configuration factory = new Controller_RTZ32_Factory_Configuration(this.device_rtz32.getDeviceConfiguration().rtz32); // factory.serialize(); // Serialization.serialize(this.device_rtz32.getDeviceConfiguration().rtz32, new File("test1.json")); // RTZ32_Configuration config = factory.deserialize(); // Serialization.serialize(config, new File("test2.json")); RTZ32_Window_Realtime window = new RTZ32_Window_Realtime(device_rtz32); window.setSize(new Dimension(1024, 768)); window.setLocation(0, 0); window.setVisible(true); // RTZ32_Window window2 = new RTZ32_Window(device_rtz32, null); // window2.setSize(new Dimension(1024, 768)); // window2.setLocation(0, 0); // window2.setVisible(true); } catch (Exception exception) { exception.printStackTrace(); } // Initialise if ((this.device_rtz32.status == null) || (this.device_rtz32.getDeviceStatus().rtz32 == null)) { this.device_rtz32.status = new RTZ32_ControllerStatus(); this.device_rtz32.getDeviceStatus().rtz32 = new RTZ32_Status(); } if ((this.device_rtz32.realtime == null) || (this.device_rtz32.getDeviceRealtime().rtz32 == null)) { this.device_rtz32.realtime = new RTZ32_ControllerRealtime(); this.device_rtz32.getDeviceRealtime().rtz32 = new RTZ32_Realtime(); } if ((this.device_rtz32.configuration == null) || (this.device_rtz32.getDeviceConfiguration().rtz32 == null)) { this.device_rtz32.configuration = new RTZ32_ControllerConfiguration(); this.device_rtz32.getDeviceConfiguration().rtz32 = new RTZ32_Configuration(); } try { this.device_rtz32.configuration = ((RTZ32_Controller)Serialization.deserialize(RTZ32_Controller.class, new File("controller-zaragoza-90020_v1.json"))).configuration; } catch (Exception exception) { } talker = new RTZ32_Talker(name, this.device_rtz32, Serialization.clone(this.device_rtz32)); talker.setListener(new RTZ32_Talker_Listener() { public void connected() { device_rtz32.realtime = null; forceUpdate = true; } public void disconnected() { device_rtz32.realtime = null; forceUpdate = true; } }); } public RTZ32_Controller getController() { return this.device_rtz32; } public void run() { Shared.traceInformation(name, "Starting"); try { if (Shared.isServerEnabled() == true) { update(); } long timestamp1 = System.currentTimeMillis(); while ((isInterrupted() == false) && (exit == false)) { long timestamp2 = timestamp1 + (device_rtz32.getDeviceInformation().polling * 1000); if ((System.currentTimeMillis() > timestamp2) || (forceUpdate == true)) { try { forceUpdate = false; if (Shared.isServerEnabled() == true) { update(); } } catch (Exception exception) { } timestamp1 = timestamp1 + device_rtz32.getDeviceInformation().polling * 1000; } sleep(1000); if (Shared.model.existsDevice(device_rtz32.getIdentifier()) == false) { Shared.println(name, Shared.getMessage("Device no longer exists")); Shared.traceInformation(name, "Device no longer exists"); exit = true; } } } catch (Exception exception) { } Shared.traceInformation(name, "Finishing"); } // public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException, SerializationException, Exception { RTZ32_ControllerCommands commands = parameters.getBodyContentValue(RTZ32_ControllerCommands.class); RTZ32_Controller clon = Serialization.clone(device_rtz32); return Controller_RTZ32_Actions.sendCommands(this, clon, parameters, commands); } // // boolean init = true; protected void update() throws Exception { RTZ32_Controller rtz32 = getController(); RTZ32_Controller clon = talker.getRTZ32(); // Tables // try // { // if (talker.isConnected() == true) // { // if (rtz32.getDeviceConfiguration().rtz32 == null) // { // configurationRead(Shared.getApplicationName(), rtz32, clon); // } // } // } // catch (Exception exception) // { // } // States and alarms mutexUpdate.lockWrite(); try { if (talker.isConnected() == true) { clon.setAlarm("alarm_offline", false); // talker.writer.CTRL_DC1(); // RTZ no hace falta, RMZ no sabemos // talker.writer.PET_ESTADO(); // talker.writer.PET_ALARMAS(0x01); // talker.writer.PET_ALARMAS(0x02); // talker.writer.PET_ALARMAS(0x03); // talker.writer.PET_ALARMAS(0x04); // talker.writer.PET_ALARMAS(0x05); // talker.writer.PET_ALARMAS(0x06); // talker.writer.PET_ALARMAS(0x07); // talker.writer.PET_ALARMAS(0x08); // talker.writer.PET_ALARMAS(0x09); // talker.writer.PET_ALARMAS(0x0A); // talker.writer.PET_ALARMAS(0x0F); // talker.writer.PET_FH(); // talker.writer.PET_VERSION(); talker.writer.PET_PLAN_DESFASE(); // talker.writer.PET_CONTADORES(); // talker.writer.PET_DETECTORES(); // talker.writer.PET_GRUPOS(); if (rtz32.getDeviceStatus().rtz32.realtime != null) { RTZ32_Status_Realtime status_realtime = rtz32.getDeviceStatus().rtz32.realtime; if (status_realtime.junction == true) { talker.writer.ORD_TIEMPO_REAL_CRUCE(true); talker.writer.ORD_TIEMPO_REAL_FASES(true); } else if ((status_realtime.junction == false) && (status_realtime.phases == false) && (status_realtime.groups == false)) { talker.writer.ORD_TIEMPO_REAL_CRUCE(false); talker.writer.ORD_TIEMPO_REAL_FASES(false); // rtz32.getDeviceRealtime().rtz32.clearImpulsesGroups(); //TODO // rtz32.getDeviceRealtime().rtz32.clearImpulsesPhases(); } else if ((status_realtime.junction == false) && (status_realtime.phases == true) && (status_realtime.groups == false)) { talker.writer.ORD_TIEMPO_REAL_CRUCE(false); talker.writer.ORD_TIEMPO_REAL_FASES(true); // rtz32.getDeviceRealtime().rtz32.clearImpulsesGroups(); //TODO } else if ((status_realtime.junction == false) && (status_realtime.phases == false) && (status_realtime.groups == true)) { talker.writer.ORD_TIEMPO_REAL_CRUCE(true); talker.writer.ORD_TIEMPO_REAL_FASES(false); // rtz32.getDeviceRealtime().rtz32.clearImpulsesPhases(); //TODO } } // Realtime timestamp update if (rtz32.getDeviceRealtime().rtz32 != null) { long timestamp = rtz32.getDeviceRealtime().getLastTimestampUpdate(); if (rtz32.getDeviceRealtime().rtz32.counters != null) timestamp = Math.max(timestamp, rtz32.getDeviceRealtime().rtz32.counters.lastTimestampUpdate); if (rtz32.getDeviceRealtime().rtz32.groups != null) timestamp = Math.max(timestamp, rtz32.getDeviceRealtime().rtz32.groups.lastTimestampUpdate); if (rtz32.getDeviceRealtime().rtz32.detectors != null) timestamp = Math.max(timestamp, rtz32.getDeviceRealtime().rtz32.detectors.lastTimestampUpdate); if (rtz32.getDeviceRealtime().rtz32.phase != null) timestamp = Math.max(timestamp, rtz32.getDeviceRealtime().rtz32.phase.lastTimestampUpdate); if (rtz32.getDeviceRealtime().rtz32.clock != null) timestamp = Math.max(timestamp, rtz32.getDeviceRealtime().rtz32.clock.lastTimestampUpdate); rtz32.getDeviceRealtime().setLastTimestampUpdate(timestamp); } // Centralized command update // Realtime command update if (centralized != null) clon.getDeviceStatus().rtz32.centralized = centralized; if (((realtime_junction != null) || (realtime_phases != null) || (realtime_groups != null)) && (clon.getDeviceStatus().rtz32.realtime == null)) { clon.getDeviceStatus().rtz32.realtime = new RTZ32_Status_Realtime(); if (realtime_junction != null) clon.getDeviceStatus().rtz32.realtime.junction = realtime_junction; if (realtime_phases != null) clon.getDeviceStatus().rtz32.realtime.phases = realtime_phases; if (realtime_groups != null) clon.getDeviceStatus().rtz32.realtime.groups = realtime_groups; } } else { clon.setAlarm("alarm_offline", true); if (device_rtz32.realtime != null) { device_rtz32.realtime = null; device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); } } } catch (Exception exception) { Shared.printerr(name, exception.getMessage()); clon.setAlarm("alarm_offline", true); if (device_rtz32.realtime != null) { device_rtz32.realtime = null; device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); } } finally { update(rtz32, clon); mutexUpdate.releaseWrite(); } } private void update(RTZ32_Controller rtz32, RTZ32_Controller clon) { try { //TODO:quitar RTZ32_Controller borrar = (RTZ32_Controller)Serialization.deserialize(RTZ32_Controller.class, new File("controller-zaragoza-90020_v1.json")); clon.configuration = borrar.configuration; if (Shared.model.updateDevice(rtz32, clon) == true) { Serialization.serialize(rtz32, new File(rtz32.getIdentifier() + ".json")); Shared.println(name, "Updated"); } } catch (Exception exception) { Shared.printerr(name, exception.getMessage()); } } // // protected void configurationRead(String username) throws Exception { RTZ32_Controller rtz32 = getController(); RTZ32_Controller clon = talker.getRTZ32(); configurationRead(username, rtz32, clon); } private void configurationRead(String username, RTZ32_Controller rtz32, RTZ32_Controller clon) throws ServerException { if ((rtz32.getDeviceRealtime().rtz32 != null) && (rtz32.getDeviceRealtime().rtz32.reading != null)) { // Already writing throw new ServerException("Already reading tables"); } if (((rtz32.getDeviceRealtime().rtz32 != null) && rtz32.getDeviceRealtime().rtz32.writing != null) ) { // Already writing throw new ServerException("Controller is writing tables"); } mutexUpdate.lockWrite(); rtz32.getDeviceRealtime().rtz32.reading = new RTZ32_Realtime_Reading(); rtz32.getDeviceRealtime().rtz32.reading.timestamp = System.currentTimeMillis(); rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); try { talker.writer.CTRL_DC1(); for (int i=1; i<=7; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=11; i<=28; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=31; i<=40; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=42; i<=46; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=50; i<=60; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=65; i<=75; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=78; i<=85; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); for (int i=91; i<=95; i++) PET_TABLA(i, rtz32.getDeviceRealtime().rtz32.reading); // Tables readed, insert in historical RTZ32_Persistent_Configuration configuration = new RTZ32_Persistent_Configuration(); configuration.device = rtz32.getIdentifier(); configuration.datetime = new Date(); configuration.username = username; configuration.action = "Download tables"; configuration.operation = "All"; configuration.distribution = "All"; configuration.description = "Download tables from controller"; configuration.configuration = clon.getDeviceConfiguration(); Controller_RTZ32_Database.addConfiguration(configuration); } catch (Exception exception) { // We let same configuration we have clon.configuration = Serialization.clone(rtz32.configuration); throw new ServerException(exception.getMessage()); } finally { rtz32.getDeviceRealtime().rtz32.reading = null; rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); mutexUpdate.releaseWrite(); } } private void PET_TABLA(int number, RTZ32_Realtime_Reading reading) throws Exception { reading.table = number; long timestamp1 = System.currentTimeMillis(); talker.writer.PET_TABLA(number); long timestamp2 = System.currentTimeMillis(); if ((timestamp2-timestamp1) > talker.rtz32_current.getDeviceInformation().connection.readTimeout) { throw new Exception("Timeout"); } reading.progress = reading.progress + 1.3f; device_rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); } // // public void configurationWrite(String username, RTZ32_Commands_Configuration_Write command) throws ServerException { if (device_rtz32.getDeviceRealtime().rtz32.writing != null) { // Already writing throw new ServerException("Already writing tables"); } if (device_rtz32.getDeviceRealtime().rtz32.reading != null) { // Already writing throw new ServerException("Conntroller is reading tables"); } mutexUpdate.lockWrite(); RTZ32_Controller rtz32 = getController(); RTZ32_Controller clon = talker.getRTZ32(); rtz32.getDeviceRealtime().rtz32.writing = new RTZ32_Realtime_Writing(); rtz32.getDeviceRealtime().rtz32.writing.timestamp = System.currentTimeMillis(); rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); try { // Tables erite, insert in historical RTZ32_Persistent_Configuration configuration = new RTZ32_Persistent_Configuration(); configuration.device = rtz32.getIdentifier(); configuration.datetime = new Date(); configuration.username = username; configuration.action = "Uploaded tables"; if (command.operationTables == true) configuration.operation = "All"; else configuration.operation = "None"; if (command.distributionTables == true) { if ((command.programs == null) || (command.programs.size() == 0)) { configuration.distribution = "All"; } else { configuration.distribution = command.getProgramsTables(); } } else { configuration.distribution = "None"; } configuration.description = "Uploaded tables to controller"; ; configuration.configuration = clon.getDeviceConfiguration(); Controller_RTZ32_Database.addConfiguration(configuration); } catch (Exception exception) { } finally { rtz32.getDeviceRealtime().rtz32.reading = null; rtz32.getDeviceRealtime().setLastTimestampUpdate(System.currentTimeMillis()); mutexUpdate.releaseWrite(); } } // }