class CommaAPI::Athena

Constants

RpcId
STATE

Public Class Methods

androidLog() click to toggle source
# File lib/athena_api.rb, line 30
def androidLog
  post_rpc m: "getMessage", service: "androidLog"
end
carState() click to toggle source
# File lib/athena_api.rb, line 22
def carState
  post_rpc m: "getMessage", service: "carState"
end
gpsLocation() click to toggle source
# File lib/athena_api.rb, line 38
def gpsLocation
  post_rpc m: "getMessage", service: "gpsLocation"
end
health() click to toggle source
# File lib/athena_api.rb, line 18
def health
  post_rpc m: "getMessage", service: "health"
end
logMessage() click to toggle source
# File lib/athena_api.rb, line 26
def logMessage
  post_rpc m: "getMessage", service: "logMessage"
end
parse_rpc(resp, service:) click to toggle source
# 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:) click to toggle source
# 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
procLog() click to toggle source
# File lib/athena_api.rb, line 34
def procLog
  post_rpc m: "getMessage", service: "procLog"
end
rpc_msg(m:, service:) click to toggle source

# 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
thermal() click to toggle source
# 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:) click to toggle source

# 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