123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using AGV_WPF_Global;
- using System.ComponentModel;
- namespace AGV_WPF.Models
- {
- public class TrafficAreaModel : INotifyPropertyChanged
- {
- public int TrafficNum { get; set; } //管制区编号
- public string TrafficName { get; set; } //管制区别名
- public AGVInfoModel MainAgv { get; set; } //管制区主机
- public DateTime EnterTime { get; set; } //管制区主机
- public bool IsTraffic { get; set; } //管制区主机
- private IList<AGVInfoModel> ListAgvQueue; //排队AGV
- private IList<int> ListMark; //包含的地标卡号
- public IList<AGVInfoModel> listAgvQueue
- {
- get
- {
- if (ListAgvQueue != null)
- {
- return ListAgvQueue;
- }
- else
- {
- ListAgvQueue = new List<AGVInfoModel>();
- return ListAgvQueue;
- }
- }
- }
- public IList<int> listMark
- {
- get
- {
- if (ListMark != null)
- {
- return ListMark;
- }
- else
- {
- ListMark = new List<int>();
- return ListMark;
- }
- }
- }
- object objLock = new object();
- public event PropertyChangedEventHandler PropertyChanged;
- //有agv进入该管制区
- public bool InTraffic(AGVInfoModel agv)
- {
- bool re = false;
- if (!this.listAgvQueue.Contains(agv))
- {
- lock (objLock)
- {
- this.listAgvQueue.Add(agv);
- re = true;
- }
- }
- if (this.MainAgv == null)
- {
- if (this.listAgvQueue.Count > 0)
- {
- this.MainAgv = this.listAgvQueue[0];
- this.IsTraffic = true;
- if (agv == this.listAgvQueue[0])
- {
- agv.TrafficFlag = false;
- agv.TRAFFICAGV = null;
- re = true;
- }
- else
- {
- agv.TrafficFlag = true;
- agv.TRAFFICAGV = this.MainAgv;
- re = true;
- }
- }
- }
- else
- {
- if (this.MainAgv == agv)
- {
- agv.TRAFFICAGV = null;
- agv.TrafficFlag = false;
- re = true;
- }
- else
- {
- agv.TRAFFICAGV = this.MainAgv;
- agv.TrafficFlag = true;
- re = true;
- }
- }
- return re;
- }
- //有AGV进入混合管制区(交叉区域)
- public bool InMixTraffic(AGVInfoModel agv)
- {
- bool re = false;
- if (!this.listAgvQueue.Contains(agv))
- {
- lock (objLock)
- {
- this.listAgvQueue.Insert(0, agv);
- re = true;
- }
- }
- if (this.MainAgv == null)
- {
- if (this.listAgvQueue.Count > 0)
- {
- this.MainAgv = this.listAgvQueue[0];
- this.IsTraffic = true;
- if (agv == this.listAgvQueue[0])
- {
- agv.TrafficFlag = false;
- agv.TRAFFICAGV = null;
- re = true;
- }
- else
- {
- agv.TrafficFlag = true;
- agv.TRAFFICAGV = this.MainAgv;
- re = true;
- }
- }
- }
- else
- {
- if (this.MainAgv == agv)
- {
- agv.TRAFFICAGV = null;
- agv.TrafficFlag = false;
- re = true;
- }
- else
- {
- agv.TRAFFICAGV = this.MainAgv;
- agv.TrafficFlag = true;
- re = true;
- }
- }
- return re;
- }
- //有agv退出该管制区
- public bool OutTraffic(AGVInfoModel agv)
- {
- bool re = false;
- if (this.listAgvQueue.Contains(agv))
- {
- lock (objLock)
- {
- this.listAgvQueue.Remove(agv);
- agv.IsMainAgv = false;
- agv.TrafficFlag = false;
- //agv.TRAFFICNUM.Remove(this);
- agv.TRAFFICAGV = null;
- }
- }
- //更新管制区状态
- if (agv == this.MainAgv)
- {
- if (this.listAgvQueue.Count > 0)
- {
- this.IsTraffic = true;
- AGVInfoModel tempAgv = this.listAgvQueue[0];
- //获取AGV对象
- tempAgv.TrafficFlag = false;
- tempAgv.IsMainAgv = true;
- tempAgv.TRAFFICAGV = null;
- this.MainAgv = tempAgv;
- foreach (var i in this.listAgvQueue)
- {
- if (i != this.MainAgv)
- {
- i.TRAFFICAGV = this.MainAgv;
- }
- }
- }
- else
- {
- this.IsTraffic = false;
- this.MainAgv = null;
- }
- re = true;
- }
- return re;
- }
- //AGV进入管制区
- public void AGVINTER(AGVInfoModel agv, bool falg)
- {
- bool agvfirst = falg;
- if (!this.IsTraffic) { this.IsTraffic = true; }
- if (this.MainAgv != agv && !this.listAgvQueue.Contains(agv))
- ++agv.ReleaseCount;
- if (this.MainAgv != null)
- {
- #region 管制区已经有主机占领了
- if (this.MainAgv != agv)
- {
- #region 主机不是我,这个管制区不是我管制
- foreach (var traffic in agv.TRAFFICNUM)
- {
- //如果我所管制的区域里的等候名单中有即将进入的这个管制区的主机
- //表明将会发生死管现象,应该避免。所以在此检查一下。
- if (traffic.listAgvQueue.Contains(this.MainAgv))
- {
- ReleaseMixMainAgv();
- break;
- }
- }
- //排队等候
- agv.TRAFFICAGV = this.MainAgv;
- if (!this.listAgvQueue.Contains(agv))
- {
- if (!agvfirst)//看是否进入交叉管制区
- {
- this.listAgvQueue.Add(agv);
- }
- else
- {
- this.listAgvQueue.Insert(0, agv);
- }
- //this.listAgvQueue.Add(agv);
- }
- #endregion
- }
- else
- {
- #region 主机是我,这个管制区是我管制的
- agv.TRAFFICAGV = null;
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- #endregion
- }
- #endregion
- }
- else
- {
- #region 这个管制区没有主机,我将成为主机占领该区
- if (this.listAgvQueue.Count > 0 && this.listAgvQueue[0] != agv)
- {
- this.MainAgv = this.listAgvQueue[0];
- this.listAgvQueue.RemoveAt(0);
- if (!agvfirst)//看是否进入交叉管制区修改
- {
- this.listAgvQueue.Add(agv);
- }
- else
- {
- this.listAgvQueue.Insert(0, agv);
- }
- //this.listAgvQueue.Add(agv);
- }
- else
- {
- this.MainAgv = agv;
- agv.IsMainAgv = true;
- agv.TrafficFlag = false;//---罗国--2017-10-30成主机后状态变成不管制,为了解决互相管制的问题。
- foreach (var a in this.listAgvQueue)
- {
- a.TRAFFICAGV = agv;
- }
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- //this.listAgvQueue.Add(agv);
- }
- #endregion
- }
- }
- //AGV进入管制区
- public void AGVINTER0(AGVInfoModel agv, bool falg)
- {
- bool agvfirst = falg;
- if (!this.IsTraffic) { this.IsTraffic = true; }
- if (this.MainAgv != agv && !this.listAgvQueue.Contains(agv))
- ++agv.ReleaseCount;
- //if (!GlobalPara.IsRealOpenSystem)
- //{
- // if (!GlobalPara.dicTraffic.ContainsKey(this.TrafficNum))
- // {
- // GlobalPara.dicTraffic.Add(this.TrafficNum, agv);
- // this.MainAgv = agv;
- // if (this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Remove(agv);
- // }
- // if (!this.IsTraffic)
- // {
- // this.IsTraffic = true;
- // }
- // }
- // else
- // {
- // if (this.listMark.Contains(agv.MARKNUM))
- // {
- // int index = this.listMark.IndexOf(agv.MARKNUM);
- // if (index > this.listMark.IndexOf((GlobalPara.dicTraffic[this.TrafficNum].MARKNUM)))
- // {
- // this.MainAgv = agv;
- // if (!this.IsTraffic)
- // {
- // this.IsTraffic = true;
- // }
- // GlobalPara.dicTraffic[this.TrafficNum].TRAFFICAGV = this.MainAgv;
- // GlobalPara.dicTraffic[this.TrafficNum].TrafficFlag = true;
- // if (this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Remove(agv);
- // }
- // if (!this.listAgvQueue.Contains(GlobalPara.dicTraffic[this.TrafficNum]))
- // {
- // this.listAgvQueue.Add(GlobalPara.dicTraffic[this.TrafficNum]);
- // }
- // GlobalPara.dicTraffic[this.TrafficNum] = agv;
- // }
- // else
- // {
- // if (index == this.listMark.IndexOf((GlobalPara.dicTraffic[this.TrafficNum].MARKNUM)))
- // {
- // int trafficAGVNum = GlobalPara.dicTraffic[this.TrafficNum].AGVNUM;
- // int trafficIndex = GlobalPara.lastReadMarkList.FindIndex(o => o.Split(',')[0] == agv.AGVNUM.ToString());
- // int curIndex = GlobalPara.lastReadMarkList.FindIndex(o => o.Split(',')[0] == agv.AGVNUM.ToString());
- // if (trafficIndex > -1 && curIndex > -1)
- // {
- // string[] trafficAry = GlobalPara.lastReadMarkList[trafficIndex].Split(',');
- // string[] curAry = GlobalPara.lastReadMarkList[curIndex].Split(',');
- // DateTime trTraTime, curTime;
- // if (trafficAry.Length > 2 && curAry.Length > 2 && trafficAry[1] == curAry[1])
- // {
- // bool b1 = DateTime.TryParse(trafficAry[2], out trTraTime);
- // bool b2 = DateTime.TryParse(curAry[2], out curTime);
- // if (b1 && b2 && curTime < trTraTime)
- // {
- // this.MainAgv = agv;
- // if (!this.IsTraffic)
- // {
- // this.IsTraffic = true;
- // }
- // GlobalPara.dicTraffic[this.TrafficNum].TRAFFICAGV = null;
- // GlobalPara.dicTraffic[this.TrafficNum].TrafficFlag = false;
- // GlobalPara.dicTraffic[this.TrafficNum] = agv;
- // }
- // else
- // {
- // agv.TRAFFICAGV = GlobalPara.dicTraffic[this.TrafficNum];
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // }
- // }
- // else
- // {
- // agv.TRAFFICAGV = GlobalPara.dicTraffic[this.TrafficNum];
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // }
- // }
- // else
- // {
- // agv.TRAFFICAGV = GlobalPara.dicTraffic[this.TrafficNum];
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // }
- // }
- // else
- // {
- // agv.TRAFFICAGV = GlobalPara.dicTraffic[this.TrafficNum];
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // }
- // }
- // }
- // }
- //}
- //else
- {
- if (this.MainAgv != null)
- {
- #region 管制区已经有主机占领了
- if (this.MainAgv != agv)
- {
- if (this.listMark.Contains(agv.MARKNUM))
- {
- int index = this.listMark.IndexOf(agv.MARKNUM);
- if (index < this.listMark.IndexOf(this.MainAgv.MARKNUM))
- {
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- // this.MainAgv.TRAFFICAGV = agv;
- // this.MainAgv.TrafficFlag = true;
- if (!this.listAgvQueue.Contains(this.MainAgv))
- {
- this.listAgvQueue.Add(this.MainAgv);
- }
- for (int i = 0; i < this.listAgvQueue.Count; i++)
- {
- this.listAgvQueue[i].TRAFFICAGV = agv;
- this.listAgvQueue[i].TrafficFlag = true;
- }
- this.MainAgv = agv;
- if (!this.IsTraffic)
- {
- this.IsTraffic = true;
- }
- }
- else
- {
- if (index == this.listMark.IndexOf(this.MainAgv.MARKNUM))
- {
- // int trafficAGVNum = GlobalPara.dicTraffic[this.TrafficNum].AGVNUM;
- // int trafficIndex = GlobalPara.lastReadMarkList.FindIndex(o => o.Split(',')[0] == this.MainAgv.AGVNUM.ToString());
- // int curIndex = GlobalPara.lastReadMarkList.FindIndex(o => o.Split(',')[0] == agv.AGVNUM.ToString());
- // if (trafficIndex > -1 && curIndex > -1)
- // {
- // string[] trafficAry = GlobalPara.lastReadMarkList[trafficIndex].Split(',');
- // string[] curAry = GlobalPara.lastReadMarkList[curIndex].Split(',');
- DateTime trTraTime, curTime;
- // if (trafficAry.Length > 2 && curAry.Length > 2 && trafficAry[1] == curAry[1])
- // {
- // bool b1 = DateTime.TryParse(trafficAry[2], out trTraTime);
- // bool b2 = DateTime.TryParse(curAry[2], out curTime);
- // if (b1 && b2 && curTime < trTraTime)
- if (agv.LastModifyMarkTime < this.MainAgv.LastModifyMarkTime)
- {
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- //this.MainAgv.TRAFFICAGV = agv;
- //this.MainAgv.TrafficFlag = true;
- if (!this.listAgvQueue.Contains(this.MainAgv))
- {
- this.listAgvQueue.Add(this.MainAgv);
- }
- for (int i = 0; i < this.listAgvQueue.Count; i++)
- {
- this.listAgvQueue[i].TRAFFICAGV = agv;
- this.listAgvQueue[i].TrafficFlag = true;
- }
- this.MainAgv = agv;
- if (!this.IsTraffic)
- {
- this.IsTraffic = true;
- }
- }
- else
- {
- agv.TRAFFICAGV = this.MainAgv;
- agv.TrafficFlag = true;
- if (!this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Add(agv);
- }
- }
- // }
- // else
- // {
- // agv.TRAFFICAGV = this.MainAgv;
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // }
- //}
- //else
- //{
- // agv.TRAFFICAGV = this.MainAgv;
- // agv.TrafficFlag = true;
- // if (!this.listAgvQueue.Contains(agv))
- // {
- // this.listAgvQueue.Add(agv);
- // }
- //}
- }
- else
- {
- agv.TRAFFICAGV = this.MainAgv;
- agv.TrafficFlag = true;
- if (!this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Add(agv);
- }
- }
- }
- }
- #region 主机不是我,这个管制区不是我管制
- //foreach (var traffic in agv.TRAFFICNUM)
- //{
- // //如果我所管制的区域里的等候名单中有即将进入的这个管制区的主机
- // //表明将会发生死管现象,应该避免。所以在此检查一下。
- // if (traffic.listAgvQueue.Contains(this.MainAgv))
- // {
- // ReleaseMixMainAgv();
- // break;
- // }
- //}
- ////排队等候
- //agv.TRAFFICAGV = this.MainAgv;
- //if (!this.listAgvQueue.Contains(agv))
- //{
- // if (!agvfirst)//看是否进入交叉管制区
- // {
- // this.listAgvQueue.Add(agv);
- // }
- // else
- // {
- // this.listAgvQueue.Insert(0, agv);
- // }
- // //this.listAgvQueue.Add(agv);
- //}
- #endregion
- }
- else
- {
- #region 主机是我,这个管制区是我管制的
- agv.TRAFFICAGV = null;
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- #endregion
- }
- #endregion
- }
- else
- {
- #region 这个管制区没有主机,我将成为主机占领该区
- // else
- {
- if (this.listAgvQueue.Count > 0 && this.listAgvQueue[0] != agv)
- {
- {
- this.MainAgv = this.listAgvQueue[0];
- this.listAgvQueue.RemoveAt(0);
- if (!agvfirst)//看是否进入交叉管制区修改
- {
- this.listAgvQueue.Add(agv);
- }
- else
- {
- this.listAgvQueue.Insert(0, agv);
- }
- }
- //this.listAgvQueue.Add(agv);
- }
- else
- {
- this.MainAgv = agv;
- agv.IsMainAgv = true;
- agv.TrafficFlag = false;//---罗国--2017-10-30成主机后状态变成不管制,为了解决互相管制的问题。
- foreach (var a in this.listAgvQueue)
- {
- a.TRAFFICAGV = agv;
- }
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- //this.listAgvQueue.Add(agv);
- }
- }
- #endregion
- }
- }
- }
- //AGV离开管制区
- public void AGVLEAVE(AGVInfoModel agv)
- {
- --agv.ReleaseCount;
- //解除管制
- if (this.MainAgv == agv)
- {
- this.IsTraffic = false;
- this.MainAgv = null;
- }
- else if (this.MainAgv == null)
- {
- this.IsTraffic = false;
- }
- if (this.listAgvQueue.Contains(agv))
- {
- this.listAgvQueue.Remove(agv);
- }
- if (agv.TRAFFICAGV != null && agv.TRAFFICAGV.TRAFFICNUM.Contains(this))
- {
- this.listAgvQueue.Insert(0, agv.TRAFFICAGV);
- }
- }
- public void ReleaseAgv()
- {
- AGVInfoModel reAgv = this.listAgvQueue[0];
- if (reAgv.ReleaseCount > 1)//本来是按照1来判断
- {
- #region 它相对这个管制区是管制者,但它不被允许放行,因为还有其他区域的管制者管制着它。
- this.IsTraffic = true;
- this.MainAgv = reAgv;
- if (this.listAgvQueue.Contains(reAgv))
- {
- this.listAgvQueue.Remove(reAgv);
- }
- reAgv.IsMainAgv = true;
- foreach (var a in this.listAgvQueue)
- {
- a.TRAFFICAGV = reAgv;
- }
- //判断AGV是否可以被放行
- bool fx = true;
- foreach (var t in reAgv.TRAFFICNUM)
- {
- if (t.MainAgv != reAgv)
- {
- fx = false;
- }
- }
- if (fx)
- {
- reAgv.TrafficFlag = false;
- this.AGVINTER(reAgv, false);
- }
- else
- {
- reAgv.TrafficFlag = true;
- }
- #endregion
- }
- else
- {
- //reAgv.TrafficFlag = false;
- this.AGVINTER(reAgv, false);
- }
- }
- public void ReleaseAgv(AGVInfoModel reAgv)
- {
- if (reAgv.ReleaseCount > 1)
- {
- #region 它相对这个管制区是管制者,但它不被允许放行,因为还有其他区域的管制者管制着它。
- this.IsTraffic = true;
- this.MainAgv = reAgv;
- if (this.listAgvQueue.Contains(reAgv))
- {
- this.listAgvQueue.Remove(reAgv);
- }
- reAgv.IsMainAgv = true;
- foreach (var a in this.listAgvQueue)
- {
- a.TRAFFICAGV = reAgv;
- }
- //判断AGV是否可以被放行
- bool fx = true;
- foreach (var t in reAgv.TRAFFICNUM)
- {
- if (t.MainAgv != reAgv)
- {
- fx = false;
- }
- }
- if (fx)
- {
- reAgv.TrafficFlag = false;//放行之前把标识位给置为false
- this.AGVINTER(reAgv, false);
- }
- else
- {
- reAgv.TrafficFlag = true;
- }
- #endregion
- }
- else
- {
- //reAgv.TrafficFlag = false;
- this.AGVINTER(reAgv, false);
- }
- }
- public void ReleaseMainAgv()
- {
- if (this.MainAgv.ReleaseCount < 2)//被管制的数量小于2时,说明管制区里只有1车被管制,则主机可以随便走。
- {
- this.MainAgv.TrafficFlag = false;
- if (this.MainAgv.TRAFFICAGV != null) this.MainAgv.TRAFFICAGV = null;
- if (this.listAgvQueue.Contains(this.MainAgv)) this.listAgvQueue.Remove(this.MainAgv);
- }
- }
- public void ReleaseMixMainAgv()
- {
- this.MainAgv.TrafficFlag = false;
- if (this.MainAgv.TRAFFICAGV != null) this.MainAgv.TRAFFICAGV = null;
- if (this.listAgvQueue.Contains(this.MainAgv)) this.listAgvQueue.Remove(this.MainAgv);
- }
- public int Blockade(AGVInfoModel agv)
- {
- if (!this.IsTraffic) this.IsTraffic = true; //确保管制区的状态是管制中,封锁该管制区。
- if (this.MainAgv != null)
- {
- if (this.MainAgv != agv)
- {
- this.listAgvQueue.Add(agv); //把该AGV加入到管制区前的等候队伍中等候。
- return 2;
- }
- }
- else
- {
- this.MainAgv = agv;//直接让该AGV占领该管制区成为主机。
- return 1;
- }
- return 0;
- }
- private void NotifyPropertyChanged(String info)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(info));
- }
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- }
|