| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
local assert = assert |
| 10 |
local string = string |
| 11 |
local tonumber = tonumber |
| 12 |
local math = math |
| 13 |
local setmetatable = setmetatable |
| 14 |
local table = table |
| 15 |
local io = { |
| 16 |
open = io.open |
| 17 |
} |
| 18 |
local mouse = mouse |
| 19 |
|
| 20 |
local naughty = require 'naughty' |
| 21 |
local awful = require 'awful' |
| 22 |
local wibox = require 'wibox' |
| 23 |
|
| 24 |
local http = require 'socket.http' |
| 25 |
local json = require 'json' |
| 26 |
|
| 27 |
local lib = { |
| 28 |
hooks = require("obvious.lib.hooks"), |
| 29 |
markup = require("obvious.lib.markup") |
| 30 |
} |
| 31 |
|
| 32 |
local widget = wibox.widget { |
| 33 |
align = 'right', |
| 34 |
widget = wibox.widget.textbox, |
| 35 |
} |
| 36 |
|
| 37 |
local position = { |
| 38 |
date = 0, |
| 39 |
time = 0, |
| 40 |
latitude = 0.0, |
| 41 |
longitude = 0.0, |
| 42 |
quality = 0, |
| 43 |
altitude = 0.0, |
| 44 |
satellites = 0, |
| 45 |
satellitesData = {}, |
| 46 |
dilution = 0.0, |
| 47 |
height = 0.0, |
| 48 |
status = "V", |
| 49 |
speed = 0.0, |
| 50 |
course = 0.0, |
| 51 |
variation = 0.0, |
| 52 |
node = "" |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
local device = "/dev/rfcomm0" |
| 58 |
local function set_device(dev) |
| 59 |
device = dev |
| 60 |
end |
| 61 |
|
| 62 |
|
| 63 |
local browser = "/usr/bin/uzbl" |
| 64 |
local function set_browser(path) |
| 65 |
browser = path |
| 66 |
end |
| 67 |
|
| 68 |
|
| 69 |
local function get_data() |
| 70 |
return position |
| 71 |
end |
| 72 |
|
| 73 |
|
| 74 |
local function parse_nmea(line) |
| 75 |
local msg = string.sub(line,0,6) |
| 76 |
if (msg == "$GPGGA") then |
| 77 |
|
| 78 |
local time, latitude, longitude, quality, satellites, dilution, altitude, height, checksum = line:match("^%$GPGGA,([0-9.]+),([0-9.]+),[NS],([0-9.]+),[EW],([0-8]?),([0-9]+),([0-9.]*),([0-9.-]+),M,([0-9.]+),M,,%*(.*)") |
| 79 |
latitude = tonumber(latitude) |
| 80 |
longitude = tonumber(longitude) |
| 81 |
local dd = math.modf(latitude / 100) |
| 82 |
position.latitude = dd + (latitude - dd * 100) / 60 |
| 83 |
dd = math.modf(longitude / 100) |
| 84 |
position.longitude = dd + (longitude - dd * 100) / 60 |
| 85 |
position.time = time |
| 86 |
position.quality = tonumber(quality) |
| 87 |
position.satellites = tonumber(satellites) |
| 88 |
position.dilution = tonumber(dilution) |
| 89 |
position.altitude = tonumber(altitude) |
| 90 |
position.height = tonumber(height) |
| 91 |
elseif (msg == "$GPRMC") then |
| 92 |
|
| 93 |
local time, status, latitude, longitude, speed, course, date, variation, node, checksum = line:match("^%$GPRMC,([0-9.]+),[AV],([0-9.]+),[NS],([0-9.]+),[EW],([0-9.]+),([0-9.]+),([0-9]+),([0-9.]*),[EW]?,([NADE])%*(.*)") |
| 94 |
latitude = tonumber(latitude) |
| 95 |
longitude = tonumber(longitude) |
| 96 |
local dd = math.modf(latitude / 100) |
| 97 |
position.latitude = dd + (latitude - dd * 100) / 60 |
| 98 |
dd = math.modf(longitude / 100) |
| 99 |
position.longitude = dd + (longitude - dd * 100) / 60 |
| 100 |
position.time = time |
| 101 |
position.date = date |
| 102 |
position.status = status |
| 103 |
position.speed = tonumber(speed) |
| 104 |
position.course = tonumber(course) |
| 105 |
position.variation = tonumber(variation) |
| 106 |
position.node = node |
| 107 |
elseif (msg == "$GPGSV") then |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
end |
| 115 |
end |
| 116 |
|
| 117 |
|
| 118 |
local function reverse_geocode() |
| 119 |
local c, err, h = http.request("http://ws.geonames.org/findNearbyPlaceNameJSON?lat="..position.latitude.."&lng="..position.longitude) |
| 120 |
if c then |
| 121 |
c = json.decode(c) |
| 122 |
return c.geonames[1] |
| 123 |
end |
| 124 |
return nil |
| 125 |
end |
| 126 |
|
| 127 |
|
| 128 |
local function update() |
| 129 |
local fh = io.open(device) |
| 130 |
if fh then |
| 131 |
local line = fh:read "*l" |
| 132 |
local maxlines = 3 |
| 133 |
local l = 0 |
| 134 |
while line and l < maxlines do |
| 135 |
parse_nmea(line) |
| 136 |
l = l + 1 |
| 137 |
end |
| 138 |
fh:close() |
| 139 |
end |
| 140 |
widget.text = string.format("%f,%f", position.latitude, position.longitude) |
| 141 |
end |
| 142 |
|
| 143 |
|
| 144 |
local function detail() |
| 145 |
local d = string.format("Latitude:\t%f\nLongitude:\t%f\nAltitude:\t%f\nDilution:\t%f", position.latitude, position.longitude, position.altitude, position.dilution) |
| 146 |
naughty.notify({ |
| 147 |
text = d, |
| 148 |
screen = mouse.screen |
| 149 |
}) |
| 150 |
end |
| 151 |
|
| 152 |
|
| 153 |
local function lookup() |
| 154 |
local geodata = reverse_geocode() |
| 155 |
if geodata then |
| 156 |
local d = string.format("Country:\t%s\nName:\t%s", geodata.countryName, geodata.name) |
| 157 |
naughty.notify({ |
| 158 |
text = d, |
| 159 |
screen = mouse.screen |
| 160 |
}) |
| 161 |
end |
| 162 |
end |
| 163 |
|
| 164 |
|
| 165 |
local function openmap() |
| 166 |
local link = "http://www.openstreetmap.org/?lat="..position.latitude.."&lon="..position.longitude.."&zoom=14&layers=B00FTF" |
| 167 |
awful.util.spawn(browser .. " " .. link) |
| 168 |
end |
| 169 |
|
| 170 |
widget:buttons(awful.util.table.join( |
| 171 |
awful.button({ }, 1, detail), |
| 172 |
awful.button({ }, 2, openmap), |
| 173 |
awful.button({ }, 3, lookup) |
| 174 |
)) |
| 175 |
|
| 176 |
update() |
| 177 |
lib.hooks.timer.register(60, 300, update) |
| 178 |
lib.hooks.timer.start(update) |
| 179 |
|
| 180 |
return setmetatable({ |
| 181 |
get_data = get_data, |
| 182 |
set_browser = set_browser, |
| 183 |
set_device = set_device, |
| 184 |
}, { __call = function () return widget end }) |
| 185 |
|
| 186 |
|