MainWindow.xaml.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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using XingYe_ACS.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel.Web;
using XingYe_ACS.WebApi;
using System.Data;
using XingYe_ACS.Business;
using HslCommunication.Profinet.Siemens;
using System.IO;
using Point = XingYe_ACS.BaseStruct.Point;

namespace XingYe_ACS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private Thread thSocket;
        private Thread thTaskOperator;
        private Socket SvrSocket;
        private SiemensS7Net siemensS7Net;
        private Sharp canvasSharp = new Sharp();
        private List<SiemensS7Net> S7s = new List<SiemensS7Net>();
        private WinTaskManager WinTaskManager = new WinTaskManager();
        private WinTest WinText = new WinTest();
        private WinRunData WinRunData = new WinRunData();
        private WinOrganization WinOrganization = new WinOrganization();
        private WinHistory WinHistory = new WinHistory();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            App.AppDispatcher = this.Dispatcher;
            App.WinMain = this;
            //设置全屏
            Width = SystemParameters.PrimaryScreenWidth;
            Height = SystemParameters.WorkArea.Height;
            Left = (SystemParameters.PrimaryScreenWidth - this.Width) / 2;
            Top = (SystemParameters.WorkArea.Height - this.Height) / 2;
            App.Canvas_Monit = Canvas_Monitor;
            string initErr = "";
            Init.InitData(ref initErr);
            if (initErr != "")
            {
                MessageBox.Show(initErr);
                Environment.Exit(0);
            }
            if (App.PointList.Count != 0)
            {
                int[] Area = canvasSharp.MoniterAreaInit();
                Canvas_Monitor.Width = Area[0];
                Canvas_Monitor.Height = Area[1];
                try
                {
                    //设置货架位置
                    canvasSharp.ShelfSet();
                    canvasSharp.AgvPointSet();
                    canvasSharp.PointSet();
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.ToString());
                }
            }

            DGrid_Agv.ItemsSource = App.AgvList.OrderBy(a => a.strAgvNo);

            //开启webapi通讯接口
            thSocket = new Thread(StartWebApi);
            thSocket.IsBackground = true;
            thSocket.Start();

            //和AGV通讯
            thSocket = new Thread(SocketBuild);
            thSocket.IsBackground = true;
            thSocket.Start();

            //任务处理
            thTaskOperator = new Thread(TaskOperator);
            thTaskOperator.IsBackground = true;
            thTaskOperator.Start();//任务提交、任务完成、充电

            //开启S7长连接
            siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, App.S7IP)
            { ConnectTimeOut = 5000, ReceiveTimeOut = 5000 };
            var result = siemensS7Net.ConnectServer();
            S7s.Add(siemensS7Net);

            OpenS7();
            #region 注释ModbusTCP通讯
            //busTcpClient = new ModbusTcpNet(hoister.hoisterAddress, 502);
            //busTcpClient.ConnectServer();
            //thSocket = new Thread(OpenModbusTCP);
            //thSocket.IsBackground = true;
            //thSocket.Start();
            #endregion 注释ModbusTCP通讯
        }

        /// <summary>
        /// 开启web通讯接口
        /// </summary>
        private void StartWebApi()
        {
            try
            {
                string ip = ConfigurationManager.AppSettings["WebApiIp"].ToString();
                WebServiceHost host = new WebServiceHost(typeof(ServiceImpl), new Uri(ip));
                host.Open();
                AcsPushBuild.AcsPushBuildInit();
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("StartWcf", "开放接口失败:" + ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// Socket通讯
        /// </summary>
        private void SocketBuild()
        {
            if (SvrSocket != null)
            {
                SvrSocket.Close();
                SvrSocket = null;
            }
            try
            {
                IPAddress ip = IPAddress.Parse(App.Ip);
                IPEndPoint ipe = new IPEndPoint(ip, App.Port);
                SvrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                SvrSocket.Bind(ipe);
                ManageTcp.ServerListen(App.ServerConactNum, SvrSocket);
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("SocketBuild", "创建AGV监听线程失败:" + Ex.Message.Replace(Environment.NewLine, ""));
                thSocket = new Thread(SocketBuild);
                thSocket.IsBackground = true;
                thSocket.Start();
            }
        }

        /// <summary>
        /// 任务处理
        /// </summary>
        private void TaskOperator()
        {
            try
            {
                while (true)
                {
                    //获取小车
                    int intTaskNum = TaskManage.AgvGetTask();
                    //任务分解
                    TaskManage.TaskSplit();
                    //分配路径
                    GetPath.AssignePath();
                    //清理无任务小车路径点
                    ComnMethod.ClearPointDirection();
                    //清理无任务小车点占用
                    ComnMethod.SetPointOccupy();
                    //20180925解锁充电桩
                    ComnMethod.UnLockStation();
                    //建立充电任务
                    ChargeManager.AgvCharge(intTaskNum);
                    #region 测试任务
                    //简易版取消充电
                    //Business.TaskManage.ChargeCancle();
                    //调用WCS补空货架
                    //Business.TaskManage.PlatformListen();
                    //建立回库任务
                    //Business.TaskManage.MoveBack();
                    //建立循环行走任务
                    if (App.StartWalk == 1)
                    {
                        Business.TaskManage.WalkTask();
                    }
                    //if (App.RotateTask == 1)
                    //{
                    //    Business.TaskManage.RotateTask();
                    //}
                    //if (App.LiftTask == 1)
                    //{
                    //    Business.TaskManage.LiftTask();
                    //}
                    //if (App.PalletTask == 1)
                    //{
                    //    Business.TaskManage.PalletTask();
                    //}
                    #endregion 测试任务
                    //判断心跳
                    ComnMethod.CheckAgvOnline();
                    //判断手自动 决定小车是否充电
                    MsgManage.IsCharage();
                    Thread.Sleep(1000);
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("TaskOperator", "任务处理线程失败:" + Ex.Message.Replace(Environment.NewLine, ""));
                thTaskOperator = new Thread(TaskOperator);
                thTaskOperator.IsBackground = true;
                thTaskOperator.Start();
            }
        }

        #region
        /// 打开ModBusTcp通讯
        //private void OpenModbusTCP()
        //{
        //    try
        //    {
        //        while (true)
        //        {
        //            foreach (ModbusTcpNet modbusTcpNet in modbusTcps)
        //            {
        //                OperateResult<Int16[]> read = modbusTcpNet.ReadInt16("0", 10);
        //                if (read.IsSuccess)
        //                {
        //                    //任务创建
        //                    var result = TaskManage.HoisterTaskBuild(read.Content);
        //                    var writeresult = modbusTcpNet.Write("0", result);
        //                    if (!writeresult.IsSuccess)
        //                    {
        //                        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + "{心跳}" + writeresult.Message);
        //                    }
        //                }
        //                Thread.Sleep(1000);
        //            }
        //        }
        //    }
        //    catch (Exception Ex)
        //    {
        //        App.ExFile.MessageError("OpenModbusTCP", "开启Modbus通讯线程失败:" + Ex.StackTrace);
        //    }
        //}
        #endregion

        private void OpenS7()
        {
            try
            {
                foreach (SiemensS7Net siemensS7Net in S7s)
                {
                    Thread thread = new Thread(ReadWriteS7);
                    thread.Start(siemensS7Net);
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("OpenS7", "开启S7通讯线程失败:" + Ex.StackTrace);
            }
        }

        private void ReadWriteS7(object S7Net)
        {

            SiemensS7Net siemensS7Net = (SiemensS7Net)S7Net;
            while (true)
            {
                try
                {
                    App.ExFile.MessageLog("S7", "进入s7判断");
                    //var result = TaskManage.ReturnAGVState();

                    var read = siemensS7Net.Read("DB402.10", 2);
                    if (read.IsSuccess)
                    {
                        //创建交互站台任务
                        var result = TaskManage.StationTaskBuild(read.Content);

                        #region 写入AGV基础信息及交互信息

                        //var result = TaskManage.ReturnAGVState();

                        #endregion
                        if (result != null)
                        {
                            var writeresult = siemensS7Net.Write("DB402.0", result[0]);
                            var writeresult1 = siemensS7Net.Write("DB402.1", result[1]);
                            if (!writeresult.IsSuccess || !writeresult1.IsSuccess)
                            {
                                App.ExFile.MessageError("OpenS7", "S7异常写入:");
                            }
                        }
                    }else
                    {
                        App.ExFile.MessageLog("S7", "没信号判断是否充电");
                        Agv agv = App.AgvList.Find(a => a.strAgvNo == "A11_3108");

                        Point point = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                        if (App.TaskList.ToList().Exists(a => a.strTaskID == EnumMsg.TaskType.充电.ToString())) continue;
                        if (agv.strBarcode != "746"|| agv.strBarcode != "296") continue;
                        if (agv.currentCharge > 90) continue;
                        if (App.TaskList.Count > 0)
                        {
                            UpdateManage.DeleteTask();
                            ComnMethod.DeleteTask(agv.agvTask);
                            Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                            Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                            TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                        }
                        else
                        {

                            Point startPoint = App.PointList.Find(a => a.strBarcode == agv.strBarcode);
                            Point endPoint = App.PointList.Find(a => a.strBarcode == "296");
                            TaskManage.TaskBuild("充电", "", "", 1, agv.strAgvNo, "", startPoint, endPoint, "");
                        }
                    }

                    //写入AGV信息到指定DB块中
                    Thread.Sleep(1000);
                }
                catch (Exception Ex)
                {
                    App.ExFile.MessageError("OpenS7", "S7异常读取或写入:" + Ex.StackTrace);
                }
            }
        }

        #region 界面操作

        /// <summary>
        /// 关闭程序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确认是否退出程序?\n退出后将无法继续全部或部分功能!",
                "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Environment.Exit(0);
            }
        }

        /// <summary>
        /// 左键拖框事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        /// <summary>
        /// 最小化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        /// <summary>
        /// 最大化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMax_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
            else if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }

        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("请确认是否退出程序?\n退出后将无法继续全部或部分功能!",
                "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Environment.Exit(0);
            }
        }

        /// <summary>
        /// 菜单栏点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn == null) return;
            //任务管理
            if (btn.Name == Btn_TaskManager.Name)
            {
                if (WinTaskManager == null || WinTaskManager.IsVisible == false)
                {
                    WinTaskManager.Show();
                }
                else
                {
                    WinTaskManager.Activate();
                    WinTaskManager.WindowState = WindowState.Normal;
                }
            }
            //手动任务
            else if (btn.Name == Btn_Test.Name)
            {
                if (WinText == null || WinText.IsVisible == false)
                {
                    WinText.Show();
                }
                else
                {
                    WinText.Activate();
                    WinText.WindowState = WindowState.Normal;
                }
            }
            //运行数据
            else if (btn.Name == Btn_RunData.Name)
            {
                if (WinRunData == null || WinRunData.IsVisible == false)
                {
                    WinRunData.Show();
                }
                else
                {
                    WinRunData.Activate();
                    WinRunData.WindowState = WindowState.Normal;
                }
            }
            else if (btn.Name == Btn_MachineManager.Name)
            {
                if (WinOrganization == null || WinOrganization.IsVisible == false)
                {
                    WinOrganization.Show();
                }
                else
                {
                    WinOrganization.Activate();
                    WinOrganization.WindowState = WindowState.Normal;
                }
            }
            //历史查询
            else if (btn.Name == Btn_History.Name)
            {
                if (WinHistory == null || WinHistory.IsVisible == false)
                {
                    WinHistory.Show();
                }
                else
                {
                    WinHistory.Activate();
                    WinHistory.WindowState = WindowState.Normal;
                }
            }
        }

        /// <summary>
        /// 异常信息清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TBox_EMessage_Clear_Click(object sender, RoutedEventArgs e)
        {
            TBox_EMessage.Clear();
        }

        /// <summary>
        /// 系统异常信息清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TBox_BussErrMsg_Clear_Click(object sender, RoutedEventArgs e)
        {
            TBox_BussErrMsg.Clear();
        }

        #endregion
    }

    public class MakeupFonts : System.Windows.Markup.MarkupExtension
    {
        private string SizeType;

        public MakeupFonts(string fontType)
        { SizeType = fontType; }

        public override object ProvideValue(IServiceProvider Isp)
        {
            double FontSize = 0d;
            switch (SizeType)
            {
                case "Title":
                    FontSize = 20;
                    break;

                case "Normal":
                    FontSize = 12;
                    break;

                case "BottomTitle":
                    FontSize = 20;
                    break;
            }
            return FontSize;
        }
    }

    //定义值转换器
    [ValueConversion(typeof(string), typeof(string))]
    //小车背景色
    public class AgvBackGroundConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return new SolidColorBrush(Colors.Transparent);

            if ((int)value == 0) return new SolidColorBrush(Colors.Transparent);
            else if ((int)value == 1) return new SolidColorBrush(Colors.Red);
            else if ((int)value == 2) return new SolidColorBrush(Colors.Yellow);
            else if ((int)value == 3) return new SolidColorBrush(Colors.SkyBlue);
            else if ((int)value == 4) return new SolidColorBrush(Colors.LightGreen);
            else if ((int)value == 90) return new SolidColorBrush(Colors.Gray);
            else return new SolidColorBrush(Colors.Red);
            //return Application.res
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}