Class: Ardtweeno::SerialParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ardtweeno/serialparser.rb

Overview

Ardtweeno::SerialParser class for the Ardtweeno system

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @log = options[:log] ||= Logger.new(STDOUT)
  @log.level = options[:level] ||= Logger::WARN
  @testing = options[: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.message
    raise e
  end
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



40
41
42
# File 'lib/ardtweeno/serialparser.rb', line 40

def log
  @log
end

#spObject

Returns the value of attribute sp.



40
41
42
# File 'lib/ardtweeno/serialparser.rb', line 40

def sp
  @sp
end

#testingObject

Returns the value of attribute testing.



40
41
42
# File 'lib/ardtweeno/serialparser.rb', line 40

def testing
  @testing
end

Instance Method Details

#closeObject

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

#readObject

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 : -

Returns:

  • (Boolean)


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