Home > Software engineering >  Execute different pieces of code based on a value in C#
Execute different pieces of code based on a value in C#

Time:01-18

I have a piece of C# code that reacts to an HTTP trigger from Azure and gets executed. The piece of code sends an email containing a warning:

#r "Newtonsoft.Json"
#r "SendGrid"
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static SendGridMessage Run(HttpRequest req, ILogger log)
{
    string requestBody = new StreamReader(req.Body).ReadToEnd();
    log.LogInformation(requestBody);
    var notifications = JsonConvert.DeserializeObject<IList<Notification>>(requestBody);

    SendGridMessage message = new SendGridMessage();
    message.Subject = "Sensor Anomaly Warning";

    var content = "The following device has started registering anomalies:<br/><br/><table><tr><th>time</th><th>value</th><th>lower bound</th><th>upper bound</th><th>device</th></tr>";
    foreach(var notification in notifications) {
        log.LogInformation($" -  time: {notification.time}, value: {notification.value}, lowerbound: {notification.lowerbound}, upperbound: {notification.upperbound}, device: {notification.device}");
        content  = $"<tr><td>{notification.time}</td><td>{notification.value}</td><td>{notification.lowerbound}</td><td>{notification.upperbound}</td><td>{notification.device}</td></tr>";
    }
    content  = "</table>";
    message.AddContent("text/html", content);  

    return message;
}

public class Notification
{
    public string time { get; set; }
    public string value { get; set; }
    public string lowerbound { get; set; }
    public string upperbound { get; set; }
    public string device { get; set; }
}

Now, I want to execute this same piece of code that sends an email, but based on the value of notification.alert which stores a zero if an anomaly started and a 1 if the alert stopped. Coming from python, this would be as easy as setting an "if else" statement, where the function is called in each case.

However for this C# code, there is no function to call. The piece of code sends an email but it's just creating a class. In any case, I'm wondering if I can use a "if else" statement in C# based on the value of notification.alert that in one case sends an email saying something like "the device has started registering anomalies" and in the other "the device has stopped registering anomalies". I just can't get to do this, as I have to address the notification object, which is already inside the class.

I must say that I am not a C# developer but a Python developer, hence the doubts.

CodePudding user response:

You can use if/else statement or switch to update the email body based on notification.alert. this function is not sending the email it's just setting up a message for the email body text.

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static SendGridMessage Run(HttpRequest req, ILogger log)
{
    string requestBody = new StreamReader(req.Body).ReadToEnd();
    log.LogInformation(requestBody);
    var notifications = JsonConvert.DeserializeObject<IList<Notification>>(requestBody);

SendGridMessage message = new SendGridMessage();
message.Subject = "Sensor Anomaly Warning";

var content = "The following device has started registering anomalies:   <br/><br/><table><tr><th>time</th><th>value</th><th>lower bound</th><th>upper bound</th><th>device</th></tr>";
foreach(var notification in notifications) {
    log.LogInformation($" -  time: {notification.time}, value: {notification.value}, lowerbound: {notification.lowerbound}, upperbound: {notification.upperbound}, device: {notification.device}");
    content  = notification.alert == 0 ? "the device has started registering anomalies" : "the device has stopped registering anomalies";
}
content  = "</table>";
message.AddContent("text/html", content);  

return message;
}

public class Notification
{
    public string time { get; set; }
    public string value { get; set; }
    public string lowerbound { get; set; }
    public string upperbound { get; set; }
    public string device { get; set; }
    public int alert { get; set;}
}
  • Related