-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
======= | ||
CHANGES | ||
======= | ||
|
||
v0.1.0 | ||
------- | ||
|
||
* initial release | ||
* supports querying the latest values and pushing data | ||
* compatible with GSN v2.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
=============================== | ||
Python wrapper for the Global Sensor Networks API | ||
=============================== | ||
|
||
.. contents:: **Table of Contents** | ||
|
||
------------------------- | ||
Installation | ||
------------------------- | ||
|
||
> pip install gsn | ||
|
||
------------------------- | ||
Usage | ||
------------------------- | ||
|
||
The wrapper uses the `sanction`_ Oauth2 library to authenticate against GSN services. | ||
Before starting you must create a "client" on GSN services and get the client_id, client_secret and redirect_uri. | ||
As we will be using client_credentials, the client must be linked to the user from which it will inherit the access rights. | ||
The value of redirect uri is not used, but must nevertheless match. | ||
|
||
Then in your python code, you can use it this way:: | ||
|
||
> import gsn | ||
> a = gsn.API(service_url="http://localhost:9000/ws", client_id="client", client_secret="secret", redirect_uri="http://localhost") | ||
> s = a.get_latest_values("push") | ||
> s.values = [[1469959498000, 18]] | ||
> r = a.push_values(s) | ||
|
||
.. _sanction: https://github.com/demianbrecht/sanction | ||
|
||
Sensor object | ||
=============================== | ||
|
||
This object abstract the json representation used by GSN for the stream elements. It has the following fields: | ||
|
||
* fields: list of triples representing the name, type and units | ||
* name: name of the sensor | ||
* values: list of lists. The inner lists must have the same size as the fields list and represent the data of a stream element. | ||
* location: (optional) a triple representing the location of the sensor as latitude, longitude, altitude. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from sanction import Client, transport_headers | ||
from .sensor import Sensor | ||
try: | ||
from urllib2 import HTTPError | ||
except: | ||
from urllib.error import HTTPError | ||
|
||
|
||
class API(object): | ||
""" Global Sensor Networks api client object | ||
""" | ||
|
||
def __init__(self, service_url=None, client_id=None, client_secret=None, redirect_uri=None): | ||
""" Instantiates a GSN API client to authorize and authenticate a user | ||
:param service_url: The authorization endpoint provided by GSN | ||
services. | ||
:param client_id: The client ID. | ||
:param client_secret: The client secret. | ||
""" | ||
assert service_url is not None | ||
assert client_id is not None and client_secret is not None | ||
|
||
self.client = Client(token_endpoint="{}/oauth2/token".format(service_url), | ||
resource_endpoint="{}/api".format(service_url), | ||
client_id=client_id, client_secret=client_secret, | ||
token_transport=transport_headers | ||
) | ||
self.client.request_token(grant_type='client_credentials', redirect_uri=redirect_uri) | ||
|
||
def get_latest_values(self, vs_name=None): | ||
""" Query the API to get the latest values of a given virtual sensor. | ||
:param vs_name: The name of the virtual sensor. | ||
:returns: A Sensor object. | ||
""" | ||
assert vs_name is not None | ||
|
||
data = self.client.request("/sensors/{}?latestValues=True".format(vs_name)) | ||
return Sensor(geojson_object=data) | ||
|
||
def push_values(self, sensor_data=None): | ||
""" Push sensor data into GSN's API. The corresponding virtual sensor | ||
must be of type zeromq-push. | ||
:param sensor_data: A Sensor object containing the sensor values. | ||
:returns: The server response. | ||
""" | ||
|
||
assert sensor_data is not None | ||
|
||
try: | ||
res = self.client.request("/sensors/{}/data".format(sensor_data.name), | ||
data=sensor_data.to_geojson().encode('utf_8'), | ||
headers={'Content-type': 'application/json'}) | ||
except HTTPError as e: | ||
return e.readlines() | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from json import dumps | ||
|
||
|
||
class Sensor(object): | ||
|
||
def __init__(self, geojson_object=None, name=None, fields=None, location=None): | ||
""" Instantiates a Sensor object defining its structure | ||
either from a geojson object or from the related parameters. | ||
:param geojson_object: A dict loaded from a geojson object. | ||
:param name: The name of the sensor. Must only by used in | ||
conjunction with fields. | ||
:param fields: A list of triples containing the name, data-type | ||
and units of the sensor fields, as strings. | ||
See GSN's documentation for a list of data-types. | ||
:param location: A triple giving the latitude, longitude and | ||
altitude of the sensor, not mandatory. | ||
""" | ||
assert geojson_object is None or (fields is None and name is None) | ||
|
||
if geojson_object: | ||
self.fields = [(f['name'], f['type'], f['unit']) | ||
for f in geojson_object['properties']['fields']] | ||
self.name = geojson_object['properties']['vs_name'] | ||
self.values = geojson_object['properties']['values'] | ||
self.location = geojson_object['geometry']['coordinates'] | ||
else: | ||
self.fields = fields | ||
self.name = name | ||
self.values = [] | ||
self.location = location | ||
|
||
def to_geojson(self): | ||
|
||
r = {"type": "Feature", | ||
"properties": {"vs_name": self.name, | ||
"values": self.values, | ||
"fields": [{"name": f[0], "type": f[1], "unit": f[2]} for f in self.fields], | ||
"stats": {}, | ||
"geographical": "", | ||
"description": "Generated from python-gsn client" | ||
}, | ||
"geometry": {"type": "Point", "coordinates": self.location}, | ||
"total_size": 1, | ||
"page_size": 1 | ||
} | ||
return dumps(r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[bdist_wheel] | ||
universal=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""A python wrapper for Global Sensor Networks API | ||
https://github.com/LSIR/python-gsn | ||
""" | ||
|
||
# Always prefer setuptools over distutils | ||
from setuptools import setup, find_packages | ||
# To use a consistent encoding | ||
from codecs import open | ||
from os import path | ||
|
||
here = path.abspath(path.dirname(__file__)) | ||
|
||
# Get the long description from the README file | ||
with open(path.join(here, 'README.rst'), encoding='utf-8') as f: | ||
long_description = f.read() | ||
|
||
with open(path.join(here, 'CHANGES.rst'), encoding='utf-8') as f: | ||
long_description += f.read() | ||
|
||
setup( | ||
name='gsn', | ||
|
||
# Versions should comply with PEP440. | ||
version='0.1.0', | ||
|
||
description='A python wrapper for Global Sensor Networks API', | ||
long_description=long_description, | ||
|
||
# The project's main homepage. | ||
url='https://github.com/LSIR/python-gsn', | ||
|
||
# Author details | ||
author='The GSN Team', | ||
author_email='[email protected]', | ||
|
||
# Choose your license | ||
license='GPLv3+', | ||
|
||
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers | ||
classifiers=[ | ||
# How mature is this project? Common values are | ||
# 3 - Alpha | ||
# 4 - Beta | ||
# 5 - Production/Stable | ||
'Development Status :: 3 - Alpha', | ||
|
||
# Indicate who your project is intended for | ||
'Intended Audience :: Developers', | ||
'Topic :: Software Development :: Libraries :: Python Modules', | ||
|
||
# Pick your license as you wish (should match "license" above) | ||
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', | ||
|
||
# Specify the Python versions you support here. In particular, ensure | ||
# that you indicate whether you support Python 2, Python 3 or both. | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 2.6', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.3', | ||
'Programming Language :: Python :: 3.4', | ||
'Programming Language :: Python :: 3.5', | ||
], | ||
|
||
# What does your project relate to? | ||
keywords='iot sensors networks', | ||
|
||
# You can just specify the packages manually here if your project is | ||
# simple. Or you can use find_packages(). | ||
packages=find_packages(exclude=['contrib', 'docs', 'tests']), | ||
|
||
# Alternatively, if you want to distribute just a my_module.py, uncomment | ||
# this: | ||
# py_modules=["my_module"], | ||
|
||
# List run-time dependencies here. These will be installed by pip when | ||
# your project is installed. For an analysis of "install_requires" vs pip's | ||
# requirements files see: | ||
# https://packaging.python.org/en/latest/requirements.html | ||
install_requires=['sanction'], | ||
|
||
# List additional groups of dependencies here (e.g. development | ||
# dependencies). You can install these using the following syntax, | ||
# for example: | ||
# $ pip install -e .[dev,test] | ||
extras_require={ | ||
'dev': ['check-manifest'], | ||
'test': ['coverage'], | ||
}, | ||
|
||
# If there are data files included in your packages that need to be | ||
# installed, specify them here. If using Python 2.6 or less, then these | ||
# have to be included in MANIFEST.in as well. | ||
package_data={}, | ||
|
||
# Although 'package_data' is the preferred approach, in some case you may | ||
# need to place data files outside of your packages. See: | ||
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa | ||
# In this case, 'data_file' will be installed into '<sys.prefix>/my_data' | ||
data_files=[], | ||
|
||
# To provide executable scripts, use entry points in preference to the | ||
# "scripts" keyword. Entry points provide cross-platform support and allow | ||
# pip to create the appropriate form of executable for the target platform. | ||
entry_points={}, | ||
) |