change network request callback
This commit is contained in:
@@ -63,7 +63,9 @@ First line<br/>Second line
|
||||
* `http:put(url, [id])` - выполняет запрос HTTP PUT;
|
||||
* `http:delete(url, [id])` - выполняет запрос HTTP DELETE.
|
||||
|
||||
Эти функции не возвращают никакого значения, а вместо этого вызывают колбек `on_network_result(string, [code], [id])`. Первый аргумент: тело ответа, второй (опциональный) - код (200, 404 и т.д.), третий (опциональный) - строка, идентифицирующая запрос, если она была указана в запросе.
|
||||
Эти функции не возвращают никакого значения, а вместо этого вызывают колбек `on_network_result(string, [code])`. Первый аргумент: тело ответа, второй (опциональный) - код (200, 404 и т.д.).
|
||||
|
||||
Если в запросе был указан `id`, то функция вместо описанного выше колбека вызовет `on_network_result_$id(string, [code])`. То есть если id равен "server1", то колбек будет иметь вид `on_network_result_server1(string, [code])`.
|
||||
|
||||
# Функции обработки данных
|
||||
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
ip_service_url = "https://freegeoip.app/json/"
|
||||
addr_service_url = "https://nominatim.openstreetmap.org/reverse?format=json"
|
||||
|
||||
function on_alarm()
|
||||
request_ip()
|
||||
http:get(ip_service_url, "ip")
|
||||
end
|
||||
|
||||
function on_network_result(result, code, id)
|
||||
if id == "ip" then
|
||||
local location = {
|
||||
ajson:get_value(result, "object string:latitude"),
|
||||
ajson:get_value(result, "object string:longitude")
|
||||
}
|
||||
request_addr(location)
|
||||
elseif id == "adr" then
|
||||
local adr = ajson:get_value(result, "object string:display_name")
|
||||
ui:show_text(adr)
|
||||
end
|
||||
function on_network_result_ip(result)
|
||||
local location = {
|
||||
ajson:get_value(result, "object string:latitude"),
|
||||
ajson:get_value(result, "object string:longitude")
|
||||
}
|
||||
http:get(addr_service_url.."&lat="..location[1].."&lon=".. location[2].."&addressdetails=1", "addr")
|
||||
end
|
||||
|
||||
function request_ip()
|
||||
http:get("https://freegeoip.app/json/", "ip")
|
||||
end
|
||||
|
||||
function request_addr(location)
|
||||
http:get("https://nominatim.openstreetmap.org/reverse?format=json&lat="..location[1].."&lon=".. location[2].."&addressdetails=1", "adr")
|
||||
function on_network_result_addr(result)
|
||||
local adr = ajson:get_value(result, "object string:display_name")
|
||||
ui:show_text(adr)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user