0

I'm designing a system that manages multiple hardware boards. Each board has a class that provides core functionality, such as:

  • Device enumeration
  • Capability enumeration (power management, polling, hardware info, etc.)
  • Restart
  • hardware information like serial number

Now I want to introduce a Maintenance mode.

Requirements for Maintenance mode:

  • Software upgrades are only allowed when the board is in this state.
  • LEDs on the board should switch to a maintenance configuration.
  • Active alarms should be suppressed.

I see two ways of implementing this:

  1. Inside the board class so that in clasd I would have a method called set_maintenance_mode that would suppress alarms and *turn on LEDs

  2. Outside board class as a result of the MaintenanceStateEnabled event raised by the board

I'm inclined to go for 2nd solution, especially in case of alarms, but on the other hand, setting LEDs as a response for entering a maintenance mode is kinda a hardware thing, so this suggests that perhaps I should do this directly in a class representing hardware.

What do you think? How to decide what should be an action that happens directly when some method is called (eg, set maintenance mode configures the LED) and what should be like a side effect handled by an event handler (eg, Maintenance Mode Event handler changes the configuration of the LED)

8
  • I think the crucial detail is: what initiates the transition into maintenance mode? Is this something a human does by pressing a button, or activating a switch? Is this done once for each board, or is there some central control hub that broadcasts this message to all the boards? Commented Sep 11 at 20:39
  • It can be triggered by both, user interface (web "virtual" button) and physical button Commented Sep 11 at 21:02
  • Is your code running on the board or externally? Commented Sep 11 at 21:03
  • 1
    Please edit your post to include this information. Responding as a comment is too easily lost when other people read your question. Commented Sep 11 at 21:03
  • @bielu000 Does each board have the same type/number of alarms and/or LEDs? Or is there a different set or alarms/LEDs per board type? Commented Sep 11 at 21:30

1 Answer 1

3

A choice between a direct call and an event depends:

  1. On the ownership,
  2. On the number of potential listeners—and more precisely their dynamic nature,
  3. On the importance of the order in which the event must be treated.

Basic example. I have a vector containing something. Whenever the elements get added or removed to and from this vector, something may need to react to that. For instance, if the elements of this vector are being shown to the user through a GUI, the visual interface needs to be notified about the changes. If the vector contains sensitive financial operations, the audit system should be notified.

The vector doesn't own the GUI or the audit system—it may be a template vector that doesn't even know what is exactly the information it stores. Moreover, the objects interested in receiving the changes to a given vector may change over time. For instance, a change in the financial operations may also trigger a replication of the data to an off-site database.

Events is the way to go here.

A different example. An OCR needs to convert a scanned document (a bunch of images) into a PDF file with actual text.

In order to do that, the OCR needs to be able to read the .jpg files. It needs to convert them to bitmaps. Then, it needs to analyze the actual image, extract text, recreate the PDF and save it to a file.

While the steps could change over time (for instance instead of converting JPEG to bitmaps, it might use a different library that will be happy to have JPEG as input directly), there is still a strong ownership here: it belongs to the OCR to know which components to call in which order.

Direct calls are therefore a correct choice here.

In your case, I would be inclined to think that one could use events. The board just informs whoever interested that it is going into maintenance mode. Zero or more components may react to that, in arbitrary order. For instance, the LEDs could start blinking orange. Or the logger may switch to debug mode. Or a given service may stop. Or the fans may start spinning at full speed.

Things would have been different if you were facing a maintenance process, as opposed to maintenance mode. A maintenance process would be a suite of ordered operations. For instance, in order to update the firmware of the board, one needs to log a message, spin the fans at full speed, switch LEDs to blinking mode, write a file to a special location, and request a reboot. Here, the maintenance process owns all those steps—instead of an event, it would actually call the system that controls the LEDs or the fans.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.