Esempio n. 1
0
        public async Task <EvaluationResult> GetReadings(DeviceWebAPIParameters parms)
        {
            var result = new EvaluationResult {
                Code = 0, Message = "Processed telemetry", Passed = true
            };

            try
            {
                var iotReg            = new IotUtilities.IotRegistry(parms.IotConnection);
                var connectionStrings = await iotReg.GetTwinsConnectionString();

                if (connectionStrings.Count != 3)
                {
                    throw new IndexOutOfRangeException("Incorrect number of device connection strings. There should be three.");
                }
                List <Task> tasks   = new List <Task>();
                var         devices = generateDevices(connectionStrings, parms.Interval);
                for (int i = 0; i < 3; i++)
                {
                    tasks.Add(deviceReadings(devices[i], parms.Iterations));
                }

                Task.WaitAll(tasks.ToArray());
            }
            catch (Exception ex)
            {
                result.Passed  = false;
                result.Code    = ex.HResult;
                result.Message = ex.Message;
            }
            return(result);
        }
Esempio n. 2
0
        public async Task <EvaluationResult> ReceiveCommand(DeviceWebAPIParameters parms)
        {
            var result = new EvaluationResult {
                Code = 0, Message = "Received a command", Passed = true
            };
            var iotReg            = new IotUtilities.IotRegistry(parms.IotConnection);
            var connectionStrings = await iotReg.GetTwinsConnectionString();

            var client = DeviceClient.CreateFromConnectionString(connectionStrings[0], s_transportType);

            if (client == null)
            {
                throw new ArgumentException("Failed to create a device client");
            }

            Message receivedMessage;


            receivedMessage = await client.ReceiveAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false);

            if (receivedMessage != null)
            {
                result.Message = Encoding.ASCII.GetString(receivedMessage.GetBytes());
                await client.CompleteAsync(receivedMessage).ConfigureAwait(false);
            }
            else
            {
                result.Message = "Device message receive timed out.  You must add a message within 30 seconds.";
                result.Passed  = false;
                result.Code    = -1;
            }
            return(result);
        }
Esempio n. 3
0
 public EventHubContext(DeviceWebAPIParameters parms, string tableName, string containerName)
 {
     this.tableName     = tableName;
     this.containerName = containerName;
     this.parms         = parms;
     storage            = new StorageContext(parms.EhubStorage, tableName, containerName);
 }
        public CosmosDBContext(DeviceWebAPIParameters parms)
        {
            this.parms = parms;
            var    uriClause = parms.CosmosDBConnection.Split(';')[0];
            var    keyClause = parms.CosmosDBConnection.Split(';')[1];
            var    eqPos     = keyClause.IndexOf('=');
            var    key       = keyClause.Substring(eqPos + 1, keyClause.Length - eqPos - 1);
            string uri       = uriClause.Split('=')[1];

            client = new DocumentClient(new Uri(uri), key);
        }
Esempio n. 5
0
        public async Task <EvaluationResult> GetDeviceCount(DeviceWebAPIParameters parms)
        {
            var result = new EvaluationResult {
                Code = 0, Message = "Processed telemetry", Passed = true
            };
            var retry   = true;
            var retries = 3;

            while (retry && (retries < 3))
            {
                try
                {
                    var iotReg      = new IotUtilities.IotRegistry(parms.IotConnection);
                    var deviceCount = (await iotReg.GetTwinsConnectionString()).Count;
                    result.Passed  = deviceCount == 3;
                    result.Message = $"There are {deviceCount} devices registered in IoY Hub";
                    result.Code    = deviceCount == 3 ? 0 : -1;
                    retry          = false;
                }
                catch (Exception ex)
                {
                    retries++;
                    if (retries == 3)
                    {
                        result.Passed  = false;
                        result.Code    = ex.HResult;
                        result.Message = $"Error: {ex.Message}";
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
            return(result);
        }
Esempio n. 6
0
        public async Task <EvaluationResult> ReceiveCommand(DeviceWebAPIParameters parms)
        {
            var result = new EvaluationResult {
                Code = 0, Message = "Received a command", Passed = false
            };

            //Added Try-Catch to prevent instantation errors from breaking webpage. 2021/05/11 - Anders Grasdal
            //Updated Method to include a serviceClient which sends the test message.
            //Updated evaluation response to be falsey by default and true only after success.
            try
            {
                /**
                 * var serviceClient = IotD.ServiceClient.CreateFromConnectionString(parms.IotConnection);
                 * if (serviceClient == null)
                 * {
                 *  throw new ArgumentException("Failed to create a service client ");
                 * }
                 *
                 * string messageContent = "This is a test message from the cloud to Building001";
                 * IotD.Message sentMessage = new IotD.Message(Encoding.ASCII.GetBytes(messageContent));
                 * try
                 * {
                 *  await serviceClient.SendAsync("Building001", sentMessage);
                 * }
                 * catch
                 * {
                 *  throw new ArgumentException("Failed to send message to Building001, device does not exist.");
                 * }
                 **/



                var iotReg            = new IotUtilities.IotRegistry(parms.IotConnection);
                var connectionStrings = await iotReg.GetTwinsConnectionString();

                var found = false;

                foreach (var deviceConnectionString in connectionStrings)
                {
                    if (deviceConnectionString.Contains("DeviceId=Building001"))
                    {
                        found = true;
                        var client = DeviceClient.CreateFromConnectionString(deviceConnectionString, s_transportType);
                        if (client == null)
                        {
                            throw new ArgumentException("Failed to create a device client");
                        }

                        Message receivedMessage;
                        receivedMessage = await client.ReceiveAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false);

                        if (receivedMessage != null)
                        {
                            result.Message = Encoding.ASCII.GetString(receivedMessage.GetBytes());
                            await client.CompleteAsync(receivedMessage).ConfigureAwait(false);

                            result.Passed = true;
                        }
                        else
                        {
                            result.Message = "Device message receive timed out.  You must add a message to Building001 within 30 seconds.";
                            //result.Passed = false;
                            result.Code = -1;
                        }
                        break;
                    }
                }
                if (!found)
                {
                    result.Message = "The \"Building001\" device does not exist.";
                    result.Code    = -1;
                }
            }
            catch (Exception ex)
            {
                //result.Passed = false;
                result.Code    = ex.HResult;
                result.Message = $"Error: {ex.Message}";
            }
            return(result);
        }
Esempio n. 7
0
        public async Task <EvaluationResult> DeviceTwin(DeviceWebAPIParameters parms)
        {
            // Update, default pass/fail state is now false, will only become true after complete success. 2021/05/12 - Anders Grasdal
            var result = new EvaluationResult {
                Code = 0, Message = "Received a command", Passed = false
            };

            try
            {
                var iotReg            = new IotUtilities.IotRegistry(parms.IotConnection);
                var connectionStrings = await iotReg.GetTwinsConnectionString();

                var retries = 0;
                var retry   = true;
                var found   = false;
                //Added basic retry pattern
                while ((retry) && (retries < 3))
                {
                    try
                    {
                        //Iterate over each device twin connection string until the one for Building001 is found, then perform tests against that.
                        //2021/05/12 - Anders Grasdal
                        foreach (var deviceConnectionString in connectionStrings)
                        {
                            if (deviceConnectionString.Contains("DeviceId=Building001"))
                            {
                                found = true;
                                var client = DeviceClient.CreateFromConnectionString(deviceConnectionString, s_transportType);
                                if (client == null)
                                {
                                    throw new ArgumentException("Failed to create a device client");
                                }
                                Twin twin = await client.GetTwinAsync().ConfigureAwait(false);

                                if (twin.Properties.Desired.Contains("sample"))
                                {
                                    dynamic prop = twin.Properties.Desired["sample"];
                                    result.Message = $"The \"sample\" property of Building001 is: \"{prop}\"";
                                    TwinCollection reportedProperties = new TwinCollection();
                                    reportedProperties["sample"] = prop;
                                    try
                                    {
                                        await client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
                                    }
                                    catch
                                    {
                                        //Ignore error here.  This is a write operation and it fails if the value already exists.
                                    }
                                    result.Passed = true;
                                }
                                else
                                {
                                    result.Message = $"The \"sample\" property does not exist for the Building001 device.";
                                    //result.Passed = false;
                                    result.Code = -1;
                                }
                                break;
                            }
                        }


                        if (!found)
                        {
                            result.Message = "The \"Building001\" device does not exist.";
                            result.Code    = -1;
                        }
                        retry = false;
                    }
                    catch (Exception inner)
                    {
                        retries++;
                        if (retries < 3)
                        {
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            //result.Passed = false;
                            result.Code    = inner.HResult;
                            result.Message = $"Error: {inner.Message}";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //result.Passed = false;
                result.Code    = ex.HResult;
                result.Message = $"Error: {ex.Message}";
            }
            return(result);
        }
 public StreamAnalyticsContext(DeviceWebAPIParameters parms)
 {
     storage    = new StorageContext(parms.EhubStorage, "averages", null);
     this.parms = parms;
 }
Esempio n. 9
0
        public async Task <EvaluationResult> DeviceTwin(DeviceWebAPIParameters parms)
        {
            var result = new EvaluationResult {
                Code = 0, Message = "Received a command", Passed = true
            };

            try
            {
                var iotReg            = new IotUtilities.IotRegistry(parms.IotConnection);
                var connectionStrings = await iotReg.GetTwinsConnectionString();

                var retries = 0;
                var retry   = true;
                //Added basic retry pattern
                while ((retry) && (retries < 3))
                {
                    try
                    {
                        var client = DeviceClient.CreateFromConnectionString(connectionStrings[0], s_transportType);
                        if (client == null)
                        {
                            throw new ArgumentException("Failed to create a device client");
                        }
                        Twin twin = await client.GetTwinAsync().ConfigureAwait(false);

                        if (twin.Properties.Desired.Contains("sample"))
                        {
                            dynamic prop = twin.Properties.Desired["sample"];
                            result.Message = prop;
                            TwinCollection reportedProperties = new TwinCollection();
                            reportedProperties["sample"] = result.Message;
                            try
                            {
                                await client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
                            }
                            catch
                            {
                                //Ignore error here.  This is a write operation and it fails if the value already exists.
                            }
                        }
                        else
                        {
                            result.Message = $"The \"sample\" property does not exist for the {twin.DeviceId} device.";
                            result.Passed  = false;
                            result.Code    = -1;
                        }
                        retry = false;
                    }
                    catch (Exception inner)
                    {
                        retries++;
                        if (retries < 3)
                        {
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            result.Passed  = false;
                            result.Code    = inner.HResult;
                            result.Message = $"Error: {inner.Message}";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Passed  = false;
                result.Code    = ex.HResult;
                result.Message = $"Error: {ex.Message}";
            }
            return(result);
        }
Esempio n. 10
0
 public EventHubContext(DeviceWebAPIParameters parms)
 {
     this.parms = parms;
     storage    = new StorageContext(parms.EhubStorage, tableName, containerName);
 }