Server/CecClient.cs
changeset 201 6213f42f1983
parent 200 663c1ef0de59
child 202 8784c59c784e
     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  }