I am looking for alternative ideas of how to implement the following situation (.NET Core, C#)
Having multiple sensors that communicate over a RS485 interface with the host. Each sensor is implemented using an generic
ISensor interface (All sensor types have a certain function which must be implemented over the
ISensor interface) There are all kinds of sensors that communicate over varying channels with the host, TCP, Serial, USB, custom hardware, etc. This class of sensors is somehow special in that they share the same communication channel, which can be opened only once.
This special group of sensors use the RS485 serial port to communicate, each sensor has an id which is included in every data packet sent or received. So when I send a data packet with an address of “1” all sensors on this port will receive it but only the one with address “1” will react and possibly respond.
Only one logical connection over the serial port can be opened to talk to all sensors and receive data from all. Therefore I must have a central point of distribution from where all inputs are dispatched and to which all output is sent. From the point of the using app, it only talks to the
ISensor interface and doesn’t care how the sensor communicates, if anyhow. (many other sensors already implemented are simple in that they own their connection)
Every interaction with a sensor contains a data structure with an corresponding answer data structure (or in case of failure an error data structure identical for all errors)
I am wondering in what OO pattern to implement this scenario, a few come to my mind but none satisfies me too much.
The app creates, lets say 3 instances of the same sensor (configuration tells that 3 are connected), configures them by using something like
ISensor.Setup() and then
ISensor.Start() after that, each of the sensors starts to send in data periodically.
Now I have already an protocol class which does the parsing of all communication data, independent of sensor ID or underlying communication channel.
My idea was to have an queue where all incoming data is arranged and the sensors subscribe to it and pick out the data for them ( the subscription may already filter them ). Outgoing data should be also queued from all sensors. Not sure now where to plug in the protocol parser and the communication class.
Its kinda related to Controlling a sensor network which didnt get any useful answers though.
Bandwidth is not so much a problem as there are packets only every 100ms incoming, from up to 8 sensors per serial port.