Esempio n. 1
0
        public async Task <string> ConnectAsync(string hostname, int port)
        {
            Timeout  = 5000;
            Hostname = hostname;
            Port     = port;

            if (App.Mock)
            {
                await Task.Delay(1);

                Connected = true;
                return("Forcing mock connection");
            }

            using (await BusyObject.LockAsync())
            {
                IsBusy = true;
                using (var client = new TelnetClient(hostname, port, Timeout))
                {
                    if (await client.Connect())
                    {
                        var suppressDebug = false;
                        await client.WriteLineAsync(":*IDN?", suppressDebug);

                        var s = await client.ReadStringAsync(suppressDebug);

                        Connected = true;
                        return(s);
                    }
                    Connected = false;
                    IsBusy    = false;
                    return(string.Empty);
                }
            }
        }
Esempio n. 2
0
        public async Task <string> SendCommandAsync(string command, bool getResponse)
        {
            if (App.Mock)
            {
                await Task.Delay(1);

                if (getResponse)
                {
                    throw new Exception($"Cannot determine command response when mocking (command: '{command}'");
                }
                else
                {
                    return("");
                }
            }

            using (await BusyObject.LockAsync())
            {
                IsBusy = true;
                using (var client = new TelnetClient(Hostname, Port, Timeout))
                {
                    if (!await client.Connect())
                    {
                        return(null);
                    }
                    var suppressDebug = command == ":TRIG:STAT?";
                    await client.WriteLineAsync(command, suppressDebug);

                    IsBusy = false;
                    if (getResponse)
                    {
                        return(await client.ReadStringAsync(suppressDebug));
                    }
                    else
                    {
                        if (AutoGetScreenshotAfterCommand && !IsQuery(command))
                        {
                            Task.Run(async() =>
                            {
                                await Task.Delay(100);
                                await GetScreenshot();
                            }).NoAwait();
                        }
                        return("");
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task <byte[]> GetScreenshot()
        {
            if (App.Mock)
            {
                await Task.Delay(1);

                string resourceID = string.Format("Skippy.Resources.mock.png");
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                System.IO.Stream           stream   = assembly.GetManifestResourceStream(resourceID);
                using (var memoryStream = new System.IO.MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    var bytes = memoryStream.ToArray();
                    return(bytes);
                }
            }

            // skip if busy
            //if (IsBusy)
            //{
            //    return null;
            //}

            using (await BusyObject.LockAsync())
                using (var client = new TelnetClient(Hostname, Port, Timeout))
                {
                    IsBusy = true;
                    client.DebugEnabled = false;
                    if (!await client.Connect())
                    {
                        return(null);
                    }
                    var suppressDebug = true;
                    await client.WriteLineAsync(":DISP:DATA? ON,OFF,PNG", suppressDebug);

                    // read header start character '#'
                    var char1 = await client.ReadCharAsync();

                    if (char1 == '#')
                    {
                        // read header length
                        var char2 = await client.ReadCharAsync();

                        if (int.TryParse(char2.ToString(), out var headerLength))
                        {
                            // read length of data
                            var dataLengthString = await client.ReadStringAsync(headerLength);

                            if (int.TryParse(dataLengthString, out var dataLength))
                            {
                                var data = await client.ReadBytesAsync(dataLength);

                                //Debug.WriteLine("Got Screenshot");
                                IsBusy = false;
                                return(data);
                            }
                            else
                            {
                                Debug.WriteLine("Screenshot response data length not recognized");
                            }
                        }
                        else
                        {
                            Debug.WriteLine("Screenshot response missing header length");
                        }
                    }
                    else
                    {
                        Debug.WriteLine("Screenshot response missing header character");
                    }
                    client.DebugEnabled = true;
                    IsBusy = false;
                    return(null);
                }
        }