-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from synthead/add-time-for-protocol-1
Add Time model for protocol 1
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
# @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<Array<Integer>>] 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 1997-09-19 19:36:55" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters