0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Traccar Client の位置情報を Dawarich (OwnTracks形式) へ変換して保存するプロキシを構築する

0
Last updated at Posted at 2026-04-30

はじめに
位置情報セルフホストサーバー Dawarich は非常に強力ですが、標準では OwnTracks 形式のJSONを期待します。一方、オフライン蓄積機能に定評のある Traccar Client はデータ構造が異なるため、そのままではデータを受理してもらえません。

本記事では、Python (Flask) を使って、Traccar形式をOwnTracks形式にリアルタイム変換する「翻訳プロキシ」を構築し、この問題を解決する方法を紹介します。

  1. 課題:データ構造の不一致
    Traccar Client が送信する JSON は location キーの中に座標データがネストされていますが、Dawarich(OwnTracksモード)が期待するのはフラットな構造です。

Traccar形式 (受信データ)
JSON
{
"location": {
"coords": {
"latitude": 35.100,
"longitude": 138.800,
"accuracy": 15.0,
"speed": 0.0,
"altitude": 10.0
},
"timestamp": "2026-04-29T13:16:59.474Z"
}
}
OwnTracks形式 (Dawarichの期待値)
JSON
{
"_type": "location",
"lat": 35.100,
"lon": 138.800,
"acc": 15.0,
"vel": 0.0,
"alt": 10.0,
"tst": 1714393019
}
2. 解決策:Pythonによる「翻訳プロキシ」
Raspberry Pi 等のサーバー上で、この差分を埋める Flask サーバーを稼働させます。

準備
必要なライブラリをインストールします(環境に応じて pip または apt を使用)。

Bash
sudo apt update
sudo apt install python3-flask python3-requests
プロキシスクリプト (proxy.py)
Python
import requests
from flask import Flask, request
from datetime import datetime

app = Flask(name)

DawarichのAPIエンドポイント(各自のAPIキーに差し替えてください)

DAWARICH_URL = "http://localhost:3000/api/v1/owntracks/points?api_key=YOUR_API_KEY"

@app.route('/traccar', methods=['POST'])
def proxy():
data = request.json
try:
loc = data['location']
coords = loc['coords']

    # ISO形式の時間をUNIXタイムスタンプに変換
    dt = datetime.strptime(loc['timestamp'], "%Y-%m-%dT%H:%M:%S.%fZ")
    
    # OwnTracks形式へ整形
    payload = {
        "_type": "location",
        "lat": coords['latitude'],
        "lon": coords['longitude'],
        "alt": coords['altitude'],
        "vel": max(0, coords['speed']), # 負の値は0にする
        "tst": int(dt.timestamp()),
        "acc": coords['accuracy'],
        "tid": "GTI" # 任意。地図上の識別子
    }

    # Dawarichへ転送
    res = requests.post(DAWARICH_URL, json=payload)
    print(f"Forwarded to Dawarich: {res.status_code}")
    return "OK", res.status_code
except Exception as e:
    print(f"Error: {e}")
    return str(e), 500

if name == 'main':
# 5055番ポートで待ち受け
app.run(host='0.0.0.0', port=5055)
3. サーバー常駐化 (systemd)
サーバー再起動時にも自動起動するよう、systemd に登録します。

/etc/systemd/system/dawarich-proxy.service を作成:

Ini, TOML
[Unit]
Description=Dawarich Traccar-to-OwnTracks Proxy
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/dawarich-proxy
ExecStart=/usr/bin/python3 /home/pi/dawarich-proxy/proxy.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
サービスの有効化と起動:

Bash
sudo systemctl daemon-reload
sudo systemctl enable dawarich-proxy.service
sudo systemctl start dawarich-proxy.service
4. 設定の仕上げ
Traccar Client (スマホアプリ):

サーバーURL: http://[サーバーのIPアドレス]:5055/traccar

Dawarich:

サーバー設定で OwnTracks 経由の入力を許可しておく。

まとめ
この構成により、「オフライン蓄積とバッテリー効率に優れる Traccar Client」 を使いつつ、「リッチなUIを持つ Dawarich」 でログを管理できるようになります。
「既存の優れたアプリを活かしつつ、中継層(Proxy)でデータの不整合を解消する」というアプローチは、セルフホストにおける柔軟な解決策として非常に有効です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?