The problem
Let’s assume the following scenario:
- List of IoT devices (one model?)
- Each list item has (among other fields) a
type
field - Depending on the value in the
type
field, device may report different values (by number and sometimes type)
And I need to save them in the database (related to reporting device, of course).
So for example:
-
type
=1
(thermometer): only one parameter:-
temperature
(INT – max. 99 + comma + two decimal places)
-
-
type
=2
(GPS locator): four parameters:-
lat
,long
(INT – max 999 + comma + 6 decimal places), -
height
(INT – max 99999, without fractional part), -
speed
(INT – max 999 + comma + 2 places).
-
Etc.
These are of course examples, not an actual case.
I need to model such thing in MVC. Assuming (best of both worlds):
- It should be relatively easy to implement (the project behind is way off time),
- It should be done at least semi-professionally (one day a new project can be created based on this code and we don’t want to rewrite 300 classes from scratch, if that happens).
Can you suggest something around the above?
This is not a “homework” or do-it-for-me question. I don’t need a mature, out-of-the-box solution, only guiding into a correct direction.
An attempt to resolve
I am by no means system architect, I don’t know design patterns very well. So the only thing that came to my mind so far was the simplest concept of two models or tables:
-
devices
:id
type
- etc.
-
values
:id
device_id
param_type
value
But this seams very redundant (storing the type in each measurement?).
Plus, it only covers data storage itself takes. It does nothing around “configuration” of these parameters (input validation).
For example, how to guarantee that:
- A device with
type
=1
will report only one parameter (and only of a specific type) - A device with
type
=2
will report exactly four parameters and always of the specified types
And any other attempt will be rejected.
To resolve this, at the very beginning I was thinking about using separate models. But this seems to be another overkill. As there may be hundreds of types of devices. Which would inflict hundreds of such models.
Own research
This question is somewhat similar to this one. Only mine deals with model / data storage and the mentioned one is purely focused on presentation layer (which isn’t a problem for me).
My question is also quite similar to this one. But the mentioned one isn’t answered for three years and I have the feeling that I have come a bit further here.