Class: CommaAPI::Athena

Inherits:
Object
  • Object
show all
Extended by:
ConfigAthena, HTTP
Defined in:
lib/athena_api.rb

Constant Summary collapse

STATE =
{
  rpc_id: 0
}
RpcId =
-> {
  STATE[:rpc_id] += 1
}

Constants included from ConfigAthena

ConfigAthena::API_HOST, ConfigAthena::DONGLE_ID_DEFAULT

Class Method Summary collapse

Methods included from HTTP

http, post_request, request

Class Method Details

.androidLogObject



30
31
32
# File 'lib/athena_api.rb', line 30

def androidLog
  post_rpc m: "getMessage", service: "androidLog"
end

.carStateObject



22
23
24
# File 'lib/athena_api.rb', line 22

def carState
  post_rpc m: "getMessage", service: "carState"
end

.gpsLocationObject



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

def gpsLocation
  post_rpc m: "getMessage", service: "gpsLocation"
end

.healthObject



18
19
20
# File 'lib/athena_api.rb', line 18

def health
  post_rpc m: "getMessage", service: "health"
end

.logMessageObject



26
27
28
# File 'lib/athena_api.rb', line 26

def logMessage
  post_rpc m: "getMessage", service: "logMessage"
end

.parse_rpc(resp, service:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/athena_api.rb', line 78

def parse_rpc(resp, service:)
  return RPCError.new if resp["error"] == "Timed out"
  return RPCError.new if resp["error"] && resp["error"]["message"] == "Server error"
  result = resp.fetch "result"
  result = result[service] if result[service]
  result = result["androidLogEntry"] if service == "androidLog"
  # TODO: use inflecto gem to snakecase keys
  result = JSON.parse result if result.is_a? String # TODO: do this only for `logMessage` service
  result.sym_keys
end

.post_rpc(m:, service:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/athena_api.rb', line 66

def post_rpc(m:, service:)
  puts "-> rpc message: m: #{m.inspect}, service: #{service.inspect}"
  url = "#{API_HOST}/#{DONGLE_ID_DEFAULT}"
  data = rpc_msg(m: m, service: service)
  begin
    resp = post_request url: url, data: data
  rescue
    return RPCError.new "JSON Parse Error"
  end
  parse_rpc resp, service: service
end

.procLogObject



34
35
36
# File 'lib/athena_api.rb', line 34

def procLog
  post_rpc m: "getMessage", service: "procLog"
end

.rpc_msg(m:, service:) ⇒ Object




57
58
59
60
61
62
63
64
# File 'lib/athena_api.rb', line 57

def rpc_msg(m:, service:)
  {
    method: m,
    params: { service: service, timeout: 3000 },
    jsonrpc: "2.0",
    id: RpcId.(),
  }.str_keys.to_json
end

.thermalObject



42
43
44
45
46
# File 'lib/athena_api.rb', line 42

def thermal
  data = post_rpc m: "getMessage", service: "thermal"
  data[:cpu] = thermal_cpu_avg(data: data)
  data
end

.thermal_cpu_avg(data:) ⇒ Object




50
51
52
53
# File 'lib/athena_api.rb', line 50

def thermal_cpu_avg(data:)
  temps = [data.f(:cpu0), data.f(:cpu1), data.f(:cpu2), data.f(:cpu3)]
  (temps.reduce(:+).to_f / temps.size).round
end