def __init__(self):
     print("SimulationController")
     self.simulator = ReeferSimulator()
     self.metricsProducer = MetricsEventsProducer()
Esempio n. 2
0
from flask import Flask, request, jsonify, abort
import os, time
from datetime import datetime
from infrastructure.MetricsEventsProducer import MetricsEventsProducer
from domain.reefer_simulator import ReeferSimulator

VERSION = "Reefer Container simulator v0.0.4"
application = Flask(__name__)

metricsProducer = MetricsEventsProducer()


@application.route("/")
def hello():
    return VERSION


@application.route("/control", methods=['POST'])
def runSimulator():
    print("post received: ")
    print(request.json)
    if not 'containerID' in request.json:
        abort(400)
    control = request.json
    simulator = ReeferSimulator()
    if control["simulation"] == ReeferSimulator.SIMUL_POWEROFF:
        metrics = simulator.generatePowerOffTuples(
            control["containerID"], int(control["nb_of_records"]),
            float(control["good_temperature"]))
    elif control["simulation"] == ReeferSimulator.SIMUL_CO2:
        metrics = simulator.generateCo2Tuples(
class SimulationController(Resource):
    def __init__(self):
        print("SimulationController")
        self.simulator = ReeferSimulator()
        self.metricsProducer = MetricsEventsProducer()

    @swag_from('version.yaml')
    def get(self):
        return jsonify({"version": "v0.0.2"})

    def sendEvents(self, metrics):
        """
        Send Events to Kafka
        """
        logging.info(metrics)
        for metric in metrics:
            evt = {
                "containerID": metric[0],
                "timestamp": str(metric[1]),
                "type": "ReeferTelemetries",
                "payload": str(metric)
            }
            self.metricsProducer.publishEvent(evt, "containerID")

    # Need to support asynchronous HTTP Request, return 202 accepted while starting
    # the processing of generating events. The HTTP header needs to return a
    # location to get the status of the simulator task
    @track_requests
    @swag_from('controlapi.yml')
    def post(self):
        logging.info("post control received: ")
        control = request.get_json(force=True)
        logging.info(control)
        if not 'containerID' in control:
            abort(400)

        nb_records = int(control["nb_of_records"])
        if control["simulation"] == ReeferSimulator.SIMUL_POWEROFF:
            metrics = self.simulator.generatePowerOffTuples(
                control["containerID"], nb_records, control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_CO2:
            metrics = self.simulator.generateCo2Tuples(control["containerID"],
                                                       nb_records,
                                                       control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_O2:
            metrics = self.simulator.generateO2Tuples(control["containerID"],
                                                      nb_records,
                                                      control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_TEMPERATURE:
            metrics = self.simulator.generateTemperatureTuples(
                control["containerID"], nb_records, control["product_id"])
        elif control["simulation"] == ReeferSimulator.NORMAL:
            metrics = self.simulator.generateNormalTuples(
                control["containerID"], nb_records, control["product_id"])
        else:
            return {"error": "Wrong simulation controller data"}, 404

        self.sendEvents(metrics)

        return {
            "reason": "Simulation started for " + str(nb_records) + " records"
        }, 202
Esempio n. 4
0
from flask import Flask, request, jsonify, abort
import os, time
from datetime import datetime
from infrastructure.MetricsEventsProducer import MetricsEventsProducer
from domain.reefer_simulator import ReeferSimulator

VERSION = "Reefer Container simulator v0.0.3"
application = Flask(__name__)

kp = MetricsEventsProducer()


@application.route("/")
def hello():
    return VERSION


@application.route("/control", methods=['POST'])
def runSimulator():
    print(request.json)
    if not 'containerID' in request.json:
        abort(400)
    control = request.json
    simulator = ReeferSimulator()
    if control["simulation"] == ReeferSimulator.SIMUL_POWEROFF:
        metrics = simulator.generatePowerOffTuples(
            control["containerID"], int(control["nb_of_records"]),
            float(control["good_temperature"]))
    elif control["simulation"] == ReeferSimulator.SIMUL_CO2:
        metrics = simulator.generateCo2Tuples(
            control["containerID"], int(control["nb_of_records"]),