So, I tried creating an alternative of aldente in Node Js. Here’s what I found.
Origin of the Idea:
I brought my Macbook air m1 in 2022, over the past two years its battery degraded to absolutely nothing, My mac used to show blank when I click on battery health and it hardly lasted for one second without power?
So, I had to replace its battery? Which I eventually replaced last month(september 2024).
But soon after replacement on a lousy morning I was working on my mac, It suddenly starts to heat. And I got frustrated and scared due to this. The thing is repair and replacement of any apple product is expensive. And I hadn’t had any regular source of income that time.
So I had to find a solution?
So I got on web, and it hardly took me a minute to find about aldente?
So what Aldente do?
Aldente is an application for mac os build by AppHouseKitchen. This application helps mac users to boost battery life of their machine. And it do this by some cool feature.
Aldente lets you choose how you charge your battery. You can restrict charging your mac after some percentage (e.g. 80%) It also has calibration mode and heat protection. You can even schedule your charge timing.
This app comes with freemium model and for me free version was more than sufficient for me. Click this link to find more about them.
All these things made me curious how these things are done as a developer and how can I achieve that. At the very least I was interested in tech behind this.
So for the next two days I started to research on this thing, but before that let me tell about some of my approaches
My Initial thinking
So I’m a MERN stack developer. And My initial approach was to do it using nodeJs. But there’s a cache?
Let me Tell you something about node?
NodeJs can’t directly communicate with kernel. It is only ideal for application logic, APIs, and scripting but cannot interact with hardware directly. So for controlling hardware, networking, and system calls requires low-level languages like C/C++. You may now ask then how does Nodejs manage file system. Well it does with the help of fs module which is done by implementation of libuv. It’s not only the node js, java also can’t do that.
Now lets apply what we have found till now.
Certainly we can’t build aldente in node js. But let see how far can we go there. Can we atleast get notification about when my battery exceeds threshold limit. Lets try
const batteryLevel = require("battery-level");
const notifier = require("node-notifier");
const isCharging = require("is-charging");
const CHARGE_LIMIT = 80;
const DISCHARGE_THRESHOLD = 40;
async function checkBatteryStatus() {
const level = (await batteryLevel()) * 100;
console.log(`Current battery level: ${level.toFixed(2)}%`);
const isChargingNow = await isCharging();
console.log(`Battery is ${isChargingNow ? "charging" : "not charging"}`);
if (level >= CHARGE_LIMIT && isChargingNow) {
notifier.notify({
title: "Battery Charge Alert",
message: `Battery has reached ${CHARGE_LIMIT}%. Unplug the charger to prolong battery life.`,
});
}
if (level <= DISCHARGE_THRESHOLD && !isChargingNow) {
notifier.notify({
title: "Battery Charge Alert",
message: `Battery level is low (${level.toFixed(
2
)}%). Consider plugging in the charger.`,
});
}
}
setInterval(checkBatteryStatus, 5 * 60 * 1000);
If you see at the code. All I have did is written some if else statements and installed 3 packages to get notification. The real hero in achieving this thing is Andreas Gillström who wrote battery-level and is-charging package.
Final thought
- So if you are a mac user, take a minute to thank Aldente team for the stuff they have done for us. Thank you.