How to implement arrays of events and listeners

11 visualizaciones (últimos 30 días)
Jon
Jon el 22 de Jun. de 2023
Comentada: Jon el 22 de Ag. de 2023
I would like to be able to apply the event - listener methodology to obtain notification when specified, individual elements of an array of values are changed. It seems that there is no built in support for arrays of event, the events for a class are just specified by individual names. If anyone has an idea for a good way to have a listener be notified when an individual array element (property of the class) is changed I would appreciate it. Additional details, for context are below.
The situation is the following.
I am implementing a prototype of a practical industrial control system using MATLAB.
I have multiple identical machines identified by their machine number 1,2,3... numMachines. Each machine is equipped with sensors at identical locations identified by sensor ID's 1,2, ... numSensors. So one can imagine the sensor readings, e.g. temperature values, being held in a numSensor by numMachine array.
The individual sensor values are updated (changed) occassionaly at unpredictable (non-periodic) intervals.
Each machine has numControllers controllers. Each of these controllers is interested in a particular subset of the sensor values. The controllers for each machine are identical. So for example the controller 2 for machine 3 might be interested in the measurements for sensor 2,8 and 11 of machine 3. Controller 2 for any other machine will be interested in the same set of sensor values but for the corresponding machine.
So what I would like to do (functionally), is have each controller listen for changes to the sensor values it is interested in. That is, changes in a particular machine number and sensor number. On the other side, when ever a value for a particular machine and sensor changes a notification should be made by the corresponding event.
The basic capability to do this seems to be available with MATLAB event-listener mechanism. The difficulty I am having is how to implement this for an array of events (e.g. numSensor by numMachine array). Typically 30 sensors by 12 machines. I only see the ability to define events by individual names, e.g. event1, event2 etc. I don't see anyway to define an array of events for this purpose. Of course I could include the machine number and sensor id in the event names, for example event_m03s29 for machine 3 sensor 29. This seems like generally bad practice, and would at the least be very tedious to code for the dimensions of intererst.
As an alternative, I could just blast out an event notification to all of the controllers, with a subclassed event data to pass the machine id and sensor id, and value for the changed value. Then each controller could look and see if it were interested. It seems inefficient though, to have everyone receive notifications, which usually are not of interest.
If anyone has suggestion on a good way to implement the functionality I have described I would appreciate any ideas.
  1 comentario
Jon
Jon el 23 de Jun. de 2023
Edited question to provide high level summary at top of question

Iniciar sesión para comentar.

Respuestas (1)

Manoj Mirge
Manoj Mirge el 17 de Ag. de 2023
The array of event listeners in MATLAB can be implemented using dynamic properties on the instance of class. You may create the instance of class and add the different attributes representing the sensors of machine programmatically.
Please refer to the attached link to add dynamic properties to the class:
Then you can set the “SetObservable” property of these dyanamic properties as “true” and attach the listeners for the “PostSet” event for the dynamic properties.
Kindly refer to the below code to create and attach listeners to dynamic properties in MATLAB:
classdef MyClass < dynamicprops
properties
end
methods
function obj = first(NumberOfMachines,NumberOfSensors)
for i=1:NumberOfMachines
for j=1:NumberOfSensors
Prop="Machine_"+i+"Sensor_"+j;
DynamicProp=obj.addprop(prop);
DynamicProp.SetObservable=true;
addlistener(obj,Prop,'PostSet',@MyClass.handleSensorChange);
end
end
end
methods (Static)
function handlePropEvents(src,evnt)
switch src.Name
case 'Machine_1_Sensor_1'
% The value of sensor 1 of Machine 1 has been changed
case 'Machine_1_Sensor_2'
% The value of sensor 2 of Machine 1 has been changed
% So on for rest of the properties
end
end
end
end
Hope this helps.
  1 comentario
Jon
Jon el 22 de Ag. de 2023
Thank you for your suggestion. I am not yet fully understanding the role of the dynamic properties. I will review that some more. Nevertheless, it still does not seem that we are truly able to have a arrays of events and listeners as we are instead building string names that contain the array indexing information. Then we have to tediously build a long switch case statement to sort things out by source name.
This would be analogous to using variable names like A12, A23, A45, instead of making an array A, and then indexing it as A(1,2), A(2,3), A(4,5). Certainly using variable names like A12, A23 instead of indexing is very poor MATLAB coding practice. Here we have a similar situation with handling the event names, by including the indexing in the name.
I guess this may just be a limitation of the MATLAB Object Oriented functionality, but I would like a true array based approach, just like everything else in MATLAB.

Iniciar sesión para comentar.

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by