Common.cs 12.9 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;

namespace AGVControl
{
    /// <summary>
    /// 公共参数
    /// </summary>
    public static class Common
    {
        /// <summary>
        /// 节点信息
        /// </summary>
        public static List<ClientNode> nodeInfo;
        /// <summary>
        /// 小车信息
        /// </summary>
        public static List<Agv_Info> agvInfo;
        public static System.Windows.Forms.TextBox logTextBox;
        public static System.Windows.Forms.DataGridView missionView;
        public static BLL.AgvServer server;
        public static BLL.Control control;
        public static BLL.MiR_API mir;
        public static BLL.WebService web;
        public static ChargeStatus chargeStatus;
        public static string itsHttp;
        public static log4net.ILog log;
        public static Dictionary<string, string> agvMission;
        public static Dictionary<string, string> showNameMissionName;
        public static Dictionary<string, string> agvProductionLine;
        public static System.Configuration.Configuration appConfig;
        public static MissionManager missionManager;
        public static readonly string CONFIG_PATH = AppDomain.CurrentDomain.BaseDirectory + "Config\\";
        /// <summary>
        /// E21 E22 是否允许离开/进入
        /// </summary>
        public static bool IsAllowLeaveOrEnter = false;
        private static List<string> msg = new List<string>();
        private static string preLog = "";


        public static void LogInfo(string text, bool isShow = true)
        {
            if (logTextBox.InvokeRequired)
            {
                logTextBox.Invoke(new Action(() => LogInfo(text, isShow)));
                return;
            }

            if (preLog.Equals(text))//连续重复的日志只打印一次
                return;
            preLog = text;
            if (msg.Count > 25)
            {
                msg.RemoveRange(0, 10);
            }
            log.Info(text);
            string tmpStr = "";
            if (isShow)
            {
                msg.Add(string.Format("[{0}] {1}\r\n", DateTime.Now.ToString("HH:mm:ss"), text));
                msg.ForEach(s => tmpStr += s);
                logTextBox.Text = tmpStr;
            }
        }
        public static void ReadLinePlace()
        {
            if (!System.IO.File.Exists(CONFIG_PATH + "LinePlace.txt"))
            {
                File.Create(CONFIG_PATH + "LinePlace.txt");
                return;
            }

            string[] s = System.IO.File.ReadAllLines(CONFIG_PATH + "LinePlace.txt");
            for (int i = 0; i < s.Count(); i++)
            {
                string[] mission = s[i].Split(',');
                if (mission.Length != 2)
                    continue;
                missionManager.missionList.Add(new MissionStru(mission[0], mission[1]));

            }

        }


        public static bool AddLinePlace(string nodeName)
        {
            int idx = Common.nodeInfo.FindIndex(s => s.Name == nodeName);
            if (idx > -1)
            {
                Common.missionManager.missionList.Add(new MissionStru(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), nodeName));
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(CONFIG_PATH + "LinePlace.txt"))
                {
                    foreach (var item in Common.missionManager.missionList)
                    {
                        file.WriteLine(string.Format("{0},{1}", item.CreateTime, item.NodeName));
                    }

                }

                return true;
            }
            else
            {
                log.Error("AddLinePlace 失败 节点" + nodeName + "不存在");
                return false;
            }

        }

        public static bool DelLinePlace(string nodeName)
        {
            int idx = Common.nodeInfo.FindIndex(s => s.Name == nodeName);
            if (idx > -1)
            {
                idx = Common.missionManager.missionList.FindIndex(s => s.NodeName == nodeName);
                if (idx > -1)
                {
                    Common.missionManager.missionList.RemoveAt(idx);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(CONFIG_PATH + "LinePlace.txt"))
                    {
                        foreach (var item in Common.missionManager.missionList)
                        {
                            file.WriteLine(string.Format("{0},{1}", item.CreateTime, item.NodeName));
                        }

                    }

                    return true;
                }
                log.Error("DelLinePlace 失败 节点任务" + nodeName + "不存在");
                return false;
            }
            else
            {
                log.Error("DelLinePlace 失败 节点" + nodeName + "不存在");
                return false;
            }

        }

        public static void CheckAGVMissionState()
        {
            foreach (Agv_Info agv in agvInfo)
            {
                Thread.Sleep(50);
                bool rtn = Common.mir.Get_Register(agv, 20, out int regValue);
                if (rtn)
                {
                    agv.GetPlace(regValue);
                    Common.log.Debug(string.Format("软件开启:{0} Get_Register PLC{1}={2} Place={3} PlaceState={4}", agv.Name, 20, regValue, agv.Place, agv.PlaceState));
                    if (!agv.Place.Equals(""))
                    {
                        int idx = nodeInfo.FindIndex(s => s.Name == agv.Place);
                        if (idx > -1)
                        {
                            nodeInfo[idx].AgvName = agv.Name;
                        }
                        agv.TaskSend = "Move" + agv.Place;
                    }
                }
                else
                {
                    Common.log.Debug("CheckAGVMissionState 获取PLC20失败");
                }
            }
        }
        public static void GetNodesPosition()
        {
            Agv_Info agv = agvInfo[0];
            foreach (ClientNode clientNode in nodeInfo)
            {
                Thread.Sleep(50);
                bool rtn = Common.mir.Get_Node_Pos(agv, clientNode, out BLL.MirPosition mirPosition);
                if (rtn)
                {
                    clientNode.position.X = mirPosition.pos_x;
                    clientNode.position.Y = mirPosition.pos_y;
                    Common.log.Debug(string.Format("软件开启:{0} 获取节点位置({1},{2})", clientNode.Name, clientNode.position.X, clientNode.position.Y));
                }
                else
                {
                    Common.log.Debug("GetNodesPosition 获取节点位置失败");
                }
            }
        }
    }

    public static class API
    {
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("user32.dll ", SetLastError = true)]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
        public const int SW_RESTORE = 9;

    }

    /// <summary>
    /// 客户端
    /// </summary>
    public class Client
    {
        /// <summary>
        /// 循环
        /// </summary>
        public bool Loop;
        /// <summary>
        /// IP地址
        /// </summary>
        public string IP;
        /// <summary>
        /// 是否连接
        /// </summary>
        public bool IsConn;
        /// <summary>
        /// 节点名称集合
        /// </summary>
        public List<string> nodeName;
        /// <summary>
        /// 套接字
        /// </summary>
        public Socket Socket;
        /// <summary>
        /// 接收数据线程
        /// </summary>
        public Thread ListenNet;



    }
    
   

    /// <summary>
    /// 客户端的动作
    /// </summary>
    public enum ClientAction : byte
    {
        /// <summary>
        /// 没有动作
        /// </summary>
        None = 0,
        /// <summary>
        /// 需要7寸D料架
        /// </summary>
        NeedD = 1,
        /// <summary>
        /// 需要大尺寸C料架
        /// </summary>
        NeedC = 2,
        /// <summary>
        /// 需要进入料架
        /// </summary>
        NeedEnter = 3,
        /// <summary>
        /// 需要出去料架
        /// </summary>
        NeedLeave = 4,
        /// <summary>
        /// 需要进入离开料架
        /// </summary>
        NeedEnterLeave = 5,
        /// <summary>
        /// 准备进入,服务器发送
        /// </summary>
        ReadyEnter = 6,
        /// <summary>
        /// 可以进入料架
        /// </summary>
        MayEnter = 7,
        /// <summary>
        /// 完成进入料架
        /// </summary>
        FinishEnter = 8,
        /// <summary>
        /// 准备离开,服务器发送
        /// </summary>
        ReadyLeave = 9,
        /// <summary>
        /// 可以出去料架
        /// </summary>
        MayLeave = 10,
        /// <summary>
        /// 完成出去料架
        /// </summary>
        FinishLeave = 11,

    }

    /// <summary>
    /// 地点状态
    /// </summary>
    public enum PlaceState
    {
        /// <summary>
        /// 没有任务
        /// </summary>
        None = 0,
        /// <summary>
        /// 小车移动任务
        /// </summary>
        Move = 1,
        /// <summary>
        /// 小车移动任务完成
        /// </summary>
        MoveFinish = 2,
        /// <summary>
        /// 小车Enter任务
        /// </summary>
        Enter = 3,
        /// <summary>
        /// 小车Enter任务完成
        /// </summary>
        EnterFinish = 4,
        /// <summary>
        /// 小车Leave任务
        /// </summary>
        Leave = 5,
        /// <summary>
        /// 小车Leave任务完成
        /// </summary>
        LeaveFinish = 6,
        /// <summary>
        /// 出错
        /// </summary>
        Error = 9
    }

    public struct PositionStru
    {
        public double X;
        public double Y;
    }
    public class ChargeStatus
    {
        /// <summary>
        /// 1号充电桩的AGV名称
        /// </summary>
        public string charge3 = "";
        /// <summary>
        /// 1号充电桩的AGV名称
        /// </summary>
        public string charge4 = "";
        /// <summary>
        /// 1号充电桩的AGV名称
        /// </summary>
        public string charge5 = "";
        /// <summary>
        /// 1号充电桩的AGV名称
        /// </summary>
        public string charge6 = "";
        /// <summary>
        /// 充电等待时间(s)
        /// </summary>
        public int chargeWait = 0;
        /// <summary>
        /// 充电最大电量,小于该值等待指定时间去充电
        /// </summary>
        public int chargeMax;
        /// <summary>
        /// 充电最小电量,小于该值直接去充电
        /// </summary>
        public int chargeMin;
        /// <summary>
        /// 两车充电间隔时间(ms)
        /// </summary>
        public long chargeInterval;

        private bool _autoCharge;

        public bool AutoCharge
        {
            set
            {
                _autoCharge = value;
                Common.appConfig.AppSettings.Settings["AutoCharge"].Value = value.ToString();
                Common.appConfig.Save();
            }
            get
            {
                return _autoCharge;
            }
        }

        public ChargeStatus()
        {
            _autoCharge = Convert.ToBoolean(Common.appConfig.AppSettings.Settings["AutoCharge"].Value);
            chargeWait = Convert.ToInt32(Common.appConfig.AppSettings.Settings["ChargeWait"].Value);

            string s = Common.appConfig.AppSettings.Settings["ChargeThreshold"].Value;
            string[] arr = s.Split(',');
            chargeMin = Convert.ToInt32(arr[0]);
            chargeMax = Convert.ToInt32(arr[1]);

            chargeInterval = 0;
        }

    }

    /// <summary>
    /// 任务信息结构
    /// </summary>
    public struct MissionStru
    {
        public string NodeName;
        public string CreateTime;
        /// <summary>
        /// 任务结构
        /// </summary>
        /// <param name="dateTime">创建时间</param>
        /// <param name="name">节点名称</param>
        public MissionStru(string dateTime, string name)
        {
            NodeName = name;
            CreateTime = dateTime;
        }
    }
    /// <summary>
    /// 任务管理
    /// </summary>
    public class MissionManager
    {
        /// <summary>
        /// 任务列表
        /// </summary>
        public List<MissionStru> missionList;
        /// <summary>
        /// 主动执行空架任务的小车名称
        /// </summary>
        public string AGV_Name_EmptyTask { get; set; } = "";

        public MissionManager()
        {
            missionList = new List<MissionStru>();
        }
    }

}