using EC.Utils;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
namespace EC.AutoWeightServer.IfManager.Light
{
public class LightManager : ILightManager
{
private LightRelay _lightRelay;
private string _comName;
private int _baudRate;
private string _fileName;
public LightManager()
{
}
public LightManager(string comName, int baudRate, string fileName)
{
_comName = comName;
_baudRate = baudRate;
_fileName = fileName;
_readCodeFlag = ReadLightCodeJson();
}
#region Start & Stop Server
///
/// 开启连接
///
///
public bool StartServer()
{
_lightRelay = new LightRelay(_comName, _baudRate);
var ret = _lightRelay.StartServer();
return ret;
}
///
/// 关闭连接
///
///
public bool StopServer()
{
_lightRelay?.StopServer();
_lightRelay = null;
return true;
}
public bool IsOpen()
{
return _lightRelay?.IsOpen() ?? false;
}
#endregion Start & Stop Server
#region Control Lights Code
private bool _readCodeFlag;
///
/// 控制所有灯编码,1 为开启所有灯,2 为关闭所有灯
///
private List _ctrlAllLightCodeList;
///
/// 开启某一路灯编码,不互斥,index 互相对应
///
private List _openLightCodeList;
///
/// 关闭某一路灯编码,不互斥,index 互相对应
///
private List _closeLightCodeList;
///
/// 互锁编码,控制只有一路灯亮,index 互相对应
///
private List _lockLightOnCodeList;
///
/// 互锁编码,控制只有一路灯灭,index 互相对应
///
private List _lockLightOffCodeList;
private bool ReadLightCodeJson()
{
if (!File.Exists(_fileName)) return false;
JObject obj = CommonUtil.ReadFileJson(_fileName);
_ctrlAllLightCodeList = obj["CtrlAllLightCodeList"].ToObject>();
_openLightCodeList = obj["OpenLightCodeList"].ToObject>();
_closeLightCodeList = obj["CloseLightCodeList"].ToObject>();
_lockLightOnCodeList = obj["LockLightOnCodeList"].ToObject>();
_lockLightOffCodeList = obj["LockLightOffCodeList"].ToObject>();
return true;
}
public bool IsReadCodeSuccess()
{
return _readCodeFlag;
}
#endregion Control Lights Code
#region Control Lights
///
/// 开启所有灯
///
///
public bool OpenAllLight()
{
var codeList = _ctrlAllLightCodeList;
var index = 1;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 关闭所有灯
///
///
public bool CloseAllLight()
{
var codeList = _ctrlAllLightCodeList;
var index = 2;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 开启某一路灯
///
///
///
public bool OpenLight(int index)
{
var codeList = _openLightCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 关闭某一路灯
///
///
///
public bool CloseLight(int index)
{
var codeList = _closeLightCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 互斥开某一路灯
///
///
///
public bool LockLightOn(int index)
{
var codeList = _lockLightOnCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 互斥关某一路灯
///
///
///
public bool LockLightOff(int index)
{
var codeList = _lockLightOffCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
///
/// 判断功能码是否可以使用
///
///
///
///
public bool IsIndexValid(List codeList, int index)
{
if (codeList == null)
return false;
if (index < 1 || index > codeList.Count - 1)
return false;
return true;
}
#endregion Control Lights
}
}