Class: Ardtweeno::SerialParser
- Inherits:
-
Object
- Object
- Ardtweeno::SerialParser
- Defined in:
- lib/ardtweeno/serialparser.rb
Overview
Ardtweeno::SerialParser class for the Ardtweeno system
Instance Attribute Summary collapse
-
#log ⇒ Object
Returns the value of attribute log.
-
#sp ⇒ Object
Returns the value of attribute sp.
-
#testing ⇒ Object
Returns the value of attribute testing.
Instance Method Summary collapse
-
#close ⇒ Object
Ardtweeno::SerialParser#close Closes the SerialPort instance.
-
#initialize(dev, speed, timeout, options = {}) ⇒ SerialParser
constructor
Ardtweeno::SerialParser#new constructor for the Ardtweeno system.
-
#listen(key) ⇒ Object
Ardtweeno::SerialParser#listen listens for a packet of data to be received on the serial => device then posts it to the Ardtweeno API.
-
#read ⇒ Object
Ardtweeno::SerialParser#read Reads data from the active SerialPort device and then validates returns data if valid JSON, otherwise returns empty JSON hash.
-
#valid_json?(json_) ⇒ Boolean
Ardtweeno::SerialParser#valid_json? validates JSON data.
Constructor Details
#initialize(dev, speed, timeout, options = {}) ⇒ SerialParser
Ardtweeno::SerialParser#new constructor for the Ardtweeno system
-
Args :
-
++ -> dev String, speed Fixnum, timeout Fixnum, :log
-
-
Returns : -
-
Raises : -
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ardtweeno/serialparser.rb', line 52 def initialize(dev, speed, timeout, ={}) @log = [:log] ||= Logger.new(STDOUT) @log.level = [:level] ||= Logger::WARN @testing = [:testing] ||= false if @testing @log.debug "Creating instance of Ardtweeno::SerialParser for testing" else @log.debug "Creating instance of Ardtweeno::SerialParser" end begin @sp = SerialPort.new(dev, speed) @sp.read_timeout = timeout rescue Exception => e @log.fatal e. raise e end end |
Instance Attribute Details
#log ⇒ Object
Returns the value of attribute log.
40 41 42 |
# File 'lib/ardtweeno/serialparser.rb', line 40 def log @log end |
#sp ⇒ Object
Returns the value of attribute sp.
40 41 42 |
# File 'lib/ardtweeno/serialparser.rb', line 40 def sp @sp end |
#testing ⇒ Object
Returns the value of attribute testing.
40 41 42 |
# File 'lib/ardtweeno/serialparser.rb', line 40 def testing @testing end |
Instance Method Details
#close ⇒ Object
Ardtweeno::SerialParser#close Closes the SerialPort instance
-
Args :
-
++ ->
-
-
Returns : -
-
Raises : -
152 153 154 155 |
# File 'lib/ardtweeno/serialparser.rb', line 152 def close() @log.debug "Closing SerialPort device" @sp.close end |
#listen(key) ⇒ Object
Ardtweeno::SerialParser#listen listens for a packet of data to be received on the serial
> device then posts it to the Ardtweeno API
-
Args :
-
++ ->
-
-
Returns : -
-
Raises : -
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ardtweeno/serialparser.rb', line 102 def listen(key) data = "" 1.upto(10) do |i| data += read() if valid_json?(data) @log.debug "Posting to the API" @log.debug "#{data}" begin return postToAPI(data, key) rescue Exception => e raise e end break end end end |
#read ⇒ Object
Ardtweeno::SerialParser#read Reads data from the active SerialPort device and then validates returns data if valid JSON, otherwise returns empty JSON hash
-
Args :
-
++ -> String, Fixnum, Fixnum, :log
-
-
Returns :
-
JSON if valid, otherwise empty JSON hash
-
-
Raises : -
85 86 87 |
# File 'lib/ardtweeno/serialparser.rb', line 85 def read() return @sp.read end |
#valid_json?(json_) ⇒ Boolean
Ardtweeno::SerialParser#valid_json? validates JSON data
-
Args :
-
++ -> JSON String
-
-
Returns :
-
true || false
-
-
Raises : -
131 132 133 134 135 136 137 138 |
# File 'lib/ardtweeno/serialparser.rb', line 131 def valid_json?(json_) begin JSON.parse(json_) return true rescue Exception => e return false end end |