You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.7 KiB

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;
ReadLightCodeJson();
}
#region Start & Stop Server
/// <summary>
/// 开启连接
/// </summary>
/// <returns></returns>
public bool StartServer()
{
_lightRelay = new LightRelay(_comName, _baudRate);
var ret = _lightRelay.StartServer();
return ret;
}
/// <summary>
/// 关闭连接
/// </summary>
/// <returns></returns>
public bool StopServer()
{
_lightRelay?.StopServer();
_lightRelay = null;
return true;
}
public bool IsOpen()
{
return _lightRelay?.IsOpen() ?? false;
}
#endregion Start & Stop Server
#region Control Lights Code
/// <summary>
/// 控制所有灯编码,1 为开启所有灯,2 为关闭所有灯
/// </summary>
private List<string> _ctrlAllLightCodeList;
/// <summary>
/// 开启某一路灯编码,不互斥,index 互相对应
/// </summary>
private List<string> _openLightCodeList;
/// <summary>
/// 关闭某一路灯编码,不互斥,index 互相对应
/// </summary>
private List<string> _closeLightCodeList;
/// <summary>
/// 互锁编码,控制只有一路灯亮,index 互相对应
/// </summary>
private List<string> _lockLightOnCodeList;
/// <summary>
/// 互锁编码,控制只有一路灯灭,index 互相对应
/// </summary>
private List<string> _lockLightOffCodeList;
private bool ReadLightCodeJson()
{
if (!File.Exists(_fileName)) return false;
JObject obj = CommonUtil.ReadFileJson(_fileName);
_ctrlAllLightCodeList = obj["CtrlAllLightCodeList"].ToObject<List<string>>();
_openLightCodeList = obj["OpenLightCodeList"].ToObject<List<string>>();
_closeLightCodeList = obj["CloseLightCodeList"].ToObject<List<string>>();
_lockLightOnCodeList = obj["LockLightOnCodeList"].ToObject<List<string>>();
_lockLightOffCodeList = obj["LockLightOffCodeList"].ToObject<List<string>>();
return true;
}
#endregion Control Lights Code
#region Control Lights
/// <summary>
/// 开启所有灯
/// </summary>
/// <returns></returns>
public bool OpenAllLight()
{
var codeList = _ctrlAllLightCodeList;
var index = 1;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 关闭所有灯
/// </summary>
/// <returns></returns>
public bool CloseAllLight()
{
var codeList = _ctrlAllLightCodeList;
var index = 2;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 开启某一路灯
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool OpenLight(int index)
{
var codeList = _openLightCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 关闭某一路灯
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool CloseLight(int index)
{
var codeList = _closeLightCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 互斥开某一路灯
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool LockLightOn(int index)
{
var codeList = _lockLightOnCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 互斥关某一路灯
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool LockLightOff(int index)
{
var codeList = _lockLightOffCodeList;
if (!IsIndexValid(codeList, index))
return false;
var code = codeList[index];
return _lightRelay.SendHex(code);
}
/// <summary>
/// 判断功能码是否可以使用
/// </summary>
/// <param name="codeList"></param>
/// <param name="index"></param>
/// <returns></returns>
public bool IsIndexValid(List<string> codeList, int index)
{
if (codeList == null)
return false;
if (index < 1 || index > codeList.Count - 1)
return false;
return true;
}
#endregion Control Lights
}
}