Files
vms2016_kadisp/Viewer/TrendViewer/pub.cs
2024-11-26 20:15:16 +09:00

289 lines
10 KiB
C#

using System.Collections.Generic;
using System;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using System.Xml.Linq;
using AR;
using System.Windows.Media;
using System.Media;
using System.IO;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Threading.Tasks;
namespace vmsnet
{
sealed class PUB
{
//public static bool MonitorOn = false;
public static AR.Log log;
public static bool RaiseSound1 = false;
public static bool RaiseSound2 = false;
public static bool RaiseSound = false;
public static string ExtCommand = "";
public static bool setupmode = false;
public static bool NotExitMonitor = false;
public static CFDB DB;
public static CFDBA Alarm;
public static CFDBK KADB;
public static CSetting CONFIG;
public static CTrendSetting TREND;
public static int sleepoffset = 0; ////쓰레드슬립시간
public static int sleeplimit = 1000; ////쓰레드슬립최대값
public static int[,,] Values; ////이차원배열이다.
public static string[,,] Times;
public static string lasttime = "";
public static event EventHandler<RemoteCommand> RemoteCommandEvent;
public static DocumentElement DS = new DocumentElement();
public static bool runerror = false; ////실행오류
public static string lastStatueMessage = "";
public static System.Text.StringBuilder StartErrmsg = new System.Text.StringBuilder();
public static int WindowCount = 0;
private static Frm_SMsg fSMsg = null;
// private static Frm_Msg fMsg = null;
private static MediaPlayer mplayer = null;
/// <summary>
/// 입력한 채널의 파일버퍼기준의 인덱스값을 반환합니다.
/// 각 파일은 최대 160개의 채널데이터를 가지고 있다.
/// 161번 채널의 경우 2번째 파일의 001번 데이터이다
/// 160번 채널의 경우 1번째 파일의 160번 데이터이다
/// </summary>
/// <param name="ValueIdx">채널번호(1~)</param>
/// <returns></returns>
public static int GetChannelIndexFromFileBuffer(int ValueIdx)
{
//각 파일당 160개씩 저장되어있으므로 채널번호를 통해서 파일의 위치를 파악해야한다.
int quotient = ValueIdx / 160; // 몫
int remainder = ValueIdx % 160; // 나머지
//나머지가 0인경우에는 마지막데이터이다
//그외의 경우에는 나머지가 해당 인덱스가 된다.
return remainder == 0 ? quotient * 160 : remainder;
}
public static void RaiseRemoteCommandEvent(rCommand cmd, object data = null)
{
RemoteCommandEvent?.Invoke(null, new RemoteCommand(cmd, data));
}
public static void smsg(string m)
{
if (fSMsg == null || fSMsg.IsDisposed)
fSMsg = new Frm_SMsg();
if (fSMsg.Visible == false) fSMsg.Show();
fSMsg.Activate();
fSMsg.Label1.Text = m;
Application.DoEvents();
if (m == "") fSMsg.Close();
}
public static Font GetFontFromStr(string strfontbuffer)
{
try
{
string[] fontstr = strfontbuffer.Split(",".ToCharArray());
FontStyle fstyle = (FontStyle)(int.Parse(fontstr[2]));
return new Font(fontstr[0], float.Parse(fontstr[1]), fstyle);
}
catch
{
return new Font("Tahoma", 10, FontStyle.Bold);
}
}
//public static void workmsg(string m)
//{
// if (fMsg == null || fMsg.IsDisposed)
// fMsg = new Frm_Msg();
// if (fMsg.Visible == false) fMsg.Show();
// fMsg.Activate();
// fMsg.lb_Status.Text = m;
// Application.DoEvents();
// if (m == "") fMsg.Close();
//}
////타임정보를 숫자로 반환함
public static decimal get_TimeNumber(DateTime time)
{
return time.Hour * 3600 + time.Minute * 60 + time.Second;
}
public static void ShowForm(Form f, Type ftype, params object[] data)
{
if (f == null || f.IsDisposed)
f = (Form)Activator.CreateInstance(ftype, data);
f.Show();
if (f.WindowState == FormWindowState.Minimized)
f.WindowState = FormWindowState.Normal;
else if (f.Visible == false)
f.Visible = true;
f.Activate();
}
public static string get_TimeString(decimal timenumber, string fn)
{
////파일명에 년/월/일이 있음
string[] fnb = fn.Split("\\".ToCharArray());
string Filename = fnb[fnb.Length - 1];
string daystr = fnb[fnb.Length - 2];
string monstr = fnb[fnb.Length - 3];
string yearstr = fnb[fnb.Length - 4];
////timenumber를 다시 시,분,초 로 변환한다.
decimal namugy = timenumber;
int hour = (int)(Math.Floor(namugy / 3600));
namugy = namugy - (hour * 3600);
int min = (int)(Math.Floor(namugy / 60));
namugy = namugy - (min * 60);
int sec = (int)namugy;
return yearstr + "-" + monstr + "-" + daystr + " " + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
}
public static void AddMeasureValue(int idx, int unit, int ch, int value, string time)
{
var l0 = PUB.Values.GetUpperBound(0) + 1;
var l1 = PUB.Values.GetUpperBound(1) + 1;
var l2 = PUB.Values.GetUpperBound(2) + 1;
if (idx < l0 && unit < l1 && ch > 0 && ch <= l2)
{
try
{
PUB.Values[idx, unit, ch - 1] = value;
PUB.Times[idx, unit, ch - 1] = time;
}
catch (Exception)
{
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine($"수신데이터입력실패 M/C:{idx},Unit:{unit},CH:{ch - 1}");
}
}
}
else
{
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine($"수신데이터배열크기오류 mc:{idx},unit:{unit},ch:{ch}");
}
}
}
/// <summary>
/// 채널인덱스를 가지고 버퍼인덱스를 계산 합니다.
/// 1개의 버퍼는 최대 160개를 저장 합니다.
/// </summary>
/// <param name="chidx"></param>
/// <returns></returns>
public static int ConvertChIndexToBufferIndex(int chidx)
{
var ValueIdx = chidx;
int quotient = ValueIdx / 160; // 몫
int remainder = ValueIdx % 160; // 나머지
if (remainder == 0) ValueIdx = quotient - 1;
else ValueIdx = quotient;
if (ValueIdx < 0) ValueIdx = 0;
return ValueIdx;
}
static string lastDAQerrMsg = "";
static DateTime lastDAQerrTime = DateTime.Now;
public static void INIT(out List<string> errormessages)
{
errormessages = new List<string>();
var errorlist = new List<string>();
log = new AR.Log();//.ArinLog();
CONFIG = new CSetting(); ////설정파일 초기화 150319
CONFIG.filename = new System.IO.FileInfo("..\\Setting.xml").FullName;
CONFIG.Load();
TREND = new CTrendSetting();
TREND.filename = new System.IO.FileInfo("TrendSetting.xml").FullName;
TREND.Load();
if (UTIL.IsDriveValid(CONFIG.GetDatabasePath()) == false)
{
errorlist.Add($"저장경로의 드라이브가 유효하지 않아 임시경로로 변경 됩니다\n" +
$"전:{CONFIG.GetDatabasePath()}\n" +
$"후:{UTIL.CurrentPath}");
CONFIG.databasefolder = UTIL.CurrentPath;
}
if (System.IO.Directory.Exists(CONFIG.GetDatabasePath()) == false)
{
try
{
System.IO.Directory.CreateDirectory(CONFIG.GetDatabasePath());
errorlist.Add($"저장경로 신규생성\n{CONFIG.GetDatabasePath()}");
}
catch (Exception ex)
{
//CONFIG.GetDatabasePath() = UTIL.CurrentPath;
errorlist.Add($"저장경로 생성실패\n{CONFIG.GetDatabasePath()}\n{ex.Message}");
}
}
DB = new CFDB(CONFIG.GetDatabasePath(), "Database", "volt");
Alarm = new CFDBA(CONFIG.GetDatabasePath(), "Database", "Alarm");
KADB = new CFDBK(CONFIG.GetDatabasePath(), "Database", "Ka"); ////ka 전용데이터베이스 150310
log.Add(AR.Log.ETYPE.NORMAL, "설정읽어오기완료");
////설정정보를 가져온다.
//List<string> errorlist = new List<string>();
System.Threading.Tasks.Task task = Task.Factory.StartNew(() =>
{
string[] BackTables = new string[] { "DEVICE", "GRP", "NORMAL", "WIN", "VIEWGROUP", "CHANNEL", "VIEWGROUPR" };
foreach (string tabname in BackTables)
{
var fname = System.IO.Path.Combine(CONFIG.GetDatabasePath(), "Database", "Config", $"{tabname}.xml");
var fn = new System.IO.FileInfo(fname);
if (fn.Exists)
{
PUB.DS.Tables[tabname].ReadXml(fn.FullName);
PUB.log.Add($"Load File({tabname}) : {fn.FullName}");
}
else if (tabname.StartsWith("VIEWGROUP") == false)
errorlist.Add($"설정파일없음\n{tabname}");
}
});
task.Wait();
if (errorlist.Any()) errormessages.AddRange(errorlist);
}
}
}