LEDExcute.cs 6.81 KB

using LEDhelp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LedHelper
{
    public class LEDExcute
    {
        LEDHelper ProduceLED;
        LEDHelper BatteriesLED;
        LEDHelper PartsStation1LED;
        LEDHelper PartsStation2LED;
        public Queue<string> ProduceQueue { get; set; } = new Queue<string>();
        public Queue<string> BatteriesQueue { get; set; } = new Queue<string>();
        public Queue<string> PartsStation1Queue { get; set; } = new Queue<string>();
        public Queue<string> PartsStation2Queue { get; set; } = new Queue<string>();

        public LEDExcute()
        {
            string produceLedIP = ConfigurationManager.AppSettings["ProductLedIP"];
            string batteriesLedIP = ConfigurationManager.AppSettings["BatteriesLedIP"];
            string partsStation1LedIP = ConfigurationManager.AppSettings["PartsStation1LedIP"];
            string partsStation2LedIP = ConfigurationManager.AppSettings["PartsStation2LedIP"];
            int ledPort = Convert.ToInt32(ConfigurationManager.AppSettings["ledPort"]);
            ProduceLED = new LEDhelp.LEDHelper(produceLedIP, (uint)ledPort, 1);
            BatteriesLED = new LEDhelp.LEDHelper(batteriesLedIP, (uint)ledPort, 1);
            PartsStation1LED = new LEDhelp.LEDHelper(partsStation1LedIP, (uint)ledPort, 1);
            PartsStation2LED = new LEDhelp.LEDHelper(partsStation2LedIP, (uint)ledPort, 1);
        }

        public void BeginSendInfo()
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(2000);
                    if (ProduceLED != null && ProduceQueue != null && ProduceQueue.Count > 0)
                    {
                        SCREEN_ForceOnOff(LEDhelp.LEDHelper.Produce, LEDhelp.LEDHelper.OPEN);
                        SCREEN_DelDynamicArea(LEDhelp.LEDHelper.Produce, 0);
                        sendLedInfo(LEDhelp.LEDHelper.Produce, LEDhelp.LEDHelper.GREEN, ProduceQueue.Dequeue());
                        setBrightness(LEDhelp.LEDHelper.Produce, 5);
                    }
                    if (BatteriesLED != null && BatteriesQueue != null && BatteriesQueue.Count > 0)
                    {
                        SCREEN_ForceOnOff(LEDhelp.LEDHelper.Batteries, LEDhelp.LEDHelper.OPEN);
                        SCREEN_DelDynamicArea(LEDhelp.LEDHelper.Batteries, 0);
                        sendLedInfo(LEDhelp.LEDHelper.Batteries, LEDhelp.LEDHelper.RED, BatteriesQueue.Dequeue());
                        setBrightness(LEDhelp.LEDHelper.Batteries, 5);
                    }

                    if (PartsStation1LED != null && PartsStation1Queue != null && PartsStation1Queue.Count > 0)
                    {
                        SCREEN_ForceOnOff(LEDhelp.LEDHelper.PartsStation1, LEDhelp.LEDHelper.OPEN);
                        SCREEN_DelDynamicArea(LEDhelp.LEDHelper.PartsStation1, 0);
                        sendLedInfo(LEDhelp.LEDHelper.PartsStation1, LEDhelp.LEDHelper.RED, PartsStation1Queue.Dequeue());
                        setBrightness(LEDhelp.LEDHelper.PartsStation1, 5);
                    }

                    if (PartsStation2LED != null && PartsStation2Queue != null && PartsStation2Queue.Count > 0)
                    {
                        SCREEN_ForceOnOff(LEDhelp.LEDHelper.PartsStation2, LEDhelp.LEDHelper.OPEN);
                        SCREEN_DelDynamicArea(LEDhelp.LEDHelper.PartsStation2, 0);
                        sendLedInfo(LEDhelp.LEDHelper.PartsStation2, LEDhelp.LEDHelper.RED, PartsStation2Queue.Dequeue());
                        setBrightness(LEDhelp.LEDHelper.PartsStation2, 5);
                    }
                }
            });
        }

        private void sendLedInfo(int direction, int color, String sendText)
        {
            if (color == LEDhelp.LEDHelper.GREEN)
            {
                sendText = "\\C2" + sendText;
            }
            else if (color == LEDhelp.LEDHelper.YELLOW)
            {
                sendText = "\\C3" + sendText;
            }
            else
            {
                sendText = "\\C1" + sendText;
            }
            if (direction == LEDhelp.LEDHelper.Produce)
            {
                ProduceLED.SendLedInfo(sendText);
            }

            if(direction == LEDhelp.LEDHelper.Batteries)
            {
                BatteriesLED.SendLedInfo(sendText);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation1)
            {
                PartsStation1LED.SendLedInfo(sendText);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation2)
            {
                PartsStation2LED.SendLedInfo(sendText);
            }

        }

        private void SCREEN_ForceOnOff(int direction, byte status)
        {
            if (direction == LEDhelp.LEDHelper.Produce)
            {
                ProduceLED.SCREEN_ForceOnOff(status);
            }

            if (direction == LEDhelp.LEDHelper.Batteries)
            {
                BatteriesLED.SCREEN_ForceOnOff(status);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation1)
            {
                PartsStation1LED.SCREEN_ForceOnOff(status);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation2)
            {
                PartsStation2LED.SCREEN_ForceOnOff(status);
            }
        }

        // value 0-15 值越大亮度越大
        public void setBrightness(int direction, byte value)
        {
            if (direction == LEDhelp.LEDHelper.Produce)
            {
                ProduceLED.setBrightness(1, value, null);
            }

            if (direction == LEDhelp.LEDHelper.Batteries)
            {
                BatteriesLED.setBrightness(1, value, null);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation1)
            {
                PartsStation1LED.setBrightness(1, value, null);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation2)
            {
                PartsStation2LED.setBrightness(1, value, null);
            }

        }

        public void SCREEN_DelDynamicArea(int direction, byte DeleteAreaId)
        {
            if (direction == LEDhelp.LEDHelper.Produce)
            {
                ProduceLED.SCREEN_DelDynamicArea(DeleteAreaId);
            }

            if (direction == LEDhelp.LEDHelper.Batteries)
            {
                BatteriesLED.SCREEN_DelDynamicArea(DeleteAreaId);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation1)
            {
                PartsStation1LED.SCREEN_DelDynamicArea(DeleteAreaId);
            }


            if (direction == LEDhelp.LEDHelper.PartsStation2)
            {
                PartsStation2LED.SCREEN_DelDynamicArea(DeleteAreaId);
            }
        }
    }
}