Package: pyngsi
pyngsiWhat is it ?pyngsiis a Python framework that helps you write a pipeline for yourFiwaredataflow.Writing aNGSIagent that relies on pyngsi avoids all the plumbing so you can focus on writing your own logic to buildNGSIentities.Key FeaturesNGSI v2supportMap Python-native data to NGSI entitiesWrite NGSI entities to FiwareOrionHandle incoming data through a common interfaceCompute statisticsAllow visualization/debugging facilitiesWhere to get itThe source code is currently hosted on GitHub at :https://github.com/Orange-OpenSource/pyngsiBinary installer for the latest released version is available at thePython
package index.pipinstallpyngsiGetting startedBuild your first NGSI entityfrompyngsi.ngsiimportDataModelm=DataModel(id="Room1",type="Room")m.add_url("dataProvider","https://fiware-orion.readthedocs.io/en/master/user/walkthrough_apiv2/index.html#entity-creation")m.add("pressure",720)m.add("temperature",23.0)m.pprint()The resulting JSON looks like this :{"id":"Room1","type":"Room","dataProvider":{"value":"https://fiware-orion.readthedocs.io/en/master/user/walkthrough_apiv2/index.html#entity-creation","type":"URL"},"pressure":{"value":720,"type":"Integer"},"temperature":{"value":23.0,"type":"Float"}}Send the NGSI entity to the Orion brokerfrompyngsi.sinkimportSinkOrionsink=SinkOrion()sink.write(m.json())Develop your own NGSI AgentLet's quickly create a CSV file to store values from our room sensorsecho-e"Room1;23;720\nRoom2;21;711">room.csvLet's code a function that converts incoming rows to NGSI entitiesdefbuild_entity(row:Row)->DataModel:id,temperature,pressure=row.record.split(';')m=DataModel(id=id,type="Room")m.add_url("dataProvider",row.provider)m.add("temperature",float(temperature))m.add("pressure",int(pressure))returnmLet's use it in in our new NGSI Agentfrompyngsi.sources.sourceimportSource,Rowfrompyngsi.sinkimportSinkOrionfrompyngsi.agentimportNgsiAgentsrc=Source.from_file("room.csv")sink=SinkOrion()agent=NgsiAgent.create_agent(src,sink,process=build_entity)agent.run()This basic example shows how the pyngsi framework is used to build a NGSI Agent.Here data are stored on the local filesystem.By changing just one line you could retrieve incoming data from a FTP server or HTTP server.frompyngsi.sources.sourceimportSource,Rowfrompyngsi.sources.serverimportServerHttpUploadfrompyngsi.sinkimportSinkOrionfrompyngsi.agentimportNgsiAgentsrc=ServerHttpUpload()# line updated !sink=SinkOrion()agent=NgsiAgent.create_agent(src,sink,process=build_entity)agent.run()The HTTP server is running. Now you can send the file to the endpoint.curl-Ffile=@room.csvhttp://127.0.0.1:8880/uploadJSON and text formats are natively supported.Many sources and sinks are provided, i.e.SinkStdoutto just displays entities, eliminating the need of having an Orion server running.One could create a custom Source to handle custom data. TheMicrosoftExcelSourceis given as exemple.One could extend the framework according to his needs.Dependencieslogururequestsrequests-toolbeltshortuuidmore_itertoolsgeojsonflaskcherrypyscheduleopenpyxlLicenseApache 2.0DocumentationThe official documentation is hosted athttps://pixel-ports.github.io/pyngsi-tutorial.htmlKnown IssuesSourceMicrosoftExcel may fail to open some odd Excel files due to an openpyxl bug (i.e. sometimes cannot read graphs).In this case try to remove the offending sheet or prefer working with CSV files.BackgroundWork onpyngsistarted atOrangein 2019 for the needs of thePIXELeuropean project.Fundingpyngsihas been developed as part of thePIXELproject, H2020, funded by the EC under Grant Agreement number769355.