2026-01-26 14:51:28 +09:00

429 lines
14 KiB
C#

using Aurora.Api.DataType;
using Aurora.Api.Devices;
using Hermes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AuroraLight.ALT
{
/// <summary>
/// 4Ch 검증 X 풍산 코드 기준. Protocol Doc과 다름. 4ch 모델에 따라 다른듯.
/// </summary>
internal class ALTLight : SerialConn, IALTLight
{
private string _deviceId;
private eALTLightType _type;
public bool _commError { get; set; } = false;
private SerialConnConfig _connConfig;
readonly int BYTE_MAX = 1024;
/// <summary>
///
/// </summary>
/// <param name="deviceId">device name (eigen factor)</param>
/// <param name="isSimul">Not used</param>
/// <param name="log">Log</param>
/// <param name="type">Channel 4CH, 8CH</param>
public ALTLight(string deviceId, bool isSimul, eALTLightType type) : base(deviceId, isSimul)
{
_deviceId = deviceId;
_type = type;
LightValue = new int[(int)type];
}
public int[] LightValue { get; set; }
public bool InitInternal(SerialConnConfig conn)
{
_connConfig = conn;
LightValue = Enumerable.Repeat(0, (int)_type).ToArray<int>();
bool bRet = false;
bRet = Open(_connConfig) == E1.OK;
if (bRet) {
bRet = OffLightInternal() == E1.OK;
if (bRet)
Log.D(string.Format("[ {0} CONNECTED ]", _deviceId));
_commError = !bRet;
} else {
_commError = true;
}
return bRet;
}
public bool IsPortErrorInternal()
{
return _commError;
}
public int OffLightInternal()
{
if (_type == eALTLightType.Light_4CH) {
if (LightCtrl_4CH_OFF()) return E1.OK;
else return E2.INTERNAL_ERROR;
} else {
if (LightCtrl_8CH_OFF()) return E1.OK;
else return E2.INTERNAL_ERROR;
}
}
public int OffLightInternal(int iChannel)
{
if (ChannelValidCheck(iChannel)) {
if (_type == eALTLightType.Light_4CH) {
if (LightCtrl_4CH_CH_Send(iChannel, 0)) return E1.OK;
else return E2.INTERNAL_ERROR;
} else {
if (LightCtrl_8CH_CH_Send(iChannel, 0)) return E1.OK;
else return E2.INTERNAL_ERROR;
}
} else {
return E2.INVALID_ARGUMENT;
}
}
public int SetLightValueInternal(List<int> values)
{
if (values.Count <= (int)_type) {
if (values.Count != (int)_type) {
var list = Enumerable.Repeat(0, values.Count - (int)_type).ToArray<int>();
values.AddRange(list);
}
int[] _values = values.ToArray<int>();
switch (_type) {
case eALTLightType.Light_4CH:
if (LightCtrl_4CH_ALL_Send(_values)) {
return E1.OK;
}
break;
case eALTLightType.Light_8CH:
if (LightCtrl_8CH_ALL_Send(_values)) {
return E1.OK;
}
break;
}
return E2.INTERNAL_ERROR;
} else {
Log.W($"Not supported chanel number. Check please. ( InputValueCount : {values.Count} )");
return E2.INVALID_ARGUMENT;
}
}
public int SetLightValueInternal(int iChannel, int iValue)
{
if (ChannelValidCheck(iChannel)) {
if (!ValueValidCheck(iValue)) {
return E2.INVALID_ARGUMENT;
}
switch (_type) {
case eALTLightType.Light_4CH:
if (LightCtrl_4CH_CH_Send(iChannel, iValue)) {
return E1.OK;
}
break;
case eALTLightType.Light_8CH:
if (LightCtrl_8CH_CH_Send(iChannel, iValue)) {
return E1.OK;
}
break;
}
return E2.INTERNAL_ERROR;
} else {
return E2.INVALID_ARGUMENT;
}
}
#region Util
private bool CommandBeforeCheck()
{
if (!IsPortOpen())
Open(_connConfig);
if (!IsPortOpen()) {
_commError = true;
return false;
}
if (_commError)
return false;
return true;
}
private bool ChannelValidCheck(int iChannel)
{
if (iChannel < (int)_type && iChannel >= 0) {
return true;
} else {
Log.W($"Channel is not exist. Range : 0~{(int)_type - 1} Input : {iChannel}");
return false;
}
}
private bool ValueValidCheck(int iValue)
{
if (iValue <= 255 && iValue >= 0) {
return true;
} else {
Log.W($"LightValue Range Error. Range : 0~255, Input : {iValue}");
return false;
}
}
private string ReadString(byte[] readData)
{
if (Read(readData) > 0) {
//추후 ReadExisting 관련해서 수정 --> readData.ReadExisting
string recvMsg = Encoding.UTF8.GetString(readData).Trim('\0');
return recvMsg;
} else {
return null;
}
}
#endregion
#region ALT Command
#region ALT_2/4CH
bool LightCtrl_4CH_CH_Send(int iCH, int iValue)
{
byte[] SendData = new byte[7];
try {
if (!CommandBeforeCheck()) return false;
char[] cData = iValue.ToString().PadLeft(3, '0').ToCharArray();
SendData[0] = 0x4C;
SendData[1] = (byte)Convert.ToChar(iCH.ToString());
SendData[2] = (byte)cData[0];
SendData[3] = (byte)cData[1];
SendData[4] = (byte)cData[2];
SendData[5] = 0x0D;
SendData[6] = 0x0A;
if (Write(SendData)) {
LightValue[iCH] = iValue;
_commError = false;
return true;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
bool LightCtrl_4CH_OFF()
{
byte[] SendData = new byte[10];
try {
bool bRet = false;
string strEndCheck = "", strStartCheck = "";
byte[] rtn = new byte[BYTE_MAX];
string strRtn;
SendData = new byte[] { 0xEF, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEE, 0xEE };
SendData[7] = Convert.ToByte(SendData[3] ^ SendData[4] ^ SendData[5] ^ SendData[6]);
if (!CommandBeforeCheck()) return false;
if (Write(SendData)) {
string str = new UTF8Encoding().GetString(SendData);
if ((strRtn = ReadString(rtn)) != null) {
strStartCheck = strRtn.Substring(0, 2);
strEndCheck = strRtn.Substring(strRtn.Length - 2);
if (strStartCheck == "??" && strEndCheck == "??") {
for (int i = 0; i < LightValue.Length; i++) {
LightValue[i] = 0;
}
bRet = true;
}
} else {
bRet = false;
}
_commError = !bRet;
return bRet;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
bool LightCtrl_4CH_ALL_Send(int[] iValue)
{
byte[] SendData = new byte[10];
try {
bool bRet = false;
string strEndCheck = "", strStartCheck = "";
byte[] rtn = new byte[BYTE_MAX];
string strRtn;
SendData[0] = 0xEF;
SendData[1] = 0xEF;
SendData[2] = 0x00;
for (int i = 0; i < 4; i++) {
if (!ValueValidCheck(iValue[i])) { return false; }
SendData[3 + i] = Convert.ToByte(iValue[i]);
}
SendData[7] = Convert.ToByte(SendData[3] ^ SendData[4] ^ SendData[5] ^ SendData[6]);
SendData[8] = 0xEE;
SendData[9] = 0xEE;
if (!CommandBeforeCheck()) return false;
if (Write(SendData)) {
string str = new UTF8Encoding().GetString(SendData);
if ((strRtn = ReadString(rtn)) != null) {
strStartCheck = strRtn.Substring(0, 2);
strEndCheck = strRtn.Substring(strRtn.Length - 2);
if (strStartCheck == "??" && strEndCheck == "??") {
for (int i = 0; i < LightValue.Length; i++) {
LightValue[i] = 0;
}
bRet = true;
}
} else {
bRet = false;
}
_commError = !bRet;
return bRet;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
#endregion
#region ALT_8CH
bool LightCtrl_8CH_CH_Send(int iCH, int iValue)
{
byte[] SendData = new byte[7];
try {
SendData[0] = 0x4C;
SendData[1] = Convert.ToByte(iCH + 0x30);
SendData[2] = Convert.ToByte((iValue / 100) + 0x30);
SendData[3] = Convert.ToByte(((iValue % 100) / 10) + 0x30);
SendData[4] = Convert.ToByte(((iValue % 100) % 10) + 0x30);
SendData[5] = 0x0D;
SendData[6] = 0x0A;
if (!CommandBeforeCheck()) return false;
if (Write(SendData)) {
_commError = false;
LightValue[iCH] = iValue;
return true;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
bool LightCtrl_8CH_OFF()
{
byte[] SendData = new byte[14];
string strRtn;
try {
bool bRet = false;
byte[] rtn = new byte[BYTE_MAX];
SendData[0] = 0xEF;
SendData[1] = 0xEF;
SendData[2] = 0x00;
for (int i = 0; i < 8; i++) {
SendData[3 + i] = 0x00;
}
SendData[11] = 0x01; // CheckSum
SendData[12] = 0xEE;
SendData[13] = 0xEE;
if (!CommandBeforeCheck()) return false;
if (Write(SendData)) {
string str = new UTF8Encoding().GetString(SendData);
if ((strRtn = ReadString(rtn)) != null) {
bRet = true;
for (int i = 0; i < LightValue.Length; i++) {
LightValue[i] = 0;
}
} else {
bRet = false;
}
_commError = !bRet;
return bRet;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
bool LightCtrl_8CH_ALL_Send(int[] iValue)
{
byte[] SendData = new byte[14];
int iChkSum = 0;
string strRtn;
try {
bool bRet = false;
byte[] rtn = new byte[BYTE_MAX];
SendData[0] = 0xEF;
SendData[1] = 0xEF;
SendData[2] = 0x00;
for (int i = 0; i < 8; i++) {
if (!ValueValidCheck(iValue[i])) { return false; }
SendData[3 + i] = Convert.ToByte(iValue[i]);
}
for (int i = 0; i < 7; i++) {
iChkSum = iChkSum ^ iValue[i];
}
iChkSum = iChkSum ^ (iValue[7] + 0x01);
SendData[11] = Convert.ToByte(iChkSum);
SendData[12] = 0xEE;
SendData[13] = 0xEE;
if (!CommandBeforeCheck()) return false;
if (Write(SendData)) {
string str = new UTF8Encoding().GetString(SendData);
if ((strRtn = ReadString(rtn)) != null) {
bRet = true;
for (int i = 0; i < iValue.Length; i++) {
LightValue[i] = iValue[i];
}
} else {
bRet = false;
}
_commError = !bRet;
return bRet;
} else {
_commError = true;
return false;
}
} catch {
_commError = true;
return false;
}
}
#endregion
#endregion
}
}