def fix_data(msg): print("Reading events") data = json.loads(msg) events = [] for event in data["events"]: pEvent = dict() vehicle = get_vehicle(event["header"]["UnitId"]) if vehicle: pEvent["Dominio"] = vehicle["Registration"] pEvent["NroSerie"] = vehicle["Description"] pEvent["Codigo"] = "1" pEvent["customer_name"] = get_name(vehicle["Description"]) pEvent["Latitud"] = event["header"]["Latitude"] pEvent["Longitud"] = event["header"]["Longitude"] pEvent["Altitud"] = event["header"]["Odometer"] pEvent["Velocidad"] = event["header"]["Speed"] pEvent["FechaHoraEvento"] = Utils.format_date( Utils.utc_to_datetime(event["header"]["UtcTimestampSeconds"]), '%Y-%m-%dT%H:%M:%S') pEvent["FechaHoraRecepcion"] = Utils.format_date( Utils.string_to_date(data["date"], "%Y-%m-%d %H:%M:%S"), "%Y-%m-%dT%H:%M:%S") events.append(pEvent) else: logger.error("Vehicle not found: " + event["header"]["UnitId"], extra={ 'props': { "app": config["name"], "label": config["name"] } }) logger.info("Sending document", extra={ 'props': { "raw": events, "app": config["name"], "label": config["name"] } }) if len(events) > 0: send(events)
def fix_data(msg): print("Reading events") root = {} registro = dict() movil = [] data = json.loads(msg) goods = [] bads = [] for event in data["events"]: mov = dict() obj = event["header"] vehicle = get_vehicle(obj["UnitId"]) if vehicle is not None and "provider" in vehicle: mov["proveedor"] = "TELCEL-GVT" mov["nombremovil"] = "" mov["patente"] = vehicle["Registration"] mov["ignicion"] = "1" mov["GPSLinea"] = "1" mov["LOGGPS"] = "0" mov["puerta1"] = "0" mov["evento"] = "1" mov["latitud"] = format(float(event["header"]["Latitude"]), '.6f') mov["longitud"] = format(float(event["header"]["Longitude"]), '.6f') mov["direccion"] = event["header"]["Direction"] mov["velocidad"] = event["header"]["Speed"] mov["fecha"] = Utils.format_date( Utils.datetime_zone( Utils.utc_to_datetime( event["header"]["UtcTimestampSeconds"]), "America/Mexico_City"), '%d-%m-%Y %H:%M:%S') if mov["latitud"] == 0 or mov["longitud"] == 0: logger.error("Wrong Lat and Long", extra={ 'props': { "raw": mov, "app": config["name"], "label": config["name"] } }) bad = dict() bad["vehicle"] = vehicle["_id"] bad["unit"] = obj["UnitId"] bad["problem"] = "Latitud=" + str( mov["latitud"]) + ", Longitud=" + str(mov["longitud"]) bads.append(bad) else: movil.append(mov) good = dict() good["vehicle"] = vehicle["_id"] goods.append(good) else: bad = dict() bad["problem"] = "Vehiculo no registrado o sin proveedor" bad["unit"] = obj["UnitId"] logger.error("Vehiculo no registrado o sin proveedor", extra={ 'props': { "raw": bad, "app": config["name"], "label": config["name"] } }) registro["movil"] = movil root["registro"] = registro send(root)