Last active
July 29, 2020 09:09
-
-
Save cp2004/8ff1d64254b05e67b929728d5f7a66ba to your computer and use it in GitHub Desktop.
TPLink sqlite3 Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import os | |
import sqlite3 | |
from datetime import datetime | |
class TPLinkTest: | |
def __init__(self): | |
self.init_time = time.time() | |
print("[{}] Init".format(self.init_time)) | |
self.db_path = 'test.db' | |
self.create_db() | |
print("[{}] DB Created if didn't exist".format(time.time())) | |
self.plugs = ['192.168.0.1', '192.168.0.2', '192.168.0.3'] # 3 Plugs to simulate issue | |
def create_db(self): | |
if not os.path.exists(self.db_path): | |
print("Creating DB") | |
db = sqlite3.connect(self.db_path) | |
cursor = db.cursor() | |
cursor.execute('''CREATE TABLE energy_data(id INTEGER PRIMARY KEY, ip TEXT, timestamp TEXT, current REAL, power REAL, total REAL, voltage REAL)''') | |
db.commit() | |
db.close() | |
else: | |
print("DB exists") | |
def check_statuses(self): | |
print("[{}] Starting Checking plugs".format(time.time())) | |
for plug in self.plugs: | |
self.check_status(plug) | |
def check_status(self, plugip): | |
print("[{}] Plug IP {} check start".format(time.time(), plugip)) | |
today = datetime.today() | |
c = 0.5 # Current, A | |
v = 230 # Voltage, V | |
p = 115 # Power, W | |
t = 115 # Total usage, Wh | |
db = sqlite3.connect(self.db_path) | |
cursor = db.cursor() | |
cursor.execute('''INSERT INTO energy_data(ip, timestamp, current, power, total, voltage) VALUES(?,?,?,?,?,?)''', | |
[plugip, today.isoformat(' '), c, p, t, v]) | |
db.commit() | |
db.close() | |
print("[{}] Plug IP {} check end".format(time.time(), plugip)) | |
print("Hello!") | |
testTP = TPLinkTest() | |
testTP.check_statuses() | |
testTP.end_time = time.time() | |
print("Finished, total time: {} secs".format(testTP.end_time - testTP.init_time)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment