Menu

development:automate

Christian Foltin

Hi,

the question: is there a way to connect to a GPS to have a moving map that shows me where I am?

the answer: yes, OffRoad opens a (random) port and I used that with the following script to show the locations inside of a german ICE on my OffRoad instance.

Take the latest version of OffRoad!
The java code to read the port settings and to send the coordinates to OffRoad (pack into bahn.jar):

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.net.InetAddress;
import java.net.Socket;

public class Contact {

    public static void main(String[] args) {
        new Contact().sendGeoLocation(args);
    }

    private void sendGeoLocation(String[] pArgs) {
        if(pArgs.length != 3) {
            System.err.println("Usage: <location_of_port.txt_file> <latitude> <longitude>");
            System.exit(1);
        }
        String portFile = pArgs[0];
        if (portFile == null) {
            return;
        }
        // {{{ Try connecting to another running OffRoad instance
        if (portFile != null && new File(portFile).exists()) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(portFile));
                String check = in.readLine();
                if (!check.equals("b"))
                    throw new Exception("Wrong port file format");

                int port = Integer.parseInt(in.readLine());
                int key = Integer.parseInt(in.readLine());
                System.out.println("Connecting to port " + port);
                Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),
                        port);
                DataOutputStream out = new DataOutputStream(
                        socket.getOutputStream());
                System.out.println("Sending key...");
                out.writeInt(key);
                System.out.println("Connecting to port " + port + ". Done.");

                String position = pArgs[1]+":"+pArgs[2]+"\n";
                System.out.println("Sending position " + position);
                out.writeUTF(position);

                // block until its closed
                try {
                    socket.getInputStream().read();
                } catch (Exception e) {
                }

                in.close();
                out.close();

                System.exit(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

The go-tool to read the geo location from the ICE and to call the jar file:

package main
import "encoding/json"
import "net/http"
import "fmt"
import "io/ioutil"
import "os/exec"

type Bahn struct {
        Connection       bool    `json:"connection"`
        Servicelevel     string  `json:"servicelevel"`
        Internet         string  `json:"internet"`
        Speed            int     `json:"speed"`
        GpsStatus        string  `json:"gpsStatus"`
        Latitude         float64 `json:"latitude"`
        Longitude        float64 `json:"longitude"`
        ServerTime       int64   `json:"serverTime"`
        WagonClass       string  `json:"wagonClass"`
        NavigationChange string  `json:"navigationChange"`
}

func main() {

    // Generated by curl-to-Go: https://mholt.github.io/curl-to-go
    resp, err := http.Get("https://iceportal.de/api1/rs/status")
    if err != nil {
       // handle err
       fmt.Println(err)
       return
    }
    body, err := ioutil.ReadAll(resp.Body)
    //fmt.Println(body)
    var bahn Bahn
    json.Unmarshal([]byte(body), &bahn)
    fmt.Printf("Latitude: %f, Longitude: %f\n", bahn.Latitude, bahn.Longitude)
    cmd := exec.Command("java", "-jar", "bahn.jar", "/home/foltin/.OffRoad/port.txt", fmt.Sprintf("%f", bahn.Latitude), fmt.Sprintf("%f", bahn.Longitude))
    stdoutStderr, err := cmd.CombinedOutput()
    if err != nil {
        fmt.Print(err)
    }
    fmt.Printf("%s\n", stdoutStderr)

    defer resp.Body.Close()
}

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.