package art.servers.bannerserver.controller;
|
|
import art.library.interop.InteropParameters;
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.DeviceAction;
|
import art.library.model.devices.DeviceActionResult;
|
import art.library.model.devices.DeviceStatus;
|
import art.library.model.devices.vms.banner.Banner;
|
import art.library.model.devices.vms.banner.BannerCommands;
|
import art.library.model.devices.vms.banner.BannerRealtime;
|
import art.library.model.devices.vms.banner.information.BannerRotulosElectronicosInformationTeltonika;
|
import art.library.model.devices.vms.banner.status.BannerRotulosElectronicosStatus;
|
import art.library.net.snmp.SNMPReader;
|
import art.library.utils.licence.Licence;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.ServerException;
|
import art.servers.Shared;
|
import art.servers.bannerserver.protocol.ProtocoloRotulosElectronicos;
|
import java.io.ByteArrayOutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.Socket;
|
import java.net.URL;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
|
public class ControllerBannerRotulosElectronicos extends ControllerBanner
|
{
|
private Banner device = null;
|
private String name = null;
|
private Mutex mutex = new Mutex();
|
|
|
public ControllerBannerRotulosElectronicos(Banner device)
|
{
|
super(device);
|
this.device = device;
|
this.name = Shared.getMessage("Controller banner") + " " + device.information.name;
|
Thread.currentThread().setName(this.name);
|
}
|
|
|
public Banner getBanner()
|
{
|
return device;
|
}
|
|
|
public void run()
|
{
|
|
// Device initialisation
|
|
device.getDeviceStatus().status = DeviceStatus.STATUS_OFFLINE;
|
|
if (device.getDeviceStatus().rotulosElectronicos == null)
|
{
|
device.getDeviceStatus().rotulosElectronicos = new BannerRotulosElectronicosStatus();
|
device.getDeviceStatus().rotulosElectronicos.operationMode = BannerRotulosElectronicosStatus.OPERATION_MODE_AUTOMATIC;
|
}
|
|
|
while ((isInterrupted() == false) && (exit == false))
|
{
|
long startTimestamp = System.currentTimeMillis();
|
{
|
updateStatus();
|
}
|
|
// Wait
|
|
long timetowait = (device.getDeviceInformation().polling * 1000) - (System.currentTimeMillis() - startTimestamp);
|
timetowait = Math.min(timetowait, (device.getDeviceInformation().polling * 1000));
|
|
if (timetowait > 0)
|
{
|
try
|
{
|
sleep(timetowait);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
else
|
{
|
try{sleep(50);} catch (Exception e){};
|
}
|
|
if (Shared.model.existsDevice(device.getIdentifier()) == false)
|
{
|
exit = true;
|
}
|
}
|
|
}
|
|
|
|
|
|
|
|
|
private void updateStatus()
|
{
|
Banner bannerclone = Serialization.clone(device);
|
|
if (bannerclone == null) return; // Never should be null
|
|
try
|
{
|
BannerRealtime realtime = (BannerRealtime)bannerclone.getDeviceRealtime();
|
|
if (realtime == null)
|
{
|
realtime = new BannerRealtime();
|
bannerclone.realtime = realtime;
|
}
|
|
|
// Temperature value
|
|
realtime.temperature = ProtocoloRotulosElectronicos.responseTemperature(send(ProtocoloRotulosElectronicos.requestTemperature(bannerclone),17));
|
|
|
// Relay status
|
|
if (bannerclone.getDeviceInformation().rotulosElectronicos != null)
|
{
|
if (bannerclone.getDeviceInformation().rotulosElectronicos.teltonika != null)
|
{
|
BannerRotulosElectronicosInformationTeltonika teltonika = bannerclone.getDeviceInformation().rotulosElectronicos.teltonika;
|
SNMPReader snmpReader = new SNMPReader(teltonika.address, 161, 1, "public", 3000, 2);
|
|
try
|
{
|
String relay = snmpReader.getString("1.3.6.1.4.1.48690.5.5.0");
|
|
if (relay.equalsIgnoreCase("active"))
|
{
|
bannerclone.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_CLOSE;
|
}
|
else
|
{
|
bannerclone.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_OPEN;
|
}
|
}
|
catch (Exception e)
|
{
|
bannerclone.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_UNKNOWN;
|
}
|
|
snmpReader.disconnect();
|
}
|
}
|
|
|
// Send relay status again (it have been detecte banner state OPEN/CLOSE can change itself)
|
// TANCAT is relay open, if teltonika reboots then goes to normally open
|
// We could compare with bannerclone.getDeviceStatus().rotulosElectronicos.state, send and register
|
|
try
|
{
|
if (bannerclone.getIdentifier().equalsIgnoreCase("infopark-banner-6B"))
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
System.out.println(formato1.format(new Date()) + " : Desired state : " + bannerclone.getDeviceStatus().rotulosElectronicos.desiredState);
|
}
|
|
if (bannerclone.getDeviceInformation().rotulosElectronicos.teltonika != null)
|
{
|
switch (bannerclone.getDeviceStatus().rotulosElectronicos.desiredState)
|
{
|
case BannerRotulosElectronicosStatus.STATE_OPEN: open(bannerclone); break;
|
case BannerRotulosElectronicosStatus.STATE_CLOSE: close(bannerclone); break;
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
|
|
// Update
|
|
realtime.setLastTimestampUpdate(System.currentTimeMillis());
|
online(device, bannerclone);
|
|
}
|
catch (Exception e)
|
{
|
offline(device, bannerclone);
|
}
|
finally
|
{
|
Shared.model.updateDevice(device, bannerclone);
|
}
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Actions">
|
|
|
public DeviceAction[] sendCommands(InteropParameters parameters) throws ServerException
|
{
|
try
|
{
|
List<DeviceAction> result = new ArrayList<DeviceAction>();
|
Banner bannerclone = Serialization.clone(device);
|
BannerCommands bannerCommands = (BannerCommands)(Serialization.deserialize(BannerCommands.class, (String)parameters.getParameterValue("body-content")));
|
|
|
// Send commands
|
|
if ((bannerCommands.state == BannerCommands.STATE_OPEN) || (bannerCommands.state == BannerCommands.STATE_CLOSE))
|
{
|
BannerCommands command = new BannerCommands();
|
|
switch (bannerCommands.state)
|
{
|
case BannerCommands.STATE_OPEN: bannerclone.getDeviceStatus().rotulosElectronicos.desiredState = BannerRotulosElectronicosStatus.STATE_OPEN; break;
|
case BannerCommands.STATE_CLOSE: bannerclone.getDeviceStatus().rotulosElectronicos.desiredState = BannerRotulosElectronicosStatus.STATE_CLOSE; break;
|
}
|
|
command.state = bannerCommands.state;
|
result.add(state(parameters, bannerclone, command));
|
}
|
|
if (bannerCommands.operationMode != BannerCommands.OPERATION_NOTHING)
|
{
|
BannerCommands command = new BannerCommands();
|
command.operationMode = bannerCommands.operationMode;
|
result.add(operationMode(parameters, bannerclone, command));
|
}
|
|
if (bannerCommands.reset == BannerCommands.CONDITION_YES)
|
{
|
BannerCommands command = new BannerCommands();
|
command.reset = bannerCommands.reset;
|
result.add(reset(parameters, bannerclone, command));
|
}
|
|
if (bannerCommands.rotulosElectronicos != null)
|
{
|
BannerCommands command = new BannerCommands();
|
command.rotulosElectronicos = bannerCommands.rotulosElectronicos;
|
command.operationMode = bannerCommands.operationMode;
|
if (command.operationMode == BannerCommands.OPERATION_MANUAL)
|
{
|
bannerclone.getDeviceStatus().rotulosElectronicos.operationMode = BannerRotulosElectronicosStatus.OPERATION_MODE_MANUAL;
|
result.add(message(parameters, bannerclone, command));
|
}
|
else if (device.getDeviceStatus().rotulosElectronicos.operationMode == BannerRotulosElectronicosStatus.OPERATION_MODE_AUTOMATIC)
|
{
|
result.add(message(parameters, bannerclone, command));
|
}
|
}
|
|
|
// Update status
|
|
if (result.size() > 0)
|
{
|
if (isOnline(result) == true)
|
{
|
online(device, bannerclone);
|
}
|
else
|
{
|
offline(device, bannerclone);
|
}
|
|
|
Shared.model.updateDevice(device, bannerclone);
|
|
// Return result
|
|
return result.toArray(new DeviceAction[result.size()]);
|
}
|
else
|
{
|
throw new ServerException(Shared.getMessage("Nothing to send"));
|
}
|
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
throw new ServerException(exception);
|
}
|
}
|
|
|
|
|
|
|
private DeviceAction state(InteropParameters parameters, Banner banner, BannerCommands bannerCommand)
|
{
|
String language = (String)parameters.getParameterValue("language");
|
DeviceAction action = new DeviceAction(device, parameters, bannerCommand);
|
try
|
{
|
BannerRotulosElectronicosInformationTeltonika teltonika = banner.getDeviceInformation().rotulosElectronicos.teltonika;
|
|
if (teltonika != null)
|
{
|
try
|
{
|
// http://192.168.1.1/cgi-bin/output?username=user1&password=user1&action=on&pin=relay
|
// http://192.168.1.15/cgi-bin/output?username=admin&password=Info@nf19&action=on&pin=relay
|
|
String request = teltonika.URL;
|
request = request + "/output?username=" + Licence.decrypt(teltonika.user) + "&password=" + Licence.decrypt(teltonika.password) + "&pin=relay";
|
|
if (bannerCommand.state == BannerCommands.STATE_OPEN)
|
{
|
request = request + "&action=off";
|
}
|
else if (bannerCommand.state == BannerCommands.STATE_CLOSE)
|
{
|
request = request + "&action=on";
|
}
|
|
|
URL url = new URL(request);
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
connection.setRequestMethod("POST");
|
connection.addRequestProperty("User-Agent", "ALUVISA");
|
connection.setConnectTimeout(2000);
|
connection.setDoOutput(true);
|
|
int responseCode = connection.getResponseCode();
|
|
|
if (responseCode == 200)
|
{
|
if (bannerCommand.state == BannerCommands.STATE_OPEN)
|
{
|
banner.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_OPEN;
|
}
|
else if (bannerCommand.state == BannerCommands.STATE_CLOSE)
|
{
|
banner.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_CLOSE;
|
}
|
}
|
else
|
{
|
banner.getDeviceStatus().rotulosElectronicos.state = BannerRotulosElectronicosStatus.STATE_UNKNOWN;
|
}
|
|
if (connection != null) connection.disconnect();
|
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT);
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
|
}
|
else
|
{
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, Shared.getMessage("Operation not supported in this device"));
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
}
|
|
|
|
|
private DeviceAction operationMode(InteropParameters parameters, Banner banner, BannerCommands bannerCommand)
|
{
|
String language = (String)parameters.getParameterValue("language");
|
DeviceAction action = new DeviceAction(device, parameters, bannerCommand);
|
|
try
|
{
|
if (banner.getDeviceStatus().rotulosElectronicos == null)
|
{
|
banner.getDeviceStatus().rotulosElectronicos = new BannerRotulosElectronicosStatus();
|
}
|
banner.getDeviceStatus().rotulosElectronicos.operationMode = bannerCommand.operationMode;
|
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT);
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
catch (Exception exception)
|
{
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
}
|
|
|
|
private DeviceAction reset(InteropParameters parameters, Banner banner, BannerCommands bannerCommand)
|
{
|
String language = (String)parameters.getParameterValue("language");
|
DeviceAction action = new DeviceAction(device, parameters, bannerCommand);
|
|
try
|
{
|
// responseReset returns always true or exception
|
|
boolean result = ProtocoloRotulosElectronicos.responseReset(send(ProtocoloRotulosElectronicos.requestReset(banner), 10));
|
|
banner.getDeviceStatus().rotulosElectronicos.text = null;
|
banner.getDeviceStatus().rotulosElectronicos.textTime = 0;
|
banner.getDeviceStatus().rotulosElectronicos.foreground = BannerRotulosElectronicosStatus.COLOR_UNKNOWN;
|
banner.getDeviceStatus().rotulosElectronicos.fontSize = BannerRotulosElectronicosStatus.FONT_UNKNOWN;
|
banner.getDeviceStatus().rotulosElectronicos.horizontalAlignment = BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_UNKNOWN;
|
banner.getDeviceStatus().rotulosElectronicos.verticalAlignment = BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_UNKNOWN;
|
banner.getDeviceStatus().rotulosElectronicos.effectType = BannerRotulosElectronicosStatus.EFFECT_NONE;
|
banner.getDeviceStatus().rotulosElectronicos.effectSpeed = 0;
|
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT);
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
catch (Exception exception)
|
{
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
}
|
|
|
|
private DeviceAction message(InteropParameters parameters, Banner banner, BannerCommands bannerCommand)
|
{
|
String language = (String)parameters.getParameterValue("language");
|
DeviceAction action = new DeviceAction(device, parameters, bannerCommand);
|
|
try
|
{
|
// responseMessage returns always true or exception
|
|
boolean result = ProtocoloRotulosElectronicos.responseMessage(send(ProtocoloRotulosElectronicos.requestMessage(banner, bannerCommand.rotulosElectronicos), 14));
|
|
if (banner.getDeviceStatus().rotulosElectronicos == null)
|
{
|
banner.getDeviceStatus().rotulosElectronicos = new BannerRotulosElectronicosStatus();
|
}
|
|
banner.getDeviceStatus().rotulosElectronicos.foreground = bannerCommand.rotulosElectronicos.foreground;
|
banner.getDeviceStatus().rotulosElectronicos.fontSize = bannerCommand.rotulosElectronicos.fontSize;
|
banner.getDeviceStatus().rotulosElectronicos.horizontalAlignment = bannerCommand.rotulosElectronicos.horizontalAlignment;
|
banner.getDeviceStatus().rotulosElectronicos.verticalAlignment = bannerCommand.rotulosElectronicos.verticalAlignment;
|
banner.getDeviceStatus().rotulosElectronicos.effectType = bannerCommand.rotulosElectronicos.effectType;
|
banner.getDeviceStatus().rotulosElectronicos.effectSpeed = bannerCommand.rotulosElectronicos.effectSpeed;
|
banner.getDeviceStatus().rotulosElectronicos.text = bannerCommand.rotulosElectronicos.text;
|
banner.getDeviceStatus().rotulosElectronicos.textTime = bannerCommand.rotulosElectronicos.textTime;
|
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_CORRECT);
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
catch (Exception exception)
|
{
|
DeviceActionResult actionResult = new DeviceActionResult(DeviceActionResult.RESULT_ERROR, exception.getMessage());
|
action.setResult(actionResult);
|
Shared.model.addAction(action);
|
return action;
|
}
|
}
|
|
|
|
|
private boolean isOnline(List<DeviceAction> laction)
|
{
|
for (DeviceAction action : laction)
|
{
|
if (action.resultCode != DeviceAction.RESULT_CORRECT)
|
{
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
|
// </editor-fold>
|
|
|
|
|
|
|
|
|
|
private void online(Banner banner, Banner bannerclone)
|
{
|
try
|
{
|
bannerclone.getDeviceStatus().status = DeviceStatus.STATUS_ONLINE;
|
bannerclone.setAlarm("alarm_offline", false);
|
|
if (banner.getDeviceStatus().status != DeviceStatus.STATUS_ONLINE)
|
{
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
private void offline(Banner banner, Banner bannerclone)
|
{
|
try
|
{
|
bannerclone.getDeviceStatus().status = DeviceStatus.STATUS_OFFLINE;
|
bannerclone.alarms.clear();
|
bannerclone.setAlarm("alarm_offline", true);
|
|
if (banner.getDeviceStatus().status != DeviceStatus.STATUS_OFFLINE)
|
{
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
|
|
|
private byte[] send(byte[] request, int length) throws ServerException
|
{
|
mutex.lockWrite();
|
|
Socket socket = null;
|
|
try
|
{
|
socket = new Socket(device.getDeviceInformation().rotulosElectronicos.connectionServerTCP.address, device.getDeviceInformation().rotulosElectronicos.connectionServerTCP.port);
|
socket.setSoTimeout(device.getDeviceInformation().rotulosElectronicos.connectionServerTCP.timeout);
|
socket.getOutputStream().write(request);
|
byte[] answer = new byte[length];
|
socket.getInputStream().read(answer);
|
return answer;
|
}
|
catch (Exception e)
|
{
|
throw new ServerException(e.getMessage());
|
}
|
finally
|
{
|
try
|
{
|
if (socket != null) socket.close();
|
}
|
catch (Exception e)
|
{
|
}
|
|
mutex.releaseWrite();
|
}
|
|
|
}
|
|
|
|
|
|
private void open(Banner banner)
|
{
|
try
|
{
|
//if (banner.getDeviceStatus().rotulosElectronicos.state == BannerRotulosElectronicosStatus.STATE_OPEN) return;
|
|
BannerRotulosElectronicosInformationTeltonika teltonika = banner.getDeviceInformation().rotulosElectronicos.teltonika;
|
String request = teltonika.URL + "/output?username=" + Licence.decrypt(teltonika.user) + "&password=" + Licence.decrypt(teltonika.password) + "&pin=relay&action=on";
|
SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
System.out.println(formato1.format(new Date()) + " : " + banner.getIdentifier() + " : " + request);
|
URL url = new URL(request);
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
connection.setRequestMethod("POST");
|
connection.addRequestProperty("User-Agent", "ALUVISA");
|
connection.setConnectTimeout(10000);
|
connection.setDoOutput(true);
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
byte[] buffer = new byte[1024];
|
int length = 0;
|
while ((length = connection.getInputStream().read(buffer)) != -1) baos.write(buffer, 0, length);
|
System.out.println(new String(baos.toByteArray()));
|
baos.close();
|
|
connection.disconnect();
|
System.out.println(formato1.format(new Date()) + " : " + banner.getIdentifier() + " : " + connection.getResponseCode());
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
}
|
|
status(banner);
|
}
|
|
|
|
private void close(Banner banner)
|
{
|
try
|
{
|
if (banner.getDeviceStatus().rotulosElectronicos.state == BannerRotulosElectronicosStatus.STATE_CLOSE) return;
|
|
BannerRotulosElectronicosInformationTeltonika teltonika = banner.getDeviceInformation().rotulosElectronicos.teltonika;
|
String request = teltonika.URL + "/output?username=" + Licence.decrypt(teltonika.user) + "&password=" + Licence.decrypt(teltonika.password) + "&pin=relay&action=off";
|
SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
URL url = new URL(request);
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
connection.setRequestMethod("POST");
|
connection.addRequestProperty("User-Agent", "ALUVISA");
|
connection.setConnectTimeout(10000);
|
connection.setDoOutput(true);
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
byte[] buffer = new byte[1024];
|
int length = 0;
|
while ((length = connection.getInputStream().read(buffer)) != -1) baos.write(buffer, 0, length);
|
System.out.println(new String(baos.toByteArray()));
|
baos.close();
|
|
connection.disconnect();
|
System.out.println(formato1.format(new Date()) + " : " + banner.getIdentifier() + " : " + connection.getResponseCode());
|
}
|
catch (Exception exception)
|
{
|
exception.printStackTrace();
|
}
|
|
status(banner);
|
}
|
|
|
|
private void status (Banner banner)
|
{
|
BannerRotulosElectronicosInformationTeltonika teltonika = banner.getDeviceInformation().rotulosElectronicos.teltonika;
|
SNMPReader snmpReader = new SNMPReader(teltonika.address, 161, 1, "public", 3000, 2);
|
|
try
|
{
|
String relay = snmpReader.getString("1.3.6.1.4.1.48690.5.5.0");
|
SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
System.out.println(formato1.format(new Date()) + " : " + banner.getIdentifier() + " : " + relay);
|
}
|
catch (Exception e)
|
{
|
}
|
|
}
|
|
|
|
}
|