Esempio n. 1
0
 public Configuration()
 {
     try
     {
         if (ServerForm.DebugTraceState)
         {
             ServerForm.LogMessage(0, 0, 0, "Configuration New", "About to create base key");
         }
         baseRegistryKey = RegistryKey.OpenBaseKey(SharedConstants.ASCOM_REMOTE_CONFIGURATION_HIVE, RegistryView.Default).CreateSubKey(SharedConstants.ASCOM_REMOTE_CONFIGURATION_KEY);
         if (ServerForm.DebugTraceState)
         {
             ServerForm.LogMessage(0, 0, 0, "Configuration New", "Created base key: " + baseRegistryKey.Name);
         }
     }
     catch (Exception ex)
     {
         ServerForm.LogException(0, 0, 0, "Configuration New", ex.ToString());
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Send a command to the driver
 /// </summary>
 /// <param name="requestData">Details of the command to send</param>
 public void DriverCommand(RequestData requestData)
 {
     try // Process the command
     {
         if (ServerForm.DebugTraceState)
         {
             ServerForm.LogMessage(requestData.ClientID, requestData.ClientTransactionID, requestData.ServerTransactionID, "DriverCommand", string.Format("Received command for {0} on thread {1}", deviceKey, Thread.CurrentThread.ManagedThreadId));
         }
         Application.DoEvents();
         restServer.ProcessDriverCommand(requestData.ClientID, requestData.ClientTransactionID, requestData.ServerTransactionID, requestData.SuppliedParameters, requestData.Request, requestData.Response, requestData.Elements, requestData.DeviceKey);
         if (ServerForm.DebugTraceState)
         {
             ServerForm.LogMessage(requestData.ClientID, requestData.ClientTransactionID, requestData.ServerTransactionID, "DriverCommand", string.Format("Completed command for {0} on thread {1}", deviceKey, Thread.CurrentThread.ManagedThreadId));
         }
     }
     catch (Exception ex) // Something serious has gone wrong with the ASCOM Rest server itself so report this to the user
     {
         ServerForm.LogException(requestData.ClientID, requestData.ClientTransactionID, requestData.ServerTransactionID, "DriverCommand", ex.ToString());
         restServer.Return500Error(requestData.Request, requestData.Response, "Internal server error (DriverOnSeparateThread): " + ex.ToString(), requestData.ClientID, requestData.ClientTransactionID, requestData.ServerTransactionID);
     }
 }
Esempio n. 3
0
        private void BtnSetup_Click(object sender, EventArgs e)
        {
            // This device's ProgID is held in the variable progID so try and run its SetupDialog method
            ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Setup button pressed for device: {0}, ProgID: {1}", cmbDevice.Text, progID));

            try
            {
                // Get an instance of the driver from its ProgID and store this in a dynamic variable so that we can call its method directly
                Type ProgIdType = Type.GetTypeFromProgID(progID);
                //ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Found type: {0}", ProgIdType.Name));

                dynamic oDrv = Activator.CreateInstance(ProgIdType);
                //ServerForm.LogMessage(0, 0, 0, "Setup", "Created driver instance OK");

                try
                {
                    if (GetConnectdState(oDrv)) // Driver is connected and the Setup dialogue must be run with the device disconnected so ask whether we can disconnect it
                    {
                        DialogResult dialogResult = MessageBox.Show("Device is connected, OK to disconnect and run Setup?", "Disconnect Device?", MessageBoxButtons.OKCancel);
                        if (dialogResult == DialogResult.OK) // OK to disconnect and run setup dialogue
                        {
                            //ServerForm.LogMessage(0, 0, 0, "Setup", "User gave permission to disconnect device - setting Connected to false");
                            try { oDrv.Connected = false; } catch { }; // Set Connected to false ignoring errors
                            try { oDrv.Link = false; } catch { };      // Set Link to false (for IFocuserV1 devices) ignoring errors

                            int RemainingObjectCount = Marshal.FinalReleaseComObject(oDrv);
                            oDrv = null;
                            oDrv = Activator.CreateInstance(ProgIdType);

                            //ServerForm.LogMessage(0, 0, 0, "Setup", string.Format("Connected has bee set false and destroyed. New Connected value: {0}", oDrv.Connected));

                            //ServerForm.LogMessage(0, 0, 0, "Setup", "Device is now disconnected, calling SetupDialog method");
                            oDrv.SetupDialog();
                            //ServerForm.LogMessage(0, 0, 0, "Setup", "Completed SetupDialog method, setting Connected to true");

                            try
                            {
                                oDrv.Connected = true; // Try setting Connected to true
                            }
                            catch (Exception ex2) when(DeviceType.ToLowerInvariant() == "focuser")
                            {
                                // Connected failed so try Link in case this is an IFocuserV1 device
                                ServerForm.LogException(0, 0, 0, "Setup", $"Error setting Connected to true for focuser device {ProgID}, now trying Link for IFocuserV1 devices: \r\n{ex2.ToString()}");
                                oDrv.Link = true;
                            }

                            //ServerForm.LogMessage(0, 0, 0, "Setup", "Driver is now Connected");
                        }
                        else // Not OK to disconnect so just do nothing and exit
                        {
                            ServerForm.LogMessage(0, 0, 0, "Setup", "User did not give permission to disconnect device - no action taken");
                        }
                    }
                    else // Driver is not connected
                    {
                        //ServerForm.LogMessage(0, 0, 0, "Setup", "Device is disconnected so just calling SetupDialog method");
                        oDrv.SetupDialog();
                        //ServerForm.LogMessage(0, 0, 0, "Setup", "Completed SetupDialog method");

                        try { oDrv.Dispose(); } catch { }; // Dispose the driver if possible

                        // Release the COM object properly
                        try
                        {
                            //ServerForm.LogMessage(0, 0, 0, "Setup", "  Releasing COM object");
                            int LoopCount            = 0;
                            int RemainingObjectCount = 0;

                            do
                            {
                                LoopCount           += 1; // Increment the loop counter so that we don't go on for ever!
                                RemainingObjectCount = Marshal.ReleaseComObject(oDrv);
                                //ServerForm.LogMessage(0, 0, 0, "Setup", "  Remaining object count: " + RemainingObjectCount.ToString() + ", LoopCount: " + LoopCount);
                            } while ((RemainingObjectCount > 0) & (LoopCount < 20));
                        }
                        catch (Exception ex2)
                        {
                            ServerForm.LogMessage(0, 0, 0, "Setup", "  ReleaseComObject Exception: " + ex2.Message);
                        }

                        oDrv = null;
                    }
                }
                catch (Exception ex1)
                {
                    string errMsg = string.Format("Exception calling SetupDialog method: {0}", ex1.Message);
                    MessageBox.Show(errMsg);
                    ServerForm.LogMessage(0, 0, 0, "Setup", errMsg);
                    ServerForm.LogException(0, 0, 0, "Setup", ex1.ToString());
                }
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("Exception creating driver {0} - {1}", progID, ex.Message);
                MessageBox.Show(errMsg);
                ServerForm.LogMessage(0, 0, 0, "Setup", errMsg);
                ServerForm.LogException(0, 0, 0, "Setup", ex.ToString());
            }
        }
Esempio n. 4
0
        private void Form_Load(object sender, EventArgs e)
        {
            try
            {
                ServerForm.LogMessage(0, 0, 0, "SetupForm Load", "Start");

                // Set up the GUI components
                numPort.Value          = ServerForm.ServerPortNumber;
                chkAutoConnect.Checked = ServerForm.StartWithDevicesConnected;
                chkAccessLog.Checked   = ServerForm.AccessLogEnabled;
                chkTrace.Checked       = ServerForm.TraceState;
                chkDebugTrace.Checked  = ServerForm.DebugTraceState;
                chkDebugTrace.Enabled  = ServerForm.TraceState; // Enable or disable the debug trace check box depending on whether normal trace is enabled
                chkManagementInterfaceEnabled.Checked             = ServerForm.ManagementInterfaceEnabled;
                ChkStartWithApiEnabled.Checked                    = ServerForm.StartWithApiEnabled;
                LblDevicesNotDisconnoected.Visible                = ServerForm.devicesAreConnected;
                ChkRunDriversInSeparateThreadss.Checked           = ServerForm.RunDriversOnSeparateThreads;
                ChkLogClientIPAddress.Checked                     = ServerForm.LogClientIPAddress;
                ChkIncludeDriverExceptionsInJsonResponses.Checked = ServerForm.IncludeDriverExceptionInJsonResponse;
                TxtRemoteServerLocation.Text      = ServerForm.RemoteServerLocation;
                ChkEnableCors.Checked             = ServerForm.CorsSupportIsEnabled; // Set the CORS enabled checkbox (this doesn't fire associated event handlers if support is disabled)
                NumCorsMaxAge.Value               = ServerForm.CorsMaxAge;
                ChkCorsSupportCredentials.Checked = ServerForm.CorsCredentialsPermitted;
                ChkEnableDiscovery.Checked        = ServerForm.AlpacaDiscoveryEnabled;
                NumDiscoveryPort.Value            = ServerForm.AlpacaDiscoveryPort;
                NumMaxDevices.Value               = ServerForm.MaximumNumberOfDevices;

                // Set the IP v4 / v6 radio boxes
                if (ServerForm.IpV4Enabled & ServerForm.IpV6Enabled) // Both IPv4 and v6 are enabled so set the "both" button
                {
                    RadIpV4AndV6.Checked = true;
                }
                else // Only one of v4 or v6 is enabled so set accordingly
                {
                    RadIpV4.Checked = ServerForm.IpV4Enabled;
                    RadIpV6.Checked = ServerForm.IpV6Enabled;
                }

                // Populate the address list combo box
                PopulateAddressList();

                // CORS tab event handler
                ChkEnableCors_CheckedChanged(ChkEnableCors, new EventArgs()); // Fire the event handlers to ensure that the controls reflect the CORS enabled / disabled state
                DataGridCorsOrigins_EnabledChanged(DataGridCorsOrigins, new EventArgs());

                using (Profile profile = new Profile())
                {
                    // Populate the device types list
                    foreach (string deviceType in profile.RegisteredDeviceTypes)
                    {
                        ServerForm.LogMessage(0, 0, 0, "SetupForm Load", "Adding device type: " + deviceType);
                        registeredDeviceTypes.Add(deviceType); // Remember the device types on this system
                    }
                }
                ServerForm.LogMessage(0, 0, 0, "SetupForm Load", string.Format("Number of configured devices: {0}.", ServerForm.ConfiguredDevices.Count));

                foreach (string deviceName in ServerForm.ConfiguredDevices.Keys)
                {
                    ServerForm.LogMessage(0, 0, 0, "SetupForm Load", string.Format("ConfiguredDevices contains key {0}.", deviceName));
                }

                // Populate the device list with all configured controls
                deviceList = WalkControls(this, new List <ServedDevice>());

                // Initialise each of the device GUI components
                foreach (ServedDevice item in deviceList)
                {
                    string devicenumberString = item.Name.Substring("ServedDevice".Length);
                    ServerForm.LogMessage(0, 0, 0, "SetupForm Load", $"Init - Found device {item.Name} - {devicenumberString}");
                    if (int.Parse(item.Name.Substring("ServedDevice".Length)) < ServerForm.MaximumNumberOfDevices)   //string.Compare(item.Name, $"ServedDevice{ServerForm.MaximumNumberOfDevices - 1}") <= 0)         //item.Name <= $"servedDevice{ServerForm.MaximumNumberOfDevices-1}")
                    {
                        ServerForm.LogMessage(0, 0, 0, "SetupForm Load", string.Format("Starting Init for {0}.", item.Name));
                        item.InitUI(this);
                        ServerForm.LogMessage(0, 0, 0, "SetupForm Load", string.Format("Completed Init for {0}, now setting its parameters.", item.Name));
                        item.DeviceType             = ServerForm.ConfiguredDevices[item.Name].DeviceType;
                        item.ProgID                 = ServerForm.ConfiguredDevices[item.Name].ProgID;
                        item.DeviceNumber           = ServerForm.ConfiguredDevices[item.Name].DeviceNumber;
                        item.AllowConnectedSetFalse = ServerForm.ConfiguredDevices[item.Name].AllowConnectedSetFalse;
                        item.AllowConnectedSetTrue  = ServerForm.ConfiguredDevices[item.Name].AllowConnectedSetTrue;
                        item.AllowConcurrentAccess  = ServerForm.ConfiguredDevices[item.Name].AllowConcurrentAccess;
                        item.DevicesAreConnected    = ServerForm.devicesAreConnected;

                        ServerForm.LogMessage(0, 0, 0, "SetupForm Load", string.Format("Completed Init for {0}.", item.Name));
                    }
                }

                switch (ServerForm.MaximumNumberOfDevices)
                {
                case 10:
                    DeviceTabs.TabPages.Remove(DeviceTab1);
                    DeviceTabs.TabPages.Remove(DeviceTab2);
                    DeviceTabs.TabPages.Remove(DeviceTab3);
                    DeviceTabs.TabPages.Remove(DeviceTab4);
                    DeviceTabs.TabPages.Remove(DeviceTab5);
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 20:
                    DeviceTabs.TabPages.Remove(DeviceTab2);
                    DeviceTabs.TabPages.Remove(DeviceTab3);
                    DeviceTabs.TabPages.Remove(DeviceTab4);
                    DeviceTabs.TabPages.Remove(DeviceTab5);
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 30:
                    DeviceTabs.TabPages.Remove(DeviceTab3);
                    DeviceTabs.TabPages.Remove(DeviceTab4);
                    DeviceTabs.TabPages.Remove(DeviceTab5);
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 40:
                    DeviceTabs.TabPages.Remove(DeviceTab4);
                    DeviceTabs.TabPages.Remove(DeviceTab5);
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 50:
                    DeviceTabs.TabPages.Remove(DeviceTab5);
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 60:
                    DeviceTabs.TabPages.Remove(DeviceTab6);
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 70:
                    DeviceTabs.TabPages.Remove(DeviceTab7);
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 80:
                    DeviceTabs.TabPages.Remove(DeviceTab8);
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 90:
                    DeviceTabs.TabPages.Remove(DeviceTab9);
                    break;

                case 100:
                    break;
                }

                RecalculateDeviceNumbers();

                // Add this event handler after the initial value has been set so that this doesn't trigger an even
                this.NumMaxDevices.ValueChanged += new System.EventHandler(this.NumMaxDevices_ValueChanged);
            }
            catch (Exception ex)
            {
                ServerForm.LogException(0, 0, 0, "SetupForm Load", string.Format("Exception on loading form: {0}.", ex.ToString()));
                MessageBox.Show(string.Format("Setup exception: {0}\r\nThe form may not function correctly.", ex.Message), "Setup form load error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Called when the user presses the OK button to commit any new set up values
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (addressList.Text == SharedConstants.LOCALHOST_NAME_IPV4)
                {
                    ServerForm.ServerIPAddressString = SharedConstants.LOCALHOST_ADDRESS_IPV4;
                }
                else
                {
                    ServerForm.ServerIPAddressString = addressList.Text;
                }
                ServerForm.ServerPortNumber          = numPort.Value;
                ServerForm.StartWithDevicesConnected = chkAutoConnect.Checked;
                ServerForm.AccessLogEnabled          = chkAccessLog.Checked;
                ServerForm.TraceState                           = chkTrace.Checked;
                ServerForm.DebugTraceState                      = chkDebugTrace.Checked;
                ServerForm.ManagementInterfaceEnabled           = chkManagementInterfaceEnabled.Checked;
                ServerForm.StartWithApiEnabled                  = ChkStartWithApiEnabled.Checked;
                ServerForm.RunDriversOnSeparateThreads          = ChkRunDriversInSeparateThreadss.Checked;
                ServerForm.LogClientIPAddress                   = ChkLogClientIPAddress.Checked;
                ServerForm.IncludeDriverExceptionInJsonResponse = ChkIncludeDriverExceptionsInJsonResponses.Checked;
                ServerForm.RemoteServerLocation                 = TxtRemoteServerLocation.Text;
                ServerForm.CorsSupportIsEnabled                 = ChkEnableCors.Checked;
                ServerForm.CorsMaxAge                           = NumCorsMaxAge.Value;
                ServerForm.CorsCredentialsPermitted             = ChkCorsSupportCredentials.Checked;
                ServerForm.AlpacaDiscoveryEnabled               = ChkEnableDiscovery.Checked;
                ServerForm.AlpacaDiscoveryPort                  = NumDiscoveryPort.Value;
                ServerForm.MaximumNumberOfDevices               = (int)NumMaxDevices.Value;

                // Set the IP v4 and v6 variables as necessary
                if (RadIpV4.Checked) // The IPv4 radio button is checked so set the IP v4 and IP v6 variables accordingly
                {
                    ServerForm.IpV4Enabled = true;
                    ServerForm.IpV6Enabled = false;
                }
                if (RadIpV6.Checked) // The IPv6 radio button is checked so set the IP v4 and IP v6 variables accordingly
                {
                    ServerForm.IpV4Enabled = false;
                    ServerForm.IpV6Enabled = true;
                }
                if (RadIpV4AndV6.Checked) // The IPv4 and IPV6 radio button is checked so set the IP v4 and IP v6 variables accordingly
                {
                    ServerForm.IpV4Enabled = true;
                    ServerForm.IpV6Enabled = true;
                }

                foreach (ServedDevice item in deviceList)
                {
                    ServerForm.ConfiguredDevices[item.Name].DeviceType = item.DeviceType;

                    // Update the unique ID if the ProgID has changed
                    if (ServerForm.ConfiguredDevices[item.Name].ProgID != item.ProgID)
                    {
                        if (item.ProgID == SharedConstants.DEVICE_NOT_CONFIGURED) // Device has been de-configured
                        {
                            ServerForm.ConfiguredDevices[item.Name].UniqueID = SharedConstants.DEVICE_NOT_CONFIGURED;
                        }
                        else // Device has been changed so create a new unique ID
                        {
                            ServerForm.ConfiguredDevices[item.Name].UniqueID = Guid.NewGuid().ToString().ToUpperInvariant();
                        }
                    }

                    ServerForm.ConfiguredDevices[item.Name].ProgID                 = item.ProgID;
                    ServerForm.ConfiguredDevices[item.Name].Description            = item.Description;
                    ServerForm.ConfiguredDevices[item.Name].DeviceNumber           = item.DeviceNumber;
                    ServerForm.ConfiguredDevices[item.Name].AllowConnectedSetFalse = item.AllowConnectedSetFalse;
                    ServerForm.ConfiguredDevices[item.Name].AllowConnectedSetTrue  = item.AllowConnectedSetTrue;
                    ServerForm.ConfiguredDevices[item.Name].AllowConcurrentAccess  = item.AllowConcurrentAccess;
                }

                ServerForm.CorsPermittedOrigins = corsPermittedOriginsCopy.ToListString(); // Copy the edited list back to the master copy

                ServerForm.WriteProfile();

                if (maxDevicesHasChanged)
                {
                    MessageBox.Show("The maximum number of devices has changed, please close and restart the Remote Server before adding further devices.", "Maximum Number of Devices", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                ServerForm.LogException(0, 0, 0, "OK Button", string.Format("Exception on closing form: {0}.", ex.ToString()));
            }
        }