| 1 | using CecSharp; |
| 2 | using System.Windows.Forms; |
| 3 | |
| 4 | namespace CecConfigGui.actions |
| 5 | { |
| 6 | class ConnectToDevice : UpdateProcess |
| 7 | { |
| 8 | public ConnectToDevice(ref LibCecSharp lib, LibCECConfiguration config) |
| 9 | { |
| 10 | Lib = lib; |
| 11 | Config = config; |
| 12 | } |
| 13 | |
| 14 | public override void Process() |
| 15 | { |
| 16 | SendEvent(UpdateEventType.StatusText, "Opening connection..."); |
| 17 | SendEvent(UpdateEventType.ProgressBar, 10); |
| 18 | |
| 19 | //TODO read the com port setting from the configuration |
| 20 | CecAdapter[] adapters = Lib.FindAdapters(string.Empty); |
| 21 | if (adapters.Length == 0) |
| 22 | { |
| 23 | DialogResult result = MessageBox.Show("Could not detect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo); |
| 24 | if (result == DialogResult.No) |
| 25 | { |
| 26 | SendEvent(UpdateEventType.ExitApplication); |
| 27 | return; |
| 28 | } |
| 29 | else |
| 30 | adapters = Lib.FindAdapters(string.Empty); |
| 31 | } |
| 32 | |
| 33 | while (!Lib.Open(adapters[0].ComPort, 10000)) |
| 34 | { |
| 35 | DialogResult result = MessageBox.Show("Could not connect to any CEC adapter. Please check your configuration. Do you want to try again?", "Pulse-Eight USB-CEC Adapter", MessageBoxButtons.YesNo); |
| 36 | if (result == DialogResult.No) |
| 37 | { |
| 38 | SendEvent(UpdateEventType.ExitApplication); |
| 39 | return; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | SendEvent(UpdateEventType.ProgressBar, 20); |
| 44 | SendEvent(UpdateEventType.StatusText, "Sending power on commands..."); |
| 45 | Lib.PowerOnDevices(CecLogicalAddress.Broadcast); |
| 46 | |
| 47 | SendEvent(UpdateEventType.StatusText, "Detecting TV vendor..."); |
| 48 | SendEvent(UpdateEventType.ProgressBar, 30); |
| 49 | SendEvent(UpdateEventType.TVVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.Tv)); |
| 50 | |
| 51 | SendEvent(UpdateEventType.StatusText, "Detecting menu language..."); |
| 52 | SendEvent(UpdateEventType.ProgressBar, 40); |
| 53 | SendEvent(UpdateEventType.MenuLanguage, Lib.GetDeviceMenuLanguage(CecLogicalAddress.Tv)); |
| 54 | |
| 55 | SendEvent(UpdateEventType.ProgressBar, 50); |
| 56 | SendEvent(UpdateEventType.StatusText, "Detecting AVR devices..."); |
| 57 | |
| 58 | bool hasAVRDevice = Lib.IsActiveDevice(CecLogicalAddress.AudioSystem); |
| 59 | SendEvent(UpdateEventType.HasAVRDevice, hasAVRDevice); |
| 60 | |
| 61 | if (hasAVRDevice) |
| 62 | { |
| 63 | SendEvent(UpdateEventType.ProgressBar, 60); |
| 64 | SendEvent(UpdateEventType.StatusText, "Detecting AVR vendor..."); |
| 65 | SendEvent(UpdateEventType.AVRVendorId, (int)Lib.GetDeviceVendorId(CecLogicalAddress.AudioSystem)); |
| 66 | } |
| 67 | |
| 68 | if (!Lib.GetDevicePowerStatus(CecLogicalAddress.Tv).Equals(CecPowerStatus.On)) |
| 69 | { |
| 70 | SendEvent(UpdateEventType.ProgressBar, 70); |
| 71 | SendEvent(UpdateEventType.StatusText, "Activating the source..."); |
| 72 | Lib.SetActiveSource(CecDeviceType.Reserved); |
| 73 | } |
| 74 | |
| 75 | SendEvent(UpdateEventType.ProgressBar, 80); |
| 76 | SendEvent(UpdateEventType.StatusText, "Reading device configuration..."); |
| 77 | |
| 78 | Lib.GetCurrentConfiguration(Config); |
| 79 | SendEvent(Config); |
| 80 | |
| 81 | SendEvent(UpdateEventType.ProgressBar, 90); |
| 82 | SendEvent(UpdateEventType.StatusText, "Polling active devices"); |
| 83 | SendEvent(UpdateEventType.PollDevices); |
| 84 | |
| 85 | SendEvent(UpdateEventType.ProgressBar, 100); |
| 86 | SendEvent(UpdateEventType.StatusText, "Ready."); |
| 87 | } |
| 88 | |
| 89 | private LibCecSharp Lib; |
| 90 | private LibCECConfiguration Config; |
| 91 | } |
| 92 | } |