Class: UdooNeoRest::Base

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/udooneorest.rb

Class Method Summary collapse

Class Method Details

.axis_path(sensor) ⇒ Object

Build the path to common axis sensors

sensor : the sensor name



112
113
114
# File 'lib/udooneorest.rb', line 112

def self.axis_path(sensor)
  "/sys/class/misc/Freescale#{sensor}"
end

.axis_path_data(sensor) ⇒ Object

Build the path to common axis sensors to retrieve data

sensor : the sensor name



130
131
132
# File 'lib/udooneorest.rb', line 130

def self.axis_path_data(sensor)
  UdooNeoRest::Base.axis_path(sensor) + '/data'
end

.axis_path_enable(sensor) ⇒ Object

Build the path to common axis sensors to enable/disable

sensor : the sensor name



121
122
123
# File 'lib/udooneorest.rb', line 121

def self.axis_path_enable(sensor)
  UdooNeoRest::Base.axis_path(sensor) + '/enable'
end

.cat(file) ⇒ Object

Cat a file

file : the file to cat



81
82
83
84
85
86
87
# File 'lib/udooneorest.rb', line 81

def self.cat(file)
  begin
    return File.read(file).chomp
  rescue Exception => e
    return nil
  end
end

.cat_and_status(file) ⇒ Object

Cat a file contents and return the status

file : the file to cat



139
140
141
142
143
144
145
146
# File 'lib/udooneorest.rb', line 139

def self.cat_and_status(file)
  result = UdooNeoRest::Base.cat file
  unless result.nil?
    return UdooNeoRest::Base.status_ok result
  end

  return UdooNeoRest::Base.status_error 'GPIO/PIN could not be read. Have you already exported the GPIO/PIN?'
end

.change_state(file, value) ⇒ Object

Enable or disable one of the axis sensors

Value : 0 disables the axis, 1 enables it



153
154
155
156
# File 'lib/udooneorest.rb', line 153

def self.change_state(file, value)
  result        = UdooNeoRest::Base.echo value, file
  result.empty? ? UdooNeoRest::Base.status_ok : UdooNeoRest::Base.status_error(result)
end

.echo(value, file) ⇒ Object

Echo a value to a file

value : the value to echo to the file file : the file to echo the value too



65
66
67
68
69
70
71
72
73
74
# File 'lib/udooneorest.rb', line 65

def self.echo(value, file)
  begin
    File.write(file, value)
  rescue Errno::EINVAL
    # close always error on the udoo gpios - cause unknown
  rescue Exception => e
    return e.message
  end
  ''
end

.sensor_calc(sensor) ⇒ Object

Calculate the Barometer/Temp value from raw and scale values



92
93
94
95
96
# File 'lib/udooneorest.rb', line 92

def self.sensor_calc(sensor)
  raw   = UdooNeoRest::Base.cat(UdooNeoRest::Base.sensor_path(sensor) + '_raw').to_i
  scale = UdooNeoRest::Base.cat(UdooNeoRest::Base.sensor_path(sensor) + '_scale').to_f
  UdooNeoRest::Base.status_ok(raw * scale)
end

.sensor_path(sensor) ⇒ Object

Build the path to common sensors

sensor : the sensor name



103
104
105
# File 'lib/udooneorest.rb', line 103

def self.sensor_path(sensor)
  "/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_#{sensor}"
end

.status_error(message) ⇒ Object

Construct the status message when an error occurred

message : the message to be displayed



55
56
57
# File 'lib/udooneorest.rb', line 55

def self.status_error(message)
  [400, status_message(FAILED_MESSAGE, message)]
end

.status_message(status, message) ⇒ Object

Message constructor

status : the status of the operation message : the message to be displayed



37
38
39
# File 'lib/udooneorest.rb', line 37

def self.status_message(status, message)
  %Q({"status" : "#{status}", "message" : "#{message}"})
end

.status_ok(message = '') ⇒ Object

Construct the status message when no errors occurred

message : the message to be displayed



46
47
48
# File 'lib/udooneorest.rb', line 46

def self.status_ok(message = '')
  status_message(SUCCESS_MESSAGE, message)
end