Ejemplo n.º 1
0
def runSimulator():
    print("post received: ")
    print(request.json)
    if not 'containerID' in request.json:
        abort(400)
    control = request.json
    simulator = ReeferSimulator()
    nb_records = int(control["nb_of_records"])
    if control["simulation"] == ReeferSimulator.SIMUL_POWEROFF:
        metrics = simulator.generatePowerOffTuples(control["containerID"],
                                                   nb_records,
                                                   control["product_id"])
    elif control["simulation"] == ReeferSimulator.SIMUL_CO2:
        metrics = simulator.generateCo2Tuples(control["containerID"],
                                              nb_records,
                                              control["product_id"])
    elif control["simulation"] == ReeferSimulator.SIMUL_O2:
        metrics = simulator.generateO2Tuples(control["containerID"],
                                             nb_records, control["product_id"])
    else:
        return jsonify("Wrong simulation controller data"), 404

    if nb_records < 500:
        sendEvents(metrics)
    else:
        with ThreadPoolExecutor(max_workers=1) as executor:
            future = executor.submit(sendEvents, metrics)

    return jsonify("Simulation started"), 202
Ejemplo n.º 2
0
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"]),control["product_id"])
    elif  control["simulation"]  == ReeferSimulator.SIMUL_CO2:
        metrics=simulator.generateCo2Tuples(control["containerID"],int(control["nb_of_records"]),control["product_id"])
    elif  control["simulation"]  == ReeferSimulator.SIMUL_O2:
        metrics=simulator.generateO2Tuples(control["containerID"],int(control["nb_of_records"]),control["product_id"])
    else:
        return "Wrong simulation controller data"
    
    for metric in metrics:
        evt = {"containerID": control["containerID"],
                "timestamp": str(metric[0]),
                "type":"ReeferTelemetries",
                "payload": str(metric)}
        metricsProducer.publishEvent(evt,"containerID")
    return "Simulation started"
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
 def testGenerateO2(self):
     simul = ReeferSimulator()
     values = simul.generateO2Tuples(cid="C01",
                                     nb_records=100,
                                     product_id="P02")