Community discussions

 
User avatar
zervan
Member
Member
Topic Author
Posts: 324
Joined: Fri Aug 20, 2010 10:43 pm
Location: Slovakia
Contact:

/tool fetch - to variable

Sat Mar 04, 2017 9:33 am

Hello, I have this script:
:local IP "10.111.0.22"
:local name "Senzi"

:local file "sdcard/json-$name.txt"
/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}" mode=http dst-path=$file
:delay 1
:local json [/file get $file contents]
:if ([:find $json "video"] + 0 = 0) do={
  /log info "TV $name play"
  /tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.Open\",\"params\":{\"item\":{\"playlistid\":0},\"options\":{\"repeat\":\"all\"}},\"id\":1}" mode=http keep-result=no
}
/file remove $file
It is working, but I have more than one script (different IPs and names) and two things bother me:
  • each time a file is created - it is using flash or sdcard
  • log is full of "info - fetch: file ... downloaded"
I would like to run script each few minutes.

So - is there a way to improve the script? It would be very nice if the file will not be necessary - is it possible to fetch to variable?
Dusan Zervan from Slovakia
MTCNA, MTCRE
 
User avatar
genesispro
Member Candidate
Member Candidate
Posts: 135
Joined: Fri Mar 14, 2014 12:33 pm

Re: /tool fetch - to variable

Thu Apr 13, 2017 4:08 pm

I have a similar problem

I want to read the result of a fetch being 1 or 0
and if 1 then do something if 0 do nothing

but I always have to write to the flash to read it back unfortunately

we should be able to do something like
:local answer [/tools fetch url="http://example.com" keep-result=no]

any solutions would be appreciated
 
yyyxyyyz
just joined
Posts: 5
Joined: Sun Apr 09, 2017 7:59 pm

Re: /tool fetch - to variable

Mon Jun 12, 2017 12:56 pm

+1 useful on api source site
 
nelfou
just joined
Posts: 17
Joined: Wed Mar 22, 2017 3:10 pm

Re: /tool fetch - to variable  [SOLVED]

Tue Mar 06, 2018 4:53 pm

Same here, that would be handy.

My workaround is to send current state with url parameters, then if there is nothing to change the HTTP server answers with status code 400. This raises an error at mikrotik side and aborts the script without writing any file or log entry.
 
AvAkS
just joined
Posts: 1
Joined: Sun Jun 03, 2018 5:50 pm

Re: /tool fetch - to variable

Sun Jun 03, 2018 6:01 pm

Same here, that would be handy.

My workaround is to send current state with url parameters, then if there is nothing to change the HTTP server answers with status code 400. This raises an error at mikrotik side and aborts the script without writing any file or log entry.
This solve is not so good as can be. "Error/not error" is like an "0" or "1" answeer :). But if I want to send a data to mikrotik (some updates of parameters) as an answeer of a get-request - I can't use this solution. In a 6.42 firmware was a good news: "*) fetch - added "output" option for all modes in order to return result to file, variable or ignore it;" May be it is what we all looking for? Does anybody use this option ("output")? Official Wiki don't give any instructions about that. How we can use "output" parameter?
 
ssbaksa
newbie
Posts: 28
Joined: Tue Oct 20, 2015 10:38 am

Re: /tool fetch - to variable

Thu Jun 07, 2018 10:24 am

Same here, that would be handy.

My workaround is to send current state with url parameters, then if there is nothing to change the HTTP server answers with status code 400. This raises an error at mikrotik side and aborts the script without writing any file or log entry.
This solve is not so good as can be. "Error/not error" is like an "0" or "1" answeer :). But if I want to send a data to mikrotik (some updates of parameters) as an answeer of a get-request - I can't use this solution. In a 6.42 firmware was a good news: "*) fetch - added "output" option for all modes in order to return result to file, variable or ignore it;" May be it is what we all looking for? Does anybody use this option ("output")? Official Wiki don't give any instructions about that. How we can use "output" parameter?

It still doesn't work. Below is the answer from support@mikrotik.com
Would bi nice to have it but ...

> Hello,
> At the moment you can not read this value in variable yet. Unfortunately we have not managed to finalise this new feature and it still requires an improvements.
> Please follow our changelog and we will update it when additional feature to fetch tool will be added that allows to read received data.

> Best regards,
> Martins S.
 
User avatar
zervan
Member
Member
Topic Author
Posts: 324
Joined: Fri Aug 20, 2010 10:43 pm
Location: Slovakia
Contact:

Re: /tool fetch - to variable

Thu Sep 27, 2018 8:18 pm

Since RouterOS 6.43 it s possible to fetch to variable, no file is needed:
:local IP "10.111.0.21"
:local name "Povazie"

/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Input.Select\",\"id\":1}" mode=http keep-result=no
:local json [/tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.GetActivePlayers\",\"id\":1}" mode=http as-value output=user]
:if ([:find ($json->"data") "video"] + 0 = 0) do={
  /log info "TV $name play"
  /tool fetch url="http://$IP/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.Open\",\"params\":{\"item\":{\"directory\":\"/home/osmc/Movies\"},\"options\":{\"repeat\":\"all\"}},\"id\":1}" mode=http keep-result=no
}
I don't understand what output=user means, but it is necessary.
Dusan Zervan from Slovakia
MTCNA, MTCRE
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5934
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: /tool fetch - to variable

Sat Sep 29, 2018 8:32 am

It means that fetch output will be sent to user variable.

Who is online

Users browsing this forum: No registered users and 13 guests