added shutdown command and details to posts

Using os.system the script sets a shutdown time, the bot will then inform users for the impending shutdown.
This commit is contained in:
smallsolar 2023-12-30 19:04:43 +00:00
parent ce9d735acc
commit dcb7f0f9d3
1 changed files with 25 additions and 3 deletions

View File

@ -6,7 +6,7 @@
# Command: python3 solar_bot.py --port /dev/ttyUSB0
import settings
import argparse, os, time
import argparse, os, time, datetime
from vedirect import Vedirect
from mastodon import Mastodon
from apscheduler.schedulers.background import BackgroundScheduler
@ -46,6 +46,21 @@ def print_data_callback(packet):
r.set('yield_yesterday',packet['H22'])
r.set('max_power_yesterday',packet['H23'])
def get_shutdown():
fp = open('/run/systemd/shutdown/scheduled')
data = fp.readlines()
fp.close()
shutdown_date = data[0].split('=')[1].rstrip()
ts = int(shutdown_date) / 1000000
time_now = int(time.time())
shutdown_date = datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
time_to_shutdown = ts - time_now
time_to_date = datetime.datetime.utcfromtimestamp(time_to_shutdown).strftime('%H:%M:%S')
return shutdown_date, time_to_date
def send_toot():
logging.info('Preparing Toot')
try:
@ -57,7 +72,10 @@ def send_toot():
yield_today = int(r.get('yield_today')) / 1000
max_power_today = r.get('max_power_today')
load_power = load_current * batt_v_V
toot_to_send = 'Solarcene.community Power Data\nBattery Voltage: {}V\nBattery Current: {}mA\nPanel Voltage: {}V\nPanel Power: {}W\nLoad Current: {}A\nLoad Power: {}W\nYield Today: {}kWh\nMax Power Today: {}W\nUpdated every 60 minutes'.format(batt_v_V, main_current, panel_voltage, panel_power, load_current, load_power, yield_today, max_power_today)
final_date, time_date = get_shutdown()
toot_to_send = 'Solarcene.community Power Data\nBattery Voltage: {}V\nBattery Current: {}mA\nPanel Voltage: {}V\nPanel Power: {}W\nLoad Current: {}A\nLoad Power: {}W\nYield Today: {}kWh\nMax Power Today: {}W\nUpdated every 60 minutes\n\nShutdown planned for {}UTC (in {})'.format(batt_v_V, main_current, panel_voltage, panel_power, load_current, load_power, yield_today, max_power_today, final_date, time_date)
logging.info(toot_to_send)
except:
logging.info('Failed to construct toot')
@ -69,9 +87,12 @@ def send_toot():
logging.info('Failed to send toot')
def start_toot():
logging.info('Sleeping for 60 seconds to allow mastodon to fully start')
time.sleep(60)
try:
final_date, time_date = get_shutdown()
mastodon = Mastodon(access_token = settings.access_secret, api_base_url = settings.instance_url)
mastodon.toot('System Online')
mastodon.toot('System Online\n\nShutdown planned for: {}UTC (in {})'.format(final_date, time_date))
except:
logging.info('Failed to send toot')
@ -86,6 +107,7 @@ if __name__ == '__main__':
mastodon_login()
logging.info('Logged In')
os.system("shutdown -h 20:15")
start_toot()
ve = Vedirect(args.port, args.timeout)