def main(): agent = NgsiAgent.create_agent(process=basic_processing) # We could have used a lambda, or any function which a Row as a first argument, even an object method # agent = NgsiAgent(process=lambda x: x.record.replace("ping", "pong")) # agent = NgsiAgent(process=myObject.basic_processing) agent.run() agent.close()
def main(): src = SourceMqtt(topic="sensor/temperature") # if you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() agent = NgsiAgent.create_agent(src, sink, process=build_entity) agent.run() agent.close()
def test_agent_upload_multipart(init_agent): multipart_form_data = {'file': ("room.csv", BytesIO(b'Room1;23;710'))} response = requests.post( 'http://127.0.0.1:8880/upload', files=multipart_form_data) assert response.status_code == 200 assert agent.stats == NgsiAgent.Stats(1, 1, 1, 0, 0, 0) agent.stats.zero()
def main(): src = SourceSampleOrion() # if you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() agent = NgsiAgent.create_agent(src, sink, process=build_entity) agent.run() agent.close()
def test_agent_upload_multipart_excel(init_agent): filename = pkg_resources.resource_filename(__name__, "data/test.xlsx") multipart_form_data = {'file': ("test.xlsx", open(filename, 'rb'))} response = requests.post( 'http://127.0.0.1:8880/upload', files=multipart_form_data) assert response.status_code == 200 assert agent.stats == NgsiAgent.Stats(4, 4, 4, 0, 0, 0) agent.stats.zero()
def test_agent_with_processing(mocker): src = SourceSampleOrion(count=5, delay=0) sink = SinkNull() mocker.spy(sink, "write") agent = NgsiAgent.create_agent(src, sink, build_entity_sample_orion) agent.run() agent.close() assert sink.write.call_count == 5 # pylint: disable=no-member assert agent.stats == agent.Stats(5, 5, 5, 0, 0)
def main(): response = retrieve_latest_commits(ncommits=3) src = SourceJson(response, provider="github") # if you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() agent = NgsiAgent.create_agent(src, sink, process=build_entity) agent.run() agent.close()
def test_upload_raw_json(init_agent): data = { 'room': 'Room1', 'temperature': 23.0, 'pressure': 710} response = requests.post('http://127.0.0.1:8880/upload', json=data) assert response.status_code == 200 assert agent.stats == NgsiAgent.Stats(1, 1, 1, 0, 0, 0) agent.stats.zero()
def init_agent(): global agent Source.register_extension("xlsx", SourceMicrosoftExcel) src = ServerHttpUpload() sink = SinkNull() agent = NgsiAgent.create_agent(src, sink) _thread.start_new_thread(agent.run, ()) time.sleep(1) # let time for the server to setup yield assert agent.server_status.calls == 3 agent.close() time.sleep(1) # let time for the server to shutdown
def main(): src = SourceJson(json_obj) # in this example data are embedded in the source # you can get json data from any file or stream # example : Source.from_file("/tmp/test.json") # have a look at the factory methods available in the Source class # if you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() agent = NgsiAgent.create_agent(src, sink, process=build_entity) agent.run() agent.close()
def main(): # Get two RFC descriptions (RFC959 and RFC2228) in JSON format # For the sake of simplicity the example use a lambda and a basic string function for the match function # f_match could be a real function (not just a lambda) and could make use of regular expression # By default user/passwd is set to anonymous/guest src = SourceFtp("ftp.ps.pl", paths=[ "/pub/rfc"], f_match=lambda x: x.endswith("rfc958.json") or x.endswith("rfc2228.json")) # If you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() # The source has auto-detected that we deal with JSON files, hence has parsed json for you agent = NgsiAgent.create_agent(src, sink, process=build_entity).run() # Resources are freed. Here the agent removes the temporary directory (where files were downloaded). agent.close()
def test_agent_with_side_effect(mocker): def side_effect(row, sink, datamodel): # each time we acquire data from sensors in a room, we create (or override) a side-entity # here the side-entity is a room entity for a Building datamodel m = DataModel(id=f"Building:MainBuilding:Room:{datamodel['id']}", type="Room") sink.write(m.json()) return 1 # number of entities created in the function src = SourceSampleOrion(count=5, delay=0) sink = SinkNull() mocker.spy(sink, "write") agent = NgsiAgent.create_agent(src, sink, build_entity_sample_orion, side_effect) agent.run() agent.close() assert sink.write.call_count == 10 # pylint: disable=no-member assert agent.stats == agent.Stats(5, 5, 5, 0, 0, 5)
def main(): # just provide SourceApi with your own function (that returns an array) # default is to retrieve 5 commits # override the default provider "api" with "github" src = SourceFunc(retrieve_latest_commits, "github") # in case you want to retrieve 3 commits, you could use lambda # src = SourceFunc(lambda: retrieve_latest_commits(ncommits=3), "github") # one could prefer to use the partial function from functools # src = SourceFunc(partial(retrieve_latest_commits, ncommits=3), "github") # if you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() agent = NgsiAgent.create_agent(src, sink, process=build_entity) agent.run() agent.close()
def main(): # You declare an HTTP server that acts as your Source, listening on port 8880 src = ServerHttpUpload() # If you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() # The agent processes CSV content received from the client agent = NgsiAgent.create_agent(src, sink, process=build_entity) # You must push data to the source, here we send POST requests to the server # For example, in a bash shell, send binary content # curl --noproxy '*' -v -H "Content-Type: text/plain" -d $'Room1;23.0;720\nRoom2;21.0;711' "127.0.0.1:8880/upload" # You can also send a file, the NGSI datasource provider is set as the filename # curl -v -F [email protected] http://127.0.0.1:8880/upload # CTRL-C to stop the server agent.run() # The agent provides global statistics on its execution agent.close()
def main(): # You declare an HTTP server that acts as your Source, listening on port 8880 src = ServerHttpUpload() # If you have an Orion server available, just replace SinkStdout() with SinkOrion() sink = SinkStdout() # The agent processes JSON content received from the client agent = NgsiAgent.create_agent(src, sink, process=build_entity) # You must push data to the source, here we send POST requests to the server # For example, in a bash shell, type in : # curl -X POST -H "Content-Type: application/json" -d '{"room":"Room1","temperature":23.0,"pressure":710}' http://127.0.0.1:8880/upload # You could also send a JSON Array. For example, type in : # curl -X POST -H "Content-Type: application/json" -d '[{"room":"Room1","temperature":23.0,"pressure":710},{"room":"Room2","temperature":21.0,"pressure":711}]' http://127.0.0.1:8880/upload # You can also send a file, the NGSI datasource provider is set as the filename # curl -v -F [email protected] http://127.0.0.1:8880/upload # CTRL-C to stop the server agent.run() # The agent provides global statistics on its execution agent.close()
def main(): agent = NgsiAgent.create_agent() agent.run() agent.close()
def __init__(self): self.starttime = datetime.now() self.stats = NgsiAgent.Stats()