using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;


namespace FcTempFanVCS
{
	//*****************************************************************************
	//   t@C F frmTenpFan.cs
	//
	//   @\ F x^FANXe[^X\@\
	//
	//
	// Copyright (C) NEC Corporation 2009
	//*****************************************************************************

	public class frmTempFan : System.Windows.Forms.Form
	{
		public System.Windows.Forms.GroupBox Frame2;
		public System.Windows.Forms.Label lblTemp1;
		public System.Windows.Forms.Label lblTemp2;
		public System.Windows.Forms.GroupBox Frame4;
		public System.Windows.Forms.Label _lblFan_0;
		public System.Windows.Forms.Label _lblFan_1;
		public System.Windows.Forms.Label _lblFan_2;
		public System.Windows.Forms.Label _lblFan_3;
		public System.Windows.Forms.Label _lblFan_4;

		private System.ComponentModel.IContainer components;

		//*****************************************************************************
		// x/FAN擾p萔`
		//*****************************************************************************
		// RETURNXe[^X
		private const int FCSRASAPI_SUCCESS = 0x00;
		private const int FCSRASAPI_DRVERR = 0x01;

	    private String[] strFan = new String[5];
		private System.Windows.Forms.Timer timer1;
        public Label lblTemp3;

		// xRg[z̃tB[h쐬
		private System.Windows.Forms.Label[] lblFanAry;

		//*****************************************************************************
		// \tgRASI/F`
		//*****************************************************************************
		[DllImport("FcsrasapiP.dll")]
		private static extern int fcsoftrasOpenP();
		[DllImport("FcsrasapiP.dll")]
		private static extern int fcsoftrasCloseP();
		[DllImport("FcsrasapiP.dll")]
		private static extern int fcsoftrasGetTempP([MarshalAs(UnmanagedType.SafeArray)] ref byte[] byteData);
		[DllImport("FcsrasapiP.dll")]
		private static extern int fcsoftrasGetFanP([MarshalAs(UnmanagedType.SafeArray)] ref byte[] byteData);
		[DllImport("FcsrasapiP.dll")]
		private static extern int fcsoftrasGetFanInfP([MarshalAs(UnmanagedType.SafeArray)] ref byte[] byteData);

		public frmTempFan()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows tH[ fUCiŐꂽR[h 
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmTempFan));
            this.Frame2 = new System.Windows.Forms.GroupBox();
            this.lblTemp1 = new System.Windows.Forms.Label();
            this.lblTemp2 = new System.Windows.Forms.Label();
            this.Frame4 = new System.Windows.Forms.GroupBox();
            this._lblFan_0 = new System.Windows.Forms.Label();
            this._lblFan_1 = new System.Windows.Forms.Label();
            this._lblFan_2 = new System.Windows.Forms.Label();
            this._lblFan_3 = new System.Windows.Forms.Label();
            this._lblFan_4 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.lblTemp3 = new System.Windows.Forms.Label();
            this.Frame2.SuspendLayout();
            this.Frame4.SuspendLayout();
            this.SuspendLayout();
            // 
            // Frame2
            // 
            this.Frame2.BackColor = System.Drawing.SystemColors.Control;
            this.Frame2.Controls.Add(this.lblTemp3);
            this.Frame2.Controls.Add(this.lblTemp1);
            this.Frame2.Controls.Add(this.lblTemp2);
            this.Frame2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame2.Location = new System.Drawing.Point(11, 9);
            this.Frame2.Name = "Frame2";
            this.Frame2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame2.Size = new System.Drawing.Size(105, 105);
            this.Frame2.TabIndex = 1;
            this.Frame2.TabStop = false;
            this.Frame2.Text = "Temp";
            // 
            // lblTemp1
            // 
            this.lblTemp1.BackColor = System.Drawing.SystemColors.Control;
            this.lblTemp1.Cursor = System.Windows.Forms.Cursors.Default;
            this.lblTemp1.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.lblTemp1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblTemp1.Location = new System.Drawing.Point(16, 19);
            this.lblTemp1.Name = "lblTemp1";
            this.lblTemp1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblTemp1.Size = new System.Drawing.Size(74, 17);
            this.lblTemp1.TabIndex = 2;
            this.lblTemp1.Text = "Label1";
            // 
            // lblTemp2
            // 
            this.lblTemp2.BackColor = System.Drawing.SystemColors.Control;
            this.lblTemp2.Cursor = System.Windows.Forms.Cursors.Default;
            this.lblTemp2.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.lblTemp2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblTemp2.Location = new System.Drawing.Point(16, 35);
            this.lblTemp2.Name = "lblTemp2";
            this.lblTemp2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblTemp2.Size = new System.Drawing.Size(74, 17);
            this.lblTemp2.TabIndex = 1;
            this.lblTemp2.Text = "Label1";
            // 
            // Frame4
            // 
            this.Frame4.BackColor = System.Drawing.SystemColors.Control;
            this.Frame4.Controls.Add(this._lblFan_0);
            this.Frame4.Controls.Add(this._lblFan_1);
            this.Frame4.Controls.Add(this._lblFan_2);
            this.Frame4.Controls.Add(this._lblFan_3);
            this.Frame4.Controls.Add(this._lblFan_4);
            this.Frame4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Frame4.Location = new System.Drawing.Point(128, 9);
            this.Frame4.Name = "Frame4";
            this.Frame4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Frame4.Size = new System.Drawing.Size(105, 105);
            this.Frame4.TabIndex = 4;
            this.Frame4.TabStop = false;
            this.Frame4.Text = "FAN";
            // 
            // _lblFan_0
            // 
            this._lblFan_0.AutoSize = true;
            this._lblFan_0.BackColor = System.Drawing.SystemColors.Control;
            this._lblFan_0.Cursor = System.Windows.Forms.Cursors.Default;
            this._lblFan_0.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this._lblFan_0.ForeColor = System.Drawing.SystemColors.ControlText;
            this._lblFan_0.Location = new System.Drawing.Point(16, 19);
            this._lblFan_0.Name = "_lblFan_0";
            this._lblFan_0.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._lblFan_0.Size = new System.Drawing.Size(41, 12);
            this._lblFan_0.TabIndex = 8;
            this._lblFan_0.Text = "Label1";
            // 
            // _lblFan_1
            // 
            this._lblFan_1.AutoSize = true;
            this._lblFan_1.BackColor = System.Drawing.SystemColors.Control;
            this._lblFan_1.Cursor = System.Windows.Forms.Cursors.Default;
            this._lblFan_1.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this._lblFan_1.ForeColor = System.Drawing.SystemColors.ControlText;
            this._lblFan_1.Location = new System.Drawing.Point(16, 35);
            this._lblFan_1.Name = "_lblFan_1";
            this._lblFan_1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._lblFan_1.Size = new System.Drawing.Size(41, 12);
            this._lblFan_1.TabIndex = 7;
            this._lblFan_1.Text = "Label1";
            // 
            // _lblFan_2
            // 
            this._lblFan_2.AutoSize = true;
            this._lblFan_2.BackColor = System.Drawing.SystemColors.Control;
            this._lblFan_2.Cursor = System.Windows.Forms.Cursors.Default;
            this._lblFan_2.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this._lblFan_2.ForeColor = System.Drawing.SystemColors.ControlText;
            this._lblFan_2.Location = new System.Drawing.Point(16, 51);
            this._lblFan_2.Name = "_lblFan_2";
            this._lblFan_2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._lblFan_2.Size = new System.Drawing.Size(41, 12);
            this._lblFan_2.TabIndex = 6;
            this._lblFan_2.Text = "Label1";
            // 
            // _lblFan_3
            // 
            this._lblFan_3.AutoSize = true;
            this._lblFan_3.BackColor = System.Drawing.SystemColors.Control;
            this._lblFan_3.Cursor = System.Windows.Forms.Cursors.Default;
            this._lblFan_3.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this._lblFan_3.ForeColor = System.Drawing.SystemColors.ControlText;
            this._lblFan_3.Location = new System.Drawing.Point(16, 67);
            this._lblFan_3.Name = "_lblFan_3";
            this._lblFan_3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._lblFan_3.Size = new System.Drawing.Size(41, 12);
            this._lblFan_3.TabIndex = 5;
            this._lblFan_3.Text = "Label1";
            // 
            // _lblFan_4
            // 
            this._lblFan_4.AutoSize = true;
            this._lblFan_4.BackColor = System.Drawing.SystemColors.Control;
            this._lblFan_4.Cursor = System.Windows.Forms.Cursors.Default;
            this._lblFan_4.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this._lblFan_4.ForeColor = System.Drawing.SystemColors.ControlText;
            this._lblFan_4.Location = new System.Drawing.Point(16, 83);
            this._lblFan_4.Name = "_lblFan_4";
            this._lblFan_4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this._lblFan_4.Size = new System.Drawing.Size(41, 12);
            this._lblFan_4.TabIndex = 4;
            this._lblFan_4.Text = "Label1";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // lblTemp3
            // 
            this.lblTemp3.BackColor = System.Drawing.SystemColors.Control;
            this.lblTemp3.Cursor = System.Windows.Forms.Cursors.Default;
            this.lblTemp3.Font = new System.Drawing.Font("lr SVbN", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
            this.lblTemp3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblTemp3.Location = new System.Drawing.Point(16, 51);
            this.lblTemp3.Name = "lblTemp3";
            this.lblTemp3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.lblTemp3.Size = new System.Drawing.Size(74, 17);
            this.lblTemp3.TabIndex = 3;
            this.lblTemp3.Text = "Label1";
            // 
            // frmTempFan
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
            this.ClientSize = new System.Drawing.Size(244, 125);
            this.Controls.Add(this.Frame4);
            this.Controls.Add(this.Frame2);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Location = new System.Drawing.Point(3, 22);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmTempFan";
            this.Text = "Temp/Fan State";
            this.Closed += new System.EventHandler(this.frmTempFan_Closed);
            this.Load += new System.EventHandler(this.frmTempFan_Load);
            this.Frame2.ResumeLayout(false);
            this.Frame4.ResumeLayout(false);
            this.Frame4.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmTempFan());
		}

		//****************************************************
		//   ֐ F frmTempFan_Load
		//   @\   Fʂ̋N()
		//      F Ȃ
		//   ߂l F Ȃ
		//****************************************************
		private void frmTempFan_Load(object sender, System.EventArgs e)
		{
			int ret;

			// xRg[z̍쐬
			lblFanAry = new System.Windows.Forms.Label[5];

			// xRg[̔zɂłɍ쐬ĂCX^X
			lblFanAry[0] = _lblFan_0;
			lblFanAry[1] = _lblFan_1;
			lblFanAry[2] = _lblFan_2;
			lblFanAry[3] = _lblFan_3;
			lblFanAry[4] = _lblFan_4;

			// ƃtH_st@CtH_ɕύX DLLǂݏo
			System.IO.Directory.SetCurrentDirectory(Application.StartupPath);

			// \tgRAS̃Cu
			ret = fcsoftrasOpenP();

			strFan[0] = "FAN1 :";
			strFan[1] = "FAN2 :";
			strFan[2] = "FAN3 :";
			strFan[3] = "FAN4 :";
			strFan[4] = "FAN5 :";

			// ݂̏Ԃ擾Ɖʂւ̔f
			subSetTemp();
			subSetFan();
		
			// ʍXVp̃^C}[ݒ
			timer1.Interval = 10000;			// PObݒ
			timer1.Enabled = true;				// L
		}

		//****************************************************
		//   ֐ F frmTempFan_Closed
		//   @\   Fʂ̏I(㏈)
		//      F Ȃ
		//   ߂l F Ȃ
		//****************************************************
		private void frmTempFan_Closed(object sender, System.EventArgs e)
		{
			int ret;

			// \tgRAS̃CuI
			ret = fcsoftrasCloseP();
		}

		//****************************************************
		//   ֐ F timer1_Tick
		//   @\   F^C}[ɂʍXV
		//      F Ȃ
		//   ߂l F Ȃ
		//****************************************************
		private void timer1_Tick(object sender, System.EventArgs e)
		{

			// ݂̏Ԃ擾Ɖʂւ̔f
			subSetTemp();
			subSetFan();
		}

		//****************************************************
		//   ֐ F subSetTemp
		//   @\   Fx̏Ԃ擾Aʂ̍XVsȂ
		//      F Ȃ
		//   ߂l F Ȃ
		//****************************************************
		private void subSetTemp()
		{
			byte[] buff = new byte[6];		// xXe[^Xۑp
			int	ret;						// ߂lۑp

			ret = fcsoftrasGetTempP(ref buff);
			// xf[^擾H
			if (FCSRASAPI_SUCCESS == ret) 
			{
				lblTemp1.Text = "CPU   :" + buff[2];
				lblTemp2.Text = "INT   :" + buff[3];
                lblTemp3.Text = "EXT   :" + buff[4];
            }
			else 
			{
				lblTemp1.Text = "NG";
				lblTemp2.Text = "NG";
                lblTemp3.Text = "NG";
                timer1.Enabled = false;
			}

		}

		//****************************************************
		//   ֐ F subSetFan
		//   @\   FFAN̏Ԃ擾Aʂ̍XVsȂ
		//      F Ȃ
		//   ߂l F Ȃ
		//****************************************************
		private void subSetFan()
		{
			byte[] buff1 = new byte[1];			// FANڑXe[^Xۑp
			byte[] buff2 = new byte[12];		// FANXe[^Xۑp
			int count1;
			int ret;							// ߂lۑp

			ret = fcsoftrasGetFanInfP(ref buff1);
			// FANڑ擾H
			if (FCSRASAPI_SUCCESS == ret)
			{
				ret = fcsoftrasGetFanP(ref buff2);
				// FANf[^擾H
				if (FCSRASAPI_SUCCESS == ret) 
				{
					for (count1 = 0; count1 <= 4; count1++)
					{
						if ((buff1[0] & (0x01 << count1)) != 0)
						{
							// FANڑ
							lblFanAry[count1].Text = strFan[count1] + (buff2[count1 * 2 + 2] + (buff2[count1 * 2 + 3] * 0x100));
						}
						else
						{
							// FANڑȂ
							lblFanAry[count1].Text = strFan[count1] + "None";
						}
					}
				}
				else
				{
					lblFanAry[0].Text = "NG";
					timer1.Enabled = false;
				}
			}
			else
			{
				lblFanAry[0].Text = "NG";
				timer1.Enabled = false;
			}
		}
	}
}
