Exemplo n.º 1
0
        private static void RunSvc(string servvice_cs, string device_id, string msgOut, double ts)
        {
            int devAutoStart     = DevAutoStart;
            int devKeepListening = DevKeepListening;

            DevAutoStart     = 2;
            DevKeepListening = 2;



            DeviceStreamingCommon.DeviceTimeout = TimeSpan.FromMilliseconds(ts);

            try
            {
                if (basicMode)
                {
                    DeviceStream_Svc.RunSvc(Program.service_cs, Program.device_id, msgOut, OnSvcRecvText).GetAwaiter().GetResult();
                }
                else if (!UseCustomClass)
                {
                    DeviceStream_Svc.RunSvc(Program.service_cs, Program.device_id, msgOut, OnSvcRecvText, devKeepListening, devAutoStart, OnDeviceSvcUpdate, KeepAlive, ResponseExpected).GetAwaiter().GetResult();
                }

                else
                {
                    DeviceStream_Svc.RunSvc(Program.service_cs, Program.device_id, msgOut, OnSvcRecvText, devKeepListening, devAutoStart, OnDeviceSvcUpdate, KeepAlive, ResponseExpected, new DeviceSvcCurrentSettings_Example()).GetAwaiter().GetResult();
                }
            }
            //catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException)
            //{
            //    System.Diagnostics.Debug.WriteLine("0 Error App.RunSvc(): Hub connection failure");
            //}
            catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException)
            {
                System.Diagnostics.Debug.WriteLine("0 Error App.RunSvc(): Device not found");
            }
            catch (TaskCanceledException)
            {
                System.Diagnostics.Debug.WriteLine("0Error App.RunSvc(): Task canceled");
            }
            catch (OperationCanceledException)
            {
                System.Diagnostics.Debug.WriteLine("0 Error App.RunSvc(): Operation canceled");
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("Timeout"))
                {
                    System.Diagnostics.Debug.WriteLine("0 Error App.RunSvc(): " + ex.Message);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("0 Error App.RunSvc(): Timeout");
                }
            }
        }
        private async void Button_Click_Svc(object sender, RoutedEventArgs e)
        {
            string msgOut = tbSvcMsgIn.Text;



            //Store these current values then reset. These vales are passed to the device and remain so until changed.
            //Whereas
            int devAutoStart     = DevAutoStart;
            int devKeepListening = DevKeepListening;


            ClearAllToggles();

            //These values are passed if true with each connection. If not passed then the device clears them.
            bool keepAlive        = AppSettingsValues.Settings.KeepAliveSvc;
            bool responseExpected = AppSettingsValues.Settings.ExpectResponse;



            if (!DeviceStream_Svc.SignalSendMsgOut(msgOut, keepAlive, responseExpected))
            {
                await Task.Run(() =>
                {
                    try
                    {
                        if (svcBasicMode)
                        {
                            DeviceStream_Svc.RunSvc(service_cs, device_id, msgOut, OnSvcRecvText).GetAwaiter().GetResult();
                        }
                        else if (!svcCustomClassMode)
                        {
                            DeviceStream_Svc.RunSvc(service_cs, device_id, msgOut, OnSvcRecvText, devKeepListening, devAutoStart, OnSvcStatusUpdate, keepAlive, responseExpected).GetAwaiter().GetResult();
                        }

                        else
                        {
                            DeviceStream_Svc.RunSvc(service_cs, device_id, msgOut, OnSvcRecvText, devKeepListening, devAutoStart, OnSvcStatusUpdate, keepAlive, responseExpected, new DeviceSvcCurrentSettings_Example()).GetAwaiter().GetResult();
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        System.Diagnostics.Debug.WriteLine("Error App.RunSvc(): Task cancelled");
                    }
                    catch (OperationCanceledException)
                    {
                        System.Diagnostics.Debug.WriteLine("Error App.RunSvc(): Operation cancelled");
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Error App.RunSvc(): " + ex.Message);
                    }
                });
            }
        }