/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora.Api.Devices;
using Hermes;
using System;
using System.Text;
namespace VisionLight {
///
/// [상시 조명 컨트롤러 - 12CH / 시리얼 통신]
/// Range : (0,255) or (0,512)
/// Baudrate : 19200 bps
/// DataBit : 8 bit
/// StopBit : 1 bit
/// ParityBit : None
///
internal class Light_TSM_P12RV2 : LightBase_Serial {
public readonly string STX = "" + (char)0x2;
public readonly string ETX = "" + (char)0x3;
public readonly string CMD_ON = "ON";
public readonly string CMD_OFF = "ON"; // Off여도 Cmd가 "ON"임에 주의
public readonly string CMD_ALL_ON = "ALON";
public readonly string CMD_ALL_OFF = "ALON"; // All Off여도 Cmd가 "ALON"임에 주의
public readonly string CMD_LEVEL = "WR";
public readonly string CMD_ALL_LEVEL = "ALWR";
public readonly string CMD_READ_LEVEL = "RD";
//public readonly string CMD_SAVE_LEVEL = "SV";
public Light_TSM_P12RV2(string deviceId) : base(deviceId) {
}
#region Set On (Single or All)
protected override eLightResult SetOnImpl(int channel) {
lock (LockThis) {
if (SendData_SetOn(channel) == E1.OK) {
return eLightResult.OK;
}
else {
return eLightResult.NG;
}
}
}
private int SendData_SetOn(int channel) {
try {
byte[] sendData = CreateSetOnReqMsg(channel);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreateSetOnReqMsg(int channel) {
string msg = "";
if (channel != -1) {
// 01ON1 (Single)
msg = string.Format("{0}{1:00}{2}{3:D1}{4}", STX, channel, CMD_ON, 1, ETX);
}
else {
// ALON1 (All)
msg = string.Format("{0}{1}{2:D1}{3}", STX, CMD_ALL_ON, 1, ETX);
}
return Encoding.UTF8.GetBytes(msg);
}
#endregion
#region Set Off (Single or All)
protected override eLightResult SetOffImpl(int channel) {
lock (LockThis) {
if (SendData_SetOff(channel) == E1.OK) {
return eLightResult.OK;
}
else {
return eLightResult.NG;
}
}
}
private int SendData_SetOff(int channel) {
try {
byte[] sendData = CreateSetOffReqMsg(channel);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreateSetOffReqMsg(int channel) {
string msg = "";
if (channel != -1) {
// 01ON0 (Single)
msg = string.Format("{0}{1:00}{2}{3:D1}{4}", STX, channel, CMD_OFF, 0, ETX);
}
else {
// ALON0 (All)
msg = string.Format("{0}{1}{2:D1}{3}", STX, CMD_ALL_OFF, 0, ETX);
}
return Encoding.UTF8.GetBytes(msg);
}
#endregion
#region Get Level
private int SendData_GetLevel(int channel) {
try {
// 01RD120 (Single)
string msg = string.Format("{0}{1:00}{2}{3}", STX, channel, CMD_READ_LEVEL, ETX);
byte[] sendData = Encoding.UTF8.GetBytes(msg);
SerialConn.Write(sendData, sendData.Length); // READ 명령 이후에 값은 어떻게 받을 것인가?
// TEST CODE
byte[] readData = new byte[100];
SerialConn.Read(readData);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
#endregion
#region Set Level (Single or All)
protected override eLightResult SetLevelImpl(int level, int channel) {
lock (LockThis) {
if (SendData_SetLevel(level, channel) == E1.OK) {
return eLightResult.OK;
}
else {
return eLightResult.NG;
}
}
}
private int SendData_SetLevel(int level, int channel) {
try {
byte[] sendData = CreateSetLevelReqMsg(level, channel);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreateSetLevelReqMsg(int level, int channel) {
string msg = "";
if (channel != -1) {
// 01WR120 (Single)
msg = string.Format("{0}{1:00}{2}{3:D3}{4}", STX, channel, CMD_LEVEL, level, ETX);
}
else {
// ALWR120 (All)
msg = string.Format("{0}{1}{2:D3}{3}", STX, CMD_ALL_LEVEL, level, ETX);
}
return Encoding.UTF8.GetBytes(msg);
}
#endregion
}
}