import daqss
from daqss import Daqss
import pandas as pd

key = 'DHDNAG3E43AV'
pswd = 'S9L5NNDLKAJU'

print ("daqss library version: {0}".format(daqss.__version__))

"""
Explanation: Daqss API Tutorial
Overview
An application program interface (API) is a set of routines, protocols, and tools for building software applications. A good APi makes it easier to develop a program by providing the building blocks. A programmer then puts those blocks together.
The Daqss API provides an easy way to read and write data to/from the remote server. It is protected by key/password combinations and allows Users read-only access. The following describes how to use the Python wrapper that is built on top of the Daqss API.
Contents

Installation
API Credentials
API Endpoints
Using the Python API Wrapper

<a id="#installation"></a>
Installation
To install the python-daqss python API wrapper, download from the Github repository here, or clone the repository to your computer.
Download and unzip
&gt;&gt;&gt; wget https://github.com/dhhagan/python-daqss/archive/master.zip
&gt;&gt;&gt; unzip master.zip
&gt;&gt;&gt; cd python-daqss/
&gt;&gt;&gt; sudo python setup.py install

Install via git
&gt;&gt;&gt; git clone https://github.com/dhhagan/python-daqss.git
&gt;&gt;&gt; cd python-daqss/
&gt;&gt;&gt; sudo python setup.py install

You should now have the python API wrapper installed on your computer.
<a id="#api-credentials"></a>
API Credentials
The Daqss API is protected using a key, password system that prevents unwanted people from injecting data into the database, or deleting entries. In order to use the API to communicate with the database, you must first request your own set of API credentials.
As of right now, you must email david@davidhhagan.com for API credentials!
API Endpoints
API endpoints at their most basic level, share a common structure; essentially, you send a url that is formatted in a specific way, accompanied by your credentials, and recieve data (or an error!) in return in json format. All of the available API endpoints are layed out in detail in the full documentation. 
For example, say we wanted to request the data for one of the alphasense gas sensors. The documentation looks something like this:
Resource URL

GET api/< api-version >/alphasense/< sn >/data/date/< basedate >/< period >
GET api/< api-version >/alphasense/< sn >/data/date/< basedate >/< enddate >

| api-version | The API version. Currently 1.0 |
|:-|:-|
| sn | The serial number for the alphasense sensor of interest|
| basedate | The beginning date for the data you want to recieve in the format YYYY-MM-DD |
| period | The period (length) for which you want data beginning at the basedate. One of 1d, 2w, 4m, 1y |
| enddate | The ending date for the data you want to recieve in the format YYYY-MM-DD |
<br />
Thus, once you build the resource url, you might end up with something like this:  api/v1.0/alphasense/A123/data/date/2015-01-01/1w
Using the python API wrapper
Luckily, a python wrapper has been written around the Daqss API to make things easy for everyone!
End of explanation
"""


# Set up an instance of the class Daqss with keyword arguments key and password
api = Daqss(key, pswd)

# Ping to see if you have a connection
api.ping()

"""
Explanation: Set up the class instance of Daqss
End of explanation
"""


status, req = api.get_nodes()

print ("The Status Code: {0}".format(status))
print ("The response: \n")
req['nodes']

"""
Explanation: Your first API call
To begin, let's try grabbing all of the nodes from the database. Instead of worrying about the API endpoint, we can just call api.get_nodes() and you will get what you desire!
Everything is in json format, so you will need to learn how to deal with that..don't worry, it's easy!
End of explanation
"""


# Read into a DataFrame
df = pd.DataFrame.from_dict(req['nodes'])

# Print the info
df.info()

"""
Explanation: Making the data more usable
Let's take advantage of the amazing library pandas and DataFrame's so we can easily manipulate the data and export to csv if we want to.
We will do so by reading the nodes portion of the json response into a pandas DataFrame. All of the column names and everything should be automatically set.
End of explanation
"""


df.to_csv("Nodes.csv")

"""
Explanation: Exporting
Now we are going to export this data to a csv for whatever reason!
End of explanation
"""


status, data = api.get_alphasense_data('203490202', basedate = '2015-05-01', period='6m')

# read into a dataframe
df = pd.DataFrame.from_dict(data['data'])

# Re-index so we have a nice datetime index to work with
df.index = pd.to_datetime(df['timestamp'])

# Export to csv!
df.to_csv("O3 Node 3 May 2015.csv")

# Print the info
df.info()

status, data = api.get_rht_data('RHT001', basedate = '2015-05-01', period = '1w')

# read into a dataframe
df = pd.DataFrame.from_dict(data['data'])

# Re-index so we have a nice datetime index to work with
df.index = pd.to_datetime(df['timestamp'])

# Print the info
df.info()

"""
Explanation: Getting the Data!
Now, let's download some data and export it. We are going to download one months worth of data for the alphasense sensor associated with Node 3. From the config file, we know that the SN for this O3 sensor is 203490202. According to the documentation for the API wrapper, we can get the alphasense data using the following api call:
get_alphasense_data(self, sn, basedate = None, enddate = None, period = None)
Let's grab the O3 data for the time period of 1 month beginning on May 1, 2015.
End of explanation
"""


