<commit_before>'use strict';

/**
 * define for the senseBox request body encoded in JSON
 * @apiDefine RequestBody JSON request body
 */

/**
 * @apiDefine CommonBoxJSONBody
 *
 * @apiParam (RequestBody) {String} name the name of this senseBox.
 * @apiParam (RequestBody) {String} grouptag the grouptag of this senseBox.
 * @apiParam (RequestBody) {String="indoor","outdoor"} exposure the exposure of this senseBox.
 * @apiParam (RequestBody) {String="fixed"} boxType the type of the senseBox. Currently only 'fixed' is supported.
 * @apiParam (RequestBody) {String="homeEthernet","homeWifi"} model specify the model if you want to use a predefined senseBox model.
 * @apiParam (RequestBody) {Sensor[]} sensors an array containing the sensors of this senseBox. Only use if model is unspecified
 * @apiParam (RequestBody) {MqttOption} sensors an array containing the sensors of this senseBox.
 * @apiParam (RequestBody) {Location} loc the location of this senseBox. Must be a GeoJSON Point Feature. (RFC7946)
 *
 */

/**
 * @apiDefine BoxIdParam
 *
 * @apiParam {String} :senseBoxId the ID of the senseBox you are referring to.
 */

const { mongoose } = require('../db'),
  timestamp = require('mongoose-timestamp'),
  Schema = mongoose.Schema,
  sensorSchema = require('./sensor').schema,
  integrations = require('./integrations'),
  sensorLayouts = require('../sensorLayouts'),
  mqttClient = require('../mqtt'),
  Measurement = require('./measurement').model,
  utils = require('../utils'),
  parseTimestamp = utils.parseTimestamp,
  transform = require('stream-transform'),
  log = require('../log');

//Location schema
const locationSchema = new Schema({
  type: {
    type: String,
    required: true,
    default: 'Feature',
    enum: ['Feature', 'feature']
  },
  geometry: {
    type: {
      type: String,
      required: true,
      default: 'Point',
      enum: ['Point', 'point']
    },
    coordinates: {
      type: Array,
      required: true
    }
  },
  properties: Schema.Types.Mixed
});
locationSchema.index({ 'geometry': '2dsphere' });

locationSchema.set('toJSON', {
  version: false,
  transform: function transform (doc, ret) {
    delete ret._id;

    return ret;
  }
});

//senseBox schema
const boxSchema = new Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  loc: {
    type: [locationSchema],
    required: true
  },
  boxType: {
    type: String,
    required: true
  },
  exposure: {
    type: String,
    required: false
  },
  grouptag: {
    type: String,
    required: false
  },
  model: {
    type: String,
    required: false
  },
  weblink: {
    type: String,
    required: false
  },
  description: {
    type: String,
    required: false
  },
  sensors: [sensorSchema]
}, { strict: false });
boxSchema.plugin(timestamp);
boxSchema.index({ 'loc.geometry': '2dsphere' });

const BOX_PROPS_FOR_POPULATION = {
  boxType: 1,
  createdAt: 1,
  exposure: 1,
  model: 1,
  grouptag: 1,
  image: 1,
  name: 1,
  updatedAt: 1,
  'loc.geometry': 1,
  'loc.type': 1,
  sensors: 1,
  description: 1,
};

const BOX_SUB_PROPS_FOR_POPULATION = [
  {
    path: 'sensors.lastMeasurement', select: { value: 1, createdAt: 1, _id: 0 }
  }
];

boxSchema.set('toJSON', {
  version: false,
  transform: function transform (doc, ret, options) {
    const box = {};

    for (const prop of Object.keys(BOX_PROPS_FOR_POPULATION)) {
      box[prop] = ret[prop];
    }
    box._id = ret._id;
    box.loc = ret.loc;

    if (options && options.includeSecrets) {
      box.integrations = ret.integrations;
    }

    return box;
  }
});

// initializes unsaved but validated Box document
boxSchema.statics.initNew = function (req) {
  const boxData = {
    name: req.params.name,
    boxType: req.params.boxType,
    loc: req.params.loc,
    grouptag: req.params.tag,
    exposure: req.params.exposure,
    _id: mongoose.Types.ObjectId(),
    model: req.params.model,
    sensors: req.params.sensors,
    integrations: { mqtt: { enabled: false } }
  };
  if (req.params.mqtt) {
    const { enabled, url, topic, decodeOptions, connectionOptions, messageFormat } = req.params.mqtt;
    boxData.integrations.mqtt = {
      enabled, url, topic, decodeOptions, connectionOptions, messageFormat
    };
  }

  if (req.params.ttn) {
      const { app_id, dev_id, port, profile, decodeOptions } = req.params.ttn;
      boxData.integrations.ttn = { app_id, dev_id, port, profile, decodeOptions };
  }

  // check if either a model or a sensor array has been given<commit_msg>eslint for f4570aa<commit_after>'use strict';

/**
 * define for the senseBox request body encoded in JSON
 * @apiDefine RequestBody JSON request body
 */

/**
 * @apiDefine CommonBoxJSONBody
 *
 * @apiParam (RequestBody) {String} name the name of this senseBox.
 * @apiParam (RequestBody) {String} grouptag the grouptag of this senseBox.
 * @apiParam (RequestBody) {String="indoor","outdoor"} exposure the exposure of this senseBox.
 * @apiParam (RequestBody) {String="fixed"} boxType the type of the senseBox. Currently only 'fixed' is supported.
 * @apiParam (RequestBody) {String="homeEthernet","homeWifi"} model specify the model if you want to use a predefined senseBox model.
 * @apiParam (RequestBody) {Sensor[]} sensors an array containing the sensors of this senseBox. Only use if model is unspecified
 * @apiParam (RequestBody) {MqttOption} sensors an array containing the sensors of this senseBox.
 * @apiParam (RequestBody) {Location} loc the location of this senseBox. Must be a GeoJSON Point Feature. (RFC7946)
 *
 */

/**
 * @apiDefine BoxIdParam
 *
 * @apiParam {String} :senseBoxId the ID of the senseBox you are referring to.
 */

const { mongoose } = require('../db'),
  timestamp = require('mongoose-timestamp'),
  Schema = mongoose.Schema,
  sensorSchema = require('./sensor').schema,
  integrations = require('./integrations'),
  sensorLayouts = require('../sensorLayouts'),
  mqttClient = require('../mqtt'),
  Measurement = require('./measurement').model,
  utils = require('../utils'),
  parseTimestamp = utils.parseTimestamp,
  transform = require('stream-transform'),
  log = require('../log');

//Location schema
const locationSchema = new Schema({
  type: {
    type: String,
    required: true,
    default: 'Feature',
    enum: ['Feature', 'feature']
  },
  geometry: {
    type: {
      type: String,
      required: true,
      default: 'Point',
      enum: ['Point', 'point']
    },
    coordinates: {
      type: Array,
      required: true
    }
  },
  properties: Schema.Types.Mixed
});
locationSchema.index({ 'geometry': '2dsphere' });

locationSchema.set('toJSON', {
  version: false,
  transform: function transform (doc, ret) {
    delete ret._id;

    return ret;
  }
});

//senseBox schema
const boxSchema = new Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  loc: {
    type: [locationSchema],
    required: true
  },
  boxType: {
    type: String,
    required: true
  },
  exposure: {
    type: String,
    required: false
  },
  grouptag: {
    type: String,
    required: false
  },
  model: {
    type: String,
    required: false
  },
  weblink: {
    type: String,
    required: false
  },
  description: {
    type: String,
    required: false
  },
  sensors: [sensorSchema]
}, { strict: false });
boxSchema.plugin(timestamp);
boxSchema.index({ 'loc.geometry': '2dsphere' });

const BOX_PROPS_FOR_POPULATION = {
  boxType: 1,
  createdAt: 1,
  exposure: 1,
  model: 1,
  grouptag: 1,
  image: 1,
  name: 1,
  updatedAt: 1,
  'loc.geometry': 1,
  'loc.type': 1,
  sensors: 1,
  description: 1,
};

const BOX_SUB_PROPS_FOR_POPULATION = [
  {
    path: 'sensors.lastMeasurement', select: { value: 1, createdAt: 1, _id: 0 }
  }
];

boxSchema.set('toJSON', {
  version: false,
  transform: function transform (doc, ret, options) {
    const box = {};

    for (const prop of Object.keys(BOX_PROPS_FOR_POPULATION)) {
      box[prop] = ret[prop];
    }
    box._id = ret._id;
    box.loc = ret.loc;

    if (options && options.includeSecrets) {
      box.integrations = ret.integrations;
    }

    return box;
  }
});

// initializes unsaved but validated Box document
boxSchema.statics.initNew = function (req) {
  const boxData = {
    name: req.params.name,
    boxType: req.params.boxType,
    loc: req.params.loc,
    grouptag: req.params.tag,
    exposure: req.params.exposure,
    _id: mongoose.Types.ObjectId(),
    model: req.params.model,
    sensors: req.params.sensors,
    integrations: { mqtt: { enabled: false } }
  };
  if (req.params.mqtt) {
    const { enabled, url, topic, decodeOptions, connectionOptions, messageFormat } = req.params.mqtt;
    boxData.integrations.mqtt = {
      enabled, url, topic, decodeOptions, connectionOptions, messageFormat
    };
  }

  if (req.params.ttn) {
    const { app_id, dev_id, port, profile, decodeOptions } = req.params.ttn;
    boxData.integrations.ttn = { app_id, dev_id, port, profile, decodeOptions };
  }

  // check if either a model or a sensor array has been given
{'fraction_non_alphanumeric': 0.135108153078203, 'fraction_numerical': 0.005102606766500277, 'mean_word_length': 3.3159406414552417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2177286', 'n_tokens_mistral': 2981, 'n_tokens_neox': 2914, 'n_words': 813}