Package: piscada_cloud
Piscada CloudLibrary for the Piscada Cloud including authentication and data access.FeaturesLogin to Piscada Cloud and retrieve credentialsPersist credentialss locallyRead historic values for multiple tags as a Pandas DataFramePossible apply time-based linear interpolation to measurementsUtils to add fractional representations of periods: day, week, yearInstallInstall from PyPI:pipinstallpiscada-cloudorpoetryaddpiscada-cloudInstall from local source:pipinstall--editablepath/to/piscada_cloudorpoetryaddpath/to/piscada_cloudUsageAuthenticationTo log-in interactively and persist the retrieved credentials on disk (under$HOME/.piscada_credentials) simply run:python-mpiscada_cloud.authorpoetryrunpython-mpiscada_cloud.authAny future invocation, e.g.credentials = piscada_cloud.auth.persisted_login()will return the credentials on disk without user interaction.credentials = piscada_cloud.auth.login(username, password, host)can be used to retrieve the credentials programmatically.Getting DataThe credentials retrieved through the login can be used to get the host and acccesss-token for the historical data API:frompiscada_cloudimportauthcredentials=auth.login_persisted()host,token=auth.get_historian_credentials(credentials)The host and token can be used to retrieve historic data as a Pandas DataFrame.
Theget_historic_valuesmethod takes a row of parameters:start: Datetime objectend: Datetime objecttags: List ofTagobjectshost(optional): Endpoint to which we send the historian queries. e.g.historian.piscada.online.token(optional): Access token, associated with the endpoint, used for authentication.The if thehostortokenarguments are not provided, the environment variablesHISTORIAN_HOSTandHISTORIAN_TOKENare used in stead, respectively.fromdatetimeimportdatetime,timedelta,timezonefrompiscada_cloud.dataimportget_historic_valuesfrompiscada_cloud.mappingsimportTagtags=[Tag(controller_id="fe7bd2c3-6c20-44d4-aecc-df5822457400",name="ServerCpuUsage"),Tag(controller_id="fe7bd2c3-6c20-44d4-aecc-df5822457400",name="ServerMemoryUsage"),]df=get_historic_values(start=datetime.now(timezone.utc)-timedelta(days=30),end=datetime.now(timezone.utc),tags=tags)Write DataIn this example the columnoCU135001RT90_MVis selected and the average value is calculated using the method.mean().To write the result back to the Piscada Cloud, thedatamodule offers thewrite_valuefunction. It takes these arguments:tag: ATagobjectvalue: The float, string, or dict value to write to the tag. Float and string will be sent as is, dict will be serialised as JSON string.timestamp(optional): The timestamp in milliseconds since epoch at which to write the value, by defaultint(time.time() * 1000).host: Endpoint to send post request. Overrides the default, which isos.environ['WRITEAPI_HOST'].token: Access token accosiated with the host. Overrides the default, which isos.environ['WRITEAPI_TOKEN'].TheTag.namemust use the prefixpy_as this is the only namespace allowed for writing data via the API.frompiscada_cloud.dataimportwrite_valuefrompiscada_cloud.mappingsimportTagmean=df["oCU135001RT90_MV"].mean()response=write_value(Tag(controller_id="0798ac4a-4d4f-4648-95f0-12676b3411d5",name="py_oCU135001RT90_MV_1h_mean"),value=mean)ifresponse.ok:print("OK")else:print(response.text)Theresponsereturned by thewrite_valuemethod allows to check if the writing of data was successfulresponse.ok == True.ManipulationsIn order to support analysis in the context of periodic patters, themanipulationsallow you to add fractional representations of day, week, and year as additional columns in the DataFrame:00:00:00 -> 0.0 --- 23:59:59 -> 1.0Monday 00:00:00 -> 0.0 --- Sunday 23:59:59 -> 1.01st Jan. 00:00:00 -> 0.0 --- 31st Dec. 23:59:59 -> 1.0frompiscada_cloudimportmanipulationsmanipulations.add_weekdays(data)manipulations.add_day_fraction(data)manipulations.add_week_fraction(data)manipulations.add_year_fraction(data)DevelopmentRun QA as a pre-commit hookEnable the provided git pre commit hook:ln -s ./qa.sh .git/hooks/pre-commitDocumentationBuild and run MkDocs documentation:poetryrunmkdocsbuild
poetryrunmkdocsserveNote: If you want to deploy a new version of your documentation, use below commands instead:poetryrunmikedeploy[version]poetryrunmikeserveRequirementsThe package will support the two latest version of Python.AuthorsTim Jagenbergtim.jagenberg@piscada.comFilip Henrik Larsenfilip.larsen@piscada.comAleksandra Zajdelaleksandra.zajdel@piscada.comLicense© Piscada AS 2019