Class: PlantWatchdog::Model::Measurement
Class Method Summary
collapse
Instance Method Summary
collapse
metadata, metadata=
#==, 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
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 metadata, csv
setters = metadata.dataclass.columns.collect{ |col| (col.name + "=").to_sym }
result = self.from_CSV metadata.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
result = {}
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];
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
#line ⇒ Object
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
|
#time ⇒ Object
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) 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_csv ⇒ Object
245
246
247
248
249
250
251
|
# File 'lib/plantwatchdog/model.rb', line 245
def to_csv
meta = self.class.metadata.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
|