BoardManager.cs 11.9 KB
using log4net;
using TSA_V.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TSA_V.DeviceLibrary
{
    public class BoardManager
    {
        private static int MaxId = 0;
        /// <summary>
        /// 板子列表
        /// </summary>
        public static List<BoardInfo> boardList = new List<BoardInfo>();

        public static Image defaultImage = null;
        /// <summary>
        /// 当前正在使用的板子
        /// </summary> 
        public static BoardInfo CurrBoard = null;

        public static int GetNextId()
        {
            MaxId++;
            return MaxId;
        }
        public static string GetConfigPath()
        {
            string appPath = Application.StartupPath;
            string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Board_ConfigPath);
            return filePath;
        }
        public static void LoadBoard()
        {
            LogUtil.info("开始加载电路板列表");
            //   CurrBoardId = ConfigAppSettings.GetIntValue(Setting_Init.Default_BoardID);
            string filePath = GetConfigPath();
            if (!File.Exists(filePath))
            {
                LogUtil.error("加载产品失败:文件[" + filePath + "]不存在");
                return;
            }
            string[] lines = File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
            boardList = new List<BoardInfo>();
            List<int> idList = new List<int>();
            List<string> nameList = new List<string>();
            foreach (string str in lines)
            {
                try
                {
                    string newStr = str;
                    string errMsg = "\"System.Drawing.Bitmap\"";
                    if (str.Contains(errMsg))
                    {
                        newStr = str.Replace(errMsg, "null");
                    }
                    BoardInfo board = JsonHelper.DeserializeJsonToObject<BoardInfo>(newStr);
                    if (board != null && board.boardName != null)
                    {
                        if (board.smtList == null)
                        {
                            board.smtList = new List<SMTPointInfo>();
                        }

                        foreach (SMTPointInfo sm in board.smtList)
                        {
                            if (sm.PartNum == null)
                            {
                                sm.PartNum = "";
                            } 

                        }
                        if (board.boardId > MaxId)
                        {
                            MaxId = board.boardId;
                        }
                        if (idList.Contains(board.boardId))
                        {
                            LogUtil.error("加载产品配置出错:id=" + board.boardId + "的数据重复!");
                        }
                        if (nameList.Contains(board.boardName))
                        {
                            LogUtil.error("加载产品配置出错:name=" + board.boardName + "的数据重复!");
                        }
                        if (board.AOIProName.Equals(""))
                        {
                            board.AOIProName = board.boardName + ".data";
                        }
                        idList.Add(board.boardId);
                        nameList.Add(board.boardName);

                        if (board.orgType == 0)
                        {
                            board.orgType = 1;
                        }
                        boardList.Add(board);
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error("出错:" + ex.ToString());
                }
            }

            LogUtil.info("加载电路板完成,共加载【" + boardList.Count + "】块电路板配置");
            string path = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
            string imagePath = Application.StartupPath + @"\" + path + ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_DEFAULT);

            if (File.Exists(imagePath))
            {
                defaultImage = Image.FromFile(imagePath);
            }
            else
            {
                LogUtil.error("未找到默认图片:" + imagePath);
            }
        }
        public static void Update(BoardInfo board)
        {
            int index = 0;
            board.myImage = null;
            foreach (BoardInfo bo in boardList)
            {
                if (bo.boardId.Equals(board.boardId))
                {
                    boardList[index] = board;
                    break;
                }
                index++;
            }
            SaveListToFile(boardList);
        }

        public static void Add(BoardInfo board)
        {
            boardList.Add(board);

            SaveListToFile(boardList);
        }

        private static bool SaveListToFile(List<BoardInfo> list)
        {
            string[] lines = new string[boardList.Count];
            int index = 0;
            foreach (BoardInfo bo in list)
            {
                lines[index] = JsonHelper.SerializeObject(bo);
                index++;
            }
            string filePath = GetConfigPath();
            try
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                File.WriteAllLines(filePath, lines, Encoding.GetEncoding("gbk"));

                //备份保存
                try
                {
                    FileInfo file = new FileInfo(filePath);
                    string date = DateTime.Now.ToString("yyyy-MM-dd");
                    string targetBackPath = @"C:\configBack\" + date + @"\";
                    if (!Directory.Exists(targetBackPath))
                    {
                        Directory.CreateDirectory(targetBackPath);
                    }
                    string fileName = file.Name;
                    string backFile = targetBackPath + fileName;
                    if (File.Exists(backFile))
                    {
                        File.Delete(backFile);
                    }
                    File.WriteAllLines(backFile, lines, Encoding.GetEncoding("gbk"));
                }
                catch (Exception e)
                {
                    LogUtil.error("备份配置到C:configBack出错:" + e.ToString());
                }
                //备份保存
                try
                {
                    FileInfo file = new FileInfo(filePath);
                    string date = DateTime.Now.ToString("yyyy-MM-dd");
                    string targetBackPath = @"D:\configBack\" + date + @"\";
                    if (!Directory.Exists(targetBackPath))
                    {
                        Directory.CreateDirectory(targetBackPath);
                    }
                    string fileName = file.Name;
                    string backFile = targetBackPath + fileName;
                    if (File.Exists(backFile))
                    {
                        File.Delete(backFile);
                    }
                    File.WriteAllLines(backFile, lines, Encoding.GetEncoding("gbk"));
                }
                catch (Exception e)
                {
                    LogUtil.error("备份配置到D:configBack出错:" + e.ToString());
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
                return false;
            }
            return true;
        }



        public static void Delete(BoardInfo board)
        {
            boardList.Remove(board);
            string[] lines = new string[boardList.Count];
            int index = 0;

            foreach (BoardInfo bo in boardList)
            {
                lines[index] = JsonHelper.SerializeObject(bo);
                index++;
            }
            SaveListToFile(boardList);
        }

        public static bool HasId(int boardId)
        {
            foreach (BoardInfo board in boardList)
            {
                if (board.boardId.Equals(boardId))
                {
                    return true;
                }
            }
            return false;
        }
        public static BoardInfo getBoardById(int PreboardId)
        {
            foreach (BoardInfo board in boardList)
            {
                if (board.boardId.Equals(PreboardId))
                {
                    return board;
                }
            }
            return null;
        }
        public static BoardInfo getBoardByName(string name)
        {
            foreach (BoardInfo board in boardList)
            {
                if (board.boardName.Equals(name))
                {
                    return board;
                }
            }
            return null;
        }
        /// <summary>
        /// 修正前备份
        /// </summary> 
        private static bool UpdateBack(List<BoardInfo> list, double xUpdate, double yUpdate)
        {
            string[] lines = new string[boardList.Count];
            int index = 0;
            foreach (BoardInfo bo in list)
            {
                lines[index] = JsonHelper.SerializeObject(bo);
                index++;
            }
            string appPath = Application.StartupPath;
            string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");
            string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Board_ConfigPath) + "." + date + "(" + xUpdate.ToString() + "," + yUpdate.ToString() + ")" + ".back";

            try
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                File.WriteAllLines(filePath, lines, Encoding.GetEncoding("gbk"));
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
                return false;
            }
            return true;
        }
        public static void UpdatePointSize(int boardId, int pSizeX, int pSizeY, int pointType, int penWidth)
        {
            foreach (BoardInfo board in boardList)
            {
                if (boardId.Equals(-1) || board.boardId.Equals(boardId))
                {
                    LogUtil.info("电路板【" + board.boardName + "】点大小更改为: [" + pSizeX + "," + pSizeY + "] 点类型更改为: [" + pointType + "]画笔宽度:[" + penWidth + "]");
                    foreach (SMTPointInfo point in board.smtList)
                    {
                        point.PointSizeX = pSizeX;
                        point.PointSizeY = pSizeY;
                        point.PointType = pointType;
                        point.PenWidth = penWidth;
                    }
                }
            }
            SaveListToFile(boardList);
        }

        public static BoardInfo StringToBoard(string str, out bool IsNeedUpdate)
        {
            IsNeedUpdate = false;
            BoardInfo board = null;
            try
            {
                string newStr = str;
                string errMsg = "\"System.Drawing.Bitmap\"";
                if (str.Contains(errMsg))
                {
                    newStr = str.Replace(errMsg, "null");
                }
                board = JsonHelper.DeserializeJsonToObject<BoardInfo>(newStr);
                //  bool isUpdate = false;
                if (board != null)
                {
                    if (board.boardId > MaxId)
                    {
                        MaxId = board.boardId;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }
            return board;
        }

    }
}