Assuming the 'once' parameter works and that '/system/gps/monitor' works in the API much like
changip's CLI example, then using the
Ruby API command-line example script
tikjson.rb and
changip's example GPS output, one could retrieve the GPS info thus:
user@unixhost:/path% ./tikjson.rb 10.20.30.40 userid password /system/gps/monitor =once=
[{'date-and-time': 'may/26/2008 02:16:19','longitude': 'W 117 X\' 51\"','latitude': 'N 33 X\' 48'\'','altitude': '201.699997m','speed': '0.166680 km/h','valid': 'yes','.tag': 2,'!re': NULL},{'!done': NULL,'.tag': 2}]
user@unixhost:/path%
Or using the Ruby GEM:
#!/usr/local/bin/ruby
require 'rubygems'
require 'mtik'
TIKHOST='10.20.30.40'
TIKUSER='admin'
TIKPASS='password'
def get_location(ip, user, pass)
location = MTik::command(
:host => TIKHOST,
:user => TIKUSER,
:pass => TIKPASS,
:command => [
'/system/gps/monitor',
'=once='
]
)[0]
unless location.key?('!re')
raise RuntimeError.new("No valid reply from MikroTik device to /system/gps/monitor command.")
end
unless location['valid'] == 'yes'
raise RuntimeError.new("GPS location was read, but invalid.")
end
return location
end
l = get_location(TIKHOST, TIKUSER, TIKPASS)
puts "GPS Latitude #{l['latitude']} Longitude #{l['longitude']}"
puts "Altitude #{l['altitude']} Date/Time #{l['date-and-time']} Speed #{l['speed']}"
Which might output (barring errors/exceptions):
GPS Latitude N 33 X' 48' Longitude W 117 X' 51''
Altitude 201.699997m Date/Time may/26/2008 02:16:19 Speed 0.166680 km/h
If you want to save the location in a database, require 'mysql' or another DB (or even 'activerecord') and insert the data. You could even run an eternal script that connected to the device, then continuously monitored the GPS location (by not using the '=once=' parameter) and processing each reply sentence as received:
#!/usr/local/bin/ruby
require 'rubygems'
require 'mtik'
TIKHOST='10.20.30.40'
TIKUSER='admin'
TIKPASS='password'
tikcon = MTik::Connection.new(
:host => TIKHOST,
:user => TIKUSER,
:pass => TIKPASS
)
tikcon.get_reply_each('/system/gps/monitor') do |req, sentence|
if sentence.key?('!re') && sentence['valid'] == 'yes'
puts "Time: #{sentence['date-and-time'] Lat: #{sentence['latitude']} " +
"Lon: #{sentence['longitude']} Alt: #{sentence['altitude']} " +
"Speed: #{sentence['speed']}\n"
end
end
## This runs the event loop which should keep reading replies for as long as the device sends 'em
tikcon.wait_all
## This should never happen:
tikcon.close
Which might output a sequence like:
Time: may/26/2008 02:16:19 Lat: N 33 X' 48' Lon: W 117 X' 51'' Alt: 201.699997m Speed: 0.166680 km/h
Time: may/26/2008 02:16:20 Lat: N 33 X' 48' Lon: W 117 X' 50'' Alt: 202.262352m Speed: 2.235236 km/h
Time: may/26/2008 02:16:21 Lat: N 33 X' 48' Lon: W 117 X' 50'' Alt: 201.693527m Speed: 1.012523 km/h
Time: may/26/2008 02:16:22 Lat: N 33 X' 49' Lon: W 117 X' 50'' Alt: 200.838820m Speed: 5.936262 km/h
...
The API in any of the API languages is very useful.
Aaron out.
Disclaimer: No code above was tested for syntatic correctness, and I don't have a GPS attached to a 'Tik so I dunno exact API commands nor output sentence structure. But the basics should be there...