Code Monkey home page Code Monkey logo

pygps_client's Introduction

PYGPS Client

pygps

  • Read Serial interface and grab data from the GPS receiver
  • Calculate distance and speed between two successive reads
  • Publish the data to a MQ (AMQP)

pygps-consumer

  • Read the message published to MQ (AMQP)
  • Enrich the message with context data
  • POST the data to a remote API endpoint

Hardware

Raspberry Pi Zero W
GSM/GPRS/GNSS/Bluetooth HAT for Raspberry Pi, Based on SIM868
Huawei E3372

CLIENT ENDPOINT

cat /etc/issue.net
Raspbian GNU/Linux 9

All the files are installed in /home/[SCRIPT]/pygps
PYGPS will run as user [SCRIPT]

Test:

cd endpointpublis/certificates
token=$(curl --key clientA-key.pem --cert clientA-crt.pem --cacert ca-crt.pem https://[JWT API ENDPOINT]/get/jwt/ -H 'x-ip-address: xxx' -H 'Content-Type: application/json' | jq -r '.token'); curl -X POST -i https://[API GW]/track/items/ -H "Authorization: Bearer ${token}" -H "X-DEBUG: true" -H 'x-ip-address: xxx'

Prerequisites:

Packages: python3 (3.7.x), pip3, pip-tools, systemd, git, rabbitmq-server, memcached

As user root

cd /home/[SCRIPT]/pygps
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
pip-compile requirements.in -o requirements.txt
pip3 install -r requirements.txt
cd /etc/systemd/system/

pygps.service

[Unit]
Description=Python GPS Service
After=network.target memcached.service rabbitmq-server.service

[Service]
User=[SCRIPT]
Group=[SCRIPT]
Type=simple
ExecStart=/usr/bin/python3 /home/[SCRIPT]/pygps/pygps.py
Restart=always
RestartSec=3
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
Alias=pygps.service

pygps-consumer.service

[Unit]
Description=Python GPS consumer Service
After=network.target pygps.service

[Service]
User=[SCRIPT]
Group=[SCRIPT]
Type=simple
ExecStart=/usr/bin/python3 /home/[SCRIPT]/pygps/pygps-consumer.py
Restart=always
RestartSec=3
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
Alias=pygps-consumer.service

pygps-ipaddress.service

[Unit]
Description=Python GPS Service IP Req
After=network.target

[Service]
User=ipirva
Group=ipirva
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/python3.7 /home/[SCRIPT]/pygps/cron/getipaddress.py
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
Alias=pygps-ipaddress.service
systemctl daemon-reload

systemctl enable pygps.service
systemctl enable pygps-consumer.service
systemctl enable pygps-ipaddress.service

systemctl start pygps.service
systemctl start pygps-consumer.service
systemctl start pygps-ipaddress.service

journalctl -u pygps.service
journalctl -u pygps-consumer.service
journalctl -u pygps-ipaddress.service

Log rotate

cd /etc/logrotate.d/

pygps

/home/[SCRIPT]/pygps/logs/pygps-consumer.log
/home/[SCRIPT]/pygps/logs/pygps.log
/home/[SCRIPT]/pygps/cron/logs/getipaddress.log
{
	rotate 30
	maxsize 50M
	notifempty
	missingok
	nocreate
	daily
	compress
	delaycompress
}

As user [SCRIPT]

crontab -l
*/10 * * * * python3 /home/[SCRIPT]/pygps/cron/getipaddress.py

Change file access rights for the certificate files.

cd /home/[SCRIPT]/pygps/endpointpublish/certificates
chmod go-r *.pem

Install MemcacheDB

As root:

apt-get install memcached
update-rc.d memcached defaults

Configuration: /etc/memcached.conf

-d
logfile /var/log/memcached.log
-m 64
-p 21201
-u memcache
-l 127.0.0.1
service memcached start

Install RabbitMQ (AMQP)

As root:

apt-get install rabbitmq-server -y
mkdir -p /etc/rabbitmq/ 
echo "[rabbitmq_management]." > /etc/rabbitmq/enabled_plugins
update-rc.d rabbitmq-server defaults
service rabbitmq-server start
RabbitMQ server definition
{
    "rabbit_version": "3.6.15",
    "users": [
        {
            "name": "admin",
            "password_hash": "cArRhHjba55meGIPRP7xZJdfvQ0Je1hqvbUWtqbwLezcnC41",
            "hashing_algorithm": "rabbit_password_hashing_sha256",
            "tags": "administrator"
        },
        {
            "name": "pygps",
            "password_hash": "YtZBOA1fvv8KAKhQNdCQvqu7fNk5no/bZIgeB2OkkAgP3aMi",
            "hashing_algorithm": "rabbit_password_hashing_sha256",
            "tags": ""
        }
    ],
    "vhosts": [
        {
            "name": "/"
        }
    ],
    "permissions": [
        {
            "user": "pygps",
            "vhost": "/",
            "configure": ".*",
            "write": ".*",
            "read": ".*"
        },
        {
            "user": "admin",
            "vhost": "/",
            "configure": ".*",
            "write": ".*",
            "read": ".*"
        }
    ],
    "parameters": [],
    "global_parameters": [
        {
            "name": "cluster_name",
            "value": "rabbit@raspberrypi"
        }
    ],
    "policies": [],
    "queues": [
        {
            "name": "GPSDetails",
            "vhost": "/",
            "durable": true,
            "auto_delete": false,
            "arguments": {
                "x-message-ttl": 432000000
            }
        }
    ],
    "exchanges": [],
    "bindings": []
}

Install Python3.7.x on Raspberry PI

As a non-root user:

cat /etc/issue.net
Raspbian GNU/Linux 9
pythonversion="3.7.5"
pythonversionmajor="3.7"

As root user:

apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y

As a non-root user:

cd /tmp
wget https://www.python.org/ftp/python/${pythonversion}/Python-${pythonversion}.tar.xz
tar xf Python-${pythonversion}.tar.xz

cd Python-${pythonversion}
./configure 
make -j 4

As root user:

make altinstall

for root and the user script pygps

As the respective user:

sed -i '/alias python3/d' ~/.bashrc
echo "alias python3=python${pythonversionmajor}" >> ~/.bashrc
source ~/.bashrc


cd /tmp
curl -O https://bootstrap.pypa.io/get-pip.py

As root user:

python3 get-pip.py

for root and the user script pygps

As the respective user:

sed -i '/alias pip3/d' ~/.bashrc
echo "alias pip3=pip${pythonversionmajor}" >> ~/.bashrc
source ~/.bashrc
Clean up

As root user:

cd /tmp
rm -fr Python-${pythonversion}
rm Python-${pythonversion}.tar.xz

apt-get --purge remove build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y
apt-get autoremove -y
apt-get clean

pygps_client's People

Contributors

ipirva avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.