Пример #1
0
        async Task <Tuple <string, string> > CreateBlockBlobAsync(string accountName, string containerName)
        {
            string rval             = string.Empty;
            string credentialString = string.Empty;
            // Construct the blob container endpoint from the arguments.
            string containerEndpoint = $"https://{accountName}.blob.core.windows.net/{containerName}";

            // Get a credential and create a client object for the blob container.
            var credential = new DefaultAzureCredential();

            credentialString = credential.ToString();
            BlobContainerClient containerClient = new BlobContainerClient(new Uri(containerEndpoint),
                                                                          credential);

            try
            {
                // Create the container if it does not exist.
                await containerClient.CreateIfNotExistsAsync();

                // Upload text to a new block blob.
                string blobContents = "This is a block blob.";
                byte[] byteArray    = Encoding.ASCII.GetBytes(blobContents);

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    var blobName = System.Environment.TickCount.ToString() + ".txt";
                    await containerClient.UploadBlobAsync(blobName, stream);

                    rval = blobName;
                }
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
            return(new Tuple <string, string>(rval, credentialString));
        }
Пример #2
0
        public async void Run([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log)
        {
            try
            {
                // After this is deployed, you need to turn the Managed Identity Status to "On",
                // Grab Object Id of the function and assigned "Azure Digital Twins Owner (Preview)" role
                // to this function identity in order for this function to be authorized on ADT APIs.
                //Authenticate with Digital Twins
                var credentials = new DefaultAzureCredential();
                log.LogInformation(credentials.ToString());
                DigitalTwinsClient client = new DigitalTwinsClient(
                    new Uri(adtServiceUrl), credentials, new DigitalTwinsClientOptions
                {
                    Transport = new HttpClientTransport(httpClient)
                });
                log.LogInformation($"ADT service client connection created.");
                if (eventGridEvent.Data.ToString().Contains("Alert"))
                {
                    JObject alertMessage = (JObject)JsonConvert.DeserializeObject(eventGridEvent.Data.ToString());
                    string  deviceId     = (string)alertMessage["systemProperties"]["iothub-connection-device-id"];
                    var     ID           = alertMessage["body"]["TurbineID"];
                    var     alert        = alertMessage["body"]["Alert"];
                    log.LogInformation($"Device:{deviceId} Device Id is:{ID}");
                    log.LogInformation($"Device:{deviceId} Alert Status is:{alert}");

                    var updateProperty = new JsonPatchDocument();
                    updateProperty.AppendReplace("/Alert", alert.Value <bool>());
                    updateProperty.AppendReplace("/TurbineID", ID.Value <string>());
                    log.LogInformation(updateProperty.ToString());
                    try
                    {
                        await client.UpdateDigitalTwinAsync(deviceId, updateProperty);
                    }
                    catch (Exception e)
                    {
                        log.LogInformation(e.Message);
                    }
                }
                else if (eventGridEvent != null && eventGridEvent.Data != null)
                {
                    JObject deviceMessage = (JObject)JsonConvert.DeserializeObject(eventGridEvent.Data.ToString());
                    string  deviceId      = (string)deviceMessage["systemProperties"]["iothub-connection-device-id"];
                    var     ID            = deviceMessage["body"]["TurbineID"];
                    var     TimeInterval  = deviceMessage["body"]["TimeInterval"];
                    var     Description   = deviceMessage["body"]["Description"];
                    var     Code          = deviceMessage["body"]["Code"];
                    var     WindSpeed     = deviceMessage["body"]["WindSpeed"];
                    var     Ambient       = deviceMessage["body"]["Ambient"];
                    var     Rotor         = deviceMessage["body"]["Rotor"];
                    var     Power         = deviceMessage["body"]["Power"];

                    log.LogInformation($"Device:{deviceId} Device Id is:{ID}");
                    log.LogInformation($"Device:{deviceId} Time interval is:{TimeInterval}");
                    log.LogInformation($"Device:{deviceId} Description is:{Description}");
                    log.LogInformation($"Device:{deviceId} CodeNumber is:{Code}");
                    log.LogInformation($"Device:{deviceId} WindSpeed is:{WindSpeed}");
                    log.LogInformation($"Device:{deviceId} Ambient Temperature is:{Ambient}");
                    log.LogInformation($"Device:{deviceId} Rotor RPM is:{Rotor}");
                    log.LogInformation($"Device:{deviceId} Power is:{Power}");
                    var updateProperty   = new JsonPatchDocument();
                    var turbineTelemetry = new Dictionary <string, Object>()
                    {
                        ["TurbineID"]    = ID,
                        ["TimeInterval"] = TimeInterval,
                        ["Description"]  = Description,
                        ["Code"]         = Code,
                        ["WindSpeed"]    = WindSpeed,
                        ["Ambient"]      = Ambient,
                        ["Rotor"]        = Rotor,
                        ["Power"]        = Power
                    };
                    updateProperty.AppendAdd("/TurbineID", ID.Value <string>());

                    log.LogInformation(updateProperty.ToString());
                    try
                    {
                        await client.PublishTelemetryAsync(deviceId, Guid.NewGuid().ToString(), JsonConvert.SerializeObject(turbineTelemetry));
                    }
                    catch (Exception e)
                    {
                        log.LogInformation(e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                log.LogInformation(e.Message);
            }
        }