This is more of a programming question and it can apply to any good object orientated programming language - Which are most modern full stack languages
I suggest finding and reading something about "Good Object Orientated Programming"
to help you avoid basic mistakes and broaden your understanding of the
options available.
There are many ways to map values to each other. I will provide you with and abstract code class, that I (a professional .NET Developer) would use.
To create a lookup method
- a static (one instance in entire program)
- dictionary "like" (not a real dictionary but the only way to have compile time defined set of constants which is extremely fast for looking up values after compilation)(you can only change these values by recompiling - if you need dynamic lookup then using a Dictionary or Hashset is the preferred way)
So a basic lookup code would look like this
public static string GetValueByGPIO(int PinNumber)
{
switch (PinNumber)
{
case 5: return "Door";
case 6: return "Windows";
default: return "NA";
}
}
You can call that method from anywhere in your program due to it being marked as static - That is a very simple example and you can actually return classes ready to be consumed by your database saving code
So something like this is more reusable and follows basic SOLID principles
public class SensorStateEntity
{
public int PinNumber { get; set; }
public string Name { get; set; }
public bool IsOpen { get; set; }
}
public static SensorStateEntity GetValueByGPIO(int PinNumber, bool state)
{
switch (PinNumber)
{
case 5: return new SensorStateEntity() { Name = "Door", PinNumber = PinNumber, IsOpen = state };
case 6: return new SensorStateEntity() { Name = "Windows", PinNumber = PinNumber, IsOpen = state };
default:
return new SensorStateEntity() { Name = "NA", PinNumber = PinNumber, IsOpen = state };
}
}
And unless you are hitting some kind of performance issue there is absolutely no reason to micromanage the code in terms of is this the most efficient or not. C# compiler and the IL (Intermediate Language) does a fantastic job at super optimising code (a team of highly skilled programmers out numbering the skills of entire stack exchange made sure of that) - So things like caching static lookups, managing memory, efficiency is all done as best as possible during compile.