Jargendas / MMM-MyBMW

Magic Mirror Module to display data from MyBMW for your car.
MIT License
0 stars 0 forks source link

Range / Mileage in KMs for US Market #2

Open stephenmelody opened 3 months ago

stephenmelody commented 3 months ago

Could the code be changed to allow for Miles in the US market, or a configuration variable to switch between KM and Miles?

Jargendas commented 2 months ago

As far as I'm aware, there is no code in this repository requesting any specific unit. Have you tried changing the region parameter, does that not change the unit?

stephenmelody commented 2 months ago

I had it set to US, but in the end I just modified my local copy slightly to multiply the km by the conversion factor.

Jargendas commented 2 months ago

Yeah, I think that's the correct way. If I understand correctly from the bimmer_connected documentation, the API returns all values in km now.

stephenmelody commented 2 months ago

I guess the MyBMW phone app must do the same conversion because the app shows miles.

stephenmelody commented 2 months ago

IMG_0562

stephenmelody commented 2 months ago

IMG_0563

gbassaragh commented 1 month ago

Can you share how you modified your copy to account for miles vs km?

stephenmelody commented 1 month ago

I made some other changes, and I think this is the latest version. I can’t check as the MM is offline as we’re about to move house!

`Module.register("MMM-MyBMW", { defaults: { region: 'rest', refresh: 15, vehicleOpacity: 0.75, showMileage: true, showElectricPercentage: true, showElectricRange: true, showFuelRange: true, showLastUpdated: true, lastUpdatedText: "last updated" },

getStyles: function () { return ["MMM-MyBMW.css"]; },

getScripts: function () { return ["moment.js"]; },

start: function () { Log.info("Starting module: " + this.name); this.sendSocketNotification("MMM-MYBMW-CONFIG", this.config); this.bmwInfo = {}; this.getInfo(); this.timer = null; },

getInfo: function () { clearTimeout(this.timer); this.timer = null; this.sendSocketNotification("MMM-MYBMW-GET", { instanceId: this.identifier, vin: this.config.vin
});

var self = this;
this.timer = setTimeout(function () {
  self.getInfo();
}, this.config.refresh * 60 * 1000);

},

socketNotificationReceived: function (notification, payload) { if ( notification === "MMM-MYBMW-RESPONSE" + this.identifier && Object.keys(payload).length > 0 ) { this.bmwInfo = payload; this.updateDom(1000); // 1000 milliseconds delay } },

faIconFactory: function (icon) { var faIcon = document.createElement("i"); faIcon.classList.add("fas"); faIcon.classList.add(icon); return faIcon; },

convertKmToMiles: function (km) { return Math.round(parseFloat(km) * 0.621371) + ' miles'; },

getDom: function () { var wrapper = document.createElement("div"); wrapper.classList.add("bmw-wrapper");

if (this.config.email === "" || this.config.password === "") {
  wrapper.innerHTML = "Missing configuration.";
  return wrapper;
}

if (Object.keys(this.bmwInfo).length === 0) {
  wrapper.innerHTML = this.translate("LOADING");
  wrapper.className = "dimmed light small";
  return wrapper;
}

if (!!this.bmwInfo.error) {
  wrapper.innerHTML = this.bmwInfo.error;
  wrapper.className = "dimmed light small";
  return wrapper;
}

let info = this.bmwInfo;

var carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");

var imageContainer = document.createElement("span");
var imageObject = document.createElement("img");
imageObject.setAttribute('src', info.imageUrl);
imageObject.setAttribute('style', 'opacity: ' + this.config.vehicleOpacity + ';');
imageContainer.appendChild(imageObject);
carContainer.appendChild(imageContainer);

wrapper.appendChild(carContainer);

carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");

var mileageContainer = document.createElement("div");
mileageContainer.classList.add("mileage-container");
var mileage = document.createElement("span");
mileage.classList.add("mileage");
if (this.config.showMileage) {
  mileage.appendChild(this.faIconFactory("fa-road"));
  mileage.appendChild(document.createTextNode(this.convertKmToMiles(info.mileage)));
} else {
  mileage.appendChild(document.createTextNode("\u00a0"));
}
mileageContainer.appendChild(mileage);

var fuelRangeContainer = document.createElement("div");
fuelRangeContainer.classList.add("fuelRange-container");
var fuelRange = document.createElement("span");
fuelRange.classList.add("fuelRange");
if (this.config.showFuelRange && (info.fuelRange != '')) {
  fuelRange.appendChild(this.faIconFactory("fa-gas-pump"));
  fuelRange.appendChild(document.createTextNode(this.convertKmToMiles(info.fuelRange)));
} else {
  fuelRange.appendChild(document.createTextNode("\u00a0"));
}
fuelRangeContainer.appendChild(fuelRange);

carContainer.appendChild(fuelRangeContainer);
carContainer.appendChild(mileageContainer);
wrapper.appendChild(carContainer);

carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");

var elecRange = document.createElement("span");
elecRange.classList.add("elecRange");
if (this.config.showElectricRange && (info.electricRange != '')) {
  elecRange.appendChild(this.faIconFactory("fa-charging-station"));
  elecRange.appendChild(document.createTextNode(this.convertKmToMiles(info.electricRange)));
} else {
  elecRange.appendChild(document.createTextNode("\u00a0"));
}
carContainer.appendChild(elecRange);
wrapper.appendChild(carContainer);

carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
carContainer.classList.add("spacer");
wrapper.appendChild(carContainer);

carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");

var locked = document.createElement("span");
locked.classList.add("locked");
if (info.doorLock === true) {
  locked.appendChild(this.faIconFactory("fa-lock"));
} else if (info.doorLock === false) {
  locked.appendChild(this.faIconFactory("fa-lock-open"));
} else {
  console.log("info.doorLock is not a boolean value: ", info.doorLock);
}
carContainer.appendChild(locked);
wrapper.appendChild(carContainer);

carContainer = document.createElement("div");
carContainer.classList.add("bmw-container");
carContainer.classList.add("updated");

var updated = document.createElement("span");
updated.classList.add("updated");
if (this.config.showLastUpdated) {
  updated.appendChild(this.faIconFactory("fa-info"));
  var lastUpdateTime = moment(info.updateTime).format('HH:mm');
  var lastUpdateText = this.config.lastUpdatedText + " " + lastUpdateTime;
  updated.appendChild(document.createTextNode(lastUpdateText));
}
carContainer.appendChild(updated);
wrapper.appendChild(carContainer);

return wrapper;

} }); `

stephenmelody commented 1 month ago

That didn’t render as I wanted it to… I’m on my cell.

gbassaragh commented 1 month ago

Thank you!! I'll take a look see if I can parse through it :)

stephenmelody commented 1 month ago

It’s this bit as far as I remember:

convertKmToMiles: function (km) { return Math.round(parseFloat(km) * 0.621371) + ' miles';