Metadata-Version: 2.4
Name: pytrain-ogr-api
Version: 1.1.16
Summary: Restful API to control Lionel TMCC/Legacy engines, switches, and accesseries from a Raspberry Pi
Author-email: Dave Swindell <pytraininfo@gmail.com>
License:                    GNU LESSER GENERAL PUBLIC LICENSE
                               Version 3, 29 June 2007
        
         Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
         Everyone is permitted to copy and distribute verbatim copies
         of this license document, but changing it is not allowed.
        
        
          This version of the GNU Lesser General Public License incorporates
        the terms and conditions of version 3 of the GNU General Public
        License, supplemented by the additional permissions listed below.
        
          0. Additional Definitions.
        
          As used herein, "this License" refers to version 3 of the GNU Lesser
        General Public License, and the "GNU GPL" refers to version 3 of the GNU
        General Public License.
        
          "The Library" refers to a covered work governed by this License,
        other than an Application or a Combined Work as defined below.
        
          An "Application" is any work that makes use of an interface provided
        by the Library, but which is not otherwise based on the Library.
        Defining a subclass of a class defined by the Library is deemed a mode
        of using an interface provided by the Library.
        
          A "Combined Work" is a work produced by combining or linking an
        Application with the Library.  The particular version of the Library
        with which the Combined Work was made is also called the "Linked
        Version".
        
          The "Minimal Corresponding Source" for a Combined Work means the
        Corresponding Source for the Combined Work, excluding any source code
        for portions of the Combined Work that, considered in isolation, are
        based on the Application, and not on the Linked Version.
        
          The "Corresponding Application Code" for a Combined Work means the
        object code and/or source code for the Application, including any data
        and utility programs needed for reproducing the Combined Work from the
        Application, but excluding the System Libraries of the Combined Work.
        
          1. Exception to Section 3 of the GNU GPL.
        
          You may convey a covered work under sections 3 and 4 of this License
        without being bound by section 3 of the GNU GPL.
        
          2. Conveying Modified Versions.
        
          If you modify a copy of the Library, and, in your modifications, a
        facility refers to a function or data to be supplied by an Application
        that uses the facility (other than as an argument passed when the
        facility is invoked), then you may convey a copy of the modified
        version:
        
           a) under this License, provided that you make a good faith effort to
           ensure that, in the event an Application does not supply the
           function or data, the facility still operates, and performs
           whatever part of its purpose remains meaningful, or
        
           b) under the GNU GPL, with none of the additional permissions of
           this License applicable to that copy.
        
          3. Object Code Incorporating Material from Library Header Files.
        
          The object code form of an Application may incorporate material from
        a header file that is part of the Library.  You may convey such object
        code under terms of your choice, provided that, if the incorporated
        material is not limited to numerical parameters, data structure
        layouts and accessors, or small macros, inline functions and templates
        (ten or fewer lines in length), you do both of the following:
        
           a) Give prominent notice with each copy of the object code that the
           Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the object code with a copy of the GNU GPL and this license
           document.
        
          4. Combined Works.
        
          You may convey a Combined Work under terms of your choice that,
        taken together, effectively do not restrict modification of the
        portions of the Library contained in the Combined Work and reverse
        engineering for debugging such modifications, if you also do each of
        the following:
        
           a) Give prominent notice with each copy of the Combined Work that
           the Library is used in it and that the Library and its use are
           covered by this License.
        
           b) Accompany the Combined Work with a copy of the GNU GPL and this license
           document.
        
           c) For a Combined Work that displays copyright notices during
           execution, include the copyright notice for the Library among
           these notices, as well as a reference directing the user to the
           copies of the GNU GPL and this license document.
        
           d) Do one of the following:
        
               0) Convey the Minimal Corresponding Source under the terms of this
               License, and the Corresponding Application Code in a form
               suitable for, and under terms that permit, the user to
               recombine or relink the Application with a modified version of
               the Linked Version to produce a modified Combined Work, in the
               manner specified by section 6 of the GNU GPL for conveying
               Corresponding Source.
        
               1) Use a suitable shared library mechanism for linking with the
               Library.  A suitable mechanism is one that (a) uses at run time
               a copy of the Library already present on the user's computer
               system, and (b) will operate properly with a modified version
               of the Library that is interface-compatible with the Linked
               Version.
        
           e) Provide Installation Information, but only if you would otherwise
           be required to provide such information under section 6 of the
           GNU GPL, and only to the extent that such information is
           necessary to install and execute a modified version of the
           Combined Work produced by recombining or relinking the
           Application with a modified version of the Linked Version. (If
           you use option 4d0, the Installation Information must accompany
           the Minimal Corresponding Source and Corresponding Application
           Code. If you use option 4d1, you must provide the Installation
           Information in the manner specified by section 6 of the GNU GPL
           for conveying Corresponding Source.)
        
          5. Combined Libraries.
        
          You may place library facilities that are a work based on the
        Library side by side in a single library together with other library
        facilities that are not Applications and are not covered by this
        License, and convey such a combined library under terms of your
        choice, if you do both of the following:
        
           a) Accompany the combined library with a copy of the same work based
           on the Library, uncombined with any other library facilities,
           conveyed under the terms of this License.
        
           b) Give prominent notice with the combined library that part of it
           is a work based on the Library, and explaining where to find the
           accompanying uncombined form of the same work.
        
          6. Revised Versions of the GNU Lesser General Public License.
        
          The Free Software Foundation may publish revised and/or new versions
        of the GNU Lesser General Public License from time to time. Such new
        versions will be similar in spirit to the present version, but may
        differ in detail to address new problems or concerns.
        
          Each version is given a distinguishing version number. If the
        Library as you received it specifies that a certain numbered version
        of the GNU Lesser General Public License "or any later version"
        applies to it, you have the option of following the terms and
        conditions either of that published version or of any later version
        published by the Free Software Foundation. If the Library as you
        received it does not specify a version number of the GNU Lesser
        General Public License, you may choose any version of the GNU Lesser
        General Public License ever published by the Free Software Foundation.
        
          If the Library as you received it specifies that a proxy can decide
        whether future versions of the GNU Lesser General Public License shall
        apply, that proxy's public statement of acceptance of any version is
        permanent authorization for you to choose that version for the
        Library.
        
Project-URL: Homepage, https://github.com/cdswindell/PyTrainApi
Project-URL: Source, https://github.com/cdswindell/PyTrainApi
Project-URL: Issues, https://github.com/cdswindell/PyTrainApi/issues
Keywords: alexa,api,rest,pytrain,legacy,lionel,model railroad,o-gauge,raspberry pi,tmcc,trains
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Other Audience
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
Classifier: Natural Language :: English
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: AUTHORS
Requires-Dist: pytrain-ogr>=1.3.26
Requires-Dist: fastapi>=0.115.14
Requires-Dist: fastapi-utils>=0.8.0
Requires-Dist: pyjwt>=2.10.1
Requires-Dist: python-dotenv>=1.1.1
Requires-Dist: python_multipart>=0.0.20
Requires-Dist: range-key-dict>=1.1.0
Requires-Dist: setuptools>=80.9.0
Requires-Dist: setuptools_scm>=8.3.1
Requires-Dist: typing-inspect>=0.9.0
Requires-Dist: uvicorn>=0.35.0
Dynamic: license-file

# PyTrain API

This project implements a RESTful API to [PyTrain](https://github.com/cdswindell/PyLegacy). Via the API, you can
control and operate trains, switches, accessories, and any other equipment that use Lionel's Legacy/TMCC command
protocol. **PyTrain Api** is used by the **PyTrain** Alexa skill to enables voice-control of your layout.

**PyTrain Api** is developed in pure Python. It uses the [FastAPI](https://fastapi.tiangolo.com) framework and
includes an ASGI-compliant web server, [Uvicorn](https://www.uvicorn.org). Once installed, it only takes one
command to launch the **PyTrain Api** and begin serving requests.

**PyTrain Api** runs as a **PyTrain** client connected to another **PyTrain** server, or can act as both
a **PyTrain** server _and_ a **PyTrain Api** server. And, like **PyTrain**, **PyTrain Api** can run on a Raspberry Pi
running 64 bit Bookworm distribution or later.

<!-- TOC -->

* [Quick Start](#quick-start)
    * [Requirements](#requirements)
    * [Installation](#installation)
        * [Create a Python Virtual Environment](#create-a-python-virtual-environment)
        * [Run __PyTrain Api__ Directly](#run-__pytrain-api__-directly)
        * [Run __PyTrain Api__ from a Development Environment](#run-__pytrain-api__-from-a-development-environment)
    * [Configuration](#configuration)
* [Alexa Skill](#alexa-skill)
    * [Configuration](#configuration)
    * [Additional Security](#additional-security)

<!-- TOC -->

## Quick Start

### Requirements

Minimum requirements to use **PyTrain API** are:

* A Lionel Base 3 running the most current Lionel firmware and configured in *infrastructure* mode
* A network-capable Raspberry Pi 4 or 5 running Raspberry PI OS 64-bit Bookworm
* A Mac or Windows computer to set up the Raspberry Pi(s)
* All hardware connected to the same Wi-Fi network
* Python 3.10 - 3.12 installed (Python 3.11 is standard with the Bookworm release of Raspberry Pi OS)
* Internet access (to download software)

Recommended:

* A **PyTrain** server running on a separate Raspberry Pi 4 or 5 Wi-Fi-equipped computer with at least 2 GB
  of RAM running Raspberry PI OS 64-bit Bookworm

Notes:

* It is recommended to have a Lionel LCS Ser2 module connected to your **PyTrain** server, as
  the Lionel Base 3 **_does not_** broadcast all layout activity
* **PyTrain** is a command-line tool. It must be run from a Terminal window (macOS/Linux/Pi) or a Cmd
  shell (Windows). **PyTrain** does _not_ have a GUI nor run as a native app.
* **PyTrain** _may_ work with an LCS Wi-Fi module, but this configuration hasn't been tested
* The **PyTrain** CLI can be run on a Mac or Windows system. It allows complete control of _all_ TMCC or
  Legacy-equipped devices as well as allows you to monitor all TMCC and Legacy commands

### Installation

**PyTrain Api** can be run directly from a Python virtual environment or
from a **PyTrain Api** development environment, The former is preferable if you do not plan
on changing the **PyTrain Api** code itself. Both methods require the creation of a
Python [virtual environment](https://developer.vonage.com/en/blog/a-comprehensive-guide-on-working-with-python-virtual-environments#using-venv).

#### Create a Python Virtual Environment

* Open a Terminal shell window and navigate to the folder/directory where
  you will install __PyTrain Api__
* Create a virtual environment with the command:

```aiignore
python3 -m venv PyTrainApiEnv
```

* In the same terminal window, `cd` into the directory you created above and activate the environment:

```aiignore
cd PyTrainApiEnv
source bin/activate
```

#### Run __PyTrain Api__ Directly

* Use `pip` to install the __PyTrain Api__ package from `pypi`:

```aiignore
pip3 install pytrain-ogr-api
```

* Run the __PyTrain Api__ server:

```aiignore
pytrain_api
```

This command launches a webserver that accepts requests on port `8000`and connects to a __PyTrain__ server.
To see the list of available Api endpoints, open a web browser and go to `http://<your local IP>:8000/pytrain`.

#### Run __PyTrain Api__ from a Development Environment

* In the same terminal window where you created the virtual environment, `cd` into the directory you
  created above and download the __PyTrain Api__ source code (this assumes you have installed
  the [GitHub CLI](https://cli.github.com)):

```aiignore
gh repo clone cdswindell/PyTrainApi
```

* `cd` into the directory `PyTrainApi` and activate the environment:

```aiignore
cd PyTrainApi
source ../bin/activate
```

* Install the __PyTrain Api__ required packages:

```aiignore
pip3 install -r requirements.txt
```

* Run the __PyTrain Api__ server:

```aiignore
cli/pytrain_api
```

This command launches a webserver that accepts requests on port `8000`and connects to a __PyTrain__ server.
To see the list of available Api endpoints, open a web browser and go to `http://<your local IP>:8000/pytrain`.

### Configuration

The **PyTrain Api** endpoints are all protected against unintensional as well as unwanted access. It does so by
requiring an *api token* to be present in the header of each request. Modern programming languages make this
easy to do.

Api tokens should be kept secure, just like the key to your house, otherwise, anyone could learn and use it.
For this reasn, ypu must generate the required keys yourself and keep them in a special file, the `.env` file.
This file must be placed in the same directory you run the **PyTrain Api** program from.

**PyTrain Api** provides a special command to generate a correctly-configured `.env` file and populate it
with an api token for your own use. To do so, `cd` to the directory where you plan to run the Api and type:

- Run **PyTrain Api** Directly

```aiignore
pytrin_api -env
```

- Run **PyTrain Api** from a Development Environment

```aiignore
cli/pytrin_api -env
```

Use the value assigned to the tag `API_TOKEN` as your secure **PyTrain Api** token. Simply include it in
your request header with the tag `X-API-Key`, and you will be good to go. Below is an example of how to do
this from Python:

```aiignore
headers = {"X-API-Key": "<Replace with the API_TOKEN from .env file>"}
response = requests.post(url, headers=headers)
```

You *should not* modify any values in `.env` unless you are using the [Alexa Skill](#alexa-skill) (see below).
Cnanging other values will
break **PyTrain Api**. If you do mistakenly modify the file's contents or delete it, you can simply
regenerate it using the appropriate command above.

## Alexa Skill

__PyTrain Api__ is required to use the Alexa __PyTrain__ skill. You may install __PyTrain Api__
using either method described above.

### Configuration

Before getting into the configuration details, lets look at the moving parts involved:

1. Your Lionel Base 3. This device is connected to your local network and receives commands from a Cab 2, Cab 3,
   and/or **PyTrain** server. These commands are relayed onto your layout to control your equipment.

2. Your local network. This can be completely wireless or a hybrid of wired and wireless devices.

3. Your **PyTrain API** server. This is a computer on your local network where you have installed **PyTrain API**.
   It takes requests from the web in a specific format (HTTP or HTTPS), and relays them to your **PyTrain** server.
   **PyTrain API** can run on one of your existing computers (Mac or Windows), or, and a small, inexpensive
   [Raspberry Pi](https://www.raspberrypi.com).

4. Your **PyTrain** server. This is another computer on your local network where you have installed
   [**PyTrain**](https://pypi.org/project/pytrain-ogr/). **PyTrain** receives requests from **PyTrain API**
   and relays them to your Lionel Base 3 in the format it requires. It can also take input from a command
   line (CLI), or be embedded into one or more control panels to operate your layout via physical buttons,
   lights, dials, and knobs. It should run its own [Raspberry Pi](https://www.raspberrypi.com).

5. An [Alexa](https://www.amazon.com/s?k=alexa&crid=7OAPBGIW11Q4&sprefix=alexa%2Caps%2C107&ref=nb_sb_noss_1).
   This device is a wireless speaker and microphone. It supports *skills*, which are voice-activated apps
   that runs on Alexa-enabled devices. You can use Alexa skills to perform tasks like telling jokes,
   getting weather updates, or controlling your Lionel layout. It connects to your local network.

6. Your Gateway. This device connects your local network to *the internet*. It may have been provided by
   your Internet Service Provider (ISP), or you may have purchased it yourself. It is the bridge that lets devices
   on your local network talk to the internet, and lets the internet talk to devices on your local network,
   like your Lionel Base 3.

7. Amazon AWS. Amazon runs several large computing facilities around the world. Alexa skills send the voice
   input from your local alexa's to these computers to interpret, then execute software to fulfill your requests. The
   **PyTrain** Alexa skill runs on these computers, takes yur voice input and then calls back to the **PyTrain API**
   on your local network to control your layout.

Phew! Although there are a lot of moving parts, getting everything up and running is straightforward,
thanks to the **PyTrain** software.

Here is what you need to do:

1. Configure 2 Raspberry Pi 4 or 5 computers. Units with WiFi are preferred, as there are less wires.
2. Install **PyTrain** on one of them and start it with the command:

```aiignore
pytrain -base <ip address of your Lionel Base 3>
```

If you have a Lionel Lcs Ser2, connect it to this computer and start **PyTrain** with this command:

```aiignore
pytrain -ser -base <ip address of your Lionel Base 3>
```

3. Install **PyTrain Api** on the other Raspberry Pi (using one of the two methods above) and start
   it with the appropriate command:

```aiignore
pytrain_api 

or

cli/pytrain_api
```

**PyTrain Api** will automatically find and connect to your **PyTrain** server.

4. Go to your gateway and determine its *external* IP address. This address is assigned by your ISP
   (Comcast, Verizon, Spectrum, etc.). Write it down.
5. Consult your gateway documentation how to forward port 80 to port 8000 on your **PyTrain API** server.
   This will send all traffic from the outside world to port 8000 on your **PyTrain Api** server. You will need
   to repeat this step any time your external IP address changes (see Notes below).
6. Say the following to your Alexa: *Alexa, Open PyTrain*
7. The *PyTrain* skill will ask you for the external IP address of your network. Give it the number
   you wrote down above.

Your **PyTrain** skill is now active and connected to your layout. To test it out, assuming you have an
engine on the tracks with TMCC ID 67, try the following:

* Ask PyTrain to start up engine 67
* Blow the horn on engine 67
* toggle the bell on engine 67
* accelerate engine 67 to speed step 10
* stop engine 67
* shut down engine 67 with dialog

As long as you see the *blue light* on your Alexa, the **PyTrain** skill is listening. If the light goes out, simply say
*Alexa, Open PyTrain*, and speak your request.

Notes:

* If your gateway supports Dynamic DNS, you can assign a *domain name* to your external network, and your gateway will
  automatically update it with your external IP address, should your ISP change it.
  [Netgear](https://kb.netgear.com/23860/How-do-I-set-up-a-NETGEAR-Dynamic-DNS-account-on-my-NETGEAR-router),
  [No-ip.com](https://www.noip.com), and [Dyn.com](https://www.oracle.com/cloud/networking/dns/?er=221886)
  all provide dynamic dns services at reasonable cost.

### Additional Security

For additional security, you can configure an HTTPS server to proxy requests from the Alexa skill
to the system running __PyTrain API__. This requires:

* Install an additional web server that supports HTTPS secure communication.
  The [nginx](https://nginx.org) web server is an excellent candidate.
* Configure `ngnix` to proxy HTTPS requests from port 443 to port 8000, where they are processed by __PyTrain API__.
* Install an SSL certificate on your dynamic dns service
* Configure the certificate and private key on your local proxy server.

These steps are not required but are suggested if you leave your layout unattended and powered on in order
to prevent unwanted access.
