Class: TellStickR::Sensor

Inherits:
Object
  • Object
show all
Defined in:
lib/tellstickr/sensor.rb

Constant Summary collapse

PREDEFINED_SENSORS =

A mapping of known protocols and models for different sensor products.

{
  # Clas Ohlson Esic 36-179* sensors
  wt450h: {
    protocol: "mandolyn",
    model: "temperaturehumidity"
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol, model, id) ⇒ Sensor

Returns a new instance of Sensor.



7
8
9
10
11
12
13
# File 'lib/tellstickr/sensor.rb', line 7

def initialize(protocol, model, id)
  @protocol = protocol
  @model = model
  @id = id
  @callback_functions = {}
  TellStickR::Core.tdInit
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/tellstickr/sensor.rb', line 5

def id
  @id
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/tellstickr/sensor.rb', line 5

def model
  @model
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/tellstickr/sensor.rb', line 5

def protocol
  @protocol
end

Class Method Details

.discoverObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tellstickr/sensor.rb', line 59

def self.discover
  TellStickR::Core.tdInit
  sensors = []
  result = TellStickR::Core::TELLSTICK_SUCCESS
  while(true) do
    protocol = FFI::MemoryPointer.new(:string, 32)
    model  = FFI::MemoryPointer.new(:string, 32)
    data_types  = FFI::MemoryPointer.new(:int, 2)
    id = FFI::MemoryPointer.new(:int, 2)
    if TellStickR::Core.tdSensor(protocol, 32, model, 32, id, data_types) == TellStickR::Core::TELLSTICK_SUCCESS
      sensors << Sensor.new(protocol.get_string(0, 32), model.get_string(0, 32), id.get_int32(0))
    else
      break
    end
  end
  sensors
end

.from_predefined(key, id) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/tellstickr/sensor.rb', line 87

def self.from_predefined(key, id)
  predefined = PREDEFINED_SENSORS[key.to_sym]
  if predefined
    return Sensor.new(predefined[:protocol], predefined[:model], id)
  else
    raise "Unknown sensor product. Predefined sensors: #{PREDEFINED_SENSORS.map{|k,v| k}.join(', ')}"
  end
end

Instance Method Details

#humidityObject



22
23
24
25
26
27
# File 'lib/tellstickr/sensor.rb', line 22

def humidity
  value = FFI::MemoryPointer.new(:string, 4)
  time = FFI::MemoryPointer.new(:int, 2)
  TellStickR::Core.tdSensorValue(@protocol, @model, @id, TellStickR::Core::TELLSTICK_HUMIDITY, value, 4, time)
  {value: value.get_string(0,4).to_f, time: Time.at(time.get_int32(0))}
end

#register_callback(proc) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tellstickr/sensor.rb', line 29

def register_callback(proc)
  object_id  = FFI::MemoryPointer.new(:int32)
  object_id.write_int32(self.object_id)
  callback = Proc.new do |protocol, model, id, data_type, value, timestamp, callback_id, context|
    sensor = ObjectSpace._id2ref(context.get_int32(0))
    if id == sensor.id
      sensor.callback_functions[callback_id].call({
        kind: (data_type == TellStickR::Core::TELLSTICK_TEMPERATURE ? :temperature : :humidity),
        value: value.to_f, time: Time.at(timestamp.to_i)
      })
    end
  end
  id = TellStickR::Core.tdRegisterSensorEvent(callback, object_id)
  @callback_functions[id] = proc
  id
end

#temperatureObject



15
16
17
18
19
20
# File 'lib/tellstickr/sensor.rb', line 15

def temperature
  value  = FFI::MemoryPointer.new(:string, 4)
  time  = FFI::MemoryPointer.new(:int, 2)
  TellStickR::Core.tdSensorValue(@protocol, @model, @id, TellStickR::Core::TELLSTICK_TEMPERATURE, value, 4, time)
  {value: value.get_string(0,4).to_f, time: Time.at(time.get_int32(0))}
end

#unregister_callback(id) ⇒ Object



46
47
48
49
# File 'lib/tellstickr/sensor.rb', line 46

def unregister_callback(id)
  TellStickR::Core.tdUnregisterCallback(id)
  @callback_functions.delete(id)
end

#unregister_callbacksObject



51
52
53
54
55
56
57
# File 'lib/tellstickr/sensor.rb', line 51

def unregister_callbacks
  @callback_functions.each do |k,v|
    TellStickR::Core.tdUnregisterCallback(k)
    @callback_functions.delete(k)
  end
  @callback_functions
end