if (Meteor.isServer) {
  Meteor.startup(function() {
    // code to run on server at startup
    SyncedCron.start();

    Meteor.zoblak.server.on_all_controllers(function(controller_id) {
      var jobName = "automatic_alarm_" + controller_id;

      SyncedCron.remove(jobName);
      SyncedCron.add({
        name: jobName,
        schedule: function(parser) {
          return parser.text('every 10 seconds');
        },
        job: function() {
          reactToAlarmData(controller_id);
        }
      });
    });
  });


  // Global API configuration
  var Api = new Restivus({
    useDefaultAuth: true,
    prettyJson: true
  });

  Api.addRoute('sensorData', {
    authRequired: false
  }, {
    post: function() {
      SensorData.insert({
        temperatureValue: parseFloat(this.bodyParams.temperatureValue),
        humidityValue: parseFloat(this.bodyParams.humidityValue),
        tankLevel0: this.bodyParams.tankLevel0,
        tankLevel1: this.bodyParams.tankLevel1,
        tankLevel2: this.bodyParams.tankLevel2,
        tankLevel3: this.bodyParams.tankLevel3,
        tankLevel4: this.bodyParams.tankLevel4,
        tankFull: this.bodyParams.tankFull,
        startPumpingAt: this.bodyParams.startPumpingAt,
        stopPumpingAt: this.bodyParams.stopPumpingAt,
        owner: this.bodyParams.owner,
        created_at: new Date()
      });
      return [];
    }
  });


}
