Skip to content

Commit

Permalink
ffac-update-location-gps: add first version to update a location base…
Browse files Browse the repository at this point in the history
…d on an attached GPS device
  • Loading branch information
maurerle committed Jul 22, 2024
1 parent e3e0b5a commit fb6764b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ffac-update-location-gps/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2024 Florian Maurer, xelo
# SPDX-License-Identifier: MIT
include $(TOPDIR)/rules.mk

PKG_NAME:=ffac-update-location-gps
PKG_VERSION:=1.0
PKG_RELEASE:=1

PKG_LICENSE:=MIT

include $(TOPDIR)/../package/gluon.mk

define Package/$(PKG_NAME)
TITLE:=Use attached GPS controller to update location from it
DEPENDS:=+kmod-usb-core +kmod-usb-acm +kmod-usb-serial-pl2303
endef

define Package/$(PKG_NAME)/description
The functionality of this is meant to update the location based on the NMEA output of a location system
endef

$(eval $(call BuildPackageGluon,$(PKG_NAME)))
2 changes: 2 additions & 0 deletions ffac-update-location-gps/files/etc/config/update-location-gps
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config settings 'settings'
option enabled '0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
TTYPATH="/sys${DEVPATH}/tty"
LOCKPATH="/tmp/update-location-gps"

ENABLED=$(uci get update-location-gps.settings.enabled)

if [ "${ACTION}" = "add" ]; then
[ "${ENABLED}" != "1" ] && exit 0
test -e "${LOCKPATH}" && exit 0
if test -e "${TTYPATH}"; then
TTY_NAME=$(find "${TTYPATH}" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sed -n 's|.*/tty/||p')
logger -t hotplug "USB GPS device was plugged in"
echo "${DEVPATH}" > "${LOCKPATH}"
echo "*/5 * * * * /usr/bin/update-location-gps ${TTY_NAME}" > "/usr/lib/micron.d/update-location-gps"
fi
fi

if [ "${ACTION}" = "remove" ]; then
if [ "x${DEVPATH}" = "x$(cat ${LOCKPATH})" ]; then
logger -t hotplug "USB GPS device was removed"
rm -f "/usr/lib/micron.d/update-location-gps"
fi
fi
73 changes: 73 additions & 0 deletions ffac-update-location-gps/luasrc/usr/bin/update-location-gps
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/lua

-- Get GPS device tty
local TTY_DEVICE = arg[1]

local uci = require('simple-uci').cursor()

os.execute("stty -F " .. TTY_DEVICE .. " raw 4800 cs8 clocal -parenb crtscts -cstopb")

-- Use GPS as Stream
local file = io.open(TTY_DEVICE, "r")

while true do
local this_line = file:read("*line")
if not this_line then break end -- Exit loop if no more lines

local nc = this_line:match("^([^,]+)")

if nc == '$GPRMC' then
local fields = {}
for field in this_line:gmatch("([^,]+)") do
table.insert(fields, field)
end

local valid = fields[3]

if valid == "A" then
-- First: Retrieve coordinate
local lat = fields[4]
local lon = fields[6]

-- Second: Determine if coordinate is oriented North/South or East/West
local latdir = fields[5]
local londir = fields[7]

-- Split DEGREES from coordinate
local latdeg = tonumber(lat:sub(1, 2))
local londeg = tonumber(lon:sub(1, 3))

-- Split MINUTES.SECONDS from coordinate
local latmin = tonumber(lat:sub(3))
local lonmin = tonumber(lon:sub(4))

-- Convert from Degree-Minutes to Decimal-Minutes
local latdec = latmin / 60
local londec = lonmin / 60

-- Use negative notation instead of North/South or East/West
if latdir == 'S' then
latdeg = -latdeg
end
if londir == 'W' then
londeg = -londeg
end
lat = string.format("%f", latdeg + latdec)
lon = string.format("%f", londeg + londec)

print("Position is valid Lat/Lon:", lat, lon)
-- set temp location in gluon-node-info
uci:set('gluon-node-info', '@location[0]', 'share_location', '1')
uci:set('gluon-node-info', '@location[0]', 'latitude', lat)
uci:set('gluon-node-info', '@location[0]', 'longitude', lon)
uci:save('gluon-node-info')
break
else
print("Position is Invalid...", valid)
break
end
end
end

file:close()
os.exit(0)

0 comments on commit fb6764b

Please sign in to comment.