package art.servers.bannerserver.protocol;
import art.library.model.devices.vms.banner.Banner;
import art.library.model.devices.vms.banner.commands.BannerRotulosElectronicosCommandsMessage;
import art.library.model.devices.vms.banner.information.BannerRotulosElectronicosInformation;
import art.library.model.devices.vms.banner.status.BannerRotulosElectronicosStatus;
import art.servers.ServerException;
import art.servers.Shared;
import java.io.ByteArrayOutputStream;
public class ProtocoloRotulosElectronicos
{
//
public static byte[] requestMessage(Banner banner, BannerRotulosElectronicosCommandsMessage command) throws ServerException
{
try
{
BannerRotulosElectronicosInformation information = banner.getDeviceInformation().rotulosElectronicos;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(0xA5);
int fontSize = 1;
int effectType = 0;
int alignment = 0;
int foreground = 0;
// Conversion from types
{
switch (command.fontSize)
{
case BannerRotulosElectronicosStatus.FONT_12 : fontSize = 1; break;
case BannerRotulosElectronicosStatus.FONT_16 : fontSize = 2; break;
}
switch (command.horizontalAlignment)
{
case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_LEFT : alignment = 0; break;
case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_CENTER : alignment = 1; break;
case BannerRotulosElectronicosStatus.ALIGNMENT_HORIZONTAL_RIGHT : alignment = 2; break;
}
switch (command.verticalAlignment)
{
case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_TOP : alignment = alignment + 0; break;
case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_CENTER : alignment = alignment + 4; break;
case BannerRotulosElectronicosStatus.ALIGNMENT_VERTICAL_BOTTOM : alignment = alignment + 8; break;
default : alignment = alignment = alignment + 4; break;
}
switch (command.effectType)
{
case BannerRotulosElectronicosStatus.EFFECT_NONE : effectType = 0; break;
case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_LEFT : effectType = 1; break;
case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_RIGHT : effectType = 2; break;
case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_HCENTER : effectType = 3; break;
case BannerRotulosElectronicosStatus.EFFECT_FROM_OPEN_FROM_VCENTER : effectType = 4; break;
case BannerRotulosElectronicosStatus.EFFECT_SHUTTER_VERTICAL : effectType = 5; break;
case BannerRotulosElectronicosStatus.EFFECT_MOVE_TO_LEFT : effectType = 6; break;
case BannerRotulosElectronicosStatus.EFFECT_MOVE_TO_RIGHT : effectType = 7; break;
case BannerRotulosElectronicosStatus.EFFECT_MOVE_UP : effectType = 8; break;
case BannerRotulosElectronicosStatus.EFFECT_MOVE_DOWN : effectType = 9; break;
case BannerRotulosElectronicosStatus.EFFECT_SCROLL_UP : effectType = 10; break;
case BannerRotulosElectronicosStatus.EFFECT_SCROLL_LEFT : effectType = 11; break;
case BannerRotulosElectronicosStatus.EFFECT_SCROLL_RIGHT : effectType = 12; break;
case BannerRotulosElectronicosStatus.EFFECT_FLICKER : effectType = 13; break;
case BannerRotulosElectronicosStatus.EFFECT_CONTINUOUS_SCROLL_LEFT : effectType = 14; break;
case BannerRotulosElectronicosStatus.EFFECT_CONTINUOUS_SCROLL_RIGHT : effectType = 15; break;
case BannerRotulosElectronicosStatus.EFFECT_SHUTTER_HORIZONTAL : effectType = 16; break;
}
switch (command.foreground)
{
case BannerRotulosElectronicosStatus.COLOR_RED : foreground = 1; break;
case BannerRotulosElectronicosStatus.COLOR_GREEN : foreground = 2; break;
case BannerRotulosElectronicosStatus.COLOR_YELLOW : foreground = 3; break;
case BannerRotulosElectronicosStatus.COLOR_BLUE : foreground = 4; break;
case BannerRotulosElectronicosStatus.COLOR_PURPLE : foreground = 5; break;
case BannerRotulosElectronicosStatus.COLOR_CYAN : foreground = 6; break;
case BannerRotulosElectronicosStatus.COLOR_WHITE : foreground = 7; break;
}
}
// CRC
{
bos.write(0x68);
bos.write(information.cardType);
bos.write(information.cardIdentifier);
bos.write(0x7B);
bos.write(0x01); // Receive response from CPU? (0 no, 1 yes)
int L = 12 + (command.text.length() * 3);
bos.write((L >> 0) & 0xFF); // Length
bos.write((L >> 8) & 0xFF); // Length
bos.write(0x00);
bos.write(0x00);
// Length
{
bos.write(0x02); // Send text
bos.write(0x00); // Window number
bos.write(effectType); // Effect type
bos.write(alignment); // Alignment
bos.write(command.effectSpeed); // Effect speed
bos.write((command.textTime >> 0) & 0xFF);
bos.write((command.textTime >> 8) & 0xFF);
for (int i=0; i> 0) & 0xFF);
bos.write((CRC >> 8) & 0xFF);
}
bos.write(0xAE);
bos.close();
return stuff(bos.toByteArray());
}
catch (Exception e)
{
throw new ServerException("Unable to construct protocol packet");
}
}
public static byte[] requestReset(Banner banner) throws ServerException
{
return stuff(getRequest(banner, 0x2D));
}
public static byte[] requestTemperature(Banner banner) throws ServerException
{
// Response {A5}{E8}{32}{01}{75}{01}{01}{01}{46}{04}{51}{00}{00}{00}{2E}{02}{AE}
return stuff(getRequest(banner, 0x75));
}
//
//
public static boolean responseReset(byte[] response) throws ServerException
{
if (response.length != 10) throw new ServerException(Shared.getMessage("Incorrect length"));
if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[response.length - 1] & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
return true;
}
public static boolean responseMessage(byte[] response) throws ServerException
{
if (response.length != 14) throw new ServerException(Shared.getMessage("Incorrect length"));
if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[response.length - 1] & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
return true;
}
public static float responseTemperature(byte[] response) throws ServerException
{
// Response
// {A5} : STX
// {E8} : Answer
// {32} : Card type
// {01} : Catd identifier
// {75} : Temperature operation
// {01}
// {01} : Query flag
// {01} : Temperature sign
// {46} : celsius
// {04}
// {51} : farenheit
// {00}{00}{00}
// {2E}{02}
// {AE}
if (response.length != 17) throw new ServerException(Shared.getMessage("Incorrect length"));
if (((int)response[0] & 0xFF) != 0xA5) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[1] & 0xFF) != 0xE8) throw new ServerException(Shared.getMessage("Incorrect response"));
if (((int)response[response.length - 1] & 0xFF) != 0xAE) throw new ServerException(Shared.getMessage("Incorrect response"));
float result = ((int)(response[7] & 0x7F) << 4) + ((float)((response[8] >> 4) & 0x0F));
if ((response[7] & 0x80) > 0)
{
result = result * -1;
}
return result;
}
//
//
private static byte[] getRequest(Banner banner, int functionCode) throws ServerException
{
try
{
BannerRotulosElectronicosInformation information = banner.getDeviceInformation().rotulosElectronicos;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(0xA5);
// CRC
{
bos.write(0x68);
bos.write(information.cardType);
bos.write(information.cardIdentifier);
bos.write(functionCode);
bos.write(0x01); // Receive response from CPU? (0 no, 1 yes)
}
// CRC calculation
{
int CRC = getCRC(bos);
bos.write((CRC >> 0) & 0xFF);
bos.write((CRC >> 8) & 0xFF);
}
bos.write(0xAE);
bos.close();
return bos.toByteArray();
}
catch (Exception e)
{
throw new ServerException("Unable to construct protocol packet");
}
}
private static int getCRC(ByteArrayOutputStream bos)
{
int CRC = 0;
byte[] data = bos.toByteArray();
for (int i=1; i
}