From 04b8ede441827f87697ce58c928e419fee63034a Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sun, 11 Sep 2022 17:52:57 -0700 Subject: [PATCH 1/5] Add Protocol1::Time. --- lib/timex_datalink_client/protocol_1/time.rb | 61 +++++++++++++++++++ .../protocol_1/time_spec.rb | 49 +++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 lib/timex_datalink_client/protocol_1/time.rb create mode 100644 spec/lib/timex_datalink_client/protocol_1/time_spec.rb diff --git a/lib/timex_datalink_client/protocol_1/time.rb b/lib/timex_datalink_client/protocol_1/time.rb new file mode 100644 index 0000000..8fec2d8 --- /dev/null +++ b/lib/timex_datalink_client/protocol_1/time.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require "timex_datalink_client/helpers/crc_packets_wrapper" + +class TimexDatalinkClient + class Protocol1 + class Time + prepend Helpers::CrcPacketsWrapper + + CPACKET_TIME = [0x30] + + attr_accessor :zone, :is_24h, :time + + # Create a Time instance. + # + # @param zone [Integer] Time zone number (1 or 2). + # @param is_24h [Boolean] Toggle 24 hour time. + # @param time [::Time] Time to set (including time zone). + # @return [Time] Time instance. + def initialize(zone:, is_24h:, time:) + @zone = zone + @is_24h = is_24h + @time = time + end + + # Compile packets for a time. + # + # @return [Array>] Two-dimensional array of integers that represent bytes. + def packets + [ + [ + CPACKET_TIME, + zone, + time.hour, + time.min, + time.month, + time.day, + year_mod_1900, + wday_from_monday, + time.sec, + is_24h_value + ].flatten + ] + end + + private + + def year_mod_1900 + time.year % 100 + end + + def wday_from_monday + (time.wday + 6) % 7 + end + + def is_24h_value + is_24h ? 2 : 1 + end + end + end +end diff --git a/spec/lib/timex_datalink_client/protocol_1/time_spec.rb b/spec/lib/timex_datalink_client/protocol_1/time_spec.rb new file mode 100644 index 0000000..246b96e --- /dev/null +++ b/spec/lib/timex_datalink_client/protocol_1/time_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe TimexDatalinkClient::Protocol1::Time do + let(:zone) { 1 } + let(:is_24h) { false } + let(:time) { Time.new(2015, 10, 21, 19, 28, 32) } + + let(:time_instance) do + described_class.new( + zone: zone, + is_24h: is_24h, + time: time + ) + end + + describe "#packets", :crc do + subject(:packets) { time_instance.packets } + + it_behaves_like "CRC-wrapped packets", [ + [0x30, 0x01, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x01] + ] + + context "when zone is 2" do + let(:zone) { 2 } + + it_behaves_like "CRC-wrapped packets", [ + [0x30, 0x02, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x01] + ] + end + + context "when is_24h is true" do + let(:is_24h) { true } + + it_behaves_like "CRC-wrapped packets", [ + [0x30, 0x01, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x02] + ] + end + + context "when time is 2015-10-21 19:28:32" do + let(:time) { Time.new(1997, 9, 19, 19, 36, 55) } + + it_behaves_like "CRC-wrapped packets", [ + [0x30, 0x01, 0x13, 0x24, 0x09, 0x13, 0x61, 0x04, 0x37, 0x01] + ] + end + end +end From 75ddb541ececb8efb4882d96acc077e21fdfc047 Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sun, 11 Sep 2022 17:53:52 -0700 Subject: [PATCH 2/5] Require timex_datalink_client/protocol_1/time in TimexDatalinkClient. --- lib/timex_datalink_client.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/timex_datalink_client.rb b/lib/timex_datalink_client.rb index 2d69afb..13486a5 100644 --- a/lib/timex_datalink_client.rb +++ b/lib/timex_datalink_client.rb @@ -6,6 +6,7 @@ require "timex_datalink_client/protocol_1/end" require "timex_datalink_client/protocol_1/start" require "timex_datalink_client/protocol_1/sync" +require "timex_datalink_client/protocol_1/time" require "timex_datalink_client/protocol_3/alarm" require "timex_datalink_client/protocol_3/eeprom" From d548fa7ee709621528f8383fba97657abc3e072a Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sun, 11 Sep 2022 17:54:24 -0700 Subject: [PATCH 3/5] Add lib/timex_datalink_client/protocol_3/time.rb to gemspec. --- timex_datalink_client.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/timex_datalink_client.gemspec b/timex_datalink_client.gemspec index 65fef5b..efb5907 100644 --- a/timex_datalink_client.gemspec +++ b/timex_datalink_client.gemspec @@ -26,6 +26,7 @@ Gem::Specification.new do |s| "lib/timex_datalink_client/protocol_1/end.rb", "lib/timex_datalink_client/protocol_1/start.rb", "lib/timex_datalink_client/protocol_1/sync.rb", + "lib/timex_datalink_client/protocol_3/time.rb", "lib/timex_datalink_client/protocol_3/alarm.rb", "lib/timex_datalink_client/protocol_3/eeprom.rb", From 616d83dd12019986ab378691dec66775413d5e02 Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sun, 11 Sep 2022 17:57:31 -0700 Subject: [PATCH 4/5] Remove time zone hint in protocol 1 Time YARDoc. --- lib/timex_datalink_client/protocol_1/time.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/timex_datalink_client/protocol_1/time.rb b/lib/timex_datalink_client/protocol_1/time.rb index 8fec2d8..43253b2 100644 --- a/lib/timex_datalink_client/protocol_1/time.rb +++ b/lib/timex_datalink_client/protocol_1/time.rb @@ -15,7 +15,7 @@ class Time # # @param zone [Integer] Time zone number (1 or 2). # @param is_24h [Boolean] Toggle 24 hour time. - # @param time [::Time] Time to set (including time zone). + # @param time [::Time] Time to set. # @return [Time] Time instance. def initialize(zone:, is_24h:, time:) @zone = zone From 9d2cf92ef04188b69bc6313bd7c2a1a23dc3bb46 Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sun, 11 Sep 2022 17:59:47 -0700 Subject: [PATCH 5/5] Use correct date in description for Time spec. --- spec/lib/timex_datalink_client/protocol_1/time_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/timex_datalink_client/protocol_1/time_spec.rb b/spec/lib/timex_datalink_client/protocol_1/time_spec.rb index 246b96e..e967e96 100644 --- a/spec/lib/timex_datalink_client/protocol_1/time_spec.rb +++ b/spec/lib/timex_datalink_client/protocol_1/time_spec.rb @@ -38,7 +38,7 @@ ] end - context "when time is 2015-10-21 19:28:32" do + context "when time is 1997-09-19 19:36:55" do let(:time) { Time.new(1997, 9, 19, 19, 36, 55) } it_behaves_like "CRC-wrapped packets", [