Commit 5c320d1a LN

代码整合

1 个父辈 6d14c4fc
......@@ -33,6 +33,11 @@ namespace SmartShelf.Common
/// <summary>
/// 料架灯类型,0=单色灯料架,1=三色灯料架
/// </summary>
public static string DeviceLedType = "DeviceLedType";
public static string DeviceLedType = "DeviceLedType";
public static string Status_Green_Lights = "Status_Green_Lights";
public static string Status_Red_Lights = "Status_Red_Lights";
public static string Status_Yellow_Lights = "Status_Yellow_Lights";
}
}
......@@ -21,10 +21,10 @@ namespace SmartShelf.DeviceLibrary
public class BOXManager
{
private static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string BoxName = "单色灯料架";
public static BOX_Config Config = null;
public static string CID = "";
public static string BoxName = "";
private static System.Timers.Timer timersTimer;
private static System.Timers.Timer serverConTimer = new System.Timers.Timer();
private static bool isInit = false;
......@@ -36,11 +36,15 @@ namespace SmartShelf.DeviceLibrary
/// </summary>
public static Dictionary<string, int> StatusMap = new Dictionary<string, int>();
public static bool IsRun = false;
/// <summary>
/// 灯闪烁列表,库位号 value为颜色
/// </summary>
public static Dictionary<string, string> TwinkleMap = new Dictionary<string, string>();
public static bool StartInit()
{
try
{
BoxCount = ConfigAppSettings.GetIntValue(Setting_Init.BoxCount);
string appPath = Application.StartupPath;
//加载位置
......@@ -66,6 +70,7 @@ namespace SmartShelf.DeviceLibrary
}
LEDManager.deviceMap = new Dictionary<string, LEDBaseModule>();
StatusMap = new Dictionary<string, int>();
TwinkleMap = new Dictionary<string, string>();
foreach (BoxPosition box in PositionMap.Values)
{
if (!LEDManager.deviceMap.ContainsKey(box.DeviceIp))
......@@ -77,10 +82,15 @@ namespace SmartShelf.DeviceLibrary
}
StatusMap.Add(box.PositionNum, -1);
TwinkleMap.Add(box.PositionNum, "");
}
CID = ConfigAppSettings.GetValue(Setting_Init.Store_CID);
BoxName = (" 料架_" + CID + " ").ToUpper();
BoxName = (" 单色灯料架_" + CID + " ").ToUpper();
if (LEDManager.DeviceLedType.Equals(1))
{
BoxName = (" 三色灯料架_" + CID + " ").ToUpper();
}
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_ConfigPath);
Config = (BOX_Config)CSVConfigReader.LoadConfig(1, CID, StoreType.RC_PLC_SM, filePath);
if (Config == null)
......@@ -88,13 +98,13 @@ namespace SmartShelf.DeviceLibrary
return false;
}
LogUtil.info(LOGGER, "加载料架完成!");
LogUtil.info(BoxName + "加载完成!");
Init();
return true;
} catch (Exception ex)
{
LogUtil.error("加载料架配置出错:" + ex.ToString());
LogUtil.error(BoxName + "加载配置出错:" + ex.ToString());
} return false;
}
......@@ -107,16 +117,16 @@ namespace SmartShelf.DeviceLibrary
{
serverConTimer = new System.Timers.Timer();
serverConTimer.Enabled = false;
serverConTimer.Interval = 600;
serverConTimer.Interval = 300;
serverConTimer.Elapsed += server_connect_timer_Tick;
serverConTimer.AutoReset = true;
timersTimer = new System.Timers.Timer();
timersTimer.Enabled = false;
timersTimer.Interval = 600;
timersTimer.Interval = 200;
timersTimer.Elapsed += timersTimer_Elapsed;
timersTimer.AutoReset = true;
isInit = true;
isInit = true;
}
}
......@@ -126,7 +136,7 @@ namespace SmartShelf.DeviceLibrary
timersTimer.Enabled = true;
serverConTimer.Enabled = true;
IsRun = true;
LEDManager. OpenSGreenLed();
LEDManager. OpenStatusLights("green");
return true;
}
......@@ -147,7 +157,7 @@ namespace SmartShelf.DeviceLibrary
{
led.AllLightOff();
}
LEDManager.CloseSLed();
LEDManager.CloseStatusLights();
IsRun = false;
LogUtil.info(LOGGER, BoxName + "停止运行,时间" + DateTime.Now.ToShortTimeString() + "!");
}
......@@ -169,16 +179,18 @@ namespace SmartShelf.DeviceLibrary
{
if (!LEDManager.CurrLedStatus.Equals(2))
{
LEDManager.OpenSYellowLed();
LEDManager.OpenStatusLights("green");
}
}
else
{
if (!LEDManager.CurrLedStatus.Equals(1))
{
LEDManager.OpenSGreenLed();
LEDManager.OpenStatusLights("yellow");
}
}
TwinkLight();
}
catch (Exception ex)
{
......@@ -205,8 +217,7 @@ namespace SmartShelf.DeviceLibrary
LogUtil.error("server_connect_timer_Tick 出错:" + ex.ToString());
}
isInProcess = false;
}
private static string LastCardResult = "";
}
private static string LastCardValue = "";
public static string StrMsg = "";
......@@ -227,8 +238,7 @@ namespace SmartShelf.DeviceLibrary
}
if (String.IsNullOrEmpty(posId).Equals(false))
{
lineOperation.data.Add(ParamDefine.posOpened, posId);
LastCardResult = "";
lineOperation.data.Add(ParamDefine.posOpened, posId);
}
foreach(string str in lineOperation.data.Keys)
......@@ -315,30 +325,121 @@ namespace SmartShelf.DeviceLibrary
}
}
}
public static void ProcessOpenDoor(string posids, string proMsg = "Revice ")
{
LogUtil.info(BoxName + proMsg + " open命令:" + posids);
string[] posArray = posids.Split('|');
foreach (string posid in posArray)
foreach (string posStr in posArray)
{
string posName = posid;
if (posid.Contains("="))
//是否需要闪烁
bool needTwinkle = false;
string posid = posStr;
string color = "Green";
if (posStr.Contains("="))
{
posName = posid.Split('=')[0];
string[] posInfos = posStr.Split('=');
posid = posInfos[0];
color = posInfos[1];
if (posInfos.Length > 2 && LEDManager.DeviceLedType.Equals(2))
{
LogUtil.info("库位【" + posid + "】需要开启闪烁" + color);
needTwinkle = true;
}
}
if (PositionMap.ContainsKey(posName))
color = color.ToLower();
if (PositionMap.ContainsKey(posid))
{
BoxPosition position = PositionMap[posName];
LEDManager.GetLedModule(position.DeviceIp).LightOn(Light.GetLights(position.DmxId, position.GetLedList()));
StatusMap[posName] = 1;
BoxPosition position = PositionMap[posid];
Light[] lights = Light.GetLights(position.DmxId, color, position.GetLedList().ToArray());
LEDManager.GetLedModule(position.DeviceIp).LightOn(lights);
StatusMap[posid] = 1;
if (needTwinkle)
{
//需要闪烁
TwinkleMap[posid] = color;
}
else
{
TwinkleMap[posid] = "";
}
}
else
{
LogUtil.error(BoxName + "打开门锁失败,未找到库位号:" + posName);
if (posid.ToLower().Equals("statuslight"))
{
LogUtil.info("状态灯指令:" + posid + "=" + color);
}
else
{
LogUtil.error(BoxName + "打开门锁失败,未找到库位号:" + posid);
}
}
}
}
private static bool TwinkleLightOpen = false;
public static void TwinkLight()
{
if (!LEDManager.DeviceLedType.Equals(1))
{
return;
}
//只有三色灯才有闪烁功能
TwinkleLightOpen = !TwinkleLightOpen;
bool hasTwinkleLight = false;
var keys = TwinkleMap.Keys.ToList();
foreach (string posid in keys)
{
string color = TwinkleMap[posid];
if (color != null && !color.Equals(""))
{
if (PositionMap.ContainsKey(posid))
{
BoxPosition position = PositionMap[posid];
if (StatusMap[posid] == 0)
{
LogUtil.info("库位【" + posid + "】灯已灭,停止闪烁" + color);
TwinkleMap[posid] = "";
LEDManager.GetLedModule(position.DeviceIp).LightOff(position.DmxId, position.GetLedList().ToArray());
}
else
{
hasTwinkleLight = true;
if (TwinkleLightOpen)
{
Light[] lights = Light.GetLights(position.DmxId, color, position.GetLedList().ToArray());
//LogUtil.info("库位【" + posid + "】闪烁开灯" + color);
LEDManager.GetLedModule(position.DeviceIp).LightOn(lights);
}
else
{
//LogUtil.info("库位【" + posid + "】闪烁灭灯" + color);
LEDManager.GetLedModule(position.DeviceIp).LightOff(position.DmxId, position.GetLedList().ToArray());
}
}
}
else
{
LogUtil.info("库位【" + posid + "】未找到,停止闪烁");
TwinkleMap[posid] = "";
}
}
}
if (hasTwinkleLight)
{
LEDManager.OpenStatusLights("red");
}
else
{
LEDManager.OpenStatusLights("red");
}
}
public static void ProcessCloseDoor(string posids, string proMsg = "Revice ")
{
LogUtil.info(BoxName + proMsg + " close命令:" + posids);
......@@ -368,6 +469,8 @@ namespace SmartShelf.DeviceLibrary
#endregion
}
}
......@@ -30,77 +30,146 @@ namespace SmartShelf.DeviceLibrary
}
return led;
}
/// <summary>
/// 1=绿灯,2=黄灯
/// </summary>
public static int CurrLedStatus = -1;
private static int StatusLedDmx = 3;
public static void OpenSGreenLed()
/// <summary>
/// 打开状态灯
/// </summary>
public static void OpenStatusLights(string color="green")
{
if ("green" == color)
{
CurrLedStatus = 1;
}
else if ("yellow" == color)
{
CurrLedStatus = 2;
}
if (DeviceLedType.Equals(1))
{
if (string.IsNullOrEmpty(color))
{
return;
}
var lights = GetStatusLights(color, out string ip);
if (!string.IsNullOrEmpty(ip))
{
GetLedModule(ip).LightOn(lights.ToArray());
}
}
else
{
CloseSLed();
CurrLedStatus = 1;
CloseStatusLights("");
foreach (LEDBaseModule module in deviceMap.Values)
{
if (module is LEDSingleModule)
{
List<int> list = new List<int>();
for (int i = 2; i < LEDSingleModule.LIGHT_COUNT_PER_DMX; i += 3)
if ("green" == color)
{
list.Add(i);
for (int i = 2; i < LEDSingleModule.LIGHT_COUNT_PER_DMX; i += 3)
{
list.Add(i);
}
}
else if ("yellow" == color)
{
for (int i = 2; i < LEDSingleModule.LIGHT_COUNT_PER_DMX; i += 3)
{
list.Add(i - 2);
list.Add(i);
}
}
module.OnlyLightOn(Light.GetLights(StatusLedDmx, list, 200));
}
}
}
}
public static void OpenSYellowLed()
/// <summary>
/// 关闭状态灯
/// </summary>
/// <param name="color"></param>
public static void CloseStatusLights(string color="")
{
if (DeviceLedType.Equals(1))
{
if (string.IsNullOrEmpty(color))
{
return;
}
var lights = GetStatusLights(color, out string ip);
var indexArr = from o in lights select o.index;
if (!string.IsNullOrEmpty(ip))
{
LEDManager.GetLedModule(ip).LightOff(-1, indexArr.ToArray());
}
}
else
{
CloseSLed();
CurrLedStatus = 2;
CurrLedStatus = 0;
foreach (LEDBaseModule module in deviceMap.Values)
{
if (module is LEDSingleModule)
{
List<int> list = new List<int>();
for (int i = 2; i < LEDSingleModule.LIGHT_COUNT_PER_DMX; i += 3)
{
list.Add(i - 2);
list.Add(i);
}
module.OnlyLightOn(Light.GetLights(StatusLedDmx, list, 200));
module.AllLightOff(StatusLedDmx);
}
}
}
}
public static void CloseSLed()
/// <summary>
/// 获取状态灯配置(192.168.100.100:1,3,23)
/// </summary>
/// <param name="color"></param>
/// <param name="ip"></param>
/// <returns></returns>
private static List<Light> GetStatusLights(string color, out string ip)
{
if (DeviceLedType.Equals(1))
List<Light> lights = new List<Light>();
string lightStrs = "";
color = color.ToLower();
if ("green" == color)
{
lightStrs = ConfigAppSettings.GetValue(Setting_Init.Status_Green_Lights);
}
else
else if ("yellow" == color)
{
CurrLedStatus = 0;
foreach (LEDBaseModule module in deviceMap.Values)
lightStrs = ConfigAppSettings.GetValue(Setting_Init.Status_Yellow_Lights);
}
else if ("red" == color)
{
lightStrs = ConfigAppSettings.GetValue(Setting_Init.Status_Red_Lights);
}else if (String.IsNullOrEmpty(color))
{
lightStrs = ConfigAppSettings.GetValue(Setting_Init.Status_Green_Lights)+"," +
ConfigAppSettings.GetValue(Setting_Init.Status_Yellow_Lights) + "," +
ConfigAppSettings.GetValue(Setting_Init.Status_Red_Lights) ;
}
ip = "";
if (!String.IsNullOrEmpty(lightStrs))
{
string[] infos = lightStrs.Split(':');
ip = infos[0];
string[] lightArr = infos[1].Split(',');
foreach (var indexStr in lightArr)
{
if (module is LEDSingleModule)
{
module.AllLightOff(StatusLedDmx);
}
int.TryParse(indexStr, out int index);
Light light = Light.GetLight(-1, index, color, 255);
lights.Add(light);
}
}
return lights;
}
}
public abstract class LEDBaseModule
......@@ -172,107 +241,91 @@ namespace SmartShelf.DeviceLibrary
public class Light
{
public static Light StoreLight(int index)
public static Light DefaultLight(int dmxId, int index)
{
return new Light(index, 50, 0, 0);
return new Light(dmxId, index, 50, 0, 0,200);
}
public static Light GetLight(int index, string color)
public static Light GetLight(int dmxId,int index, string color="green")
{
return GetLight(index, color, 50);
return GetLight(dmxId, index, color, 50);
}
public static Light[] GetLights(string color, params int[] indexes)
public static Light[] GetLights(int dmxId,string color, params int[] indexes)
{
List<Light> lights = new List<Light>();
foreach (int index in indexes)
{
lights.Add(GetLight(index, color));
lights.Add(GetLight(dmxId, index, color));
}
return lights.ToArray();
}
public static Light GetLight(int index, string color, byte bright)
public static Light GetLight(int dmxId, int index, string color, byte bright)
{
color = color.ToLower();
switch (color)
{
case "green":
return GreenLight(index, bright);
return GreenLight(dmxId, index, bright);
case "red":
return RedLight(index, bright);
return RedLight(dmxId, index, bright);
case "yellow":
return YellowLight(index, bright);
return YellowLight(dmxId, index, bright);
case "blue":
return BlueLight(index, bright);
return BlueLight(dmxId, index, bright);
}
return StoreLight(index);
}
public static Light RedLight(int index, byte bright)
{
return new Light(index, bright, 0, 0);
}
public static Light RedLight(int index)
{
return RedLight(index, 50);
}
public static Light YellowLight(int index)
{
return YellowLight(index, 50);
return DefaultLight(dmxId, index);
}
public static Light YellowLight(int index, byte bright)
public static Light RedLight(int dmxId, int index, byte bright=50)
{
return new Light(index, bright, bright, 0);
}
public static Light BlueLight(int index)
{
return BlueLight(index, 50);
}
public static Light BlueLight(int index, byte bright)
return new Light(dmxId, index, bright, 0, 0);
}
public static Light YellowLight(int dmxId, int index, byte bright=50)
{
return new Light(index, 0, 0, bright);
return new Light(dmxId, index, bright, bright, 0);
}
public static Light GreenLight(int index)
public static Light BlueLight(int dmxId, int index, byte bright=50)
{
return new Light(index, 0, 50, 0);
return new Light(dmxId, index, 0, 0, bright);
}
public static Light GreenLight(int index, byte bright)
public static Light GreenLight(int dmxId, int index, byte bright=50)
{
return new Light(index, 0, bright, 0);
return new Light(dmxId, index, 0, bright, 0);
}
public static Light CyanLight(int index)
public static Light CyanLight(int dmxId, int index)
{
return new Light(index, 0, 255, 255);
return new Light(dmxId, index, 0, 255, 255);
}
public static Light ChocolateLight(int index)
public static Light ChocolateLight(int dmxId, int index)
{
return new Light(index, 210, 105, 30);
return new Light(dmxId, index, 210, 105, 30);
}
public Light(int index, byte Red, byte Green, byte Blue)
public Light(int dmxId, int index, byte Red, byte Green, byte Blue,byte lightValue=200)
{
this.dmx = dmxId;
this.index = index;
this.Red = Red;
this.Green = Green;
this.Blue = Blue;
this.lightValue = lightValue;
}
public int index { get; set; }
public byte Red { get; set; }
public byte Green { get; set; }
public byte Blue { get; set; }
/// <summary>
/// 单色灯
/// </summary>
public static Light DefaultLight(int dmx, int index)
{
return new Light(dmx, index, LEDManager.DefaultLight);
}
///// <summary>
///// 单色灯
///// </summary>
//public static Light DefaultLight(int dmx, int index)
//{
// return new Light(dmx, index, LEDManager.DefaultLight);
//}
/// <summary>
/// 单色灯
/// </summary>
......@@ -298,8 +351,8 @@ namespace SmartShelf.DeviceLibrary
this.index = index;
this.dmx = dmx;
this.lightValue = lightValue;
}
public int dmx { get; set; }
public byte lightValue { get; set; }
}
public int dmx = -1;
public byte lightValue = LEDManager.DefaultLight;
}
}
......@@ -45,7 +45,7 @@ namespace SmartShelf.DeviceLibrary
public override void AllLightOn(int dmx = -1)
{
AllLightOn(Light.StoreLight(1));
AllLightOn(Light.DefaultLight(dmx, 1));
}
public override void AllLightOn(Light light)
......
......@@ -21,6 +21,9 @@
<add key ="DIMS" value="300"/>
<add key ="DOMS" value ="300"/>
<add key ="BoxCount" value ="2"/>
<add key ="Status_Green_Lights" value ="192.168.1.151:129,130,131,132,133,134,135,136,137,138"/>
<add key ="Status_Yellow_Lights" value ="192.168.1.151:140,141,142,143,144,145,146,147,148,149"/>
<add key ="Status_Red_Lights" value ="192.168.1.151:150,151,152,153,154,155,156,157,158,159,160"/>
</appSettings>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
......
......@@ -227,17 +227,17 @@ namespace SmartShelf
private void btnOpenSLed_Click(object sender, EventArgs e)
{
LEDManager.OpenSGreenLed();
LEDManager.OpenStatusLights("green");
}
private void btnCloseSLed_Click(object sender, EventArgs e)
{
LEDManager.CloseSLed();
LEDManager.CloseStatusLights();
}
private void button5_Click(object sender, EventArgs e)
{
LEDManager.OpenSYellowLed();
LEDManager.OpenStatusLights("yellow");
}
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!