0

let's say that I am working with an IoT device. The device can have many sensors, each of them has an address, i/o direction and a reading method.

temperature=d2,input,digital
humidity=d3,input,digital
lcd=a1,output,analog

From my program, I should read this configuration file and start getting data for example from the temperature sensor, which I know is at address d2 and provides digital input. How can I abstract this? I mean, I can't have something like

if(direction == input) {
    if(type == digital) {
        readDigital(d2)
    } else {
        readAnalog(d2)
    }
} else {
    if(type == digital) {
        writeDigital(d2)
    } else {
        writeAanalog(d2)
    }
}

This is absolutely not abstract stuff, and it kinda sucks. How can I connect my configuration to my code?

1
  • 2
    Why can't you have something like that? Depending on what you need to do with sensors, the code you wrote could be the perfect solution. Keep in mind that your code doesn't necessarily need to reflect the relationship between analog inputs, digital inputs, analog outputs and digital outputs; the best solution may be to treat those four concepts as though they were completely unrelated. – Tanner Swett Jun 7 '17 at 23:14
3

This looks like a job for polymorphism!

Stream temperature = new Input(  new DigitalProtocol( new Address(d2) )  );
Stream humidity = new Input(  new DigitalProtocol( new Address(d3) )  );
Stream lcd = new Output(  new AnalogProtocol( new Address(a1) )  );

Create whatever interfaces you need to make that legal and useful and you're on your way. Notice how adding Bidirectional is no longer traumatic?

3
  • Polymorphism is great! Good suugestion – Rhys Johns Jun 7 '17 at 22:57
  • What if I need to instantiate these streams dinamically? In the example I have those sensors in the configuration, but I could have more and not know which... – user274721 Jun 8 '17 at 7:48
  • The code I offered is dynamic. Stick it in any creational pattern you like. If you mean you have your heart set on using a configuration file you just need a parser that knows how to build these streams. Heck you can use some dependency injection framework to do it if you're really hell bent on using a configuration file. It's the main thing they offer anyway: construction code moved into another language like xml or json. – candied_orange Jun 8 '17 at 8:00
1

I would suggest an abstraction like so: enter image description here

Inspiration taken from Java's InputStream and OutputStream.

You can tailor InputSensor and OutputSensor to your needs. I added Stream because from your example, you're treating input and output streams the same, but you may not need it.

On the left side of the diagram is the method that the sensors will be constructed from configuration using the abstract factory pattern. DefaultSensorFactory would take the configuration, and construct AddressedInputSensor and AddressedOutputSensor form the description in your question.

The benefits of this design are that you can test each part independently of each other by mocking each interface, and you can use sensors in an abstract way.

2
  • 1
    Do you think it could be a little more complex? – Frank Hileman Jun 7 '17 at 21:52
  • Which part is complex Frank? I would be happy to explain it more. – Samuel Jun 7 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy