Optimizing our server

So it looks like we got our server working with the sensor readings and everything... great! However, there are a few issues with our implementation that we need to handle. Looking at the server code from the previous example, we can note the following points:

  • We are repeating a lot of code; not only does this make our code look bad, but it also makes it less malleable. What if we want to change our sensor pin number from 4 to 6? We need to change the code in two places. Even worse, what if we decide to change the sensor or even the way in which we get sensor readings in the future?
  • We are making calls to the native library every time someone hits the API. This is fine when we are testing it on our local machine, but what if we had multiple users hitting our API at the same time? It is highly inefficient to make so many calls to the native library, especially if the information we are getting is going to be the same. (If you recall, our sensor's sampling rate is one in every 2 seconds).
  • We are obtaining unneeded information. The sensor gives us readings for both the temperature and humidity at the same time (by design). The way we have separated our APIs for temperature and humidity means that we do not require the humidity information in the temperature API and vice versa.

Fortunately, all three problems are resolvable. Let's tackle them one at a time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.33.87