As I move forward with Inheritance through sub-classing I run into another question. I have many sensors which are subclass’s of TempSensor and its serialConnection class. I would like to store a parameter to use for averaging the sensor values. Since the parameter in the TempSensor Class for each sensor is reset on each iteration what might be a good approach for permanent storage. Array outside of the superclass in a module, maybe a dictionary class, or is there a better approach for this task taking advantage of OOP? I was hoping to encapsulate as much as possible.
You can store the parameter as a shared property of the class (right-click a property->convert to shared).
Shared properties behave like module properties but exist within the namespace of the class.
That did make it persistent but all the sensors seem to be using the same single average. I was trying to maintain an average for each sensor that was sub classed from TempSensor. The shared property is in TempSensor. Would that be what you would expect?
Yes that’s what I expect. I was thinking instead of putting an array property in a module outside the class make it shared and put it inside the class.
I think this tell me why it won’t work in my case.
A shared property (sometimes called a Class Property) is like a “regular” property, except it belongs to the class, not an instance of the class.
I’ll give that a try with a dictionary.
The typical approach would be to store the sensor readings in a database and pull the average from there. The table would look like
SensorName ReadingDateTime Value
Then you could grab all the values for a date/time range and average them.
as i understood the base class is tempsensor and the instance is as example kitchensensor.
for a average temperature you need a history of some values, you could add a value to a array and remove the first if you have more than a few values. based on this array you can get your average value.
TempSensor could have a Property TemperatureHistory() As Double
And .TempValue = could be a computed property that add a value to TemperatureHistory too.
With a Method GetAverageTemperature you add all data in TemperatureHistory together and divide it by its count of entries.
If the sensor instance is persistent for each measurement you could try a static array inside the temperature measuring method within the sensor instance, or just add an property array attached to the sensor instance. Keep the array to a limited size to do a rolling average. If you are creating a new instance for each measurement then you’ll need something to identify the particular sensor being recorded. You could use that in a dictionary of arrays in a shared property - with each array representing a different sensor.
you could also create a AverageValue class and add this as property to a sensor.
somehow better to use if things are separated.
You are thinking on a similar line as me but taking it a step further. I am now using a dictionary but I had not thought of an array of sensor readings stored in the shared dictionary. I haven’t used that technique yet. Sounds like a reasonable approach. So far, to smooth out my temperature readings, I have been taking the newest reading and giving it a weight of 1/10 like this:
var SensorAvgFactor as integer = 10
sensor_Avg = ((sensor_Avg * SensorAvgFactor)+ NewValue) / (SensorAvgFactor+1)
The original question was, how do I keep a persistent property for each sensor to use for storing an average and still be encapsulated. I do have a sensor name assigned by a construct for each sensor instance. This works great for the dictionary. I’m trying to get out of the habit of linear programming.
I’m going to have to chew on that one since it’s a little abstract for me at this point. Would this class be attached to each sensor or the super class of sensor? Attached how?
I am now using a dictionary to store sensor averages, but if the dictionary is stored in the sensor’s super preferences and it is declared shared, it is not persistent. I can not declare Dictionary_Temperature_Avg = New Dictionary from outside of the class if the property is in the class and if I do so in the class it becomes a new dictionary with no values on each sensor read operation.
How is it not persistent? Are you creating a new sensor object for every reading?
i see the super class as base class which have functionality for all sub classes, or properties that share all sub classes as example a id. if all sensors have a average value then in the superclass / base class as property.you can create an instance in the constructor method.it make always sense to prevent duplicate source code. the problem with the dictionary is this dictionary did not have a get average value method.
you could extend this dictionary with a special method but it would not make sense there. with a dictionary you will need a method that process the dictionary content to get a average value. i think a simple class is better for this task.
Yes, I’m creating a new sensor object for each sensor and that is why we placed a construct in each subclass, giving it a sensor name which also helps for a key in the dictionary. I was thinking that was the direction I need to head to better utilize OOP. Are you thinking this might be overkill?
Let’s say you have 16 temperature sensors that needed to be managed. They need to acquire their values through a serial connection and you want to create averaging to smooth out their values. You would also need to pull these avg sensor values from any part of your program as needed. What would be your approach?
now i know why you have headache to get average values from something that not exists
if me would have 10 sensors that get read each minute i would create them once and put them all into a list.
i would use a for each sensor in sensors
sensor.read to update all together (if that is possible in “realtime”)
for analyses each day,week,month,year i would use a database what tim mentioned.
This would take me back to square one which I did not feel was OOP in nature. That is what I originally did and I also placed this operation and many other serial read operations in a thread to free up the UI. Values were stored in a dictionary on each cycle. Duplication of code is not a problem, I have little although very little is private. I’m just trying to get a better grasp of when it’s best to use sub classing, inheritance, and encapsulation.
A sensor object for each temperature sensor is correct. But they need to be persistent, meaning you should have an array of them in your global space. That is still OOP. Each sensor object would have its own properties, such as average temperature. Those values should persist through the execution of your app. The fact that you said they don’t persist led me to wonder if you weren’t keeping the sensor objects around.
at least as long you need them,
you could also memory each temp sensor measurement object into a list for calculating the average.
obj instance 1 temp 10
obj instance 2 temp 15
obj instance 3 temp 20
obj instance 4 temp 25
and if a new measurement is necessary thow the first object away and put the new in.
you would use a method GetAverage(list)
in the same way you can draw a line chart.