Class: PlantWatchdog::Model::SyncManager
- Inherits:
-
Object
- Object
- PlantWatchdog::Model::SyncManager
- Defined in:
- lib/plantwatchdog/model.rb
Instance Method Summary collapse
- #device(user, device_unique_id) ⇒ Object
- #latest(user, device_unique_id) ⇒ Object
- #logger ⇒ Object
- #sync(user, device_unique_id, csv) ⇒ Object
Instance Method Details
#device(user, device_unique_id) ⇒ Object
419 420 421 422 423 424 |
# File 'lib/plantwatchdog/model.rb', line 419 def device(user, device_unique_id) raise SyncError.new("The user must create a plant and devices before uploading data") if user.plant.nil? devices = user.plant.devices.select { |i| i.unique_id == device_unique_id } raise SyncError.new("Could not identify device '#{device_unique_id}' of user '#{user.name}'. Found '#{devices.size}' devices.") if devices.size != 1 device = devices.first end |
#latest(user, device_unique_id) ⇒ Object
426 427 428 429 |
# File 'lib/plantwatchdog/model.rb', line 426 def latest(user, device_unique_id) device = device(user, device_unique_id) device.sync ? device.sync.last_time : 0; end |
#logger ⇒ Object
431 432 433 |
# File 'lib/plantwatchdog/model.rb', line 431 def logger return ActiveRecord::Base.logger end |
#sync(user, device_unique_id, csv) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/plantwatchdog/model.rb', line 395 def sync(user, device_unique_id, csv) # assuming that csv is a timeseries, i.e. ordered by time device = device(user, device_unique_id) data = Measurement.parse_csv(device., csv) if (data.size == 0) logger.info "The data received from user #{user.name} (id=#{user.id}) is invalid, either it is empty or can not be parsed" return data.size end sync = device.sync unless sync logger.info "Creating new sync for user '#{user.name}' and device unique id '#{device_unique_id}'" sync = Sync.new sync.device = device sync.last_time = 0 end if (data.first.time <= sync.last_time) raise SyncError.new("The period is already sync'ed.") end MeasurementChunk.save_measurements(device, data) sync.last_time = data.last.time sync.save return data.size end |