Continuing in my effort to create blog posts of old bits of code I can find lying around on my computer and internal git server, I decided to post this small C++ project. It logs the percentage of battery left at specific intervals into a SQLite3 database. Again, this is one of my first C++ projects so I'm not exactly confident it's done to a high standard. :)

Nonetheless, the code is included below and a the git repository on bitbucket can be found here: https://bitbucket.org/ryanmcconville/windows-battery-stats/

#include "stdafx.h"
#include "iostream"
#include "sstream"
#include "time.h"
#include "vector"
#include "iostream"
#include "Windows.h"
#include "fstream"
#include "Shlobj.h"
#include "stdio.h"
#include "WbemCli.h"
#include "sqlite3.h"

using namespace std;

//get the users home directory
wstring GetHomeDirectory(){
PWSTR path;
SHGetKnownFolderPath(FOLDERID_Profile, 0, NULL, &path);
return path;
}

void WriteDefaultConfiguration(char *defaulttime){
ofstream config_file;
config_file.open("config.ini", ios::out);
config_file.write(defaulttime, strlen(defaulttime));
config_file.close();
}

int ConvertToMilliseconds(int interval){
return ((interval * 60) * 1000);
}

int ReadConfigurationFile(){
char defaulttime[7] = "10";
char line[7];
ifstream config_file;
config_file.open("config.ini",ios::in);
if(!config_file.is_open()){
WriteDefaultConfiguration(defaulttime);
config_file.open("config.ini",ios::in);
if(!config_file.is_open()){
return ConvertToMilliseconds(atoi(defaulttime));
}
}
config_file.get(line, 10);
int interval;
interval = atoi(line);
int interval_milli = ConvertToMilliseconds(interval);
return interval_milli;
}

sqlite3* CreateDatabase(){
sqlite3 *db;
char *err = 0;

int rc = sqlite3_open("battery_stats.sqlite", &db);

if(rc){
fprintf(stderr, "cant open database %s\n", sqlite3_errmsg(db));
}

char *sql = "CREATE TABLE IF NOT EXISTS stat(" \
"date DATETIME NOT NULL," \
"battery_percentage INT NOT NULL," \
"ac_connected INT NOT NULL);";

sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, sql, -1, &statement, 0) == SQLITE_OK){
sqlite3_step(statement);
}
sqlite3_close(db);

return db;
}

int _tmain(int argc, _TCHAR* argv[]){

//hide console window
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
int interval_time = ReadConfigurationFile();
sqlite3 *db = CreateDatabase();

while(true){
SYSTEM_POWER_STATUS status;
::GetSystemPowerStatus(&status);//get battery life
int per = status.BatteryLifePercent;
//get time
time_t rawtime;
struct tm* printtime;
time(&rawtime);
printtime = localtime(&rawtime);
string timenow = asctime(printtime);
//create structure to hold percentage as a string
char strPer[4];
//copy int percentage to string
itoa(per, strPer, 10);
//open textfile to write to
ofstream batteryStats;
batteryStats.open(GetHomeDirectory() +L"/battery_stats.txt", ios::app);
int AC = (int) status.ACLineStatus;
string acconnected;
if(AC){
acconnected = "AC connected";
}else{
acconnected = "AC disconnected";
}

if(!batteryStats.is_open()){
cout<< "Error";
}else{
batteryStats << "\n" << timenow < }

sqlite3_stmt *statement;
char *sql = "INSERT INTO stat (date, battery_percentage, ac_connected) VALUES(?, ?, ?);";
if(sqlite3_prepare_v2(db, sql, strlen(sql), &statement, 0) == SQLITE_OK){
sqlite3_bind_text(statement, 1, asctime(printtime),strlen(asctime(printtime)), 0);
sqlite3_bind_int(statement, 2, per);
sqlite3_bind_int(statement, 3, AC);

sqlite3_step(statement);
sqlite3_finalize(statement);
} else {
fprintf(stderr, "cant update database %s\n", sqlite3_errmsg(db));
}

batteryStats.close();
//sleep for specified time
Sleep(interval_time);
}
}


This was posted on Tue 24 Jun 2014 (4 years, 10 months ago) by Ryan McConville
Comments
online pharmacy
March 26, 2019, 1:18 p.m. (3 weeks, 6 days ago)

<a href="http://canadianpharmacyxyz.com/">canadian online pharmacies</a>
http://canadianpharmacyxyz.com/
<a href=http://canadianpharmacyxyz.com/>canadian online pharmacy</a>

overseas pharmacies that deliver to usa
March 25, 2019, 8:28 p.m. (4 weeks ago)

<a href="http://canadianpharmacyxyz.com/">no 1 canadian pharcharmy online</a>
http://canadianpharmacyxyz.com/
<a href=http://canadianpharmacyxyz.com/>reputable mexican pharmacies online</a>

canadian online pharmacy
June 10, 2018, 8:11 a.m. (10 months, 2 weeks ago)

<a href="http://canadianonlinexyz.com/">prescription without a doctor's prescription</a>
http://canadianonlinexyz.com/
<a href=http://canadianonlinexyz.com/>canadian pharmacy</a>

online pharmacies
June 4, 2018, 4:23 a.m. (10 months, 3 weeks ago)

<a href="http://canadianonlinepharmacyhd.com/">canadian mail order pharmacies to usa</a>
http://canadianonlinepharmacyhd.com/
<a href=http://canadianonlinepharmacyhd.com/>mail order prescriptions from canada</a>

AnthonyVarse
May 18, 2018, 6:36 p.m. (11 months, 1 week ago)

<a href="http://canadianonlinepharmacyhd.com/">best canadian mail order pharmacies</a>
http://canadianonlinepharmacyhd.com/
<a href=http://canadianonlinepharmacyhd.com/>canadian pharmacies online</a>

Anonymous
April 25, 2018, 12:50 a.m. (12 months ago)

<a href="http://canadianonlinepharmacyhd.com/">legitimate canadian pharmacies</a>
http://canadianonlinepharmacyhd.com/
<a href=http://canadianonlinepharmacyhd.com/>canadian pharmacies that ship to us</a>

kamagra store info erfahrungen
March 29, 2018, 6:14 p.m. (1 year ago)

kamagra oral jelly 100mg reviews
<a href="http://kamagradxt.com/">kamagra 100 mg oral jelly</a>
kamagra online
<a href="http://kamagradxt.com/">kamagra 100 mg</a>
kamagra gel opinie forum
http://kamagradxt.com/
kamagra oral jelly review uk

Wyatt
Feb. 7, 2018, 5:39 a.m. (1 year, 2 months ago)

diet supplements
diet pill
metformin weight loss
weight loss for women
phentermine 37.5 without doctor's prescription

Trey
Jan. 29, 2018, 1:08 a.m. (1 year, 2 months ago)

online casino
online casino
casino online
real money casino
online casino

Valentina
Jan. 25, 2018, 9:04 a.m. (1 year, 2 months ago)

casino games
casino online
casino games
casino online
real money casino

Kelle
Jan. 3, 2018, 12:03 a.m. (1 year, 3 months ago)

payday loans bad credit
payday loans
payday loans online no credit check
payday loans no credit check

Ellen
Jan. 2, 2018, 4:36 p.m. (1 year, 3 months ago)

slots for free
free slots no download
slots casino
free slot games
slots casino games

Ivansuids
Oct. 7, 2017, 3:47 p.m. (1 year, 6 months ago)

rx price comparison
<a href="http://canadapharmaciesbsl.com/">canadapharmaciesbsl.com</a>
canada online pharmacies
<a href=http://canadapharmaciesbsl.com/pain+meds+online+without+doctor+prescription/port-vila-vanuatu-pharmacy-53.html>port vila vanuatu pharmacy</a>
canada pharmacy no prescription
<a href="http://canadapharmaciesbsl.com/canadian+pharmacy+reviews/reliable-online-pharmacy-e7.html">reliable online pharmacy</a>

Ivansuids
Oct. 4, 2017, 12:39 a.m. (1 year, 6 months ago)

canadianpharmacy
<a href="http://canadianpharmacyrxbsl.org/">canadian pharmacies shipping to usa</a>
canada meds
<a href=http://canadianpharmacyrxbsl.org/?zithromax-for-ear-infection>zithromax for ear infection</a>
prescription drugs online without
<a href="http://canadianpharmacyrxbsl.org/?how-long-does-soma-show-up-in-urine">how long does soma show up in urine</a>

Sarah Carlson
Oct. 2, 2017, 4:51 p.m. (1 year, 6 months ago)

This is a message to the RyanMcConville.com | Windows Battery Recorder webmaster. Your website is missing out on at least 300 visitors per day. Our traffic system will dramatically increase your traffic to your website: http://priscilarodrigues.com.br/url/v - We offer 500 free targeted visitors during our free trial period and we offer up to 30,000 targeted visitors per month. Hope this helps :) Unsubscribe here: http://pcgroup.com.uy/2a

FelipeTag
Sept. 22, 2017, 9:05 a.m. (1 year, 7 months ago)

internet pharmacy
<a href="http://canadianpharmacyrxbsl.com/">canadian pharmacy</a>
best online pharmacies canada
<a href=http://canadianpharmacyrxbsl.com/?sertraline-50mg>sertraline 50mg</a>
online drugs
<a href="http://canadianpharmacyrxbsl.com/?how-does-cialis-work">how does cialis work</a>

FelipeTag
Sept. 22, 2017, 12:38 a.m. (1 year, 7 months ago)

best online international pharmacies
<a href="http://canadianpharmacyrxbsl.com/">canada drug</a>
northwest pharmacy canada
<a href=http://canadianpharmacyrxbsl.com/?discount-viagra>discount viagra</a>
canada pharmacy
<a href="http://canadianpharmacyrxbsl.com/?zoloft-and-pregnancy">zoloft and pregnancy</a>

FelipeTag
Sept. 21, 2017, 6:59 p.m. (1 year, 7 months ago)

drugs from canada
<a href="http://canadianpharmacyrxbsl.com/">http://canadianpharmacyrxbsl.com/</a>
prescription drugs canada
<a href=http://canadianpharmacyrxbsl.com/?sildenafil-20-mg-tablet-reviews>sildenafil 20 mg tablet reviews</a>
canada drugs
<a href="http://canadianpharmacyrxbsl.com/?cialis-without-a-doctor-s-prescription">cialis without a doctor's prescription</a>

FelipeTag
Sept. 21, 2017, 10:45 a.m. (1 year, 7 months ago)

king pharmacy
<a href="http://canadianpharmacyrxbsl.com/">canadian pharmacy</a>
get prescription online
<a href=http://canadianpharmacyrxbsl.com/?cialis-prices>cialis prices</a>
pharmacy prices compare
<a href="http://canadianpharmacyrxbsl.com/?cialis-sans-ordonnance">cialis sans ordonnance</a>

Geraldcor
Sept. 20, 2017, 8:59 a.m. (1 year, 7 months ago)

canadian pharmacy online canada
<a href="http://canadianpharmacyrxbsl.com/">canadian pharmacy</a>
canadian online pharmacy
<a href=http://canadianpharmacyrxbsl.com/?valtrex-generic>valtrex generic</a>
approved canadian pharmacies online
<a href="http://canadianpharmacyrxbsl.com/?fluconazole-tablets">fluconazole tablets</a>

Ralphsuids
Sept. 6, 2017, 2:33 p.m. (1 year, 7 months ago)

cialis de 0.5 mg

<a href="http://cialisxrm.com/">cialis generic</a>

dans quel pays le cialis est en vente libre

<a href=http://cialisxrm.com/>cialis coupon</a>

Ann Weaver
Aug. 4, 2017, 4:37 a.m. (1 year, 8 months ago)

This is a message to the RyanMcConville.com | Windows Battery Recorder admin. Your website is missing out on at least 300 visitors per day. Our traffic system will dramatically increase your traffic to your site: http://stpicks.com/27 - We offer 500 free targeted visitors during our free trial period and we offer up to 30,000 targeted visitors per month. Hope this helps :) Unsubscribe here: http://pcgroup.com.uy/2a

Ann Weaver
July 5, 2017, 11:19 a.m. (1 year, 9 months ago)

I came across your RyanMcConville.com | Windows Battery Recorder website and wanted to let you know that we have decided to open our POWERFUL and PRIVATE web traffic system to the public for a limited time! You can sign up for our targeted traffic network with a free trial as we make this offer available again. If you need targeted traffic that is interested in your subject matter or products start your free trial today: http://tdil.co/3p Unsubscribe here: http://priscilarodrigues.com.br/url/11

Ann Weaver
June 17, 2017, 1:46 p.m. (1 year, 10 months ago)

I came across your RyanMcConville.com | Windows Battery Recorder website and wanted to let you know that we have decided to open our POWERFUL and PRIVATE website traffic system to the public for a limited time! You can sign up for our targeted traffic network with a free trial as we make this offer available again. If you need targeted traffic that is interested in your subject matter or products start your free trial today: http://aici.cf/2t1 Unsubscribe here: http://priscilarodrigues.com.br/url/11

Chelsea Wallace
May 27, 2017, 11:37 p.m. (1 year, 10 months ago)

I came across your RyanMcConville.com | Windows Battery Recorder website and wanted to let you know that we have decided to open our POWERFUL and PRIVATE website traffic system to the public for a limited time! You can sign up for our targeted traffic network with a free trial as we make this offer available again. If you need targeted traffic that is interested in your subject matter or products start your free trial today: http://magc.co/7fe Unsubscribe here: http://acortarurl.es/97

Elisa Brown
April 29, 2017, 12:13 a.m. (1 year, 11 months ago)

I discovered your RyanMcConville.com | Windows Battery Recorder page and noticed you could have a lot more traffic. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. We can send you targeted traffic and we let you try it for free. Get over 1,000 targeted visitors per day to your website. Start your free trial: http://priscilarodrigues.com.br/url/v - Unsubscribe here:http://acortarurl.es/8y