var SmSc = SmSc || {};

SmSc.Sensor = class Sensor{
    /*
    * Create a sensor instance
    * @param {number} id The id of the sensor, null if not in database
    * @param {string} location The location of the sensor
    * @param {string} type The type of the sensor
    */
    constructor(id, location, type) {
        this.id = id;
        this.location = location;
        this.type = type;
        this.readings = [];
    }

    /*
    * Add the sensor to the database and set the id of the sensor
    * @param {function} callback The callback, will be executed when done
    */
    insert(callback) {
        var sensor = this;
        callback = callback || function(){};
        $.ajax(SmSc.url + "/addSensor.php", {
            method:"GET",
            dataType:"JSON",
            data:SmSc.addCredentials({
                location:this.location,
                type:this.type
            }),
            success:function(response) {
                var nSensor = response.sensor || {};
                sensor.id = nSensor.id;

                var err = response.err || null;

                if(response.err) callback(null, err);
                else callback(sensor, err);
            },
            error:function() {
                console.log(arguments);
                callback(sensor, "the http request failed");
            }
        });
    }

    get reading() {
        return _.last(_.sortBy(this.readings, "dateTime"));
    }

    set reading(value) {
        if(typeof value == "number") {
            var reading = new SmSc.Reading(this, value, 0);
            reading.insert();
            this.readings.push(reading);
        } else {
            var reading = value;
            reading.insert();
            this.readings.push(reading);
        }
    }
}

SmSc.getSensors = function getSensors(callback) {
    $.ajax(SmSc.url + "/getSensors.php", {
        method:"GET",
        dataType:"JSON",
        data:SmSc.credentials,
        success:function(response) {
            var sensors = [];
            for(var x in response.sensors) {
                var object = response.sensors[x].sensor;
                var sensor = new SmSc.Sensor(object.id, object.location, object.type);
                for(var y in response.sensors[x].readings){
                    var object2 = response.sensors[x].readings[y];
                    var reading = new SmSc.Reading(sensor, object2.value, object2.dateTime);
                    sensor.readings.push(reading);
                }
                sensors.push(sensor);
            }
            callback(sensors);
        }
    })
}

@@ -22,22 +22,23 @@
 ;%0A%0ASmSc.
-Sensor
+Reading
  = class
@@ -42,548 +42,188 @@
 ass 
-Sensor%7B%0A    /*%0A    * Create a sensor instance%0A    * @param %7Bnumber%7D id The id of the sensor, null if not in database%0A    * @param %7Bstring%7D location The location of the sensor%0A    * @param %7Bstring%7D type The type of the sensor%0A    */%0A    constructor(id, location, type) %7B%0A        this.id = id;%0A        this.location = location;%0A        this.type = type;%0A        this.readings = %5B%5D;%0A    %7D%0A%0A    /*%0A    * Add the sensor to the database and set the id of the sensor%0A    * @param %7Bfunction%7D callback The callback, will be executed when done
+Reading %7B%0A    constructor(sensor, value, dateTime) %7B%0A        this.sensor = sensor;%0A        this.value = value %7C%7C 0;%0A        this.dateTime = dateTime %7C%7C new Date().getTime();
 %0A    
-*/
+%7D%0A
 %0A   
@@ -254,22 +254,23 @@
     var 
-sensor
+reading
  = this;
@@ -270,16 +270,17 @@
 = this;%0A
+%0A
         
@@ -347,22 +347,23 @@
  + %22/add
-Sensor
+Reading
 .php%22, %7B
@@ -439,68 +439,43 @@
 ata:
-SmSc.addCredentials(%7B%0A                location:this.location
+%7B%0A                id:this.sensor.id
 ,%0A  
@@ -492,21 +492,23 @@
     
-typ
+valu
 e:this.
-typ
+valu
 e%0A  
@@ -518,17 +518,16 @@
        %7D
-)
 ,%0A      
@@ -582,22 +582,23 @@
    var n
-Sensor
+Reading
  = respo
@@ -605,20 +605,15 @@
 nse.
-sensor %7C%7C %7B%7D
+reading
 ;%0A  
@@ -630,30 +630,74 @@
     
-sensor.id = nSensor.id
+reading.dateTime = nReading ? nReading.datetime : reading.dateTime
 ;%0A%0A 
@@ -764,82 +764,24 @@
     
-if(response.err) callback(null, err);%0A                else callback(sensor
+callback(reading
 , er
@@ -899,1313 +899,74 @@
 ack(
-sensor, %22the http request failed%22);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    get reading() %7B%0A        return _.last(_.sortBy(this.readings, %22dateTime%22));%0A    %7D%0A%0A    set reading(value) %7B%0A        if(typeof value == %22number%22) %7B%0A            var reading = new SmSc.Reading(this, value, 0);%0A            reading.insert();%0A            this.readings.push(reading);%0A        %7D else %7B%0A            var reading = value;%0A            reading.insert();%0A            this.readings.push(reading);%0A        %7D%0A    %7D%0A%7D%0A%0ASmSc.getSensors = function getSensors(callback) %7B%0A    $.ajax(SmSc.url + %22/getSensors.php%22, %7B%0A        method:%22GET%22,%0A        dataType:%22JSON%22,%0A        data:SmSc.credentials,%0A        success:function(response) %7B%0A            var sensors = %5B%5D;%0A            for(var x in response.sensors) %7B%0A                var object = response.sensors%5Bx%5D.sensor;%0A                var sensor = new SmSc.Sensor(object.id, object.location, object.type);%0A                for(var y in response.sensors%5Bx%5D.readings)%7B%0A                    var object2 = response.sensors%5Bx%5D.readings%5By%5D;%0A                    var reading = new SmSc.Reading(sensor, object2.value, object2.dateTime);%0A                    sensor.readings.push(reading);%0A                %7D%0A                sensors.push(sensor);%0A            %7D%0A            callback(sensors);%0A        %7D%0A    %7D)
+reading, %22the http request failed%22);%0A            %7D%0A        %7D)%0A    %7D
 %0A%7D%0A

Update reading.js