Module: HaystackRuby::Point

Defined in:
lib/haystack_ruby/point.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



15
16
17
# File 'lib/haystack_ruby/point.rb', line 15

def connection
  haystack_project.connection
end

#data(start, finish = nil, as_datetime = false, include_unit = false) ⇒ Object

as_datetime currently ignored



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/haystack_ruby/point.rb', line 47

def data(start, finish = nil, as_datetime = false, include_unit = false) # as_datetime currently ignored
  return unless haystack_valid? #may choose to throw exception instead

  range = [start]
  range << finish unless finish.nil?
  # clean up the range argument before passing through to hisRead
  # ----------------
  r = HaystackRuby::Range.new(range, self.haystack_time_zone)

  res = his_read r.to_s
  # puts "res in data : #{res}"
  reformat_timeseries(res['rows'], as_datetime, include_unit)
end

#haystack_projectObject



11
12
13
# File 'lib/haystack_ruby/point.rb', line 11

def haystack_project
  @project ||= HaystackRuby::Config.projects[self.haystack_project_name]
end

#haystack_valid?Boolean

is this Point valid for purposees of Project Haystack Integration?

Returns:

  • (Boolean)


7
8
9
# File 'lib/haystack_ruby/point.rb', line 7

def haystack_valid?
  return self.haystack_project_name.present? && self.haystack_point_id.present? && self.haystack_time_zone.present?
end

#his_read(range) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/haystack_ruby/point.rb', line 19

def his_read(range)
  query = ["ver:\"#{haystack_project.haystack_version}\"",'id,range',"@#{self.haystack_point_id},\"#{range}\""]
  pp query.join "\n"
  res = connection.post('hisRead') do |req|
    req.headers['Content-Type'] = 'text/plain'
    req.body = query.join("\n")
  end
  JSON.parse! res.body
end

#his_write(data) ⇒ Object

data is ascending array of hashes with format: epochtime, value: myvalue



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haystack_ruby/point.rb', line 35

def his_write(data)
  query =
    ["ver:\"#{haystack_project.haystack_version}\" id:@#{self.haystack_point_id}",'ts,val'] + data.map{ |d| "#{d[:time]},#{d[:value]}"}

  res = connection.post('hisWrite') do |req|
    req.headers['Content-Type'] = 'text/plain'
    req.body = query.join("\n")
  end

  JSON.parse(res.body)['meta']['ok'].present?
end

#meta_dataObject



29
30
31
32
# File 'lib/haystack_ruby/point.rb', line 29

def 
  # read request on project to load current info, including tags and timezone
  res = haystack_project.read({:id => "@#{self.haystack_point_id}"})['rows'][0]
end

#reformat_timeseries(data, as_datetime = false, include_unit = false) ⇒ Object

map from



73
74
75
76
77
78
79
80
81
# File 'lib/haystack_ruby/point.rb', line 73

def reformat_timeseries data, as_datetime = false, include_unit = false
  data.map do |d|
    time = (as_datetime) ? DateTime.parse(d['ts']) : DateTime.parse(d['ts']).to_i
    val = HaystackRuby::Object.new(d['val'])
    r = {:time => time, :value => val.value}
    r[:unit] = val.unit if include_unit
    r
  end
end

#write_data(data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/haystack_ruby/point.rb', line 61

def write_data(data)
  # format data for his_write
  data = data.map do |d|
    {
      time: HaystackRuby::Timestamp.convert_to_string(d[:time], self.haystack_time_zone),
      value: d[:value]
    }
  end
  his_write data
end