Skip to content
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

Add Calendar::Event time validation for protocol 7 #322

Merged
merged 3 commits into from
Aug 13, 2023

Conversation

synthead
Copy link
Owner

Fixes #321!

This PR adds an ActiveRecord validation for protocol 7's Calendar::Event's time value to ensure no events are before the device time. Due to protocol limitations related to event times being relative to the device time, it is not possible to transfer events in the past.

From #321:

Related to #316.

The DSI e-BRAIN (protocol 7) uses 5 minute intervals relative to the current date of the device time as the event time. This means that if the device time is midnight, and the event if at 00:15, the calendar event time is sent as "3". If the event is midnight on the next day, it is sent as "288" (1440 minutes in a day / 5 minutes).

Since this is relative to the device time, if a calendar event happens to be before the device time, then this method will produce negative values in its array of bytes:

def time_formatted(device_time)
device_time_midnight = Time.new(device_time.year, device_time.month, device_time.day)
seconds = (time - device_time_midnight).to_i
five_minutes = seconds / FIVE_MINUTES_SECONDS
lsb_msb_format_for(five_minutes)
end

When this happens, this exception is seen when attempting a transfer:

/home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:35:in `chr': -261 out of char range (RangeError)
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:35:in `block (2 levels) in write'
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:32:in `each'
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:32:in `block in write'
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:31:in `each'
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client/notebook_adapter.rb:31:in `write'
        from /home/jac/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/timex_datalink_client-0.12.2/lib/timex_datalink_client.rb:113:in `write'
        from ebrain.ruby:111:in `<main>'

Since it is not possible to transfer calendar events in the past on protocol 7 due to the relative time comparison, an ActiveModel validation should be added to protocol 7's Eeprom::Calendar::Event. This will ensure that invalid data is not passed to protocol 7's Eeprom, and ultimately causing exceptions when attempting to pass negative values to TimexDatalinkClient::NotebookAdapter.

@synthead synthead added bug Something isn't working protocol 7 Specific to protocol 7 labels Aug 13, 2023
@synthead synthead self-assigned this Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working protocol 7 Specific to protocol 7
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add validation to ensure calendar event time is greater or equal to device time for protocol 7
1 participant