Module: Mycroft::Messages

Includes:
Helpers
Included in:
Client
Defined in:
lib/mycroft/messages.rb

Instance Method Summary collapse

Methods included from Helpers

#parse_message, #send_message, #update_dependencies

Instance Method Details

#broadcast(content) ⇒ Object

Sends a broadcast to the mycroft message board



83
84
85
86
87
88
89
90
# File 'lib/mycroft/messages.rb', line 83

def broadcast(content)
  message = {
    id: SecureRandom.uuid,
    content: content
  }

  send_message('MSG_BROADCAST', message)
end

#connect_to_mycroftObject

connects to mycroft aka starts tls if necessary



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mycroft/messages.rb', line 10

def connect_to_mycroft
  if ARGV.include?("--no-tls")
    @client = TCPSocket.open(@host, @port)
  else
    socket = TCPSocket.new(@host, @port)
    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.cert = OpenSSL::X509::Certificate.new(File.open(@cert))
    ssl_context.key = OpenSSL::PKey::RSA.new(File.open(@key))
    @client = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
    begin
      @client.connect
    rescue
    end
  end
end

#downObject

Sends app down to mycroft



43
44
45
# File 'lib/mycroft/messages.rb', line 43

def down
  send_message('APP_DOWN')
end

#in_use(priority) ⇒ Object



47
48
49
# File 'lib/mycroft/messages.rb', line 47

def in_use(priority)
  send_message('APP_IN_USE', {priority: (priority or 30)})
end

#query(capability, action, data, priority = 30, instance_id = nil) ⇒ Object

Sends a query to mycroft



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mycroft/messages.rb', line 52

def query(capability, action, data, priority = 30, instance_id = nil)
  query_message = {
    id: SecureRandom.uuid,
    capability: capability,
    action: action,
    data: data,
    priority: priority,
    instanceId: []
  }
  query_message[:instanceId] = instance_id unless instance_id.nil?

  send_message('MSG_QUERY', query_message)
end

#query_fail(id, message) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mycroft/messages.rb', line 74

def query_fail(id, message)
  query_fail_message = {
    id: id,
    message: message
  }
  send_message('MSG_QUERY_FAIL', query_fail_message)
end

#query_success(id, ret) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/mycroft/messages.rb', line 66

def query_success(id, ret)
  query_success_message = {
    id: id,
    ret: ret
  }
  send_message('MSG_QUERY_SUCCESS', query_success_message)
end

#send_manifestObject

Sends the app manifest to mycroft



27
28
29
30
31
32
33
34
35
# File 'lib/mycroft/messages.rb', line 27

def send_manifest
  begin
    manifest = JSON.parse(File.read(@manifest))
    manifest['instanceId'] = "#{Socket.gethostname}_#{SecureRandom.uuid}" if @generate_instance_ids
    @instance_id = manifest['instanceId']
  rescue
  end
  send_message('APP_MANIFEST', manifest)
end

#upObject

Sends app up to mycroft



38
39
40
# File 'lib/mycroft/messages.rb', line 38

def up
  send_message('APP_UP')
end