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( control["containerID"], int(control["nb_of_records"]), float(control["good_temperature"])) else: return "Wrong simulation controller data" for metric in metrics: evt = { "containerID": control["containerID"], "timestamp": str(metric[0]), "type": "ContainerMetric", "payload": str(metric) } metricsProducer.publishEvent(evt, "containerID") return "Simulation started"
def testGenerateRecordSpecificValues(self): simul = ReeferSimulator() Today = datetime.datetime.today() for i in range(10): (timestamp, pwr, pwrc, co2, o2, tdoor) = simul.generateRecordSpecificValues(Today, i, 0) print((timestamp, pwr, pwrc, co2, o2, tdoor))
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
def testGeneratingCo2(self): simul = ReeferSimulator() df = simul.generateCo2Records(cid="C01", nb_records=100, product_id="P02") self.assertIsNotNone(df) self.assertEqual(df.size, 1800) # nb of rows x nbr of columns
def testGeneratingPowerOff(self): simul = ReeferSimulator() df = simul.generatePowerOffRecords(cid="C01", nb_records=100, product_id="P02") self.assertIsNotNone(df) self.assertEqual(df.size, 1800) # nb of rows x nbr of columns df2 = df.loc[df['maintenance_required'] == 1] for idx, content in df2.items(): print(content)
def __init__(self): print("SimulationController") self.simulator = ReeferSimulator() self.metricsProducer = MetricsEventsProducer()
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
fname = sys.argv[idx + 1] elif arg == "--append": append = True elif arg == "--db": useDB = True return (cid, simulation_type, nb_records, product_id, fname, append, useDB) if __name__ == "__main__": (cid, simulation_type, nb_records, product_id, fname, append, useDB) = parseArguments() if useDB: print(cid, simulation_type, nb_records, product_id) else: print(cid, simulation_type, nb_records, product_id, fname, append) simulator = ReeferSimulator() if simulation_type == ReeferSimulator.SIMUL_POWEROFF: df = simulator.generatePowerOffRecords(cid, nb_records, product_id) elif simulation_type == ReeferSimulator.SIMUL_CO2: df = simulator.generateCo2Records(cid, nb_records, product_id) elif simulation_type == ReeferSimulator.SIMUL_O2: df = simulator.generateO2Records(cid, nb_records, product_id) elif simulation_type == ReeferSimulator.SIMUL_TEMPERATURE: df = simulator.generateTemperatureRecords(cid, nb_records, product_id) elif simulation_type == ReeferSimulator.NORMAL: df = simulator.generateNormalRecords(cid, nb_records, product_id) else: print("Not a valid simulation") exit if (not useDB): saveFile(df, fname, append)
def testCreation(self): print("should have one instance") simul = ReeferSimulator() self.assertIsNotNone(simul)
def testGenerateCO2tuples(self): simul = ReeferSimulator() values = simul.generateCo2Tuples(cid="103", nb_records=5, tgood=4) print(values)
def testGenerateRecordsAtSea(self): simul = ReeferSimulator() values = simul.generatePowerOffTuples(cid="103", nb_records=5, tgood=4) print(values)
def testGeneratingCo2(self): simul = ReeferSimulator() df = simul.generateCo2(cid="102", nb_records=10, tgood=4.4) self.assertIsNotNone(df) self.assertEqual(df.size, 120) # nb of rows x nbr of columns print(df)
def testGenerateCO2tuples(self): simul = ReeferSimulator() values = simul.generateCo2Tuples(cid="C02", nb_records=100, product_id="P02")