![]() |
|
API Get help - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: API Get help (/showthread.php?tid=5116) |
API Get help - Novodk - 27.11.2023 I'm trying to get some data from an API, I managed to connect to the API and get the result in the log, but I'm having trouble understanding how to add the data to virtual group addresses. Code: http = require('socket.http')
ltn12 = require('ltn12')
res, code = http.request({
url = 'https://horsensvej.rokum.ro/api/v1/entries?token=https%3A%2F%2Fhorsensvej.rokum.ro%2F%3Ftoken%3Dknx-2e4b5b25355f26bb',
method = 'GET',
})
log(res, code)The result I get in log is: Code: * arg: 1
* string: "2023-11-27T21:26:49.676Z" 1701120409676 219 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T21:21:49.215Z" 1701120109215 207 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T21:16:47.774Z" 1701119807774 195 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T21:11:46.145Z" 1701119506145 184 "Flat" "xDrip-LimiTTer"
"2023-11-27T21:06:44.795Z" 1701119204795 183 "Flat" "xDrip-LimiTTer"
"2023-11-27T21:01:43.307Z" 1701118903307 183 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T20:56:42.074Z" 1701118602074 168 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T20:51:40.281Z" 1701118300281 153 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T20:46:38.060Z" 1701117998060 139 "SingleUp" "xDrip-LimiTTer"
"2023-11-27T20:41:37.989Z" 1701117697989 128 "FortyFiveUp" "xDrip-LimiTTer"
* arg: 2
* number: 200What now? I need to get the data sorted or something? I would prefer NOT to get the result, but some pointers so I can understand/learn it. RE: API Get help - admin - 28.11.2023 The data looks tab-separated. This code will split the data into separate lines and then each line into separate items. Code: lines = data:split('\n')
for _, line in ipairs(lines) do
items = line:split('\t')
log(items)
endRE: API Get help - Novodk - 29.01.2024 (28.11.2023, 06:56)admin Wrote: The data looks tab-separated. This code will split the data into separate lines and then each line into separate items. Okay, I have had some time to look at this again, and I admit I need more help. The code you provided put the data into separate items, that I should be able to put in group addresses with the grp.checkupdate, but I can't get it working! RE: API Get help - admin - 29.01.2024 Post your full script and describe what exactly you want to achieve. RE: API Get help - Novodk - 29.01.2024 (29.01.2024, 10:58)admin Wrote: Post your full script and describe what exactly you want to achieve. Code: http = require('socket.http')
ltn12 = require('ltn12')
res, code = http.request({
url = 'https://horsensvej.rokum.ro/api/v1/entries?token=https%3A%2F%2Fhorsensvej.rokum.ro%2F%3Ftoken%3Dknx-2e4b5b25355f26bb',
method = 'GET',
})
log(res, code)Code: lines = data:split('\n')
for _, line in ipairs(lines) do
items = line:split('\t')
log(items)
endCode: api 29.01.2024 12:55:29
Resident script:9: attempt to index global 'data' (a nil value)
stack traceback:I want to put each segment into its own group address, Date, rawvalue, value, direction and CGM Code: "2023-11-27T21:26:49.676Z" 1701120409676 219 "SingleUp" "xDrip-LimiTTer"RE: API Get help - admin - 29.01.2024 Try this: Code: res, code = require('socket.http').request({
url = 'https://horsensvej.rokum.ro/api/v1/entries?token=https%3A%2F%2Fhorsensvej.rokum.ro%2F%3Ftoken%3Dknx-2e4b5b25355f26bb',
method = 'GET',
})
if not res then
log('request failed', code)
return
end
lines = res:split('\n')
items = lines[ 1 ]:trim():split('\t')
for i, item in ipairs(items) do
items[ i ] = item:gsub('^"(.*)"$', '%1')
end
grp.checkupdate('1/1/1', items[ 1 ])
grp.checkupdate('1/1/2', items[ 2 ])
grp.checkupdate('1/1/3', items[ 3 ])
grp.checkupdate('1/1/4', items[ 4 ])
grp.checkupdate('1/1/5', items[ 5 ])RE: API Get help - Novodk - 29.01.2024 (29.01.2024, 12:54)admin Wrote: Try this: Works like a charm, as always. How would you go about changing the first item, that I assume is time/date in ISO 8601, to 3 byte time /day? RE: API Get help - admin - 29.01.2024 Use this: Code: datetime = items[ 1 ]
year, month, day, hour, minute, second = datetime:match('(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)')
date = { year = year, month = month, day = day }
time = { hour = hour, minute = minute, second = second }
grp.checkupdate('0/0/1', date)
grp.checkupdate('0/0/2', time) |