Skip to content

Commit

Permalink
Merge pull request #352 from puterboy/jjk-addgrandtotal
Browse files Browse the repository at this point in the history
Fixes	modified:   octoprint_tplinksmartplug/__init__.py
  • Loading branch information
jneilliii authored Feb 9, 2024
2 parents c96ca0c + 174594b commit 6245d9e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v7
- uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue has been automatically marked as stale because it has not had activity in 14 days. It will be closed if no further activity occurs in 7 days'
Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,15 @@ Check out my other plugins [here](https://plugins.octoprint.org/by_author/#jneil

### Sponsors
- Andreas Lindermayr
- [@Mearman](https://github.com/Mearman)
- [@TheTuxKeeper](https://github.com/thetuxkeeper)
- [@tideline3d](https://github.com/tideline3d/)
- [OctoFarm](https://octofarm.net/)
- [SimplyPrint](https://simplyprint.dk/)
- [SimplyPrint](https://simplyprint.io/)
- [Andrew Beeman](https://github.com/Kiendeleo)
- [Calanish](https://github.com/calanish)
- [Lachlan Bell](https://lachy.io/)
- [Johnny Bergdal](https://github.com/bergdahl)
- [Leigh Johnson](https://github.com/leigh-johnson)
- [Jonny Bergdahl](https://github.com/bergdahl)
- [Stephen Berry](https://github.com/berrystephenw)
- [Guyot François](https://github.com/iFrostizz)
- [Steve Dougherty](https://github.com/Thynix)
- [Flying Buffalo Aerial Photography](http://flyingbuffalo.info/)
## Support My Efforts
I, jneilliii, programmed this plugin for fun and do my best effort to support those that have issues with it, please return the favor and leave me a tip or become a Patron if you find this plugin helpful and want me to continue future development.

Expand Down
80 changes: 67 additions & 13 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,38 @@ def on_startup(self, host, port):
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)''')
'''CREATE TABLE energy_data(id INTEGER PRIMARY KEY, ip TEXT, timestamp DATETIME, voltage REAL, current REAL, power REAL, total REAL, grandtotal REAL)''')
else:
db = sqlite3.connect(self.db_path)
cursor = db.cursor()

#Update 'energy_data' table schema if 'grandtotal' column not present
cursor.execute('''SELECT * FROM energy_data''')
if 'grandtotal' not in next(zip(*cursor.description)):
#Change type of 'timestamp' to 'DATETIME' (from 'TEXT'), add new 'grandtotal' column
cursor.execute('''
ALTER TABLE energy_data RENAME TO _energy_data''')
cursor.execute('''
CREATE TABLE energy_data (id INTEGER PRIMARY KEY, ip TEXT, timestamp DATETIME, voltage REAL, current REAL, power REAL, total REAL, grandtotal REAL)''')
#Copy over table, skipping non-changed values and calculating running grandtotal
cursor.execute('''
INSERT INTO energy_data (ip, timestamp, voltage, current, power, total, grandtotal) SELECT ip, timestamp, voltage, current, power, total, grandtotal FROM
(WITH temptable AS (SELECT *, total - LAG(total,1) OVER(ORDER BY id) AS delta, LAG(power,1) OVER(ORDER BY id) AS power1 FROM _energy_data)
SELECT *, ROUND(SUM(MAX(delta,0)) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),6)
AS grandtotal FROM temptable WHERE power > 0 OR power1 > 0 OR delta <> 0 OR delta IS NULL)''')

cursor.execute('''DROP TABLE _energy_data''')
#Compact database
db.commit()
db.close()
cursor.execute('''VACUUM''')

self.last_row = list(cursor.execute('''SELECT id, timestamp, voltage, current, power, total, grandtotal
FROM energy_data ORDER BY ROWID DESC LIMIT 1''').fetchone()) or [0,0,0,0,0,0,0] #Round to remove floating point imprecision
self.last_row = self.last_row[:2] + [round(x,6) for x in self.last_row[2:]] #Round to correct floating point imprecision in sqlite
self.last_row_entered = True
self.total_correction = self.last_row[6] - self.last_row[5] #grandtotal - total
db.commit()
db.close()

def on_after_startup(self):
self._logger.info("TPLinkSmartplug loaded!")
Expand Down Expand Up @@ -512,18 +541,43 @@ def check_status(self, plugip):
p = ""
if "total_wh" in emeter_data["get_realtime"]:
t = emeter_data["get_realtime"]["total_wh"] / 1000.0
emeter_data["get_realtime"]["total_wh"] += self.total_correction * 1000.0 #Add back total correction factor, so becomes grandtotal
elif "total" in emeter_data["get_realtime"]:
t = emeter_data["get_realtime"]["total"]
t = emeter_data["get_realtime"]["total"]
emeter_data["get_realtime"]["total"] += self.total_correction #Add back total correction factor, so becomes grandtotal
else:
t = ""
if self.db_path is not None:
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()
last_p = self.last_row[4]
last_t = self.last_row[5]

if last_t is not None and t < last_t: #total has reset since last measurement
self.total_correction += t
gt = round(t + self.total_correction, 6) #Prevent accumulated floating-point rounding errors
current_row = [plugip, today.isoformat(' '), v, c, p, t, gt]

if self.last_row_entered is False and last_p == 0 and p > 0: #Go back & enter last_row on power return (if not entered already)
db = sqlite3.connect(self.db_path)
cursor = db.cursor()
cursor.execute(
'''INSERT INTO energy_data(ip, timestamp, voltage, current, power, total, grandtotal) VALUES(?,?,?,?,?,?,?)''',
self.last_row)
db.commit()
db.close()
self.last_row_entered = True
else:
self.last_row_entered = False

if t != last_t or p > 0 or last_p > 0: #Enter current_row if change in total or power is on or just turned off
db = sqlite3.connect(self.db_path)
cursor = db.cursor()
cursor.execute(
'''INSERT INTO energy_data(ip, timestamp, voltage, current, power, total, grandtotal) VALUES(?,?,?,?,?,?,?)''',
current_row)
db.commit()
db.close()

self.last_row = current_row

if len(plug_ip) == 2:
chk = self.lookup(response, *["system", "get_sysinfo", "children"])
Expand Down Expand Up @@ -573,7 +627,7 @@ def on_api_command(self, command, data):
db = sqlite3.connect(self.db_path)
cursor = db.cursor()
cursor.execute(
'''SELECT timestamp, current, power, total, voltage FROM energy_data WHERE ip=? ORDER BY timestamp DESC LIMIT ?,?''',
'''SELECT timestamp, current, power, grandtotal, voltage FROM energy_data WHERE ip=? ORDER BY timestamp DESC LIMIT ?,?''',
(data["ip"], data["record_offset"], data["record_limit"]))
response = {'energy_data': cursor.fetchall()}
db.close()
Expand Down Expand Up @@ -705,7 +759,7 @@ def on_event(self, event, payload):

hours = (payload.get("time", 0) / 60) / 60
self._tplinksmartplug_logger.debug("hours: %s" % hours)
power_used = self.print_job_power * hours
power_used = self.print_job_power
self._tplinksmartplug_logger.debug("power used: %s" % power_used)
power_cost = power_used * self._settings.get_float(["cost_rate"])
self._tplinksmartplug_logger.debug("power total cost: %s" % power_cost)
Expand Down Expand Up @@ -748,7 +802,7 @@ def on_event(self, event, payload):
# File Uploaded Event
if event == Events.UPLOAD and self._settings.get_boolean(["event_on_upload_monitoring"]):
if payload.get("print", False) or self._settings.get_boolean(
["event_on_upload_monitoring_always"]): # implemented in OctoPrint version 1.4.1
["event_on_upload_monitoring_always"]): # implemented in OctoPrint version 1.4.1
self._tplinksmartplug_logger.debug(
"File uploaded: %s. Turning enabled plugs on." % payload.get("name", ""))
self._tplinksmartplug_logger.debug(payload)
Expand Down

0 comments on commit 6245d9e

Please sign in to comment.