WebWork.cs 7.7 KB
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using Model;

namespace BLL
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class WebWork : IWeb
    {
        public WebWork()
        {
        }

        public Result TakeOldGet(string place)
        {
            Common.log.Info("takeOld[GET] place=" + place);
            return TakeOld(place);
        }

        public Result TakeOldPost(Stream info)
        {
            StreamReader sr = new StreamReader(info);
            string s = sr.ReadToEnd();
            Common.log.Info("takeOld[POST] " + s);
            s = s.ToLower().Replace("place=", "");
            return TakeOld(s);
        }

        public Result SendNewGet(string from, string place)
        {
            Common.log.Info("sendNew[GET] from=" + from + " place=" + place);
            return SendNew(from, place);
        }

        public Result SendNewPost(Stream info)
        {
            StreamReader sr = new StreamReader(info);
            string s = sr.ReadToEnd();
            Common.log.Info("sendNew[POST] " + s);

            s = s.ToLower();
            string[] arr = s.Split('&');
            arr[0] = arr[0].Replace("from=", "");
            arr[1] = arr[1].Replace("place=", "");
            return SendNew(arr[0], arr[1]);
        }

        public Result StorageGet(string place)
        {
            Common.log.Info("storage[GET] place=" + place);
            return Storage(place);
        }

        public Result StoragePost(Stream info)
        {
            StreamReader sr = new StreamReader(info);
            string s = sr.ReadToEnd();
            Common.log.Info("storage[POST] " + s);
            s = s.ToLower().Replace("place=", "");
            return Storage(s);
        }



        private Result TakeOld(string place)
        {
            Result res = new Result();
          
            try
            {
                place = place.ToUpper();
                bool rtn = FindLine(place);
                if (rtn)
                {
                    SteelManage.OldSteelWorkAdd(place);
                    Common.log.Info("TakeOld(" + place + ") OK");
                }
                else
                {
                    res = new Result { Code = -2, Msg = "Not Find " + place };
                    Common.log.Info("没有找到" + place);
                }
            }
            catch (Exception ex)
            {
                res = new Result { Code = -100, Msg = ex.Message };
                Common.log.Error("TakeOld", ex);
            }

            return res;
        }

        private Result SendNew(string from, string place)
        {
            Result res = new Result();
          
            try
            {
                from = from.ToUpper();
                place = place.ToUpper();
                string[] arr = place.Split(',');
                bool rtn = FindLine(from, arr);
                if (rtn)
                {
                    SteelManage.NewSteelWorkAdd(from, arr);
                    Common.log.Info("SendNew(" + place + ") OK");
                }
                else
                {
                    res = new Result { Code = -2, Msg = "Not Find (" + from + "," + place + ")" };
                    Common.log.Info("没有找到" + place);
                }
            }
            catch (Exception ex)
            {
                res = new Result { Code = -100, Msg = ex.Message };
                Common.log.Error("SendNew", ex);
            }

            return res;
        }

        private Result Storage(string place)
        {
            Result res = new Result();
           
            try
            {
                place = place.ToUpper();
                if (place == Common.STORAGE_ENTER_4C.ToUpper())
                {
                    res.Msg = "OK,4C";
                    SteelManage.StorageWorkAdd(place);
                }
                else if (place == Common.STORAGE_ENTER_4D.ToUpper())
                {
                    res.Msg = "OK,4D";
                    SteelManage.StorageWorkAdd(place);
                }
                else if (place == Common.STORAGE_LEAVE.ToUpper())
                {
                    SteelManage.StorageWorkAdd(place);
                }
                else if (place == Common.STORAGE_IO_ON.ToUpper())
                {
                    Common.StorageDockFinish = true;
                    UpdateStorageIO();
                    res.Msg = "OK," + FindAgv();
                }
                else if (place == Common.STORAGE_IO_OFF.ToUpper())
                {
                    Common.StorageDockFinish = false;
                    UpdateStorageIO();
                    res.Msg = "OK," + FindAgv();
                }
                else
                {
                    res = new Result { Code = -2, Msg = "Not Find " + place };
                    Common.log.Info("没有找到" + place);
                }
            }
            catch (Exception ex)
            {
                res = new Result { Code = -100, Msg = ex.Message };
                Common.log.Error("Storage", ex);
            }

            return res;
        }

        private bool FindLine(string place)
        {
            bool rtn = false;
            for (int i = 0; i < Common.agvLines.Count; i++)
            {
                rtn = Common.agvLines[i].FindLine(place);
                if (rtn) return true;
            }
            return rtn;
        }

        private bool FindLine(string from, string[] place)
        {
            bool rtn = false;

            for (int i = 0; i < Common.agvLines.Count; i++)
            {
                if (from == "STORAGE")
                    rtn = true;
                else
                    rtn = Common.agvLines[i].FindLine(from);
                if (!rtn) continue;
                for (int j = 0; j < place.Length; j++)
                {
                    if (place[j].ToUpper() == "STORAGE")
                    {
                        rtn = true;
                    }
                    else
                    {
                        rtn = Common.agvLines[i].FindLine(place[j]);
                        if (!rtn) break;
                    }
                }
                if (rtn) break;
            }
            return rtn;
        }

        private string FindAgv()
        {
            string val = "";

            for (int i = 0; i < Common.agvInfos.Count; i++)
            {
                bool rtn = Common.mir.Get_Register(Common.agvInfos[i].IP, Common.agvInfos[i].Authorization, 19, out int value);
                if (rtn)
                {
                    if (value == 1)
                    {
                        val = Common.agvInfos[i].Workshop;
                        break;
                    }
                }
            }

            return val;
        }

        private void UpdateStorageIO()
        {
            if (Common.lblStorageIO == null) return;
            int.TryParse(Common.lblStorageIO.Tag.ToString(), out int n);
            if (Common.StorageDockAlway) Common.StorageDockFinish = true;
            string s = Common.StorageDockFinish ? "ON" : "OFF";
            Common.lblStorageIO.Text = string.Format("仓库IO信号 {0} ({1})", s, n);
            Common.lblStorageIO.BackColor = Common.StorageDockFinish ? System.Drawing.Color.Lime : System.Drawing.Color.Red;
            if (n == 9)  //没有意义,仅仅是为了能看到信号在变化
                n = 0;
            else
                n++;
            Common.lblStorageIO.Tag = n.ToString();
        }
    }


}