0

hello i have a few classes and i have problem looping through some of the classes to get the values.

eg, i want to know which sensors are having issues. sensors comprises of lidar, imu, camera1234. i want so check their status and if their status is not 'good' then i need to say 'lidar not good'.

ways that i have thought of:

  1. list of classes? but what is the 'Type' for classes?
  2. using Root class, access the other classes from it, similar to how we do for iterating through properties in classes. but how do i do this?

    public class Battery
    {
        public string status { get; set; }
        public string value { get; set; }
    }

    public class Camera1
    {
        public string status { get; set; }
    }

    public class Camera2
    {
        public string status { get; set; }
    }

    public class Camera3
    {
        public string status { get; set; }
    }

    public class Camera4
    {
        public string status { get; set; }
    }

    public class Cpu
    {
        public string status { get; set; }
        public string value { get; set; }
        public string temperature { get; set; }
        public string ram { get; set; }
        public string utilisation { get; set; }
    }

    public class CurrentJob
    {
        public object job { get; set; }
        public object task { get; set; }
        public object location { get; set; }
    }

    public class Dex
    {
        public CurrentJob current_job { get; set; }
        public Battery battery { get; set; }
        public Gpu gpu { get; set; }
        public Cpu cpu { get; set; }
        public Lidar lidar { get; set; }
        public Camera1 camera1 { get; set; }
        public Camera2 camera2 { get; set; }
        public Camera3 camera3 { get; set; }
        public Camera4 camera4 { get; set; }
        public Imu imu { get; set; }
    }

    public class Gpu
    {
        public string status { get; set; }
        public string value { get; set; }
        public string memory { get; set; }
        public string utilisation { get; set; }
    }

    public class Imu
    {
        public string status { get; set; }
    }

    public class Lidar
    {
        public string status { get; set; }
    }

    public class Root
    {
        public Dex dex { get; set; }
    }

this is my json


[
    {"dex":
        {
        "current_job":
            {"job":null, "task": null, "location": null},
        "battery": 
            {"status": "good", "value": "100"},
        "gpu": 
            {"status": "bad", "value": "97", "memory":"60", "utilisation":"83.3"},
        "cpu":
            {"status": "good", "value": "32", "temperature":"37", "ram":"31.5", "utilisation":"26"},
        "lidar": 
            {"status": "good"}, 
        "camera1": 
            {"status": "good"},
        "camera2": 
            {"status": "good"},
        "camera3": 
            {"status": "error"},
        "camera4": 
            {"status": "good"},
        "imu": 
            {"status": "good"}
        }
    }
]

after implementing advice from JEremey, this is my updated classes

using System.Text;
using Microsoft.Bot.Builder;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;

namespace CoreBot.CognitiveModels
{
    public class Battery
    {
        public string status { get; set; }
        public string value { get; set; }
    }

    public class Camera1 : SensorComponents
    {
        public string status { get; set; }
    }

    public class Camera2 : SensorComponents
    {
        public string status { get; set; }
    }

    public class Camera3 : SensorComponents
    {
        public string name = "CPU";
        public string status { get; set; }
    }

    public class Camera4 : SensorComponents
    {
        public string status { get; set; }
    }

    public class Cpu
    {
        public string name = "CPU";
        public string status { get; set; }
        public string value { get; set; }
        public string temperature { get; set; }
        public string ram { get; set; }
        public string utilisation { get; set; }
    }

    public class CurrentJob
    {
        public object job { get; set; }
        public object task { get; set; }
        public object location { get; set; }
    }

    public class Dex
    {
        public CurrentJob current_job { get; set; }
        public Battery battery { get; set; }
        public Gpu gpu { get; set; }
        public Cpu cpu { get; set; }
        public Lidar lidar { get; set; }
        public Camera1 camera1 { get; set; }
        public Camera2 camera2 { get; set; }
        public Camera3 camera3 { get; set; }
        public Camera4 camera4 { get; set; }
        public Imu imu { get; set; }
    }

    public class Gpu
    {
        public string status { get; set; }
        public string value { get; set; }
        public string memory { get; set; }
        public string utilisation { get; set; }
    }

    public class Imu : SensorComponents
    {
        public string status { get; set; }
    }

    public class Lidar : SensorComponents
    {
        public string status { get; set; }
    }

    public class Root
    {
        public Dex dex { get; set; }
    }

    public class SensorComponents
    {
        public string name { get; set;}
        public string status { get; set;}
    }
}
ycyc
  • 33
  • 3
  • Use a dictionary – Jawad Oct 10 '22 at 03:57
  • 1
    not sure if the concept of class was correctly understood here. Wouldn't you want 4 _instances_ (objects) that are "Cameras", hence, of the (one and only) `Camera` class ? And all your `Camera` and `Cpu` and `Battery` could implement a common Interface (like `HasStatus`). Then, you can simply have a collection (lke a List) of references to `HasStatus` objects (`allMyStuff = new List();`and loop over them. The main point of my comment is the _common interface_ part. – Pac0 Oct 11 '22 at 03:38
  • @Pac0 the classes and data are from a json file – ycyc Oct 11 '22 at 03:43
  • @ycyc A JSON is just data, it's not actually a classs model. Your job as a C# developer is also to identify what is the proper classes that could be useful for your program and what you are doing with it. – Pac0 Oct 11 '22 at 03:51

2 Answers2

2

I'll throw you a bone. The classes all share a Status Property so create a base class called

class Device{
    public string name { get; set; }
    public string status { get; set; }
}

Now all your classes can derive from it:

public class Camera1 : Device
{ ]

Then you can have a List<Device> devices = new(); and iterate over that

foreach(var device in devices)
{ 
   if (device.status == "good") 
   {
     System.Diagnostics.Debug.WriteLine(device.Name + " all good");
   }
   else 
   {       
     System.Diagnostics.Debug.WriteLine(device.Name + " not good");
   }
}

It can depend in some scenarios it maybe better to use an Interface:

public interface IDevice{
    string GetName();
    string GetStatus();
}

Implement these in your classes:

public class Camera1 : IDevice
{
    public string GetStatus(){
         return "good";
    }
}

Then you could iterate over anything that implements the Interface (not tested):

List<IDevice> idevices = new List<IDevice>();
idevices.Add(new Camera1());
idevices.Add(new Camera2());
idevices.Add(new Camera3());

foreach(var device in idevices)
{
    System.Diagnostics.Debug.WriteLine(device.GetStatus());
}
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • im still facing issues with the loop. i used ```List components = JsonConvert.DeserializeObject>(json);``` however, it seems that ```components``` only consist of one item. and ``component.name`` and ```component.status``` is ```null```. i have added my json and updated classes in the question – ycyc Oct 12 '22 at 03:29
  • @ycyc you need to deserialize like this `var components = JsonConvert.DeserializeObject(json);` and then you'll hit an error that this Q&A solves: https://stackoverflow.com/q/8513042/495455 – Jeremy Thompson Oct 12 '22 at 06:10
2

Saw that JEremey proposed something good while I was writing, but here is "another bone", in a similar fashion.

Note:

  1. I was more minimal in my interface design (I took care only of the Status part, not even the name).

  2. As per the description of your description, I think something much more detailed is needed for status, even a class for itself. Status seems a complex thing where you want to read / store various information (like a boolean value to tell if is it good or not + a description or what is the issue if any). But below I kepty status as a simple string for a start.

using System;
using System.Linq;
using System.Collections.Generic;

public interface HasStatus
{
    string Status { get; set; }
}

public class Battery : HasStatus
{
    public string Status { get; set; }
    public string Value { get; set; }
}

public class Camera : HasStatus
{
    public string Status { get; set; }
    public int Id { get; set; }
}

public class Cpu : HasStatus
{
    public string Status { get; set; }
    public string Value { get; set; }
    public string Temperature { get; set; }
    public string Ram { get; set; }
    public string Utilisation { get; set; }
}

class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello World");
    
    var allMyStuff = new List<HasStatus>
    {
        new Camera { Id = 111, Status = "Good" },
        new Camera { Id = 200, Status = "Sensor has a problem" },
        new Camera { Id = 304, Status = "Good" },
        new Camera { Id = 467, Status = "Good" },
        new Cpu { Status = "Doesn't run very fast", Ram = "1MB" },
        new Battery { Value = "a lot", Status = "Good" }
    };
    
    int i = 0;
    foreach (var stuff in allMyStuff)
    {
        Console.WriteLine($"Status of object N° {i} of type {stuff.GetType().Name} is '{stuff.Status}'");
        i++;
    }
        
  }
}

You can filter the not-good ones, for instance with :

var allNotGoodStuff = allMyStuff.Where(x => x.Status != "Good");

foreach (var badStuff in allNotGoodStuff)
{
    // Do some display or anything else with each bad stuff here...

}
Pac0
  • 21,465
  • 8
  • 65
  • 74