Example #1
0
 def testGeneratingPowerOff(self):
     simul = ReeferSimulator()
     df = simul.generatePowerOff(cid="101", nb_records=10, tgood=4.4)
     self.assertIsNotNone(df)
     self.assertEqual(df.size, 120)  # nb of rows x nbr of columns
     print(df)
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.4"})

    def sendEvents(self, metrics):
        """
        Send Events to Kafka
        """
        logging.info(metrics)
        for metric in metrics:
            print(metric)
            evt = {
                "containerID": metric['container_id'],
                "timestamp": str(metric['measurement_time']),
                "type": "ReeferTelemetries",
                "payload": 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.generatePowerOff(control["containerID"],
                                                      nb_records,
                                                      control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_CO2:
            metrics = self.simulator.generateCo2(control["containerID"],
                                                 nb_records,
                                                 control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_O2:
            metrics = self.simulator.generateO2(control["containerID"],
                                                nb_records,
                                                control["product_id"])
        elif control["simulation"] == ReeferSimulator.SIMUL_TEMPERATURE:
            metrics = self.simulator.generateTemperature(
                control["containerID"], nb_records, control["product_id"])
        elif control["simulation"] == ReeferSimulator.NORMAL:
            metrics = self.simulator.generateNormal(control["containerID"],
                                                    nb_records,
                                                    control["product_id"])
        elif control["simulation"] == ReeferSimulator.TEMP_GROWTH:
            metrics = self.simulator.generateTemperatureGrowth(
                control["containerID"], nb_records, control["product_id"])
        else:
            return {"error": "Wrong simulation controller data"}, 404

        self.sendEvents(metrics)

        return metrics, 202