Class: TimexDatalinkCaldav::Client
- Inherits:
-
Object
- Object
- TimexDatalinkCaldav::Client
- Defined in:
- lib/timex_datalink_caldav/client.rb
Instance Method Summary collapse
- #add_anniversary(event, anniversary_map, anniversaries, summary_words, occurrences) ⇒ Object
- #add_anniversary_event(anniversary_map, anniversaries, phrase_builder, summary_words, occurrences) ⇒ Object
- #add_appointment(event, appointment_map, appointments, summary_words, occurrences) ⇒ Object
- #add_appointment_event(appointment_map, appointments, phrase_builder, summary_words, occurrences) ⇒ Object
- #all_day_event?(event) ⇒ Boolean
- #get_events ⇒ Object
- #get_localzone ⇒ Object
-
#initialize(user, password, server_url, serial_device, protocol_version, days_forward = 1) ⇒ Client
constructor
A new instance of Client.
- #parse_events ⇒ Object
- #parse_summary(event) ⇒ Object
- #write_to_watch(appointments, anniversaries) ⇒ Object
Constructor Details
#initialize(user, password, server_url, serial_device, protocol_version, days_forward = 1) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/timex_datalink_caldav/client.rb', line 12 def initialize(user, password, server_url, serial_device, protocol_version, days_forward = 1) @user = user @password = password @server_url = server_url @serial_device = serial_device @days_forward = days_forward @protocol_version = protocol_version.to_i @protocol_class = case @protocol_version when 1 then TimexDatalinkClient::Protocol1 when 3 then TimexDatalinkClient::Protocol3 when 4 then TimexDatalinkClient::Protocol4 when 6 then TimexDatalinkClient::Protocol6 when 7 then TimexDatalinkClient::Protocol7 when 9 then TimexDatalinkClient::Protocol9 else raise ArgumentError, "Invalid protocol version: #{@protocol_version}" end end |
Instance Method Details
#add_anniversary(event, anniversary_map, anniversaries, summary_words, occurrences) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/timex_datalink_caldav/client.rb', line 161 def add_anniversary(event, anniversary_map, anniversaries, summary_words, occurrences) occurrences.each do |occurrence| est_time = occurrence.start_time.in_time_zone(get_localzone) key = "#{est_time}_#{summary_words}" unless anniversary_map[key] puts "Adding anniversary: #{summary_words.join(' ')} at date #{est_time}" anniversary = @protocol_class::Eeprom::Anniversary.new( time: est_time, anniversary: summary_words.join(' ') ) anniversaries << anniversary anniversary_map[key] = true end end end |
#add_anniversary_event(anniversary_map, anniversaries, phrase_builder, summary_words, occurrences) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/timex_datalink_caldav/client.rb', line 128 def add_anniversary_event(anniversary_map, anniversaries, phrase_builder, summary_words, occurrences) occurrences.each do |occurrence| est_time = occurrence.start_time.in_time_zone(get_localzone) key = "#{est_time}_#{summary_words.join(' ')}" unless anniversary_map[key] puts "Adding anniversary event: #{summary_words.join(' ')} at date #{est_time}" event_phrase = phrase_builder.vocab_ids_for(*summary_words) anniversary = @protocol_class::Eeprom::Calendar::Event.new( time: Time.new(est_time.year, est_time.month, est_time.day, 9, 30, 0), phrase: event_phrase ) anniversaries << anniversary anniversary_map[key] = true end end end |
#add_appointment(event, appointment_map, appointments, summary_words, occurrences) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/timex_datalink_caldav/client.rb', line 145 def add_appointment(event, appointment_map, appointments, summary_words, occurrences) occurrences.each do |occurrence| est_time = occurrence.start_time.in_time_zone(get_localzone) key = "#{est_time}_#{summary_words}" unless appointment_map[key] puts "Adding appointment: #{summary_words.join(' ')} at time #{est_time}" appointment = @protocol_class::Eeprom::Appointment.new( time: est_time, message: summary_words.join(' ') ) appointments << appointment appointment_map[key] = true end end end |
#add_appointment_event(appointment_map, appointments, phrase_builder, summary_words, occurrences) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/timex_datalink_caldav/client.rb', line 111 def add_appointment_event(appointment_map, appointments, phrase_builder, summary_words, occurrences) occurrences.each do |occurrence| est_time = occurrence.start_time.in_time_zone(get_localzone) key = "#{est_time}_#{summary_words}" unless appointment_map[key] puts "Adding appointment event: #{summary_words.join(' ')} at time #{est_time}" event_phrase = phrase_builder.vocab_ids_for(*summary_words) appointment = @protocol_class::Eeprom::Calendar::Event.new( time: est_time, phrase: event_phrase ) appointments << appointment appointment_map[key] = true end end end |
#all_day_event?(event) ⇒ Boolean
97 98 99 |
# File 'lib/timex_datalink_caldav/client.rb', line 97 def all_day_event?(event) (event.dtend.to_date - event.dtstart.to_date == 1) && event.dtstart.to_datetime.hour == 0 && event.dtstart.to_datetime.min == 0 && event.dtend.to_datetime.hour == 0 && event.dtend.to_datetime.min == 0 end |
#get_events ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/timex_datalink_caldav/client.rb', line 35 def get_events credentials = Calendav::Credentials::Standard.new( host: @server_url, username: @user, password: @password, authentication: :basic_auth ) if @user && @password # Create a new client with the credentials client = Calendav.client(credentials) # Get events from the calendar for the next day caldav_events = client.events.list(@server_url, from: Time.now, to: Time.now + @days_forward*24*60*60) events = caldav_events.map { |event| Icalendar::Event.parse(event.calendar_data).first } else cal_file = URI.open(@server_url) cals = Icalendar::Calendar.parse(cal_file) cal = cals.first events = cal.events end events end |
#get_localzone ⇒ Object
31 32 33 |
# File 'lib/timex_datalink_caldav/client.rb', line 31 def get_localzone TZInfo::Timezone.get(TZInfo::Timezone.all_country_zones.detect {|z| z.period_for_local(Time.now).utc_total_offset == Time.now.utc_offset}.identifier) end |
#parse_events ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/timex_datalink_caldav/client.rb', line 60 def parse_events events = get_events appointments = [] anniversaries = [] appointment_map = {} # Used to avoid duplicate appointments anniversary_map = {} # Used to avoid duplicate anniversaries phrase_builder = SimlarWord.new(database: "pcvocab.mdb") if @protocol_version == 7 events.each do |event| next unless event && event.dtstart && event.dtend summary_words = parse_summary(event) occurrences = event.occurrences_between(Time.now, Time.now + @days_forward*24*60*60) if @protocol_version == 7 if all_day_event?(event) add_anniversary_event(anniversary_map, anniversaries, phrase_builder, summary_words, occurrences) else add_appointment_event(appointment_map, appointments, phrase_builder, summary_words, occurrences) end elsif @protocol_version == 9 or @protocol_version == 6 print "Protocol version #{@protocol_version} doesn't support events, skipping event: #{summary_words.join(' ')}\n" else if all_day_event?(event) add_anniversary(event, anniversary_map, anniversaries, summary_words, occurrences) else add_appointment(event, appointment_map, appointments, summary_words, occurrences) end end end [appointments, anniversaries] end |
#parse_summary(event) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/timex_datalink_caldav/client.rb', line 101 def parse_summary(event) event.summary.to_s.split.map do |word| if word =~ /\A[a-zA-Z]+\z/ word elsif word =~ /\A\d+\z/ word.to_i.humanize end end.compact end |
#write_to_watch(appointments, anniversaries) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/timex_datalink_caldav/client.rb', line 177 def write_to_watch(appointments, anniversaries) appointments.sort_by! { |appointment| appointment.time } # add 3 because it always seems to be about 3 seconds behind. time1 = Time.now + 3 time2 = time1.dup.utc if @protocol_version == 1 or @protocol_version == 9 time_model = @protocol_class::Time.new( zone: 1, time: time1, is_24h: false ) time_name_model = @protocol_class::TimeName.new( zone: 1, name: time1.zone ) utc_time_model = @protocol_class::Time.new( zone: 2, time: time2, is_24h: true ) utc_time_name_model = @protocol_class::TimeName.new( zone: 2, name: time2.zone ) elsif @protocol_version == 3 || @protocol_version == 4 time_model = @protocol_class::Time.new( zone: 1, name: time1.zone, time: time1, is_24h: false, date_format: "%_m-%d-%y" ) utc_time_model = @protocol_class::Time.new( zone: 2, name: "UTC", time: time2, is_24h: true, date_format: "%y-%m-%d" ) time_name_model = nil # Not needed for protocol version 3 and 4 utc_time_name_model = nil # Not needed for protocol version 3 and 4 elsif @protocol_version == 6 time_model = @protocol_class::Time.new( zone: 1, time: time1, is_24h: false, date_format: "%_m-%d-%y" ) utc_time_model = @protocol_class::Time.new( zone: 2, time: time2, flex_time: true, flex_time_zone: true, is_24h: true, date_format: "%_m-%d-%y" ) time_name_model = nil # Not needed for protocol version 6 utc_time_name_model = nil # Not needed for protocol version 6 else time_model = nil utc_time_model = nil time_name_model = nil utc_time_name_model = nil end if @protocol_version == 6 or @protocol_version == 9 models = [ @protocol_class::Sync.new, @protocol_class::Start.new, time_model, time_name_model, utc_time_model, utc_time_name_model, @protocol_class::End.new ].compact elsif @protocol_version == 7 calendar = @protocol_class::Eeprom::Calendar.new( time: time1, events: appointments ) models = [ @protocol_class::Sync.new, @protocol_class::Start.new, @protocol_class::Eeprom.new( calendar: calendar ) ] else models = [ @protocol_class::Sync.new, @protocol_class::Start.new, time_model, time_name_model, utc_time_model, utc_time_name_model, @protocol_class::Eeprom.new( appointments: appointments, anniversaries: anniversaries, appointment_notification_minutes: 5 ), @protocol_class::End.new ].compact # Remove any nil entries end timex_datalink_client = TimexDatalinkClient.new( serial_device: @serial_device, models: models, byte_sleep: 0.008, packet_sleep: 0.06, verbose: true ) timex_datalink_client.write end |