VStore.cs 19.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
using OnlineStore;
using RemoteSheardObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace DeviceLibrary
{
    public class VStore
    {
        public VServerComm ServerCM;        
        public VLog VLog;
        VStoreCollection vc;
        MsgService Msg;
        /// <summary>
        /// 当前料仓终端口的物料reelid
        /// </summary>
        public string CurrentTerminalReelID
        {
            get => ConfigHelper.Config.Get("Runtime_Tower_" + CID + "_CurrentTerminalReelID");
            set => ConfigHelper.Config.Set("Runtime_Tower_" + CID + "_CurrentTerminalReelID", value);
        }
        public string CID;
        public string TowerName;
        public TowerInfo TowerInfo;
        public VStore(TowerInfo towerInfo, VStoreCollection storeCollection) {
            vc = storeCollection;
            CID = towerInfo.TowerID;
            TowerName = towerInfo.TowerName;
            TowerInfo= towerInfo;
            VLog = new VLog(CID, CID);
            ServerCM = new VServerComm(CID,VLog);
            ServerCM.OutStoreEvent += ServerCM_OutStoreEvent;
            ServerCM.storeStatus = StoreStatus.None;
            Msg = new MsgService("RT-" + CID);
            OutStoreJobList = new StoreJobList(crc.GetString("Res0050","出库任务"));
        }
        
        public void ServerCM_OutStoreEvent(object sender, JobInfo e)
        {
            VLog.Info(sender+"出库指令信息:" + e.ToStr());
            OutStoreJobList.Enqueue(e.Clone());
            //MI.DeviceList[TowerList.List[CurrentOutJobInfo.CID].DeviceGroupName].EnqueueOutStore(CurrentOutJobInfo);
            
        }
        public string CurrentTowerStatusText="";
        public RTStoreStatus RTStoreStatus = RTStoreStatus.Offline;
        public volatile bool TerminalError = false;
        DateTime LastStatusTime = DateTime.Now;
        int LastStatusCode = 0;
        /// <summary>
        /// 最后报错重试时间
        /// </summary>
        public DateTime LastProcessErrorTime = DateTime.Now;
        public int LastProcessErrorTimes = 0;
        internal void VTowerProcess() {
            if (!TowerInfo.Enable) {
                ServerCM.storeStatus = StoreStatus.None;
                CurrentTowerStatusText = "Disable";
                RTStoreStatus = RTStoreStatus.Offline;
                TerminalError=false;
                return;
            }

            try
            {
                var ti = ServerCM.GetTowerDetailInformation(CID);
                if (ti != null)
                {
                    if (LastStatusCode != ti.StatusCode)
                    {
                        LastStatusCode = ti.StatusCode;
                        VLog.Info($"RTStoreStatus:{RTStoreStatus},StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus},capacity:{ServerCM.capacity}");
                    }
                    CurrentTowerStatusText = $"[TI={ti.StatusCode}-{ti.StatusText}]";
                    ServerCM.Temperature = ti.Temperature;
                    ServerCM.Humidity = ti.Humidity;
                    if (ServerCM.Humidity > ServerCM.Max_Humidity || ServerCM.Temperature > ServerCM.Max_Temperature)
                    {
                        Msg.add(crc.GetString("Res0098.70dccffd","温湿度超限"), MsgLevel.alarm);
                    }

                    if (ti.StatusCode == 26 && ti.StatusText == "Ready")
                    {
                        ServerCM.storeStatus = StoreStatus.StoreOnline;
                        LastProcessErrorTimes = 0;
                        if (RTStoreStatus == RTStoreStatus.OutMoveExecute)
                        {
                            TheLine.emptyOut(CurrentTerminalReelID);
                            TheLine.UpdateLocInfo("", CurrentTerminalReelID, TheLine.LineStatusE.BOXDOOR_NOREEL, CID);
                            VLog.Info("检测到料盘空出 reelid:" + CurrentTerminalReelID);
                        }
                        if (RTStoreStatus == RTStoreStatus.OutStoreReady && (DateTime.Now - LastStatusTime).TotalSeconds < 2)
                        {
                            TheLine.emptyOut(CurrentTerminalReelID);
                            TheLine.UpdateLocInfo("", CurrentTerminalReelID, TheLine.LineStatusE.BOXDOOR_NOREEL, CID);
                            VLog.Info("检测到料盘空出2秒内消失 reelid:" + CurrentTerminalReelID);
                        }

                        if (RTStoreStatus != RTStoreStatus.LockToInStore && RTStoreStatus != RTStoreStatus.LockToOutStore && RTStoreStatus != RTStoreStatus.InStoreReady)
                        {
                            if (RTStoreStatus != RTStoreStatus.Ready)
                                VLog.Info("料仓已空闲");
                            RTStoreStatus = RTStoreStatus.Ready;
                            TerminalError = false;
                            LastStatusTime = DateTime.Now;
                            if (OutStoreJobList.Count > 0)
                            {
                                if (OutStoreJobList.Dequeue(out JobInfo jobInfo))
                                {
                                    RTStoreStatus = RTStoreStatus.LockToOutStore;
                                    if (!ServerCM.BeginOutStore(jobInfo.Clone(), VLog))
                                    {
                                        Msg.add(crc.GetString("Res0105.f3331370", "发送出库指令失败") + ":" + jobInfo.WareNum, MsgLevel.alarm);
                                        RTStoreStatus = RTStoreStatus.Ready;
                                    }
                                    CurrentTerminalReelID = jobInfo.WareNum;
                                }
                            }
                            if (InStoreJobInfo != null)
                            {
                                VLog.Info("入库成功:" + InStoreJobInfo.ToStr());
                                string[] substrings = InStoreJobInfo.WareNum.Split('#');
                                if (substrings.Length > 2)
                                {
                                    string secondSubstring = substrings[1].Substring(1);
                                    InStoreEnd(secondSubstring);
                                }
                                InStoreJobInfo = null;
                            }
                        }
                        else if ((DateTime.Now - LastStatusTime).TotalSeconds > 30)
                        {

                            if (InStoreJobInfo != null)
                                CurrentTerminalReelID = InStoreJobInfo.WareNum;
                            if (RTStoreStatus == RTStoreStatus.InStoreReady)
                            {
                                TerminalError = true;//入库超时30秒没有入库
                                VLog.Error($"入库超时出错,没有执行入库过程,RTStoreStatus={RTStoreStatus}:" + InStoreJobInfo?.ToStr());
                                CurrentTowerStatusText = crc.GetString("Res0065", "入库错误,没有执行入库过程");
                                Msg.add($"TerminalError" + crc.GetString("Res0099.42525c5f", "入库超时"), MsgLevel.alarm);
                            }
                            else if (RTStoreStatus == RTStoreStatus.LockToInStore)
                            {
                                TerminalError = true;//入库超时30秒没有入库
                                VLog.Error($"入库超时出错,没有检测到料盘,RTStoreStatus={RTStoreStatus}:" + InStoreJobInfo?.ToStr());
                                CurrentTowerStatusText = crc.GetString("Res0065", "入库错误,没有执行入库过程");
                                Msg.add($"TerminalError:" + crc.GetString("Res0099.42525c5f", "入库超时"), MsgLevel.alarm);
                            }
                            else if (RTStoreStatus == RTStoreStatus.LockToOutStore)
                            {
                                VLog.Error($"出库超时出错,没有检测到出库信息,RTStoreStatus={RTStoreStatus}:" + CurrentTerminalReelID);
                                CurrentTowerStatusText = crc.GetString("Res0112.e6c4624b","出库错误,没有执行出库过程");
                                Msg.add($"TerminalError:" + crc.GetString("Res0113.71e5a53a","出库超时"), MsgLevel.alarm);
                            }
                            else
                            {
                                VLog.Error($"未知状态超时,RTStoreStatus={RTStoreStatus}:" + InStoreJobInfo?.ToStr());
                                Msg.add($"TerminalError:" + crc.GetString("Res0114.d3023f35","料仓状态异常"), MsgLevel.warning);
                            }

                        }
                    }
                    else if (ti.StatusCode == 6015 || ti.StatusText == "Reel already loaded.") //料盘已在库内
                    {
                        TerminalError = true;
                    }
                    else if (ti.StatusCode == 1031 || ti.StatusCode == 1030) //分配库位,入库过程
                    {
                        ServerCM.storeStatus = StoreStatus.InStoreExecute;
                        if (RTStoreStatus != RTStoreStatus.InStoreExecute)
                            VLog.Info("入库执行中");
                        RTStoreStatus = RTStoreStatus.InStoreExecute;
                    }
                    else if (ti.StatusCode == 1032) //料仓已经检测到料盘
                    {
                        if (TerminalError && RTStoreStatus == RTStoreStatus.InStoreError)
                        {
                            CurrentTowerStatusText = "6015-Reel already loaded.";
                            VLog.Info("料仓异常状态 TerminalError=true");
                            return;
                        }
                        if ((DateTime.Now - LastStatusTime).TotalSeconds > 30)
                        {
                            RTStoreStatus = RTStoreStatus.InStoreDataTimeOut;
                        }
                        if (RTStoreStatus != RTStoreStatus.InStoreReady && InStoreJobInfo != null)
                        {
                            LastStatusTime = DateTime.Now;
                            VLog.Info("物料已在舱口");
                            RTStoreStatus = RTStoreStatus.InStoreReady;

                            if (!ServerCM.BeginInStore(CID, InStoreJobInfo))
                            {
                                RTStoreStatus = RTStoreStatus.LockToInStore;
                                //Msg.add(CID + crc.GetString("Res0051", "无法入库"), MsgLevel.alarm);
                                VLog.Info("入库指令失败:" + InStoreJobInfo.ToStr());
                            }
                            CurrentTerminalReelID = InStoreJobInfo.WareNum;
                            //Thread.Sleep()

                        }
                    }
                    else if (ti.StatusCode == 1040) //出库过程中
                    {
                        ServerCM.storeStatus = StoreStatus.OutMoveExecute;
                        if (RTStoreStatus != RTStoreStatus.OutMoveExecute)
                            VLog.Info("出库执行中");
                        RTStoreStatus = RTStoreStatus.OutMoveExecute;
                    }
                    else if (ti.StatusCode == 1041) //出库到舱门口
                    {
                        if (InStoreJobInfo == null) //没有入库信息,判断是正常出库
                        {
                            if (RTStoreStatus != RTStoreStatus.OutStoreReady)
                            {
                                VLog.Info("出库物料已到舱门口:" + CurrentTerminalReelID);
                                OutStoreEnd(CurrentTerminalReelID, CID);
                                LastStatusTime = DateTime.Now;
                            }


                            RTStoreStatus = RTStoreStatus.OutStoreReady;
                            OutStoreJobList.ClearLastPosid();
                        }
                        else
                        {
                            TerminalError = true; //有入库信息的情况下,出现出库状态
                            VLog.Info("异常出库:" + CurrentTerminalReelID);

                        }
                    }
                    else if (ti.StatusCode == 0 || ti.StatusCode == 18)
                    {
                        RTStoreStatus = RTStoreStatus.Offline;
                        TerminalError = false;
                        ServerCM.storeStatus = StoreStatus.None;
                    }
                    else if (ti.StatusCode >= 310000)
                    {
                        //if (RTStoreStatus != RTStoreStatus.InStoreError)
                        //{
                        //    LastProcessErrorTime = DateTime.Now.AddSeconds(10);
                        //    LastProcessErrorTimes = 0;
                        //}
                        //LastProcessErrorTimes++;
                        Msg.add($"StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus}", MsgLevel.warning);
                        RTStoreStatus = RTStoreStatus.Offline;
                        TerminalError = false;
                        ServerCM.storeStatus = StoreStatus.None;

                    }
                    else if (ti.StatusCode < 10000)
                    {
                        RTStoreStatus = RTStoreStatus.Busy;
                        ServerCM.storeStatus = StoreStatus.None;
                        TerminalError = false;
                        //VLog.Info($"StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus}");
                        Msg.add($"StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus}", MsgLevel.alarm);
                    }
                    else
                    {
                        //VLog.Error($"StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus}");
                        Msg.add($"StatusCode:{ti.StatusCode},StatusText:{ti.StatusText},OnlineStatus:{ti.OnlineStatus}", MsgLevel.alarm);
                        ServerCM.storeStatus = StoreStatus.InTrouble;
                        RTStoreStatus = RTStoreStatus.Error;
                        //CurrentTowerStatusText = $"{ti.StatusText}-{ti.OnlineStatus}";
                    }
                }
                else
                {

                    ServerCM.storeStatus = StoreStatus.None;
                    RTStoreStatus = RTStoreStatus.Offline;
                }
                /*
                18 offline
                26 Ready/Idle
                1031 loading material
                1032 material put into Terminal from outside
                1040 出库执行中
                1041 unloading material 物料已到舱门口 Remove Component Reel
                1042 material unloaded to Terminal from Tower 物料已取走
                6015 Reel already loaded.
                3068 初始化中
                310117 Opertion denied
                310249 Error Reel Sensor (249)
                310107,No scancode (107)
                310234,Error clamping on opening (234)
                */
                //统计剩余容量
                if (ti != null)
                {
                    string capacity = "";
                    for (int i = 0; i < ti.Slots.Count; i++)
                    {
                        var sn = ti.Slots[i].Slotname;
                        int w = 0;
                        int h = 0;
                        if (sn.StartsWith("s15"))
                        {
                            w = 15;
                            int.TryParse(sn.Substring(3), out h);
                        }
                        else if (sn.StartsWith("s13"))
                        {
                            w = 13;
                            int.TryParse(sn.Substring(3), out h);
                        }
                        else if (sn.StartsWith("s7"))
                        {
                            w = 7;
                            int.TryParse(sn.Substring(2), out h);
                        }
                        //boxStatus.data.Add("capacity", "7X8=1000;7X12=345;13X32=100;");
                        capacity += $"{w}X{h}={ti.Slots[i].Slots_free - ti.Slots[i].Slots_used};";
                        SlotsInfo[$"{w}X{h}"] = $"{ti.Slots[i].Slots_used}/{ti.Slots[i].Slots_free}";
                    }
                    ServerCM.capacity = capacity;
                }
            }
            catch (Exception ex)
            {
                ServerCM.storeStatus = StoreStatus.None;
                RTStoreStatus = RTStoreStatus.Offline;
                VLog.Error("VStore Error:" + ex);
            }
            finally
            {
                ServerCM.ProcessMsg(Msg.msg);
                Msg.clear();
            }
        }
        public Dictionary<string, string> SlotsInfo = new Dictionary<string, string>();
        public string GetStateStr()
        {
            if (!TowerInfo.Enable)
            {
                return "Disable";
            }
            if (RTStoreStatus == RTStoreStatus.OutStoreReady)
                return crc.GetString("Res0066", "出库中") + (!string.IsNullOrEmpty(CurrentTerminalReelID) ? "SN:" + CurrentTerminalReelID : "");

            if (RTStoreStatus == RTStoreStatus.LockToInStore)
                return crc.GetString("Res0067", "等待入库信息");

            if (RTStoreStatus == RTStoreStatus.LockToOutStore)
                return crc.GetString("Res0115.44b81dd3","等待出库");

            if (RTStoreStatus == RTStoreStatus.InStoreExecute || RTStoreStatus == RTStoreStatus.InStoreReady)
                return crc.GetString("Res0068", "入库中") + " Barcode:" + InStoreJobInfo?.WareNum;

            if (RTStoreStatus == RTStoreStatus.InStoreError)
                return crc.GetString("Res0069", "入库出错,物料NG") +"LT:"+ LastProcessErrorTimes + ", Barcode:" + InStoreJobInfo?.WareNum;

            if (RTStoreStatus == RTStoreStatus.Error)
                return CurrentTowerStatusText;

            return crc.GetString("Res0070", "空闲中");
        }
        /// <summary>
        /// 当前入库任务信息
        /// </summary>
        public volatile JobInfo InStoreJobInfo;
        StoreJobList OutStoreJobList;
        public void RequestInStore(JobInfo jobInfo) {
            LastStatusTime=DateTime.Now;
            TerminalError = false;
            InStoreJobInfo = jobInfo.Clone();
           //return vc.BeginInStore(CID, jobInfo);
        }

        public bool OutStoreEnd(string reelid,string towerid) {
            CurrentTerminalReelID = reelid;
            TheLine.UpdateLocInfo("", CurrentTerminalReelID, TheLine.LineStatusE.BOXDOOR, towerid);
            return ServerCM.SendStoreState("", reelid, StoreStatus.OutStoreEnd);
        }
        public bool InStoreEnd(string posid)
        {
            return ServerCM.SendStoreState("", posid, StoreStatus.InStoreEnd);
        }

        internal void log(string v)
        {
            VLog.Info(v);
        }
    }
    public enum RTStoreStatus {
        Offline,
        Ready,
        InStoreReady,
        InStoreExecute,
        InStoreError,
        OutStoreReady,
        OutMoveExecute,
        NGOutReady,
        /// <summary>
        /// 锁定料仓准备入库
        /// </summary>
        LockToInStore,
        /// <summary>
        /// 出库锁定料仓
        /// </summary>
        LockToOutStore,
        Busy,
        Error,
        InStoreDataTimeOut
    }
}