Assignment 1: Creating a Web Server

Concept

The first assignment for Connected Devices required us to make a basic server using Node.JS and Express. For the server i drew heavily on example code used in class last Tuesday and also used Tom Igoe’s Four Line Server example. Finally the Express.JS routing guide was extremely helpful.

For my server I decided to make a basic remote thermostat that would let a user control the temperature in their apartment as well as see/change the state of the thermostat. i.e whether it is in heat/cold/off state.

The four REST endpoint I utilized are as follows:

1. /state lists the current state of the thermostat (heat, cold, off)
2. /state/(off,heat,cold) inputing any of the three states will change the current thermostat states
3. /temperature lists the current temperature inside of the apartment
4. /temperature/(increase,decrease) will either increase or decrease the current temperature

Issues

My biggest issues with completing the assignment came from my lack of experience with html and getting a better interface/more functionality out of the server. For Example I would like the user to be able to be prompted to manually set the temperature if they like. As I learn more about Node.js/Express and interfacing I know these types of issues will subside.

Code

Below is the code broken down into sections:

Here is a link to the Github Repo

//four line server example created by Tom Igoe
var express = require('express');
var server = express();
server.use('/',express.static('public'));
server.get('/temperature',temperature);
server.get('/temperature/:changeTemp',modifyTemp);
server.get('/state',checkState);
server.get('/state/:newState',changeThermState);
server.listen(8080);

//inital state of the Serverstat
var thermostatState = 'off';
var thermTemp = 64;

/*
CheckState is responsible for checking the current state of the Serverstat(either heat/off/cold) and the current
temperature in Farenheit
*/
function checkState(request, response){
  response.send('the current state is: ' + thermostatState + ', at a temperature of ' + thermTemp + ' degrees Farenheit');
  response.end;
}

/*
changeThermState allows the user to manually change the state of the thermostat when they enter
one of the three preset states
*/
function changeThermState(request, response){
  var newThermState = request.params.newState;
  if(newThermState == 'off' || newThermState == 'cold' || newThermState == 'heat'){
    thermostatState = newThermState;
    response.send('the state has been set to ' + thermostatState);
    }
    else{
      response.send('please only input "off/cold/heat"');
    }
    response.end;
}
/*
temperature sets a random temperature value to the output when the state is changed.
*/
function temperature(request, response){
  if(thermostatState == 'heat'){
    var temp =  Math.floor(Math.random()*(80-70+1)+70);
    if(temp <= 69){
      temp = 73;
    }
  }

  if(thermostatState == 'cold'){
    var temp =  Math.floor(Math.random()*(65-55+1)+55);
  }

  else{var temp =  64;}
  thermTemp = temp;
  response.send('the current temperature inside the apartment is: ' + temp + ' degrees Farenheit');
  response.end;

}
/*
modifytemp allows the user to either increase or decreaase the temperature depending
on their preferences
*/
function modifyTemp(request, response){
  var tempChange = request.params.changeTemp;;

    if(tempChange == 'increase'){
    thermTemp++;
    response.send('the temperature has been increased to ' + thermTemp);
    }

    else if(tempChange == 'decrease'){
      thermTemp--;
      response.send('the temperature has been decreased to ' + thermTemp);
    }
    else{
      response.send('Please input either "increase/decrease"');

    }
    response.end;

}