Skip to content

Randomisation not working correctly (+ solution) #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wouzer opened this issue Jan 26, 2024 · 0 comments
Open

Randomisation not working correctly (+ solution) #101

wouzer opened this issue Jan 26, 2024 · 0 comments

Comments

@wouzer
Copy link

wouzer commented Jan 26, 2024

I have been using this light scheduler for a couple of years now, almost to full satisfaction. I did notice however that every so often the lights switch off outside the timeframe defined by the randomization.

I have been looking at the code, and it seems in the randomization it uses the previous start(or end) time as basis, rather than the time as depicted in the scedule. Most of the time this levels out, but every so often it moves outside the bounderaries of 'scheduled time' +/- random minuts. And this can compound to quite a lot of minutes if the random nummer is

To illustrate I have logged the times for a while each time the randomizeSchedule was called.
The schedule was from 16.00 to 23.15, with 15 minutes randomisation. I have logged the resulting new start/end time, follewed by the basis it used for the calculation in brackets. As you can see, this basis is alway the previous starttime, not time as in the schedule (apart from the first line obviously).

  new - day 4 --> start : 15:56 (16:00), end : 23:29 (23:15)
  new - day 4 --> start : 15:51 (15:56), end : 23:40 (23:29)
  new - day 4 --> start : 15:52 (15:51), end : 23:29 (23:40) <---- end time outside schedule
  new - day 4 --> start : 15:40 (15:52), end : 23:17 (23:29)
  new - day 4 --> start : 15:39 (15:40), end : 23:04 (23:17) <---- start time outside schedule
  new - day 4 --> start : 15:47 (15:39), end : 23:03 (23:04)
  new - day 4 --> start : 15:56 (15:47), end : 23:11 (23:03)
  new - day 4 --> start : 15:47 (15:56), end : 23:12 (23:11)

After some carefull digging in my javascript knowlegde, I discovered that in the map function used in the randomizeSchedule, the eventparameter is changed and returned to be stored in runningEvents. However, as the event object in the map function is passed by reference (and not by value), the original schedule ('events') is is also changed as in the map function changes each event.

Solution is to deep clone the event into a new_event, change and return that.

      node.runningEvents = node.events.map(event => {
          var minutes = event.end.mod - event.start.mod
          let new_event = JSON.parse(JSON.stringify(event))    // deepClone the event, 

          // Prevent randomization of event that start and end at midnight to
          // prevent "gaps" when the schedule continuous past midnight.
          // TODO: Handle all events that is back-to-back after each other.
          if (event.start.mod !== 0) new_event.start.mod = event.start.mod + offsetMOD()
      
          if (event.end.mod !== 0) {
            new_event.end.mod = event.end.mod + offsetMOD()
      
            if (event.end.mod <= event.start.mod)
              // Handle "too short events"
              new_event.end.mod = event.start.mod + minutes // Events can overlap, but we don't need to care about that
          }
          return new_event
        })

Works like a charm now.

  new - dag 4 --> start : 16:05 (16:00), end : 22:59 (23:15)
  new - dag 4 --> start : 16:03 (16:00), end : 23:00 (23:15)
  new - dag 4 --> start : 16:07 (16:00), end : 22:56 (23:15)
  new - dag 4 --> start : 16:08 (16:00), end : 23:14 (23:15)
  new - dag 4 --> start : 16:12 (16:00), end : 23:14 (23:15)
  new - dag 4 --> start : 16:12 (16:00), end : 23:26 (23:15)
  new - dag 4 --> start : 16:14 (16:00), end : 23:20 (23:15)
  new - dag 4 --> start : 16:17 (16:00), end : 23:13 (23:15)
  new - dag 4 --> start : 15:58 (16:00), end : 23:17 (23:15)

I'll create a pull request for this.

niklaswall added a commit that referenced this issue Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant