This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_service.rb
86 lines (69 loc) · 1.6 KB
/
gpio_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'json'
require 'rack'
require 'yaml'
class GpioService
TYPE_PWM = 'pwm'
@@required_keys = ["pin"]
@@http_success = '200'
attr_reader :gpio
def initialize(gpio)
@gpio = gpio
end
def call(env)
req = Rack::Request.new(env)
data = req.body.read
data_hash = JSON.parse(data)
p "received: #{data}"
if hasRequiredKeys(data_hash)
pin = data_hash['pin'].to_i
value = data_hash['value']
type = data_hash['type']
unless value.nil?
gpioWrite(pin, value.to_i, type)
end
answer = gpioRead(pin).to_json
else
answer = getErrorAnswerMissingKey()
end
p answer
return response(@@http_success, answer)
end
def response(error_code, data)
[error_code, {'Content-Type' => 'application/json'}, [data]]
end
def gpioWrite(pin, value, type)
unless pin.nil? || value.nil?
p "writing pin: #{pin} value: #{value}"
if TYPE_PWM == type
gpio.pwmWrite(pin,value)
else
gpio.write(pin,value)
end
end
end
def gpioRead(pin)
unless pin.nil?
value = gpio.read(pin)
p "reading pin: #{pin} value: #{value}"
end
{"pin" => pin, "value" => value}
end
def hasRequiredKeys(data_hash)
unless data_hash.is_a?(Hash)
return false
end
@@required_keys.each do |k|
unless data_hash.has_key?(k)
return false
end
end
return true
end
def getErrorAnswerMissingKey()
createErrorAnswer('Invalid json data received')
end
def createErrorAnswer(msg)
error_msg = 'GPIO Service: ' + msg
{"error" => error_msg}
end
end