Notifications
Clear all

Witty Pi 4 L3V7 - interupting a scheduler

6 Posts
3 Users
1 Likes
77 Views
(@andrew-simpson17)
Posts: 56
Trusted Member Customer
Topic starter
 

Hi,

I use a scheduller - say turn on device at 1pm to 2pm is there anyway to 'interupt' that and change via code at all?

 
Posted : 05/05/2024 12:00 pm
(@mmoollllee)
Posts: 33
Eminent Member
 

Hey Andrew,
you can edit the schedule.wpi and then run runScript.sh to update the schedule.

I do check the config for changes and update this from Phyton with the following code:

import os
import subprocess
import log

def loadWPI(file):
   if not os.path.isfile(file):
      log.warning("WPI File not Found: " + file)
      return False
   with open(file, 'r') as f:
      content = f.read()
   return content

def schedule(conf):
   wpi = loadWPI('wittypi/schedule.wpi')
   if conf.witty:
      if not conf.witty.schedule and wpi:
         os.remove('wittypi/schedule.wpi')
      elif conf.witty.schedule and conf.witty.schedule != wpi:
         with open('wittypi/schedule.wpi', 'w', encoding='utf-8') as f:
            f.writelines(conf.witty.schedule)
         log.info("schedule.wpi updated.")
      else:
         return
      witty_runScript()
   elif not conf.witty and wpi:
      os.remove('wittypi/schedule.wpi')
      log.info("schedule.wpi removed.")
      witty_runScript()


def witty_runScript():
   env = os.environ.copy()
   env['PATH'] = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
   env['SHELL'] = '/bin/bash'
   subprocess.Popen("bash " + "wittypi/runScript.sh", shell=True, stdin=False, 
   stdout=False, stderr=False, env=env)

Just make sure to run runScript.sh in bash!

 
Posted : 06/05/2024 8:08 am
uugear reacted
(@mmoollllee)
Posts: 33
Eminent Member
 

I noticed, is not really interrupting the schedule, as scheduled startup/shutdown are not reset after deleting the schedule.wpi.

To solve it I've opened a Pull-Request for the wittyPi Software to introduce a RESET keyword. So putting RESET in the schedule.wpi, it unsets the timers too.

You could actually use it already by changing the files according to my pull-request (runScript.sh, utilities.sh, wittyPi.sh).

With this change my resulting python code looks like this:

import os
import subprocess
import log

def loadWPI(file):
   if not os.path.isfile(file):
      log.warning("WPI File not Found: " + file)
      return False
   with open(file, 'r') as f:
      content = f.read()
   return content

def schedule(conf):
   wpi = loadWPI('wittypi/schedule.wpi')
   if conf.witty:
      if not conf.witty.schedule and wpi:
         with open('wittypi/schedule.wpi', 'w', encoding='utf-8') as f:
            f.writelines("RESET")
         log.info("schedule.wpi RESET.")
      elif conf.witty.schedule and conf.witty.schedule != wpi:
         with open('wittypi/schedule.wpi', 'w', encoding='utf-8') as f:
            f.writelines(conf.witty.schedule)
         log.info("schedule.wpi updated.")
      else:
         return
      witty_runScript()
   elif not conf.witty and wpi:
      with open('wittypi/schedule.wpi', 'w', encoding='utf-8') as f:
         f.writelines("RESET")
      log.info("schedule.wpi RESET.")
      witty_runScript()


def witty_runScript():
   env = os.environ.copy()
   env['PATH'] = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
   env['SHELL'] = '/bin/bash'
   subprocess.Popen("bash " + "wittypi/runScript.sh", shell=True, stdin=False, 
   stdout=False, stderr=False, env=env)

 

 
Posted : 07/05/2024 11:22 am
(@andrew-simpson17)
Posts: 56
Trusted Member Customer
Topic starter
 

Hi

 

Sorry for the late reply was away and could not contact you.

Thanks for the scripts.

Not sure if this will help me?I will try and explain better 🙂

Suppose I set a schdeuler to run once a day for an hour.  But, for some reason I want to cancel this.  My user will not have access to manually do anything.  Their only interface is on a web page that I have provided them access to. this web page is a web server running on the Rpi.  But it seems to me the only way they can cancel or amend the scheduler is to wait until the RPi starts on the hour of that day and press 'cancel scheduler' on my web app. 

Is that correct?

 

Thanks

 

 
Posted : 12/05/2024 12:00 pm
(@admin)
Posts: 332
Member Admin
 

@andrew-simpson17 it is correct, unless you have implemented another approach to force Witty Pi to wake up the Pi immediately.

 

 
Posted : 12/05/2024 1:38 pm
(@andrew-simpson17)
Posts: 56
Trusted Member Customer
Topic starter
 

Working Sunday? Impressed 🙂

 

OK, it is what I thought.  Thanks

 

 
Posted : 12/05/2024 2:07 pm
Join Waitlist We will inform you when the product arrives in stock. Please leave your valid email address below.