ardtweeno 0.0.2 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG +179 -1
- data/COPYING +4 -3
- data/Gemfile +29 -0
- data/Gemfile.lock +76 -0
- data/INSTALL +12 -0
- data/Procfile +1 -0
- data/README.md +26 -2
- data/Rakefile +14 -7
- data/lib/ardtweeno/api.rb +542 -0
- data/lib/ardtweeno/configreader.rb +65 -0
- data/lib/ardtweeno/db.rb +51 -0
- data/lib/ardtweeno/dispatcher.rb +538 -0
- data/lib/ardtweeno/exceptions.rb +33 -0
- data/lib/ardtweeno/node.rb +117 -0
- data/lib/ardtweeno/nodemanager.rb +300 -0
- data/lib/ardtweeno/packet.rb +98 -0
- data/lib/ardtweeno/restapi.rb +266 -0
- data/lib/ardtweeno/serialparser.rb +221 -0
- data/lib/ardtweeno.rb +120 -1
- data/public/glossy_green_button.svg +123 -0
- data/public/glossy_red_button.svg +75 -0
- data/public/main.css +129 -0
- data/public/raspberrypi.jpg +0 -0
- data/resources/conf.yaml +41 -0
- data/resources/nodelist.yaml +26 -0
- data/resources/serialparser.js +84 -0
- data/test/api_test.rb +255 -0
- data/test/dispatcher_test.rb +115 -0
- data/test/node_test.rb +105 -0
- data/test/nodemanager_test.rb +167 -0
- data/test/packet_test.rb +75 -0
- data/test/parser_test.rb +147 -0
- data/test/post_watch +11 -0
- data/test/rest_api_test.rb +248 -0
- data/test/run_mock +17 -0
- data/test/run_packet_push +14 -0
- data/test/serialport_mock.rb +43 -0
- data/test/test_helper.rb +15 -0
- data/test/tty0tty-1.1/AUTHORS +1 -0
- data/test/tty0tty-1.1/COPYING +340 -0
- data/test/tty0tty-1.1/INSTALL +18 -0
- data/test/tty0tty-1.1/README +52 -0
- data/test/tty0tty-1.1/THANKS +4 -0
- data/test/tty0tty-1.1/TODO +3 -0
- data/test/tty0tty-1.1/VERSION +4 -0
- data/test/tty0tty-1.1/module/Makefile +41 -0
- data/{bin/ardtweeno → test/tty0tty-1.1/module/Module.symvers} +0 -0
- data/test/tty0tty-1.1/module/modules.order +1 -0
- data/test/tty0tty-1.1/module/tty0tty.c +678 -0
- data/test/tty0tty-1.1/module/tty0tty.ko +0 -0
- data/test/tty0tty-1.1/module/tty0tty.mod.c +51 -0
- data/test/tty0tty-1.1/module/tty0tty.mod.o +0 -0
- data/test/tty0tty-1.1/module/tty0tty.o +0 -0
- data/test/tty0tty-1.1/pts/Makefile +10 -0
- data/test/tty0tty-1.1/pts/tty0tty +0 -0
- data/test/tty0tty-1.1/pts/tty0tty.c +222 -0
- data/views/createpost.erb +45 -0
- data/views/home.erb +59 -0
- metadata +89 -37
- data/README +0 -1
- data/test/Rakefile +0 -6
- data/test/features/ardtweeno.feature +0 -14
- data/test/features/step_definitions/ardtweeno_steps.rb +0 -24
| @@ -0,0 +1,300 @@ | |
| 1 | 
            +
            ####################################################################################################
         | 
| 2 | 
            +
            # @author       David Kirwan <davidkirwanirl@gmail.com>
         | 
| 3 | 
            +
            # @description  Node Management class for the Ardtweeno Mesh Network
         | 
| 4 | 
            +
            #
         | 
| 5 | 
            +
            # @date         21-02-2013
         | 
| 6 | 
            +
            ####################################################################################################
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # Imports
         | 
| 9 | 
            +
            require 'rubygems'
         | 
| 10 | 
            +
            require 'logger'
         | 
| 11 | 
            +
            require 'yaml'
         | 
| 12 | 
            +
            require 'json'
         | 
| 13 | 
            +
            require 'ardtweeno'
         | 
| 14 | 
            +
            require 'typhoeus'
         | 
| 15 | 
            +
             | 
| 16 | 
            +
             | 
| 17 | 
            +
            module Ardtweeno
         | 
| 18 | 
            +
              
         | 
| 19 | 
            +
              ##
         | 
| 20 | 
            +
              # Ardtweeno::NodeManager class for the Ardtweeno Mesh Network
         | 
| 21 | 
            +
              #
         | 
| 22 | 
            +
              class NodeManager
         | 
| 23 | 
            +
                
         | 
| 24 | 
            +
                attr_accessor :nodeList, :zones, :log, :watchlist
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                
         | 
| 27 | 
            +
                ##
         | 
| 28 | 
            +
                # Ardtweeno::NodeManager#new constructor
         | 
| 29 | 
            +
                #
         | 
| 30 | 
            +
                # * *Args*    :
         | 
| 31 | 
            +
                #   - ++ ->   options Hash{:log Logger, :nodelist Array[Ardtweeno::Node]}
         | 
| 32 | 
            +
                # * *Returns* :
         | 
| 33 | 
            +
                #   -
         | 
| 34 | 
            +
                # * *Raises* :
         | 
| 35 | 
            +
                #
         | 
| 36 | 
            +
                def initialize(options={})
         | 
| 37 | 
            +
                  @log = Ardtweeno.options[:log] ||= Logger.new(STDOUT)
         | 
| 38 | 
            +
                  @log.level = Ardtweeno.options[:level] ||= Logger::DEBUG
         | 
| 39 | 
            +
                  
         | 
| 40 | 
            +
                  @watchlist = Array.new
         | 
| 41 | 
            +
                  
         | 
| 42 | 
            +
                  @nodeList = options[:nodelist] ||= Array.new 
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
             | 
| 46 | 
            +
                ##
         | 
| 47 | 
            +
                # Ardtweeno::NodeManager#flush empties the packetqueue inside each Ardtweeno:Node
         | 
| 48 | 
            +
                #
         | 
| 49 | 
            +
                # * *Args*    :
         | 
| 50 | 
            +
                #   - ++ ->   
         | 
| 51 | 
            +
                # * *Returns* :
         | 
| 52 | 
            +
                #   -         
         | 
| 53 | 
            +
                # * *Raises* :
         | 
| 54 | 
            +
                #   -         
         | 
| 55 | 
            +
                #
         | 
| 56 | 
            +
                def flush()
         | 
| 57 | 
            +
                  @nodeList.each do |i|
         | 
| 58 | 
            +
                    i.packetqueue = Array.new
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
             | 
| 63 | 
            +
             | 
| 64 | 
            +
                ##
         | 
| 65 | 
            +
                # Ardtweeno::NodeManager#addNode adds an Ardtweeno::Node to the managed node list
         | 
| 66 | 
            +
                #
         | 
| 67 | 
            +
                # * *Args*    :
         | 
| 68 | 
            +
                #   - ++ ->   Ardtweeno::Node
         | 
| 69 | 
            +
                # * *Returns* :
         | 
| 70 | 
            +
                #   -         true || false
         | 
| 71 | 
            +
                # * *Raises* :
         | 
| 72 | 
            +
                #   -         Ardtweeno::NotANode
         | 
| 73 | 
            +
                #
         | 
| 74 | 
            +
                def addNode(node)
         | 
| 75 | 
            +
                  if node.class == Ardtweeno::Node
         | 
| 76 | 
            +
                    @log.debug "Size of nodeList before addition: #{@nodeList.size}"
         | 
| 77 | 
            +
                    @nodeList << node
         | 
| 78 | 
            +
                    @log.debug "Size of nodeList after addition: #{@nodeList.size}"
         | 
| 79 | 
            +
                    
         | 
| 80 | 
            +
                    return true
         | 
| 81 | 
            +
                  else
         | 
| 82 | 
            +
                    raise Ardtweeno::NotANode, "Error Ardtweeno::NodeManager#addNode expects an Ardtweeno::Node as a parameter"
         | 
| 83 | 
            +
                  end
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
             | 
| 87 | 
            +
                ##
         | 
| 88 | 
            +
                # Ardtweeno::NodeManager#removeNode removes an Ardtweeno::Node from the managed nodeList
         | 
| 89 | 
            +
                #
         | 
| 90 | 
            +
                # * *Args*    :
         | 
| 91 | 
            +
                #   - ++ ->   {:node, :key}
         | 
| 92 | 
            +
                # * *Returns* :
         | 
| 93 | 
            +
                #   -         Ardtweeno::Node
         | 
| 94 | 
            +
                # * *Raises* :
         | 
| 95 | 
            +
                #   -         Ardtweeno::NotInNodeList
         | 
| 96 | 
            +
                #    
         | 
| 97 | 
            +
                def removeNode(options={})
         | 
| 98 | 
            +
                  @log.debug "removeNode function called, searching for node in list"
         | 
| 99 | 
            +
                  
         | 
| 100 | 
            +
                  begin
         | 
| 101 | 
            +
                    found = search(options)
         | 
| 102 | 
            +
                    @nodeList.delete(found)
         | 
| 103 | 
            +
                    
         | 
| 104 | 
            +
                    return found
         | 
| 105 | 
            +
                    
         | 
| 106 | 
            +
                  rescue Ardtweeno::NotInNodeList
         | 
| 107 | 
            +
                    return nil
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
                  
         | 
| 110 | 
            +
                end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
             | 
| 113 | 
            +
                ##
         | 
| 114 | 
            +
                # Ardtweeno::NodeManager#search provides an interface to search the managed Ardtweeno::Node list
         | 
| 115 | 
            +
                # takes a single parameter hash, expects :node or :key search strings corresponding with the
         | 
| 116 | 
            +
                # Ardtweeno::Node you wish to search for, and returns this Node if found
         | 
| 117 | 
            +
                # Raises Ardtweeno::NotInNodeList exception if entry not found in the list
         | 
| 118 | 
            +
                #
         | 
| 119 | 
            +
                # * *Args*    :
         | 
| 120 | 
            +
                #   - ++ ->   options Hash{:node String, :key String}
         | 
| 121 | 
            +
                # * *Returns* :
         | 
| 122 | 
            +
                #   -         Ardtweeno::Node
         | 
| 123 | 
            +
                # * *Raises* :
         | 
| 124 | 
            +
                #   -         Ardtweeno::NotInNodeList
         | 
| 125 | 
            +
                #    
         | 
| 126 | 
            +
                def search(options={})
         | 
| 127 | 
            +
                  key = options[:key] ||= nil
         | 
| 128 | 
            +
                  node = options[:node] ||= nil
         | 
| 129 | 
            +
                  
         | 
| 130 | 
            +
                  if key.nil? and node.nil? then 
         | 
| 131 | 
            +
                    raise Ardtweeno::NotInNodeList, "Error key does not match any Ardtweeno::Node being maintained by this Ardtweeno::NodeManager"
         | 
| 132 | 
            +
                  end
         | 
| 133 | 
            +
                  
         | 
| 134 | 
            +
                  unless key.nil? then 
         | 
| 135 | 
            +
                    @log.debug "Searching the Nodelist for key: " + key
         | 
| 136 | 
            +
                    
         | 
| 137 | 
            +
                    @nodeList.each do |i|
         | 
| 138 | 
            +
                      @log.debug "Comparing #{key} with #{i.key}"
         | 
| 139 | 
            +
                      if i.key == key
         | 
| 140 | 
            +
                        @log.debug "Match found, returning node"
         | 
| 141 | 
            +
                        return i       
         | 
| 142 | 
            +
                      end
         | 
| 143 | 
            +
                    end
         | 
| 144 | 
            +
                    
         | 
| 145 | 
            +
                  end
         | 
| 146 | 
            +
                  
         | 
| 147 | 
            +
                  unless node.nil? then
         | 
| 148 | 
            +
                    @log.debug "Searching the Nodelist for node: " + node
         | 
| 149 | 
            +
                    
         | 
| 150 | 
            +
                    @nodeList.each do |i|
         | 
| 151 | 
            +
                      @log.debug "Comparing #{node} with #{i.node}"
         | 
| 152 | 
            +
                      if i.node == node
         | 
| 153 | 
            +
                        @log.debug "Match found, returning #{i.to_s}"
         | 
| 154 | 
            +
                        return i       
         | 
| 155 | 
            +
                      end
         | 
| 156 | 
            +
                    end
         | 
| 157 | 
            +
                  end
         | 
| 158 | 
            +
                  
         | 
| 159 | 
            +
                  
         | 
| 160 | 
            +
                  @log.debug "Node not found!"
         | 
| 161 | 
            +
                  # Raise NotInNodeList exception if list has been traversed and a corresponding node was
         | 
| 162 | 
            +
                  # not found
         | 
| 163 | 
            +
                  raise Ardtweeno::NotInNodeList, "Error key does not match any Ardtweeno::Node being maintained by this Ardtweeno::NodeManager"
         | 
| 164 | 
            +
                end
         | 
| 165 | 
            +
                
         | 
| 166 | 
            +
                
         | 
| 167 | 
            +
                ##
         | 
| 168 | 
            +
                # Ardtweeno::NodeManager#watched? checks if a node has been added to a watchlist
         | 
| 169 | 
            +
                #
         | 
| 170 | 
            +
                # * *Args*    :
         | 
| 171 | 
            +
                #   - ++ ->   Ardtweeno::Node node
         | 
| 172 | 
            +
                # * *Returns* :
         | 
| 173 | 
            +
                #   -         True || False
         | 
| 174 | 
            +
                # * *Raises* :
         | 
| 175 | 
            +
                #   -         
         | 
| 176 | 
            +
                #
         | 
| 177 | 
            +
                def watched?(node)
         | 
| 178 | 
            +
                  
         | 
| 179 | 
            +
                  @watchlist.each do |i|
         | 
| 180 | 
            +
                    
         | 
| 181 | 
            +
                    @log.debug "Comparing " + i[:node].node + " and " + node.node
         | 
| 182 | 
            +
                    if i[:node].node == node.node 
         | 
| 183 | 
            +
                      return true
         | 
| 184 | 
            +
                    end
         | 
| 185 | 
            +
                  end
         | 
| 186 | 
            +
                  
         | 
| 187 | 
            +
                  return false
         | 
| 188 | 
            +
                  
         | 
| 189 | 
            +
                end
         | 
| 190 | 
            +
                
         | 
| 191 | 
            +
                
         | 
| 192 | 
            +
                ##
         | 
| 193 | 
            +
                # Ardtweeno::NodeManager#addWatch adds a node to the watchlist
         | 
| 194 | 
            +
                #
         | 
| 195 | 
            +
                # * *Args*    :
         | 
| 196 | 
            +
                #   - ++ ->   String node, Hash watch { String :node, String :notifyURL, 
         | 
| 197 | 
            +
                #                                       String :method, String :timeouts }
         | 
| 198 | 
            +
                # * *Returns* :
         | 
| 199 | 
            +
                #   -        
         | 
| 200 | 
            +
                # * *Raises* :
         | 
| 201 | 
            +
                #   -         Ardtweeno::NotInNodeList
         | 
| 202 | 
            +
                #
         | 
| 203 | 
            +
                def addWatch(params)
         | 
| 204 | 
            +
                  begin
         | 
| 205 | 
            +
                    node = search({:node=>params[:node]})
         | 
| 206 | 
            +
                    @log.debug "Found Node: " + node.inspect
         | 
| 207 | 
            +
                    
         | 
| 208 | 
            +
                    watch = { :node=>node, 
         | 
| 209 | 
            +
                              :notifyURL=> params[:notifyURL],
         | 
| 210 | 
            +
                              :method=>"POST", 
         | 
| 211 | 
            +
                              :timeout=>"60" 
         | 
| 212 | 
            +
                            }
         | 
| 213 | 
            +
                    
         | 
| 214 | 
            +
                    @log.debug "Adding watch: " + watch.inspect
         | 
| 215 | 
            +
                    
         | 
| 216 | 
            +
                    @watchlist << watch
         | 
| 217 | 
            +
                    
         | 
| 218 | 
            +
                  rescue Ardtweeno::NotInNodeList => e
         | 
| 219 | 
            +
                    raise e
         | 
| 220 | 
            +
                  rescue Exception => e
         | 
| 221 | 
            +
                    raise e
         | 
| 222 | 
            +
                  end
         | 
| 223 | 
            +
                end
         | 
| 224 | 
            +
                
         | 
| 225 | 
            +
                
         | 
| 226 | 
            +
                ##
         | 
| 227 | 
            +
                # Ardtweeno::NodeManager#removeWatch removes a node from the watchlist
         | 
| 228 | 
            +
                #
         | 
| 229 | 
            +
                # * *Args*    :
         | 
| 230 | 
            +
                #   - ++ ->   String node
         | 
| 231 | 
            +
                # * *Returns* :
         | 
| 232 | 
            +
                #   -        
         | 
| 233 | 
            +
                # * *Raises* :
         | 
| 234 | 
            +
                #   -         Ardtweeno::NotInNodeList
         | 
| 235 | 
            +
                #
         | 
| 236 | 
            +
                def removeWatch(node)
         | 
| 237 | 
            +
                  begin
         | 
| 238 | 
            +
                    node = search({:node=>node})
         | 
| 239 | 
            +
                    
         | 
| 240 | 
            +
                    @watchlist.each do |i|
         | 
| 241 | 
            +
                      if i[:node] == node
         | 
| 242 | 
            +
                        @watchlist.delete(i)
         | 
| 243 | 
            +
                      end
         | 
| 244 | 
            +
                    end
         | 
| 245 | 
            +
                    
         | 
| 246 | 
            +
                  rescue Ardtweeno::NotInNodeList => e
         | 
| 247 | 
            +
                    raise e
         | 
| 248 | 
            +
                  end
         | 
| 249 | 
            +
                end
         | 
| 250 | 
            +
                
         | 
| 251 | 
            +
                
         | 
| 252 | 
            +
                ##
         | 
| 253 | 
            +
                # Ardtweeno::NodeManager#pushNotification pushes a notification to the node watcher
         | 
| 254 | 
            +
                #
         | 
| 255 | 
            +
                # * *Args*    :
         | 
| 256 | 
            +
                #   - ++ ->   String node
         | 
| 257 | 
            +
                # * *Returns* :
         | 
| 258 | 
            +
                #   -         
         | 
| 259 | 
            +
                # * *Raises* :
         | 
| 260 | 
            +
                #   -         
         | 
| 261 | 
            +
                #
         | 
| 262 | 
            +
                def pushNotification(node)
         | 
| 263 | 
            +
                  
         | 
| 264 | 
            +
                  @log.debug "Traversing watchlist"
         | 
| 265 | 
            +
                  @watchlist.each do |i|
         | 
| 266 | 
            +
                    
         | 
| 267 | 
            +
                    @log.debug "Comparing " + i[:node].node + " to " + node
         | 
| 268 | 
            +
                            
         | 
| 269 | 
            +
                    if i[:node].node == node
         | 
| 270 | 
            +
                      
         | 
| 271 | 
            +
                      @log.debug "Associated watch found, checking for method " + i[:method]
         | 
| 272 | 
            +
                      
         | 
| 273 | 
            +
                      if i[:method] == "POST"
         | 
| 274 | 
            +
                        @log.debug "HTTP POST method executing"
         | 
| 275 | 
            +
                        @log.debug "URL: #{i[:notifyURL]}"
         | 
| 276 | 
            +
                        @log.debug "Title: Push notification"
         | 
| 277 | 
            +
                        @log.debug "Content: #{i[:node].node}"
         | 
| 278 | 
            +
                        @log.debug "Code: #{i[:node].to_s}"
         | 
| 279 | 
            +
                        
         | 
| 280 | 
            +
                        Typhoeus::Request.post(i[:notifyURL], 
         | 
| 281 | 
            +
                                                 :body=> { :title=>"Push notification",
         | 
| 282 | 
            +
                                                           :content=>"#{i[:node].node}",
         | 
| 283 | 
            +
                                                           :code=>""})
         | 
| 284 | 
            +
                      elsif i[:method] == "GET"
         | 
| 285 | 
            +
                        @log.debug "HTTP GET method executing" 
         | 
| 286 | 
            +
                        Typhoeus::Request.get(i[:notifyURL], 
         | 
| 287 | 
            +
                                                :body=> { :title=>"Push notification",
         | 
| 288 | 
            +
                                                          :content=>"#{i[:node].node}",
         | 
| 289 | 
            +
                                                          :code=>""})
         | 
| 290 | 
            +
                      end
         | 
| 291 | 
            +
             | 
| 292 | 
            +
                    end
         | 
| 293 | 
            +
                  end
         | 
| 294 | 
            +
                  
         | 
| 295 | 
            +
                end
         | 
| 296 | 
            +
                
         | 
| 297 | 
            +
                
         | 
| 298 | 
            +
                
         | 
| 299 | 
            +
              end
         | 
| 300 | 
            +
            end
         | 
| @@ -0,0 +1,98 @@ | |
| 1 | 
            +
            ####################################################################################################
         | 
| 2 | 
            +
            # @author       David Kirwan <davidkirwanirl@gmail.com>
         | 
| 3 | 
            +
            # @description  Packet Communication storage class for the Ardtweeno system
         | 
| 4 | 
            +
            #
         | 
| 5 | 
            +
            # @date         14-11-2012
         | 
| 6 | 
            +
            ####################################################################################################
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            # Imports
         | 
| 9 | 
            +
            require 'rubygems'
         | 
| 10 | 
            +
            require 'logger'
         | 
| 11 | 
            +
            require 'yaml'
         | 
| 12 | 
            +
            require 'json'
         | 
| 13 | 
            +
            require 'date'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
             | 
| 16 | 
            +
            module Ardtweeno
         | 
| 17 | 
            +
              
         | 
| 18 | 
            +
              ##
         | 
| 19 | 
            +
              # Ardtweeno::Packet Communication storage class for the Ardtweeno system
         | 
| 20 | 
            +
              #
         | 
| 21 | 
            +
              class Packet
         | 
| 22 | 
            +
                
         | 
| 23 | 
            +
                # Class fields
         | 
| 24 | 
            +
                attr_accessor :key, :seqNo, :date, :hour, :minute, :node, :data 
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                ##
         | 
| 27 | 
            +
                # Ardtweeno::Packet#new for the Packet class
         | 
| 28 | 
            +
                # * *Args*    :
         | 
| 29 | 
            +
                #   - ++ -> :seqNo, :key, :data  
         | 
| 30 | 
            +
                # * *Returns* :
         | 
| 31 | 
            +
                #   -
         | 
| 32 | 
            +
                # * *Raises* :
         | 
| 33 | 
            +
                #
         | 
| 34 | 
            +
                def initialize(newSeqNo, newKey, newData)
         | 
| 35 | 
            +
                  
         | 
| 36 | 
            +
                  # Create a DateTime instance
         | 
| 37 | 
            +
                  today = DateTime.now
         | 
| 38 | 
            +
                  theDate = today.year.to_s() + "-" + "%02d" % today.month.to_s() + "-" + "%02d" % today.day.to_s()
         | 
| 39 | 
            +
                  
         | 
| 40 | 
            +
                  # Default values
         | 
| 41 | 
            +
                  @date = theDate
         | 
| 42 | 
            +
                  @hour = ("%02d" % today.hour).to_s
         | 
| 43 | 
            +
                  @minute = ("%02d" % today.min).to_s
         | 
| 44 | 
            +
                  @data = newData
         | 
| 45 | 
            +
                  @key = newKey
         | 
| 46 | 
            +
                  
         | 
| 47 | 
            +
                  # Need to implement a lookup function for key to node value
         | 
| 48 | 
            +
                  @node = "defaultNode"
         | 
| 49 | 
            +
                  
         | 
| 50 | 
            +
                  if newSeqNo.class == Fixnum
         | 
| 51 | 
            +
                    @seqNo = newSeqNo
         | 
| 52 | 
            +
                  elsif newSeqNo.class == String
         | 
| 53 | 
            +
                    @seqNo = newSeqNo.to_i
         | 
| 54 | 
            +
                  end
         | 
| 55 | 
            +
                  
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                
         | 
| 59 | 
            +
                ##
         | 
| 60 | 
            +
                # Ardtweeno::Packet#to_s returns a representation of the current instance state in String form
         | 
| 61 | 
            +
                #
         | 
| 62 | 
            +
                # * *Args*    :
         | 
| 63 | 
            +
                #   - ++ ->  
         | 
| 64 | 
            +
                # * *Returns* :
         | 
| 65 | 
            +
                #   -         String
         | 
| 66 | 
            +
                # * *Raises* :
         | 
| 67 | 
            +
                #    
         | 
| 68 | 
            +
                def to_s
         | 
| 69 | 
            +
                  # Build the string up from field data
         | 
| 70 | 
            +
                  str = "Packet No: " + @seqNo.to_s + " Key: " + @key + " Node: " + @node + " Date: " + @date +
         | 
| 71 | 
            +
                        " " + @hour + ":" + @minute + " Data: " + @data.to_s  
         | 
| 72 | 
            +
                  
         | 
| 73 | 
            +
                  # Returns the built string
         | 
| 74 | 
            +
                  return str
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
                
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                ##
         | 
| 79 | 
            +
                # Ardtweeno::Packet#to_json returns a representation of the current instance state in JSON form
         | 
| 80 | 
            +
                #
         | 
| 81 | 
            +
                # * *Args*    :
         | 
| 82 | 
            +
                #   - ++ ->  
         | 
| 83 | 
            +
                # * *Returns* :
         | 
| 84 | 
            +
                #   -         String
         | 
| 85 | 
            +
                # * *Raises* :
         | 
| 86 | 
            +
                #    
         | 
| 87 | 
            +
                def to_json(options={})
         | 
| 88 | 
            +
                  
         | 
| 89 | 
            +
                  jsonStr = '{"date":"' + @date + '","hour":"' + @hour + '","minute":"' +
         | 
| 90 | 
            +
                  @minute.to_s + '","node":"' + @node + '","key":"' + @key + '","seqNo":' +
         | 
| 91 | 
            +
                  @seqNo.to_s + ',"data":' + @data.to_json + '}'
         | 
| 92 | 
            +
                  
         | 
| 93 | 
            +
                  return jsonStr
         | 
| 94 | 
            +
                end
         | 
| 95 | 
            +
                
         | 
| 96 | 
            +
                
         | 
| 97 | 
            +
              end
         | 
| 98 | 
            +
            end
         | 
| @@ -0,0 +1,266 @@ | |
| 1 | 
            +
            $stdout.sync = true
         | 
| 2 | 
            +
            ####################################################################################################
         | 
| 3 | 
            +
            # @author       David Kirwan <davidkirwanirl@gmail.com>
         | 
| 4 | 
            +
            # @description  Ardtweeno Application Gateway HTTP REST API Sinatra Script
         | 
| 5 | 
            +
            #
         | 
| 6 | 
            +
            # @date         08-02-2013
         | 
| 7 | 
            +
            ####################################################################################################
         | 
| 8 | 
            +
            ##### Require statements
         | 
| 9 | 
            +
            require 'rubygems'
         | 
| 10 | 
            +
            require 'sinatra/base'
         | 
| 11 | 
            +
            require 'ardtweeno'
         | 
| 12 | 
            +
            require 'logger'
         | 
| 13 | 
            +
            require 'rufus/scheduler'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            class RESTAPI < Sinatra::Base
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              ##### Variables
         | 
| 18 | 
            +
              enable :static, :sessions, :logging
         | 
| 19 | 
            +
              set :environment, :production
         | 
| 20 | 
            +
              set :root, File.join(File.dirname(__FILE__) + '/../../')
         | 
| 21 | 
            +
              set :public_folder, Proc.new {File.join(root, '/public')}
         | 
| 22 | 
            +
              set :views, Proc.new {File.join(root, '/views')}
         | 
| 23 | 
            +
              
         | 
| 24 | 
            +
              # Posts Array
         | 
| 25 | 
            +
              set :posts, Array.new
         | 
| 26 | 
            +
                
         | 
| 27 | 
            +
              # Create the logger instance
         | 
| 28 | 
            +
              set :log, Logger.new(STDOUT)
         | 
| 29 | 
            +
              set :level, Logger::DEBUG
         | 
| 30 | 
            +
              #set :level, Logger::INFO
         | 
| 31 | 
            +
              #set :level, Logger::WARN
         | 
| 32 | 
            +
              
         | 
| 33 | 
            +
              # Options hash
         | 
| 34 | 
            +
              set :options, {:log => settings.log, :level => settings.level}
         | 
| 35 | 
            +
              
         | 
| 36 | 
            +
              # Rufus-scheduler object
         | 
| 37 | 
            +
              set :scheduler, Rufus::Scheduler.start_new
         | 
| 38 | 
            +
              
         | 
| 39 | 
            +
              # Setup the system for use
         | 
| 40 | 
            +
              Ardtweeno.setup(settings.options)
         | 
| 41 | 
            +
              @@theDispatcher = Ardtweeno::Dispatcher.instance
         | 
| 42 | 
            +
              
         | 
| 43 | 
            +
            #########################################################################################################  
         | 
| 44 | 
            +
             | 
| 45 | 
            +
              
         | 
| 46 | 
            +
              settings.scheduler.every '60m' do
         | 
| 47 | 
            +
                  
         | 
| 48 | 
            +
                begin
         | 
| 49 | 
            +
                  settings.log.debug "Running scheduled data flush"
         | 
| 50 | 
            +
                  @@theDispatcher.flush()
         | 
| 51 | 
            +
                                    
         | 
| 52 | 
            +
                rescue Ardtweeno::DBError => e
         | 
| 53 | 
            +
                  settings.log.warn "ERROR: #{e.message}"
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
                 
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                
         | 
| 59 | 
            +
            #########################################################################################################
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                
         | 
| 62 | 
            +
              get '/' do
         | 
| 63 | 
            +
                redirect '/home'
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
                
         | 
| 66 | 
            +
                
         | 
| 67 | 
            +
              get '/home' do
         | 
| 68 | 
            +
                running = @@theDispatcher.running?
         | 
| 69 | 
            +
                theposts = settings.posts
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                if theposts.length >= 5
         | 
| 72 | 
            +
                  theposts = theposts[(theposts.length - 5), theposts.length]
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                erb :home, :locals => {:running => running, :postdata => theposts.reverse}
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
              
         | 
| 78 | 
            +
              
         | 
| 79 | 
            +
              get '/b97cb9ae44747ee263363463b7e56/create/post' do
         | 
| 80 | 
            +
                running = @@theDispatcher.running?
         | 
| 81 | 
            +
                erb :createpost, :locals => {:running => running}
         | 
| 82 | 
            +
              end
         | 
| 83 | 
            +
              
         | 
| 84 | 
            +
              
         | 
| 85 | 
            +
              post '/b97cb9ae44747ee263363463b7e56/create/post' do
         | 
| 86 | 
            +
                settings.log.debug params.inspect
         | 
| 87 | 
            +
                
         | 
| 88 | 
            +
                thePost = Hash.new
         | 
| 89 | 
            +
                
         | 
| 90 | 
            +
                unless params["title"].nil? then thePost[:posttitle] = params["title"]; end
         | 
| 91 | 
            +
                unless params["content"].nil? then thePost[:postcontent] = params["content"]; end
         | 
| 92 | 
            +
                unless params["code"].nil? then thePost[:postcode] = params["code"]; end 
         | 
| 93 | 
            +
                
         | 
| 94 | 
            +
                settings.posts << thePost
         | 
| 95 | 
            +
                
         | 
| 96 | 
            +
                redirect '/home'
         | 
| 97 | 
            +
              end
         | 
| 98 | 
            +
                  
         | 
| 99 | 
            +
                  
         | 
| 100 | 
            +
              not_found do
         | 
| 101 | 
            +
                '404 Page Not Found'
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
              
         | 
| 104 | 
            +
              
         | 
| 105 | 
            +
            #########################################################################################################
         | 
| 106 | 
            +
              
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              get '/api/v1/zones' do
         | 
| 109 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 110 | 
            +
                settings.log.debug "The retrieve zones hook has been called"
         | 
| 111 | 
            +
                
         | 
| 112 | 
            +
                begin
         | 
| 113 | 
            +
                  @@theDispatcher.retrieve_zones(params).to_json # Returns String in JSON form
         | 
| 114 | 
            +
                rescue Exception => e
         | 
| 115 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 116 | 
            +
                end
         | 
| 117 | 
            +
              end
         | 
| 118 | 
            +
              
         | 
| 119 | 
            +
              
         | 
| 120 | 
            +
              get '/api/v1/zones/:zonename' do |zoneid|
         | 
| 121 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 122 | 
            +
                settings.log.debug "The retrieve zones hook has been called"
         | 
| 123 | 
            +
                
         | 
| 124 | 
            +
                begin
         | 
| 125 | 
            +
                  @@theDispatcher.retrieve_zones(params).to_json # Returns String in JSON form
         | 
| 126 | 
            +
                rescue Exception => e
         | 
| 127 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
              end
         | 
| 130 | 
            +
              
         | 
| 131 | 
            +
              
         | 
| 132 | 
            +
            #########################################################################################################    
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                
         | 
| 135 | 
            +
              get '/api/v1/packets' do
         | 
| 136 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 137 | 
            +
                settings.log.debug "The retrieve packets hook has been called"
         | 
| 138 | 
            +
                
         | 
| 139 | 
            +
                begin
         | 
| 140 | 
            +
                  @@theDispatcher.retrieve_packets(params).to_json # Returns String in JSON form
         | 
| 141 | 
            +
                rescue Exception => e
         | 
| 142 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 143 | 
            +
                end
         | 
| 144 | 
            +
              end
         | 
| 145 | 
            +
              
         | 
| 146 | 
            +
                
         | 
| 147 | 
            +
              post '/api/v1/packets' do 
         | 
| 148 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 149 | 
            +
                settings.log.debug "The add packets hook has been called"
         | 
| 150 | 
            +
                  
         | 
| 151 | 
            +
                settings.log.debug "Add packet API request: " + params[:payload]
         | 
| 152 | 
            +
                begin
         | 
| 153 | 
            +
                  @@theDispatcher.store(params[:payload])
         | 
| 154 | 
            +
                    
         | 
| 155 | 
            +
                rescue Ardtweeno::NodeNotAuthorised => e
         | 
| 156 | 
            +
                  throw :halt, [ 401, "401 Unauthorised" ]
         | 
| 157 | 
            +
                rescue Exception => e
         | 
| 158 | 
            +
                  throw :halt, [ 400, "400 Bad Request" ]
         | 
| 159 | 
            +
                end
         | 
| 160 | 
            +
              end
         | 
| 161 | 
            +
                
         | 
| 162 | 
            +
             | 
| 163 | 
            +
            #########################################################################################################
         | 
| 164 | 
            +
             | 
| 165 | 
            +
              get '/api/v1/nodes' do
         | 
| 166 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 167 | 
            +
                settings.log.debug "The retrieve nodes hook has been called"
         | 
| 168 | 
            +
                
         | 
| 169 | 
            +
                begin
         | 
| 170 | 
            +
                  @@theDispatcher.retrieve_nodes(params).to_json # Returns String in JSON form
         | 
| 171 | 
            +
                rescue Exception => e
         | 
| 172 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 173 | 
            +
                end
         | 
| 174 | 
            +
              end
         | 
| 175 | 
            +
             | 
| 176 | 
            +
             | 
| 177 | 
            +
            #########################################################################################################
         | 
| 178 | 
            +
             | 
| 179 | 
            +
              post '/api/v1/watch/:node' do |node|
         | 
| 180 | 
            +
                settings.log.debug params.inspect
         | 
| 181 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 182 | 
            +
                settings.log.debug "The add watch to node hook has been called"
         | 
| 183 | 
            +
                
         | 
| 184 | 
            +
                begin
         | 
| 185 | 
            +
                  @@theDispatcher.addWatch(params)
         | 
| 186 | 
            +
                rescue Exception => e
         | 
| 187 | 
            +
                  throw :halt, [ 400, "400 Bad Request" ]
         | 
| 188 | 
            +
                end
         | 
| 189 | 
            +
                
         | 
| 190 | 
            +
              end
         | 
| 191 | 
            +
             | 
| 192 | 
            +
             | 
| 193 | 
            +
            #########################################################################################################
         | 
| 194 | 
            +
              
         | 
| 195 | 
            +
              get '/api/v1/system/config' do
         | 
| 196 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 197 | 
            +
                settings.log.debug "The system config hook has been called, querying the Ardtweeno gateway to retrieve config"
         | 
| 198 | 
            +
                
         | 
| 199 | 
            +
                begin
         | 
| 200 | 
            +
                  return @@theDispatcher.config.to_json
         | 
| 201 | 
            +
                rescue Exception => e
         | 
| 202 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 203 | 
            +
                end
         | 
| 204 | 
            +
                
         | 
| 205 | 
            +
              end
         | 
| 206 | 
            +
              
         | 
| 207 | 
            +
              
         | 
| 208 | 
            +
              get '/api/v1/system/start' do
         | 
| 209 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 210 | 
            +
                settings.log.debug "The system start hook has been called, launching the Ardtweeno system"
         | 
| 211 | 
            +
                  
         | 
| 212 | 
            +
                begin
         | 
| 213 | 
            +
                  @@theDispatcher.start
         | 
| 214 | 
            +
                rescue Exception => e
         | 
| 215 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 216 | 
            +
                end 
         | 
| 217 | 
            +
                  
         | 
| 218 | 
            +
                "The Ardtweeno system is launching, this will take a moment..."
         | 
| 219 | 
            +
              end
         | 
| 220 | 
            +
                
         | 
| 221 | 
            +
                
         | 
| 222 | 
            +
              get '/api/v1/system/stop' do
         | 
| 223 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 224 | 
            +
                settings.log.debug "The system stop hook has been called, shutting the Ardtweeno system down..."
         | 
| 225 | 
            +
                
         | 
| 226 | 
            +
                begin
         | 
| 227 | 
            +
                  @@theDispatcher.stop
         | 
| 228 | 
            +
                rescue Exception => e
         | 
| 229 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 230 | 
            +
                end
         | 
| 231 | 
            +
                  
         | 
| 232 | 
            +
                "The Ardtweeno system is shutting down, this will take a moment..."    
         | 
| 233 | 
            +
              end
         | 
| 234 | 
            +
              
         | 
| 235 | 
            +
              
         | 
| 236 | 
            +
              # This is currently not implemented correctly  
         | 
| 237 | 
            +
              get '/api/v1/system/reboot' do
         | 
| 238 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 239 | 
            +
                settings.log.debug "The system reboot hook has been called, rebooting the host"
         | 
| 240 | 
            +
                  
         | 
| 241 | 
            +
                begin
         | 
| 242 | 
            +
                  @@theDispatcher.reboot
         | 
| 243 | 
            +
                rescue Exception => e
         | 
| 244 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 245 | 
            +
                end
         | 
| 246 | 
            +
                
         | 
| 247 | 
            +
                "The host is rebooting, this will take a moment..."
         | 
| 248 | 
            +
              end
         | 
| 249 | 
            +
             | 
| 250 | 
            +
             | 
| 251 | 
            +
              get '/api/v1/system/status' do
         | 
| 252 | 
            +
                throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key])
         | 
| 253 | 
            +
                settings.log.debug "The system status hook has been called, reading the host configuration"
         | 
| 254 | 
            +
             | 
| 255 | 
            +
                begin
         | 
| 256 | 
            +
                  return {:running=>@@theDispatcher.running?}.to_json
         | 
| 257 | 
            +
                rescue Exception => e
         | 
| 258 | 
            +
                  throw :halt, [ 500, "500 Internal Server Error" ]
         | 
| 259 | 
            +
                end
         | 
| 260 | 
            +
             | 
| 261 | 
            +
              end
         | 
| 262 | 
            +
             | 
| 263 | 
            +
            #########################################################################################################
         | 
| 264 | 
            +
             | 
| 265 | 
            +
            # End of RESTAPI Class
         | 
| 266 | 
            +
            end
         |