천호석
2022-12-15 ac0db02b48098c7cf0bc31403224ecd0b55cb6b7
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
using System;
using System.Collections.Generic;
using log4net;
using log4net.Appender;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Threading;
 
namespace SHARP_CLAS_UI
{
    public class Panel_Info_Manager
    {
        #region Define
        string str_extension = ".xml";
        #endregion
 
        #region Logger
        /// <summary>
        /// Alarm Log
        /// </summary>
        private ILog Panel_Info_Change_Log = LogManager.GetLogger("Panel_Info_Change");
 
        /// <summary>
        /// Alarm Log Write
        /// </summary>
        /// <param name="Methodname">Method name of exception that occurred</param>
        /// <param name="ex">exception</param>
        private void Write_Panel_Info_Change_Log(string message)
        {
            if (Panel_Info_Change_Log != null) Panel_Info_Change_Log.Debug($"{message}");
        }
 
        /// <summary>
        /// Create excepton logger
        /// </summary>
        private void Create_Panel_Info_Change_logger()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            RollingFileAppender rollingAppender = new RollingFileAppender();
 
            PatternLayout layout = new PatternLayout();
            hierarchy.Configured = true;
 
            rollingAppender.Name = "Panel_Info_Change_RoolingFile";
            rollingAppender.LockingModel = new RollingFileAppender.MinimalLock();
            rollingAppender.File = $@"D:\Logger\Panel_Info_Change\";
            rollingAppender.AppendToFile = true;
            rollingAppender.DatePattern = "yyyy\\\\MM\\\\'Panel_Info_Change'_dd'.csv'";
            rollingAppender.RollingStyle = RollingFileAppender.RollingMode.Composite;
            rollingAppender.MaxSizeRollBackups = 10;
            rollingAppender.MaximumFileSize = "100MB";
            rollingAppender.StaticLogFileName = false;
            rollingAppender.Encoding = Encoding.UTF8;
            rollingAppender.PreserveLogFileNameExtension = true;
            layout = new PatternLayout("%d{yyyy/MM/dd HH:mm:ss.fff},%m%n");
            layout.ActivateOptions();
            rollingAppender.Layout = layout;
            rollingAppender.ActivateOptions();
 
            hierarchy.GetLogger("Panel_Info_Change");
            ILog log = LogManager.GetLogger("Panel_Info_Change");
            Logger l = (Logger)log.Logger;
            l.Level = log4net.Core.Level.Debug;
            l.AddAppender(rollingAppender);
 
            Panel_Info_Change_Log = LogManager.GetLogger("Panel_Info_Change");
        }
        #endregion
 
        #region Property
        private static readonly Lazy<Panel_Info_Manager> _instatnce = new Lazy<Panel_Info_Manager>(() => new Panel_Info_Manager());
 
        public static Panel_Info_Manager Instance
        {
            get
            {
                return _instatnce.Value;
            }
        }
        
        public Dictionary<Panel_Info_Names, Panel_Info> Panel_Infos { get; set; }
        
        private Xml_Manager<Panel_Info> xml_manager = new Xml_Manager<Panel_Info>();
        private string file_path;
 
        private object _lockObject;
        private bool _isLock;
        #endregion
 
        #region Construct
        private Panel_Info_Manager()
        {
            Create_Panel_Info_Change_logger();
 
            _lockObject = new object();
 
            Get_File_Path();
 
            Panel_Infos = new Dictionary<Panel_Info_Names, Panel_Info>();
 
            foreach (Panel_Info_Names info in Enum.GetValues(typeof(Panel_Info_Names)))
            {
                Panel_Infos.Add(info, new Panel_Info());
            }
        }
        #endregion
 
        #region Function
        private void Get_File_Path()
        {
            RegistryKey rkk = Registry.CurrentUser.OpenSubKey(Equipment.Registry_Path, true);
 
            try
            {
                if (rkk == null)
                {
                    rkk = Registry.CurrentUser.CreateSubKey(Equipment.Registry_Path);
 
                    rkk.SetValue("PANEL_INFO_PATH", @"C:\SHARP_CLAS\Config\PANEL_INFO\");
                    file_path = @"C:\SHARP_CLAS\Config\PANEL_INFO\";
                }
                else
                {
                    object value = rkk.GetValue("PANEL_INFO_PATH");
                    if (value == null)
                    {
                        rkk.SetValue("PANEL_INFO_PATH", @"C:\SHARP_CLAS\Config\PANEL_INFO\");
                        file_path = @"C:\SHARP_CLAS\Config\PANEL_INFO\";
                    }
                    else
                    {
                        file_path = value.ToString();
                    }
                }
            }
            finally
            {
                rkk.Close();
            }
        }
                        
        public Panel_Info Get_Panel_Info(Panel_Info_Names panel_name, int tray_panel_num = 0)
        {
            lock (_lockObject)
            {
                try
                {
                    while (_isLock)
                    {
                        Monitor.Wait(_lockObject);
                    }
                    _isLock = true;
 
                    Panel_Info info;
                    string info_name = string.Empty;
 
                    if (panel_name == Panel_Info_Names.LD_Tray_In || panel_name == Panel_Info_Names.ULD_Tray_Ok || panel_name == Panel_Info_Names.ULD_Tray_Ng)
                    {
                        info_name = $"{panel_name}_{tray_panel_num}";
                    }
                    else
                    {
                        info_name = $"{panel_name}";
                    }
 
                    info_name += str_extension;
 
                    if (File.Exists(file_path + info_name))
                    {
                        info = xml_manager.Read_File(file_path + info_name);
                    }
                    else
                    {
                        info = new Panel_Info();
                        xml_manager.Save_File(file_path + info_name, info);
                    }
 
                    if(info == null)
                    {
                        info = new Panel_Info();
                        xml_manager.Save_File(file_path + info_name, info);
                    }
 
                    Panel_Infos[panel_name] = info.Clone();
 
                    return Panel_Infos[panel_name].Clone();
 
                }
                finally
                {
                    _isLock = false;
                    Monitor.Pulse(_lockObject);
                }
            }
        }
 
        public bool Move_Panel_Info(Panel_Info_Names remove_info, Panel_Info_Names make_info)
        {
            try
            {
                bool check = true;
 
                Get_Panel_Info(remove_info);
 
                Set_Panel_Info(make_info, Panel_Infos[remove_info]);
 
                Panel_Info resetInfo = new Panel_Info();
 
                Set_Panel_Info(remove_info, resetInfo);
 
                Get_Panel_Info(remove_info);
 
                return check;
            }
            catch(Exception )
            {
                return false;
            }
        }
 
        public bool Set_Panel_Info(Panel_Info_Names name, Panel_Info info)
        {
            lock (_lockObject)
            {
                try
                {
                    while (_isLock)
                    {
                        Monitor.Wait(_lockObject);
                    }
                    _isLock = true;
 
                    string info_name = string.Empty;
 
                    if (name == Panel_Info_Names.LD_Tray_In || name == Panel_Info_Names.ULD_Tray_Ok || name == Panel_Info_Names.ULD_Tray_Ng)
                    {
                        info_name = $"{name}_{info.Panel_Num}";
                    }
                    else
                    {
                        info_name = $"{name}";
                    }
 
                    info_name += str_extension;
                    xml_manager.Save_File(file_path + info_name, info);
                    Write_Panel_Info_Change_Log($"{info_name},{info.ToString()}");
 
                    Panel_Infos[name] = info.Clone();
                    return true;
                }
                finally
                {
                    _isLock = false;
                    Monitor.Pulse(_lockObject);
                }
            }
        }
        #endregion
    }
}