Adding CEC logs and basic reconnect logic.
authorStephaneLenclud
Fri, 15 Jul 2016 18:31:17 +0200
changeset 2016213f42f1983
parent 200 663c1ef0de59
child 202 8784c59c784e
Adding CEC logs and basic reconnect logic.
Server/CecClient.cs
Server/MainForm.Designer.cs
Server/MainForm.cs
Server/Program.cs
Server/SharpDisplayManager.csproj
     1.1 --- a/Server/CecClient.cs	Thu Jul 14 19:25:52 2016 +0200
     1.2 +++ b/Server/CecClient.cs	Fri Jul 15 18:31:17 2016 +0200
     1.3 @@ -61,29 +61,68 @@
     1.4          }
     1.5  
     1.6  
     1.7 -
     1.8 +        /// <summary>
     1.9 +        /// 
    1.10 +        /// </summary>
    1.11 +        /// <param name="alert"></param>
    1.12 +        /// <param name="data"></param>
    1.13 +        /// <returns></returns>
    1.14          public virtual int ReceiveAlert(CecAlert alert, CecParameter data)
    1.15          {
    1.16 +            string log = "CEC alert: " + alert.ToString();
    1.17 +            if (data != null && data.Type == CecParameterType.ParameterTypeString)
    1.18 +            {
    1.19 +                log += " " + data.Data;
    1.20 +            }
    1.21 +
    1.22 +            Console.WriteLine(log);
    1.23 +
    1.24 +            Close();
    1.25 +            //Try reconnect
    1.26 +            Connect(1000);
    1.27              return 1;
    1.28          }
    1.29  
    1.30 +        /// <summary>
    1.31 +        /// 
    1.32 +        /// </summary>
    1.33 +        /// <param name="newVal"></param>
    1.34 +        /// <returns></returns>
    1.35          public virtual int ReceiveMenuStateChange(CecMenuState newVal)
    1.36          {
    1.37 +            Console.WriteLine("CEC menu state changed to: " + iLib.ToString(newVal));
    1.38              return 1;
    1.39          }
    1.40  
    1.41 +        /// <summary>
    1.42 +        /// 
    1.43 +        /// </summary>
    1.44 +        /// <param name="logicalAddress"></param>
    1.45 +        /// <param name="activated"></param>
    1.46          public virtual void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
    1.47          {
    1.48 +            Console.WriteLine("CEC source activated: " + iLib.ToString(logicalAddress) + "/" + activated.ToString() );
    1.49              return;
    1.50          }
    1.51  
    1.52          public override int ReceiveCommand(CecCommand command)
    1.53          {
    1.54 +            Console.WriteLine(string.Format("CEC command Src:{0} Dst:{1} Ack: {2} Eom: {3} OpcodeSet: {4} Opcode: {5} Timeout: {6}",
    1.55 +                iLib.ToString(command.Initiator),
    1.56 +                iLib.ToString(command.Destination),
    1.57 +                command.Ack.ToString(),
    1.58 +                command.Eom.ToString(),
    1.59 +                command.OpcodeSet.ToString(),
    1.60 +                iLib.ToString(command.Opcode),
    1.61 +                command.TransmitTimeout.ToString()
    1.62 +                ));
    1.63              return 1;
    1.64          }
    1.65  
    1.66          public override int ReceiveKeypress(CecKeypress key)
    1.67          {
    1.68 +            Console.WriteLine(string.Format("CEC keypress: {0} Duration:{1} Empty: {2}",
    1.69 +                key.Keycode.ToString(), key.Duration.ToString(), key.Empty.ToString()));
    1.70              return 1;
    1.71          }
    1.72  
    1.73 @@ -114,6 +153,7 @@
    1.74                  }
    1.75                  string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
    1.76                  Console.WriteLine(strLog);
    1.77 +                
    1.78              }
    1.79              return 1;
    1.80          }
    1.81 @@ -125,27 +165,74 @@
    1.82          /// <returns></returns>
    1.83          public bool Connect(int timeout)
    1.84          {
    1.85 +            Close();         
    1.86              CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
    1.87              if (adapters.Length > 0)
    1.88 -                return Connect(adapters[0].ComPort, timeout);
    1.89 +            {
    1.90 +                Connect(adapters[0].ComPort, timeout);                
    1.91 +            }                
    1.92              else
    1.93              {
    1.94 -                Console.WriteLine("Did not find any CEC adapters");
    1.95 -                return false;
    1.96 +                Console.WriteLine("CEC did not find any adapters");
    1.97              }
    1.98 +
    1.99 +            return iConnected;
   1.100          }
   1.101  
   1.102          public bool Connect(string port, int timeout)
   1.103          {
   1.104 -            return iLib.Open(port, timeout);
   1.105 +            Close();
   1.106 +            iConnected = iLib.Open(port, timeout);
   1.107 +            if (iConnected)
   1.108 +            {
   1.109 +                Scan();
   1.110 +            }
   1.111 +            return iConnected;
   1.112          }
   1.113  
   1.114          public void Close()
   1.115 -        {
   1.116 +        {            
   1.117              iLib.Close();
   1.118 +            iConnected = false;
   1.119          }
   1.120  
   1.121 -        public void ListDevices()
   1.122 +        /// <summary>
   1.123 +        /// 
   1.124 +        /// </summary>
   1.125 +        public void Scan()
   1.126 +        {
   1.127 +            Console.WriteLine("CEC bus information");
   1.128 +            Console.WriteLine("===================");
   1.129 +            CecLogicalAddresses addresses = Lib.GetActiveDevices();
   1.130 +            for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
   1.131 +            {
   1.132 +                CecLogicalAddress address = (CecLogicalAddress) iPtr;
   1.133 +                if (!addresses.IsSet(address))
   1.134 +                    continue;
   1.135 +
   1.136 +                CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
   1.137 +                bool bActive = Lib.IsActiveDevice(address);
   1.138 +                ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
   1.139 +                string strAddr = Lib.PhysicalAddressToString(iPhysicalAddress);
   1.140 +                CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
   1.141 +                CecPowerStatus power = Lib.GetDevicePowerStatus(address);
   1.142 +                string osdName = Lib.GetDeviceOSDName(address);
   1.143 +                string lang = Lib.GetDeviceMenuLanguage(address);
   1.144 +
   1.145 +                Console.WriteLine("device #" + iPtr + ": " + Lib.ToString(address));
   1.146 +                Console.WriteLine("address:       " + strAddr);
   1.147 +                Console.WriteLine("active source: " + (bActive ? "yes" : "no"));
   1.148 +                Console.WriteLine("vendor:        " + Lib.ToString(iVendorId));
   1.149 +                Console.WriteLine("osd string:    " + osdName);
   1.150 +                Console.WriteLine("CEC version:   " + Lib.ToString(iCecVersion));
   1.151 +                Console.WriteLine("power status:  " + Lib.ToString(power));
   1.152 +                if (!string.IsNullOrEmpty(lang))
   1.153 +                    Console.WriteLine("language:      " + lang);
   1.154 +                Console.WriteLine("");
   1.155 +            }
   1.156 +        }
   1.157 +
   1.158 +        public void ListAdapters()
   1.159          {
   1.160              int iAdapter = 0;
   1.161              foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
   1.162 @@ -398,5 +485,10 @@
   1.163          /// 
   1.164          /// </summary>
   1.165          private LibCECConfiguration Config;
   1.166 +
   1.167 +        /// <summary>
   1.168 +        /// 
   1.169 +        /// </summary>
   1.170 +        private bool iConnected;
   1.171      }
   1.172  }
     2.1 --- a/Server/MainForm.Designer.cs	Thu Jul 14 19:25:52 2016 +0200
     2.2 +++ b/Server/MainForm.Designer.cs	Fri Jul 15 18:31:17 2016 +0200
     2.3 @@ -113,6 +113,8 @@
     2.4              this.labelFontHeight = new System.Windows.Forms.Label();
     2.5              this.toolTip = new System.Windows.Forms.ToolTip(this.components);
     2.6              this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
     2.7 +            this.tabPageLogs = new System.Windows.Forms.TabPage();
     2.8 +            this.richTextBoxLogs = new System.Windows.Forms.RichTextBox();
     2.9              this.panelDisplay.SuspendLayout();
    2.10              this.iTableLayoutPanel.SuspendLayout();
    2.11              this.statusStrip.SuspendLayout();
    2.12 @@ -127,6 +129,7 @@
    2.13              ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGreenStart)).BeginInit();
    2.14              this.tabPageCec.SuspendLayout();
    2.15              this.tabPageApp.SuspendLayout();
    2.16 +            this.tabPageLogs.SuspendLayout();
    2.17              this.SuspendLayout();
    2.18              // 
    2.19              // panelDisplay
    2.20 @@ -552,6 +555,7 @@
    2.21              this.tabControl.Controls.Add(this.tabPageInput);
    2.22              this.tabControl.Controls.Add(this.tabPageCec);
    2.23              this.tabControl.Controls.Add(this.tabPageApp);
    2.24 +            this.tabControl.Controls.Add(this.tabPageLogs);
    2.25              this.tabControl.Location = new System.Drawing.Point(12, 139);
    2.26              this.tabControl.Name = "tabControl";
    2.27              this.tabControl.SelectedIndex = 0;
    2.28 @@ -958,6 +962,33 @@
    2.29              // 
    2.30              this.openFileDialog.Filter = "EXE files (*.exe)|*.exe|All files (*.*)|*.*";
    2.31              // 
    2.32 +            // tabPageLogs
    2.33 +            // 
    2.34 +            this.tabPageLogs.Controls.Add(this.richTextBoxLogs);
    2.35 +            this.tabPageLogs.Location = new System.Drawing.Point(4, 22);
    2.36 +            this.tabPageLogs.Name = "tabPageLogs";
    2.37 +            this.tabPageLogs.Padding = new System.Windows.Forms.Padding(3);
    2.38 +            this.tabPageLogs.Size = new System.Drawing.Size(592, 242);
    2.39 +            this.tabPageLogs.TabIndex = 8;
    2.40 +            this.tabPageLogs.Text = "Logs";
    2.41 +            this.tabPageLogs.UseVisualStyleBackColor = true;
    2.42 +            // 
    2.43 +            // richTextBoxLogs
    2.44 +            // 
    2.45 +            this.richTextBoxLogs.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    2.46 +            | System.Windows.Forms.AnchorStyles.Left) 
    2.47 +            | System.Windows.Forms.AnchorStyles.Right)));
    2.48 +            this.richTextBoxLogs.BorderStyle = System.Windows.Forms.BorderStyle.None;
    2.49 +            this.richTextBoxLogs.DetectUrls = false;
    2.50 +            this.richTextBoxLogs.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    2.51 +            this.richTextBoxLogs.Location = new System.Drawing.Point(6, 6);
    2.52 +            this.richTextBoxLogs.Name = "richTextBoxLogs";
    2.53 +            this.richTextBoxLogs.ReadOnly = true;
    2.54 +            this.richTextBoxLogs.Size = new System.Drawing.Size(580, 230);
    2.55 +            this.richTextBoxLogs.TabIndex = 1;
    2.56 +            this.richTextBoxLogs.Text = "";
    2.57 +            this.richTextBoxLogs.WordWrap = false;
    2.58 +            // 
    2.59              // MainForm
    2.60              // 
    2.61              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    2.62 @@ -1000,6 +1031,7 @@
    2.63              this.tabPageCec.PerformLayout();
    2.64              this.tabPageApp.ResumeLayout(false);
    2.65              this.tabPageApp.PerformLayout();
    2.66 +            this.tabPageLogs.ResumeLayout(false);
    2.67              this.ResumeLayout(false);
    2.68              this.PerformLayout();
    2.69  
    2.70 @@ -1081,6 +1113,8 @@
    2.71          private System.Windows.Forms.CheckBox checkBoxCecMonitorOff;
    2.72          private System.Windows.Forms.Button iButtonStartIdleClient;
    2.73          private System.Windows.Forms.CheckBox iCheckBoxStartIdleClient;
    2.74 +        private System.Windows.Forms.TabPage tabPageLogs;
    2.75 +        private System.Windows.Forms.RichTextBox richTextBoxLogs;
    2.76      }
    2.77  }
    2.78  
     3.1 --- a/Server/MainForm.cs	Thu Jul 14 19:25:52 2016 +0200
     3.2 +++ b/Server/MainForm.cs	Fri Jul 15 18:31:17 2016 +0200
     3.3 @@ -141,8 +141,12 @@
     3.4              //Have our designer initialize its controls
     3.5              InitializeComponent();
     3.6  
     3.7 -			//Populate device types
     3.8 -			PopulateDeviceTypes();
     3.9 +            //Redirect console output
    3.10 +            RichTextBoxTextWriter writer = new RichTextBoxTextWriter(richTextBoxLogs);
    3.11 +            Console.SetOut(writer);
    3.12 +
    3.13 +            //Populate device types
    3.14 +            PopulateDeviceTypes();
    3.15  
    3.16              //Populate optical drives
    3.17              PopulateOpticalDrives();
     4.1 --- a/Server/Program.cs	Thu Jul 14 19:25:52 2016 +0200
     4.2 +++ b/Server/Program.cs	Fri Jul 15 18:31:17 2016 +0200
     4.3 @@ -40,7 +40,7 @@
     4.4          static void Main()
     4.5          {
     4.6  
     4.7 -			/*
     4.8 +            /*
     4.9  			if (!IsRunAsAdministrator())
    4.10  			{
    4.11  				var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);
    4.12 @@ -65,9 +65,8 @@
    4.13  				return;
    4.14  				//Application.Current.Shutdown();
    4.15  			}*/
    4.16 -
    4.17 -
    4.18 -			Application.ApplicationExit += new EventHandler(OnApplicationExit);
    4.19 +          
    4.20 +            Application.ApplicationExit += new EventHandler(OnApplicationExit);
    4.21  			//
    4.22              Application.EnableVisualStyles();
    4.23              Application.SetCompatibleTextRenderingDefault(false);
     5.1 --- a/Server/SharpDisplayManager.csproj	Thu Jul 14 19:25:52 2016 +0200
     5.2 +++ b/Server/SharpDisplayManager.csproj	Fri Jul 15 18:31:17 2016 +0200
     5.3 @@ -185,6 +185,7 @@
     5.4        <DependentUpon>ProgressForm.cs</DependentUpon>
     5.5      </Compile>
     5.6      <Compile Include="Properties\AssemblyInfo.cs" />
     5.7 +    <Compile Include="RichTextBoxTextWriter.cs" />
     5.8      <Compile Include="Session.cs" />
     5.9      <Compile Include="Settings.cs" />
    5.10      <Compile Include="StartupManager.cs" />