Class: PlantWatchdog::Model::Measurement

Inherits:
ActiveRecord::BaseWithoutTable show all
Extended by:
MeasurementClassExtension
Defined in:
lib/plantwatchdog/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MeasurementClassExtension

metadata, metadata=

Methods inherited from ActiveRecord::BaseWithoutTable

#==, column, columns, #create_or_update, #hash, reset_column_information

Class Method Details

.from_CSV(clazz, fields, csv) ⇒ Object

create a list of Measurement instances from csv lines clazz is a sublass of Measurement csv must understand readlines, eg. be an instance of IO or StringIO the first column is a time column, other columns are defined by fields



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/plantwatchdog/model.rb', line 257

def self.from_CSV clazz, fields, csv
  # already converted ?
  return csv if csv.class == Array
  result = []
  return result if csv.nil?
  csv.each {
    |l|
    v = l.split(",")
    m = clazz.new
    m.line = l.chomp
    fields.each_index {|i| m.send(fields[i], v[i])}
    result << m
  }
  return result
end

.parse_csv(metadata, csv) ⇒ Object



277
278
279
280
281
282
# File 'lib/plantwatchdog/model.rb', line 277

def self.parse_csv , csv
  setters = .dataclass.columns.collect{ |col| (col.name + "=").to_sym }
  result = self.from_CSV .dataclass, setters, csv
  logger.debug "Read #{result.size} environment measurement entries from CSV"
  result
end

.partition_by_day(measurements) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/plantwatchdog/model.rb', line 284

def self.partition_by_day measurements
  # assuming measurements is a list of measurements sorted by time
  result = {}
  # Gruppenwechsel in time_day_of_year
  changes = (1..measurements.size-1).select { |i| measurements[i-1].time_day_of_year != measurements[i].time_day_of_year }
  changes << measurements.size unless measurements.empty?
  changes.inject(0) { |s,e|
    m = measurements[s];
    # a hack to dismiss data which has been tagged with year 2000 which is the default
    # for a fritz!box before it has polled the time from a ntp server
    result[[m.time_year, m.time_day_of_year]] = measurements.slice(s,e-s) if m.time_year>2000;
    e
  }
  return result
end

.to_csv(measurements) ⇒ Object



273
274
275
# File 'lib/plantwatchdog/model.rb', line 273

def self.to_csv measurements
  measurements.collect { |m| m.line }.join "\n"
end

Instance Method Details

#lineObject



240
241
242
243
# File 'lib/plantwatchdog/model.rb', line 240

def line
  @line = to_csv unless @line
  @line
end

#line=(original_line) ⇒ Object



236
237
238
# File 'lib/plantwatchdog/model.rb', line 236

def line= original_line
  @line= original_line
end

#timeObject



229
230
231
232
233
234
# File 'lib/plantwatchdog/model.rb', line 229

def time
  t = read_attribute("time")
  return t if t
  t = Time.utc(time_year) + ((time_day_of_year-1)*3600*24) + time_seconds_of_day
  return t.tv_sec
end

#time=(epoch_secs) ⇒ Object

set from a time object and split into year, day of year and seconds of day time t: seconds since epoch in utc



221
222
223
224
225
226
227
# File 'lib/plantwatchdog/model.rb', line 221

def time= epoch_secs
  t = Time.at(epoch_secs.to_i) # this is local time, mind to set ENV["TZ"]
  write_attribute(:time_year, t.year)
  write_attribute(:time_day_of_year, t.yday)
  write_attribute(:time_seconds_of_day, t.sec + t.min*60 + t.hour*3600)
  write_attribute("time", epoch_secs)
end

#to_csvObject



245
246
247
248
249
250
251
# File 'lib/plantwatchdog/model.rb', line 245

def to_csv
  meta = self.class..description
  logger.debug "to_csv, using metadata " + meta.to_s
  result = meta.collect { | coldesc | self[coldesc.first].to_s }.join(",")
  logger.debug "to_csv, result " + result
  result
end