Skip to content

Commit

Permalink
Fix code example
Browse files Browse the repository at this point in the history
  • Loading branch information
rjtokenring committed Oct 23, 2024
1 parent 5ce3035 commit 7b1b148
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 55 deletions.
94 changes: 43 additions & 51 deletions example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import iot_api_client as iot
from iot_api_client.rest import ApiException
from iot_api_client.configuration import Configuration
import iot_api_client.apis.tags.things_v2_api as thingApi
import iot_api_client.apis.tags.properties_v2_api as propertiesApi
import iot_api_client.apis.tags.series_v2_api as seriesApi
from iot_api_client.api import ThingsV2Api, PropertiesV2Api, SeriesV2Api
from iot_api_client.models import *

import csv
from time import sleep

HOST = "https://api2.arduino.cc/iot"
HOST = "https://api2.arduino.cc"
TOKEN_URL = "https://api2.arduino.cc/iot/v1/clients/token"

client_id="<client-id>" # get a valid one from your Arduino account
client_secret="<client-secret-id>" # get a valid one from your Arduino account
org_id="<organization id - optional>"
extract_from="2024-06-03T00:00:00Z"
extract_to="2024-06-06T00:00:00Z"

org_id="<organization-id>" # (Optional) get a valid one from your Arduino account
extract_from="2024-10-03T00:00:00Z"
extract_to="2024-10-06T00:00:00Z"
filename="dump.csv"

def get_token():
Expand All @@ -29,7 +29,7 @@ def get_token():
client_id=client_id,
client_secret=client_secret,
include_client_id=True,
audience=HOST,
audience="https://api2.arduino.cc/iot",
headers={"X-Organization":org_id}
)
return token
Expand All @@ -45,67 +45,59 @@ def init_client(token):
return client



def dump_property_data(series_api,thing_name,prop_name,thing_id,prop_id):
def dump_property_data(client,thing_name,prop_name,thing_id,prop_id):
sleep(1)
print(f"Extracting property {thing_name}.{prop_name}")
body={
'resp_version':1,
'requests': [ {'q': "property."+prop_id,'from':extract_from,'to':extract_to} ]
}
timeseries=series_api.series_v2_batch_query_raw(body)
series_api = SeriesV2Api(client)
propertyRequest = BatchQueryRawRequestMediaV1(q="property."+prop_id, var_from=extract_from, to=extract_to)
seriesRequest = BatchQueryRawRequestsMediaV1(resp_version=1, requests=[propertyRequest])
timeseries=series_api.series_v2_batch_query_raw(seriesRequest)

if timeseries.response.status==200:
data = timeseries.body['responses']
for s in data:
times = s['times']
values = s['values']
try:
for s in timeseries.responses:
i=0
while i<len(times):
writer.writerow([thing_name,prop_name,times[i],values[i]])
while i<len(s.times):
writer.writerow([thing_name,prop_name,s.times[i],s.values[i]])
i=i+1
else:
print(f"Unable to extract data for property {prop_id}")


except ApiException as e:
print("Exception n series extraction: {}".format(e))


def get_things_and_props():
token = get_token()
client = init_client(token)
things_api = thingApi.ThingsV2Api(client)
properties_api = propertiesApi.PropertiesV2Api(client)
series_api = seriesApi.SeriesV2Api(client)
things_api = ThingsV2Api(client)
properties_api = PropertiesV2Api(client)
todolist=[] #use this to track extractions to do

try:
things = things_api.things_v2_list()

if things.response.status==200:
for thing in things.body:
sleep(5)
tname=thing["name"]
print(f"Found thing: {tname}")
todo={}
todo["thing_id"]=thing["id"]
todo["thing_name"]=tname
properties=properties_api.properties_v2_list(path_params={'id': thing["id"]})
for property in properties.body:
id = property["id"]
name = property["name"]
ptype = property["type"]
value = property["last_value"]
print(f"Property: {name}::{ptype}={value}")
if ptype=="FLOAT" or ptype=="INT":
todo["prop_id"]=id
todo["prop_name"]=name
todolist.append(todo.copy())
else:
print("IoT API returned status "+things.response.status)
for thing in things:
sleep(1)
tname=thing.name
print(f"Found thing: {tname}")
todo={}
todo["thing_id"]=thing.id
todo["thing_name"]=tname
properties=properties_api.properties_v2_list(id=thing.id, show_deleted=False)
for property in properties:
name = property.name
ptype = property.type
value = property.last_value
print(f"Property: {name}::{ptype}={value}")
if ptype=="FLOAT" or ptype=="INT":
todo["prop_id"]=property.id
todo["prop_name"]=name
todolist.append(todo.copy())

except ApiException as e:
print("Exception: {}".format(e))

while len(todolist)!=0:
todo = todolist.pop()
try:
dump_property_data(series_api,todo["thing_name"],todo["prop_name"],todo["thing_id"],todo["prop_id"])
dump_property_data(client, todo["thing_name"], todo["prop_name"], todo["thing_id"], todo["prop_id"])
except ApiException as e:
print("Exception: {}".format(e))

Expand Down
3 changes: 1 addition & 2 deletions example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ oauthlib
requests-oauthlib
arduino-iot-client
typing_extensions
frozendict ~= 2.3.4
wheel
frozendict ~= 2.3.4
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
urllib3 >= 1.25.3, < 3.0.0
python_dateutil >= 2.8.2
pydantic >= 2
typing-extensions >= 4.7.1
pydantic >= 2.9.2
python-dateutil >= 2.8.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
long_description = f.read()

NAME = "arduino-iot-client"
REQUIRES = ["urllib3 >= 1.25", "python_dateutil >= 2.8.2", "pydantic >= 2", "typing-extensions >= 4.7.1"]
REQUIRES = ["urllib3 >= 1.25", "python_dateutil >= 2.8.2", "pydantic >= 2.9.2", "typing-extensions >= 4.7.1"]

setup(
name=NAME,
Expand Down

0 comments on commit 7b1b148

Please sign in to comment.