Push Data Stream Back to Senaps

Continuing on from the above guide Query Sensor Data Out of Senaps . We will now create new values from the extracted data, and push the values as observations back to a Senaps datastream. The General Flow is detailed below:

ICT Example

Before we can push values back to the platform we have to make sure that the Datastream inside the platform we wish to push to exists!

Step 1: Create a new Datastream within a platform

Click Data and then Datastream in the Senaps Dashboard

Click Add a New Stream

Fill in the required Information to Create a Datastream as shown below

  1. ID: It is highly recommended that the ID of the Datastream maintains the Senaps Standard, that is:
    platformid.new_value

where new_value is a descriptive name of the value you are modeling/creating eg. evapotranspiration

In the below image the platformid is shaded, this relates to the platform that the Datastream will be a part of, generally, you want to ensure

  1. The Datastream type as defined here
  2. The Organisation the Platform is a part of, e.g.ICT International
  3. The Group the Platform is a part of, e.g.dsapi-perpetual-loutish-grocery
  4. The Interpolation Type, e.g.Continuous
  5. Scroll down to the bottom and click Create Stream

Example Pulling Data out -> Modelling it -> Pushing data back to DataStream

from senaps_sensor.parsers import PandasObservationParser
from senaps_sensor.models import Observation, UnivariateResult
from senaps_sensor.api import API
from senaps_sensor.auth import HTTPKeyAuth
import pandas as pd
from pprint import pprint
import json
from tabulate import tabulate
import matplotlib.pyplot as plt

#i
creds_path = r"C:\Users\Quinten\Documents\Eratos_tok\Q_senaps_creds.json"

# Opening JSON file
f = open(creds_path)
  
# returns JSON object as 
# a dictionary
creds = json.load(f)

#ii
senaps_client = API(HTTPKeyAuth(creds['senaps_key']), host="senaps.eratos.com")

platform_id = 'dsapi-perpetual-loutish-grocery-ict_international.mnla1k701'

platform = senaps_client.get_platform(id = 'dsapi-perpetual-loutish-grocery-ict_international.mnla1k701')

#iii

df_ICT_weather_station_air_temp = senaps_client.get_observations(streamid=f'{platform_id}.sdi_0.air-temperature',  limit=50, 
                                                    limitstreams=50,
                                                    media="csv", 
                                                    sort="descending", 
                                                    parser=PandasObservationParser(), platformid = platform_id )

df_ICT_subset = df_ICT_subset.rename(columns={'dsapi-perpetual-loutish-grocery-ict_international.mnla1k701.sdi_0.air-temperature': 'air_temp'}

# Resample to 15 minute intervals                                     
resampled_df = df_ICT_subset.resample('15T').mean()
                                     
output_list = []                     
                                     
for ts, row in resampled_df.iterrows():
                                     
  	temp = row['air_temp']
                                     
    ## This is where the modelling happens, here the model is simply to mulitply by 10 ##       
    changed_temp = temp *10
                                     
    output_list.append(UnivariateResult(t=ts.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), v=changed_temp))  

                                    
                                     
output = Observation()
output.results = output_list
context.sensor_client.create_observations(output, streamid=f"{platform_id}.new_value")