Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/meshx-plugin-sdk.rb

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ Controller

Returns a new instance of Controller.



117
118
119
120
121
122
# File 'lib/meshx-plugin-sdk.rb', line 117

def initialize(plugin)
  @api = FoundationApi.new()
  @log = Logger.new(STDOUT)
  @config = Config.new()
  @plugin = Plugin.new(@api, @log, @config)
end

Instance Method Details

#runObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/meshx-plugin-sdk.rb', line 131

def run()
  exit_run = false
  begin
    @log.info("calling on_init()")
    @plugin.on_init()
  rescue StandardError => e
    @log.error("error calling on_init(): #{e.message}")
  end
  
  begin
    @log.info("calling on_start()")
    @plugin.on_start()
  rescue StandardError => e
    @log.error("error calling on_start(): #{e.message}")
  end

  keep_reading = true
  @log.info("starting controller loop")
  while keep_reading && exit_run == false
    begin
      exit_run = @plugin.exit?()
      rtn = @api.get_messages()
      @log.info("rtn: #{rtn.message} #{rtn.error} #{rtn.exit_code}")
      # convert message to fileevent
      if valid_json?(rtn.message)
        hash = JSON.parse()
        hash["items"].each do |item|
          @log.info("message kind: #{item["kind"]}")
          case item["kind"]
          when "FileEvent"
            @log.info("FileEvent")
            event = FileEvent.new(item)
            @plugin.on_file_event(event)
          when "FolderEvent"
            @log.info("FolderEvent")
            event = FolderEvent.new(item)
            @plugin.on_folder_event(event)
          when "CompleteEvent"
            @log.info("CompleteEvent")
            event = CompleteEvent.new(item)
            @plugin.on_complete_event(event)
            keep_reading = false
          else
            @log.info("unknown event kind: #{item["kind"]}")
          end
        end
      else
        @log.info("invalid message [#{rtn.message}]")        
      end             
    rescue Exception => e
      @log.error("error event processing: #{e.message}")
    end
  end
  @log.info("ending controller loop sending complete event")
  res2 = @api.send_complete_events()
  if res2.exit_code != 0
    @log.error("failed to send complete event: #{res2.error} #{res2.exit_code}")
  end
  
  begin
    @log.info("calling on_end()")
    @plugin.on_end()
  rescue StandardError => e
    @log.error("error calling on_end(): #{e.message}")
  end

end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
# File 'lib/meshx-plugin-sdk.rb', line 124

def valid_json?(json)
  JSON.parse(json)
  true
rescue JSON::ParserError, TypeError => e
  false
end