Class: PlantWatchdog::Model::MeasurementChunk

Inherits:
AbstractMeasurementChunk show all
Defined in:
lib/plantwatchdog/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetadataOwner

#meta, #meta=

Class Method Details

.save_measurements(device, measurements) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/plantwatchdog/model.rb', line 334

def self.save_measurements device, measurements
  return if measurements.empty?
  Measurement.partition_by_day(measurements).each_pair {
    | key, measurements |
    chunk = MeasurementChunk.find(:first, :conditions => ["device_id=? and time_year=? and time_day_of_year=?", device.id, key.first, key.last] )
    if chunk.nil? then
      chunk = MeasurementChunk.new
      chunk.device = device
      chunk. = device.
      chunk.time_year = key.first
      chunk.time_day_of_year =  key.last
    end
    chunk.append_measurements(measurements)
    chunk.save
  }
end

Instance Method Details

#append_measurements(new_measurements) ⇒ Object



318
319
320
321
322
323
# File 'lib/plantwatchdog/model.rb', line 318

def append_measurements new_measurements
  # new_measurements are supposed to be taken at the same day as the existing data
  logger.debug("Appending #{new_measurements.size} measurement(s).")
  logger.debug(measurements)
  self.measurements = measurements | new_measurements
end

#measurementsObject



313
314
315
316
# File 'lib/plantwatchdog/model.rb', line 313

def measurements
  @measurements = Model::Measurement.parse_csv(, data) unless @measurements
  @measurements
end

#measurements=(new_measurements) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/plantwatchdog/model.rb', line 325

def measurements= new_measurements
  # all measurements are supposed to be from one day
  return if new_measurements.size == 0
  logger.debug("Setting #{new_measurements.size} measurement(s) for #{time_year}-#{time_day_of_year}.")
  @measurements = new_measurements
  self.data = Measurement.to_csv(new_measurements)
  device.plant.user.data_updated time_year
end