BaseLedManager.cs 9.6 KB

using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class LEDManager
    {
        public static Dictionary<string, LEDBaseModule> deviceMap = new Dictionary<string, LEDBaseModule>();
        public static byte DefaultLight = 100;
        public static int DeviceLedType = ConfigAppSettings.GetIntValue(Setting_Init.DeviceLedType);

        public static string ColorRule = ConfigAppSettings.GetValue(Setting_Init.ColorRuleConfig).ToUpper();


        public static string DefaultIP = ConfigAppSettings.GetValue(Setting_Init.DefaultDeviceIP);
   
        public static LEDBaseModule GetLedModule(string ip)
        {
            LEDBaseModule led = null;
            if (deviceMap.ContainsKey(ip))
            {
                led = deviceMap[ip];
            }
            else
            {
                led = LEDBaseModule.GetModule(ip);
                deviceMap.Add(ip, led);
            }
            return led;
        }
        public static void Init()
        {
            ColorRule = ConfigAppSettings.GetValue(Setting_Init.ColorRuleConfig).ToUpper(); 
            if (ColorRule.Length == 3 && ColorRule.Contains("R") && ColorRule.Contains("G") && ColorRule.Contains("B"))
            {

            }
            else
            {
                LEDManager.ColorRule = "RGB";
                ConfigAppSettings.SaveValue(Setting_Init.ColorRuleConfig, LEDManager.ColorRule);
            }
            LoadStatusDMX();
        }
        public static void LoadStatusDMX()
        {
            StatusLedDmx = new List<int>();

            string str = ConfigAppSettings.GetValue(Setting_Init.StatusLedDmx);
            if (String.IsNullOrEmpty(str))
            {
                str = "0";
                ConfigAppSettings.SaveValue(Setting_Init.StatusLedDmx, str);
            }
            string[] array = str.Split(';');
            foreach (string s in array)
            {
                try
                {
                    int a = Convert.ToInt32(s);
                    StatusLedDmx.Add(a);
                }
                catch (Exception ex)
                {

                }
            }
        }
        /// <summary>
        /// 1=绿灯,2=黄灯
        /// </summary>
        public static int CurrLedStatus = -1;

        private static List<int> StatusLedDmx = new List<int>(); 

        /// <summary>
        /// 打开状态灯
        /// </summary>
        public static void OpenStatusLights(string color = "green")
        {
            CloseStatusLights("");
            foreach (LEDBaseModule module in deviceMap.Values)
            {

                List<Light> sLed = new List<Light>();
                foreach (int dmx in StatusLedDmx)
                {
                    for (int index = 0; index < module.Max_Light; index++)
                    {
                        if ("green".Equals(color))
                        {
                            CurrLedStatus = 1;
                            sLed.Add(Light.GreenLight(dmx, index));
                        }
                        else if ("yellow".Equals(color))
                        {
                            CurrLedStatus = 2;
                            sLed.Add(Light.BlueLight(dmx, index));
                        } 
                    }
                }
                module.LightOn(sLed.ToArray());
            }
        }


        /// <summary>
        /// 关闭状态灯
        /// </summary>
        /// <param name="color"></param>
        public static void CloseStatusLights(string color = "")
        {
            CurrLedStatus = 0;
            foreach (LEDBaseModule module in deviceMap.Values)
            {
                foreach (int dmx in StatusLedDmx)
                {
                    module.AllLightOff(dmx);
                }
            }
        }
    }

    public abstract class LEDBaseModule
    {

        protected ushort Max_DMX = 16;
        public int Max_Light = 170;
        public string ModuleIP = "";
        public static LEDBaseModule GetModule(string ip)
        {
            //if (LEDManager.DeviceLedType.Equals(1))
            //{
            //    LEDColorModule module = new LEDColorModule(ip);
            //    return module;
            //}
            //else if (LEDManager.DeviceLedType.Equals(2))
            //{
            LEDColorArtNet module = new LEDColorArtNet(ip);
            return module;
            //}
            //else
            //{
            //    LEDSingleModule module = new LEDSingleModule(ip);
            //    return module;
            //}
        }

        internal LEDBaseModule(string ip)
        {
            ModuleIP = ip;
        }


        public abstract void AllLightOff(int dmx = -1);

        public abstract void AllLightOn(int dmx = -1);

        public abstract void AllLightOn(Light light);


        public abstract void OnlyLightOn(params Light[] lights);


        public abstract void LightOn(params Light[] lights);

        public abstract void LightOff(int dmx, params int[] lightIndexs);

        public abstract void LightOff(params Light[] lights);

        /// <summary>
        /// 打包方法,可以将十六制字符串转成byte[] ,字符串没有空格
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        protected byte[] StringToByte(string s)
        {
            string temps = s.Replace(" ", "").Replace(":", "");
            if (temps.Length % 2 != 0)
            {
                temps = "0" + temps;
            }
            byte[] tempb = new byte[temps.Length / 2];
            int j = 0;
            for (int i = 0; i < temps.Length; i = i + 2, j++)
            {
                tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
            }
            byte[] send = new byte[j];
            Array.Copy(tempb, send, j);
            return send;
        }
    }

    public class Light
    {
        public static string defaultColor = "green";
        public static byte defaultR = 0;
        public static byte defaultG = 50;
        public static byte defaultB = 0;
        public static Light DefaultLight(int dmxId, int index)
        {
            return new Light(dmxId, index, defaultR, defaultG, defaultB, 200);
        }
        public static Light GetLight(int dmxId, int index, string color = "green")
        {
            return GetLight(dmxId, index, color, 50);
        }
        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(dmxId, index, color));
            }
            return lights.ToArray();
        }
        public static Light GetLight(int dmxId, int index, string color, byte bright)
        {
            color = color.ToLower();
            switch (color)
            {
                case "green":
                    return GreenLight(dmxId, index, bright);
                case "red":
                    return RedLight(dmxId, index, bright);
                case "yellow":
                    return YellowLight(dmxId, index, bright);
                case "blue":
                    return BlueLight(dmxId, index, bright);
            }
            return DefaultLight(dmxId, index);
        }

        public static Light RedLight(int dmxId, int index, byte bright = 50)
        {
            return new Light(dmxId, index, bright, 0, 0);
        }
        public static Light YellowLight(int dmxId, int index, byte bright = 50)
        {
            return new Light(dmxId, index, bright, bright, 0);
        }

        public static Light BlueLight(int dmxId, int index, byte bright = 50)
        {
            return new Light(dmxId, index, 0, 0, bright);
        }

        public static Light GreenLight(int dmxId, int index, byte bright = 50)
        {
            return new Light(dmxId, index, 0, bright, 0);
        }

        public static Light CyanLight(int dmxId, int index)
        {
            return new Light(dmxId, index, 0, 255, 255);
        }

        public static Light ChocolateLight(int dmxId, int index)
        {
            return new Light(dmxId, index, 210, 105, 30);
        }


        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[] GetLights(int dmx, List<int> leds, byte light = 200)
        {
            Light[] lights = new Light[leds.Count];
            int i = 0;
            foreach (int led in leds)
            {
                lights[i] = new Light(dmx, led, light);
                i++;
            }
            return lights;
        }
        /// <summary>
        /// 单色灯
        /// </summary>
        /// <param name="dmx">区域ID</param>
        /// <param name="index">索引号</param>
        /// <param name="lightValue">亮度</param>
        public Light(int dmx, int index, byte lightValue)
        {
            this.index = index;
            this.dmx = dmx;
            this.lightValue = lightValue;
        }
        public int dmx = -1;
        public byte lightValue = LEDManager.DefaultLight;
    }
}