251 lines
8.7 KiB
C#
251 lines
8.7 KiB
C#
using Aurora.Api.Devices;
|
|
using Hermes;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace AuroraLight.ERI
|
|
{
|
|
internal class ERILight : SerialConn, IERILight
|
|
{
|
|
private string _deviceId;
|
|
private int _lightChannelCount;
|
|
|
|
public bool _commError { get; set; } = false;
|
|
private SerialConnConfig _connConfig;
|
|
readonly int BYTE_MAX = 1024;
|
|
|
|
public ERILight(string deviceId, bool isSimul, int lightChannelCount) : base(deviceId, isSimul)
|
|
{
|
|
_deviceId = deviceId;
|
|
_lightChannelCount = lightChannelCount;
|
|
LightValue = new int[lightChannelCount];
|
|
IsOn = new bool[lightChannelCount];
|
|
}
|
|
public int[] LightValue { get; set; }
|
|
public bool[] IsOn { get; set; }
|
|
|
|
public bool InitInternal(SerialConnConfig conn)
|
|
{
|
|
_connConfig = conn;
|
|
LightValue = Enumerable.Repeat(0, (int)_lightChannelCount).ToArray<int>();
|
|
IsOn = Enumerable.Repeat(false, (int)_lightChannelCount).ToArray<bool>();
|
|
|
|
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()
|
|
{
|
|
try {
|
|
if (!IsConnected()) return E2.NOT_CONNECTED;
|
|
SetAllLightPowerStatus(false);
|
|
return E1.OK;
|
|
} catch (Exception e) {
|
|
Log.W($"OffLightInternal Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
public int OffLightInternal(int iChannel)
|
|
{
|
|
try {
|
|
if (!IsConnected()) return E2.NOT_CONNECTED;
|
|
SetLightPowerStatus(iChannel, false);
|
|
return E1.OK;
|
|
} catch (Exception e) {
|
|
Log.W($"{iChannel}ch OffLightInternal Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
public int SetLightValueInternal(List<int> values)
|
|
{
|
|
try {
|
|
bool bRet = true;
|
|
for (int i = 0; i < values.Count; i++) {
|
|
if (SetLightBrightness(i, values[i]) != E1.OK)
|
|
bRet = false;
|
|
}
|
|
return bRet ? E1.OK : E2.CANCELLED;
|
|
} catch (Exception e) {
|
|
Log.W($"SetLightValueInternal Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
|
|
public int SetLightValueInternal(int iChannel, int iValue)
|
|
{
|
|
try {
|
|
int nRet = SetLightBrightness(iChannel, iValue);
|
|
if (nRet != E1.OK) return nRet;
|
|
SetLightPowerStatus(iChannel, true);
|
|
return E1.OK;
|
|
} catch (Exception e) {
|
|
Log.W($"{iChannel}ch SetLightValueInternal Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
public void SetAllLightPowerStatus(bool on)
|
|
{
|
|
for (int i = 0; i < _lightChannelCount; i++) {
|
|
SetLightPowerStatus(i, on);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 조명의 채널을 키고 끄는 함수(밝기 값은 유지됨)
|
|
/// </summary>
|
|
/// <param name="channel">설정할 조명 채널(0부터 시작)</param>
|
|
/// <param name="on">ON/OFF</param>
|
|
public void SetLightPowerStatus(int channel, bool on)
|
|
{
|
|
try {
|
|
if (!IsConnected())
|
|
return;
|
|
else if (channel < 0 || _lightChannelCount - 1 < channel)
|
|
return;
|
|
|
|
lock (this) {
|
|
string value = String.Format("seton {0:D} {1:G}\r", channel, on ? 1 : 0);
|
|
byte[] command = Encoding.UTF8.GetBytes(value);
|
|
|
|
Write(command);
|
|
|
|
IsOn[channel] = on;
|
|
}
|
|
} catch (Exception e) {
|
|
Log.W($"{channel}ch SetLightPowerStatus Failed. ( {e.Message} )");
|
|
}
|
|
}
|
|
public int SetLightBrightness(int channel, int brightnessValue)
|
|
{
|
|
try {
|
|
if (!IsConnected())
|
|
return E2.NOT_CONNECTED;
|
|
|
|
if (!ChannelValidCheck(channel))
|
|
return E2.INVALID_ARGUMENT;
|
|
|
|
if (!ValueValidCheck(brightnessValue))
|
|
return E2.INVALID_ARGUMENT;
|
|
|
|
lock (this) {
|
|
string value = String.Format("setbrightness {0:D} {1:D}\r", channel, brightnessValue);
|
|
byte[] command = Encoding.UTF8.GetBytes(value);
|
|
|
|
Write(command);
|
|
|
|
LightValue[channel] = brightnessValue;
|
|
|
|
return 0;
|
|
}
|
|
} catch (Exception e) {
|
|
Log.W($"{channel}ch SetLightBrightness Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
|
|
}
|
|
private bool ChannelValidCheck(int iChannel)
|
|
{
|
|
if (iChannel < _lightChannelCount && iChannel >= 0) {
|
|
return true;
|
|
} else {
|
|
Log.W($"Channel is not exist. Range : 0~{(int)_lightChannelCount - 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;
|
|
}
|
|
}
|
|
public int GetChannelCount()
|
|
{
|
|
return _lightChannelCount;
|
|
}
|
|
|
|
public int GetLightOn(int channel, out bool isOn)
|
|
{
|
|
isOn = false;
|
|
try {
|
|
if (!IsConnected())
|
|
return E1.NETWORK;
|
|
else if (channel < 0 || _lightChannelCount - 1 < channel)
|
|
return E2.INTERNAL_ERROR;
|
|
|
|
lock (this) {
|
|
string value = String.Format("geton {0:D}\r", channel);
|
|
byte[] command = Encoding.UTF8.GetBytes(value);
|
|
string responseString = WriteAndReadLine(value);
|
|
if (responseString is null)
|
|
return E2.NO_RESPONSE;
|
|
string[] responseParts = responseString.Trim().Split(' ');
|
|
|
|
if (responseParts.Length == 4 && responseParts[0] == "geton") {
|
|
int index = int.Parse(responseParts[1]);
|
|
int responseChannel = int.Parse(responseParts[2]);
|
|
bool status = int.Parse(responseParts[3]) == 1 ? true : false;
|
|
isOn = status;
|
|
return E1.OK;
|
|
} else {
|
|
return E2.INVALID_ARGUMENT;
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Log.W($"{channel}ch GetLightOn Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
|
|
public int GetLightValue(int channel, out int value)
|
|
{
|
|
value = 0;
|
|
try {
|
|
if (!IsConnected())
|
|
return E1.NETWORK;
|
|
else if (channel < 0 || _lightChannelCount - 1 < channel)
|
|
return E2.INTERNAL_ERROR;
|
|
|
|
lock (this) {
|
|
string strValue = String.Format("getbrightness {0:D}\r", channel);
|
|
byte[] command = Encoding.UTF8.GetBytes(strValue);
|
|
string responseString = WriteAndReadLine(strValue);
|
|
string[] responseParts = responseString.Trim().Split(' ');
|
|
|
|
if (responseParts.Length == 4 && responseParts[0] == "getbrightness") {
|
|
int index = int.Parse(responseParts[1]);
|
|
int responseChannel = int.Parse(responseParts[2]);
|
|
int status = int.Parse(responseParts[3]);
|
|
value = status;
|
|
return E1.OK;
|
|
} else {
|
|
return E2.INVALID_ARGUMENT;
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Log.W($"{channel}ch GetLightValue Failed. ( {e.Message} )");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
}
|
|
}
|