BoardManager.cs 12.0 KB
using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

using System.Windows.Forms;

namespace URSoldering.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 CurrBoardId = ConfigAppSettings.GetIntValue(Setting_Init.Default_BoardID);
      public static void UpdateCurrBoard(int boardID)
      {
          foreach (BoardInfo obj in boardList)
          {
              if (obj.boardId.Equals(boardID))
              {
                  CurrBoard = obj;
                  CurrBoardId = boardID;
                  LogUtil.info(  "切换到新板子:" + CurrBoard.boardName);
                  break;
              }
          }
      }

      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()
      {
          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);
                  bool isUpdate = false;
                  if (board != null)
                  {
                      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.PartNumber==null){
                          board.PartNumber = board.boardName;
                          isUpdate = true;
                        }
                        if (board.WareCode == null)
                        {
                            board.WareCode = "";
                        }
                       
                      idList.Add(board.boardId);
                      nameList.Add(board.boardName);
                      if (board.boardId.Equals(CurrBoardId))
                      {
                          LogUtil.info("加载产品配置:name=" + board.boardName + "为启动时的默认板子!");
                          CurrBoard = board;
                      }
                      if (board.orgType == 0)
                      {
                          board.orgType = 1;
                      }

                        ////如果未添加程序,自动添加
                        //if (board.getProgram() == null)
                        //{
                        //    SProgramManager.addDefaultProgram(board.PartNumber,board.boardLength,board.boardWidth);
                        //}
                   
                      boardList.Add(board);
                  }
                  if (isUpdate)
                  {
                      SaveListToFile(boardList);
                  }
              }
              catch (Exception ex)
              {
                  LogUtil.error("出错:" + ex.ToString());
              }
          }

          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);
          }

          //如果有大于一块板子,必须有默认的电路板
          try
          {
              if (CurrBoard == null && boardList.Count > 0)
              {
                  int id = boardList[0].boardId;
                  UpdateCurrBoard(id);
                  ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, boardList[0].boardId);
                  LogUtil.info("未找到默认的电路板,更新第一块电路板 id=【" + id + "】为默认电路板");
              }
          }
          catch (Exception ex)
          {
              LogUtil.error("设置默认的电路板出错:" + ex.ToString());
          }
      }
      
        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(); 
            return FileUtil.SaveListToFile(lines, filePath);  
        }

     
      public static void Delete(BoardInfo board)
      {
          boardList.Remove(board);
          string[] lines = new string[boardList.Count];
          int index = 0;
          if (board.boardId.Equals(CurrBoardId))
          {
              CurrBoardId = 0;
              CurrBoard = null;
              ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, 0);
          }
          foreach (BoardInfo bo in boardList)
          {
              if (CurrBoardId.Equals(0))
              {
                  ConfigAppSettings.SaveValue(Setting_Init.Default_BoardID, bo.boardId);
                  UpdateCurrBoard(bo.boardId);
              }
              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;
      }
        
        public static BoardInfo getBoardByCode(string code)
        {
            List<BoardInfo> list = (from m in boardList where m.WareCode.Equals(code) select m).ToList<BoardInfo>();
            if (list.Count > 0)
            {
                return list[0];
            }return null;
        }

      public static void UpdateTime(BoardInfo board, DateTime startTime, DateTime endTime)
      {
          LogUtil.BoardSolderingLog(board.boardName, startTime, endTime);
          int index = 0;
          foreach (BoardInfo bo in boardList)
          {
              if (bo.boardId.Equals(board.boardId))
              {
                  boardList[index].solderingCount++;
                  double addtime = (endTime - startTime).TotalSeconds;
                  boardList[index].solderingTime += addtime;
                  boardList[index].solderTime += addtime;
                  boardList[index].solderCount++;
                  double value = boardList[index].solderingTime / boardList[index].solderingCount; 

                  value = Math.Round(value, 1, MidpointRounding.AwayFromZero);
                  boardList[index].planNeedTime = value;
                  LogUtil.info("电路板:" + board.boardName + ",总时间" + boardList[index].solderingTime + ",总次数" + boardList[index].solderingCount + ",本次焊接时间"+addtime+",平均焊接时间:" + boardList[index].planNeedTime);
                  break;
              }
              index++;
          }
          SaveListToFile(boardList);
      } 
       
      public static Dictionary<int, BoardInfo> GetBoardMapByPartNum(string partNum)
      {
          Dictionary<int, BoardInfo> boardMap = new Dictionary<int, BoardInfo>();
          List<BoardInfo> list = (from m in boardList where m.PartNumber.Equals(partNum)  orderby m.StationNum ascending select m).ToList<BoardInfo>();
          foreach (BoardInfo board in list)
          {
                if (!boardMap.ContainsKey(board.StationNum))
                { boardMap.Add(board.StationNum, board); }
             
          }
          return boardMap;
      }

      public static bool CanUpdate(string oldPartNum, string newPartNum)
      {
          foreach (BoardInfo board in boardList)
          {
              if (!board.PartNumber.Equals(oldPartNum))
              {
                  if (board.PartNumber.Equals(newPartNum))
                  {
                      return false;
                  }
              }
          } return true;
      }

      /// <summary>
      /// 根据一个产品的ID获取此产品在指定编码的程序
      /// </summary>
      /// <param name="BoardId"></param>
      /// <param name="LocationNum"></param>
      /// <returns></returns>
      public  static BoardInfo GetSolderingBoard(string partNum, int LocationNum)
      { 
          Dictionary<int, BoardInfo> map = GetBoardMapByPartNum(partNum);
          if (map.ContainsKey(LocationNum))
          {
              return map[LocationNum];
          } 
          return null;
      }
        /// <summary>
        /// 根据料号和编码来找产品
        /// </summary>
        /// <param name="newPart"></param>
        /// <param name="newStation"></param>
        /// <returns></returns>
        public static BoardInfo getBoard(string partNum, int stationNum)
        {
            Dictionary<int, BoardInfo> boardMap = new Dictionary<int, BoardInfo>();
            List<BoardInfo> list = (from m in boardList where m.PartNumber.Equals(partNum)&&m.StationNum.Equals(stationNum)
                                    orderby m.StationNum ascending select m).ToList<BoardInfo>();
            if (list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

    }
}