StoreManager.cs 7.8 KB
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text; 
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class StoreManager
    {
        /// <summary>
        /// 当前出入库的模式
        /// </summary>
        public static int CurrInOutType = 0;
         
        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static PackingStoreBean Store = null;
        public static Store_Config Config = null;
        public static  Dictionary<int, BaseConfig> AllConfigMap = null;
        private static bool isInit = false;
        public static bool IsConnectServer = !ConfigAppSettings.GetValue(Setting_Init.http_server).Equals("");
        public StoreManager()
        {
        }

        public static void CheckEnum(Type type)
        {
            if (type.IsEnum)
            {
                List<int> valueList = new List<int>();
                foreach (int item in Enum.GetValues(type))
                {
                    if (valueList.Contains(item))
                    {
                        LogUtil.error(LOGGER, type.Name + "枚举值:" + item + "重复存在,请检查代码!");
                        Application.Exit();
                        break;
                    }
                    valueList.Add(item);
                }
            }
        }
        public static PackingStoreBean InitStore()
        {
            try
            {
                AllConfigMap = new Dictionary<int, BaseConfig>();
                BaseConfig.ProIOIpMap = new Dictionary<string, string>();
                if (!isInit)
                {
                    string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
                    if (server.Equals(""))
                    {
                        IsConnectServer = false;
                    }
                    else
                    {
                        //IsConnectServer = true;
                    }
                    CheckEnum(typeof(StoreMoveStep));
                    CheckEnum(typeof(StoreStatus));
                    CheckEnum(typeof(StoreRunStatus));

                    isInit = true;
                    string storeType = ConfigAppSettings.GetValue(Setting_Init.Store_Type);
                    int count = ConfigAppSettings.GetIntValue(Setting_Init.store_count);
                    LogUtil.info(LOGGER, "配置的料仓 类型=" + storeType + ",开始加载料仓配置");
                    string appPath = Application.StartupPath;
                    string CID = ConfigAppSettings.GetValue(Setting_Init.Store_CID);
                    Dictionary<int, AC_BOX_Config> storeConfig = new Dictionary<int, AC_BOX_Config>();
                    if (storeType == StoreType.RC_AC_PA)
                    {

                        string linefilePath = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_ConfigPath);
                        Config = CSVConfigReader.LoadLineConfig(0, CID, "Line", linefilePath);
                        AllConfigMap.Add(0, Config);

                        string moveEquipConfig = ConfigAppSettings.GetValue(Setting_Init.BOX_ConfigPath);
                        for (int i = 1; i <= count; i++)
                        {
                            string nameStr = i.ToString().PadLeft(1, '0');
                            string config = appPath + moveEquipConfig.Replace(".csv", "_" + nameStr + ".csv");
                            string storeIdConfig = Setting_Init.Store_CID + "_" + i;
                            string boxCid = ConfigAppSettings.GetValue(storeIdConfig);
                            AC_BOX_Config moveConfig = CSVConfigReader.LoadBoxConfig(i, boxCid, "BOX", config);
                            AllConfigMap.Add(i, moveConfig);
                            storeConfig.Add(i, moveConfig);
                        }
                        string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config);
                        if (count > 1 || (!File.Exists(positionConfigFile)))
                        {
                            for (int i = 1; i <= count; i++)
                            {
                                string nameStr = i.ToString().PadLeft(1, '0');
                                string fileN = positionConfigFile. Replace(".csv", "_" + nameStr + ".csv");
                                CSVPositionReader<ACBoxPosition>.AddCSVFile(fileN);
                            }
                        }
                        else
                        {
                            CSVPositionReader<ACBoxPosition>.AddCSVFile(positionConfigFile);
                        }
                        LogUtil.info(LOGGER, "加载料仓完成!");

                    }

                    string shelfConfig = appPath + ConfigAppSettings.GetValue(Setting_Init.Shelf_Position_Config);
                    if (File.Exists(shelfConfig))
                    {
                        CSVPositionReader<ShelfPosition>.AddCSVFile(shelfConfig);
                    }
   
                    Store = new PackingStoreBean(Config, storeConfig);
                }
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:", ex);
                MessageBox.Show(ex.ToString(), "加载配置错误(请检查配置)");
                Application.Exit();
            }
            return Store;
        }
        /// <summary>
        /// 修改了料仓配置,更新缓存,更新配置文件(只能更新PRO的配置)
        /// </summary>
        /// <param name="kTK_LA_Store_Config"></param>
        public static void UpdateBoxConfig(AC_BOX_Config storeConfig)
        {
            try
            {
                //位置配置到文件中
                string appPath = Application.StartupPath; 
                string configFile = appPath + ConfigAppSettings.GetValue(Setting_Init.BOX_ConfigPath);
                bool result = CSVConfigReader.SaveBoxPosition(configFile, storeConfig);

                if (!result)
                {
                    LOGGER.Error("保存配置文件失败:" + configFile);
                }
                AllConfigMap[storeConfig.DeviceID] = storeConfig;
                Store.BoxConfigMap[storeConfig.DeviceID] = storeConfig;
                Store.BoxMap[storeConfig.DeviceID].Config = storeConfig;
                Store.BoxMap[storeConfig.DeviceID].MoveAxisConfig();
            }
            catch (Exception ex)
            {
                LOGGER.Error("出错:", ex);
            }
        }
        private static string api_communication = "service/store/communication";    //流水线状态通信接口
        private static string api_nextFeeder = "service/store/nextFeeder";  // 出库站位列表切换接口
        public static string GetPostApi(string host)
        {
            if (host == "")
            {
                host = ConfigAppSettings.GetValue(Setting_Init.http_server);
            }
            if (!host.StartsWith("http://"))
            {
                host = "http://" + host;
            }
            if (!host.EndsWith("/"))
            {
                host = host + "/";
            }
            return host + api_communication;
        }
        public static string GetNextFeederApi(string host)
        {
            if (host == "")
            {
                host = ConfigAppSettings.GetValue(Setting_Init.http_server);
            }
            if (!host.StartsWith("http://"))
            {
                host = "http://" + host;
            }
            if (!host.EndsWith("/"))
            {
                host = host + "/";
            }
            return host + api_nextFeeder;
        }
    }
}