Class: RubyFit::FitParser

Inherits:
Object
  • Object
show all
Defined in:
ext/rubyfit/rubyfit.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Object

instance methods



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/rubyfit/rubyfit.c', line 61

static VALUE init(VALUE self, VALUE handler) {
	cFitHandler = handler;
	rb_ivar_set(self, HANDLER_ATTR, handler);

	//callbacks
	cFitHandlerPrintFun = rb_intern("print_msg");
	cFitHandlerPrintErrFun = rb_intern("print_error_msg");
	cFitHandlerActivityFun = rb_intern("on_activity");
	cFitHandlerSessionFun = rb_intern("on_session");
	cFitHandlerLapFun = rb_intern("on_lap");
	cFitHandlerRecordFun = rb_intern("on_record");
	cFitHandlerEventFun = rb_intern("on_event");
	cFitHandlerDeviceInfoFun = rb_intern("on_device_info");
	cFitHandlerUserProfileFun = rb_intern("on_user_profile");
	cFitHandlerWeightScaleInfoFun = rb_intern("on_weight_scale_info");

	return Qnil;
}

Instance Attribute Details

#handlerObject

Instance Method Details

#parse(original_str) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'ext/rubyfit/rubyfit.c', line 411

static VALUE parse(VALUE self, VALUE original_str) {
	int i = 0;
	VALUE str = StringValue(original_str);
	char *p = RSTRING_PTR(str);
	char err_msg[128];

	FIT_UINT8 buf[8];
	FIT_CONVERT_RETURN convert_return = FIT_CONVERT_CONTINUE;
	FIT_UINT32 buf_size;
	FitConvert_Init(FIT_TRUE);

	if(RSTRING_LEN(str) == 0) {
		//sprintf(err_msg, "Passed in string with length of 0!");
		pass_err_message(err_msg);
		return Qnil;
	}

	while(i < RSTRING_LEN(str) && (convert_return == FIT_CONVERT_CONTINUE)) {
		for(buf_size=0;(buf_size < sizeof(buf)) && (p != NULL); buf_size++) {
			buf[buf_size] = *p;
			p++;
			i++;
		}

		do {
			convert_return = FitConvert_Read(buf, buf_size);

			switch(convert_return) {
				case FIT_CONVERT_MESSAGE_AVAILABLE: {
					const FIT_UINT8 *mesg = FitConvert_GetMessageData();
					FIT_UINT16 mesg_num = FitConvert_GetMessageNumber();

					switch(mesg_num) {
						case FIT_MESG_NUM_FILE_ID: {
							break;
						}

						case FIT_MESG_NUM_USER_PROFILE: {
							const FIT_USER_PROFILE_MESG *user_profile = (FIT_USER_PROFILE_MESG *) mesg;
							pass_user_profile(user_profile);
							break;
						}

						case FIT_MESG_NUM_ACTIVITY: {
							const FIT_ACTIVITY_MESG *activity = (FIT_ACTIVITY_MESG *) mesg;
							pass_activity(activity);

							{
								FIT_ACTIVITY_MESG old_mesg;
								old_mesg.num_sessions = 1;
								FitConvert_RestoreFields(&old_mesg);
								sprintf(err_msg, "Restored num_sessions=1 - Activity: timestamp=%u, type=%u, event=%u, event_type=%u, num_sessions=%u\n", activity->timestamp, activity->type, activity->event, activity->event_type, activity->num_sessions);
								pass_message(err_msg);
							}
							break;
						}

						case FIT_MESG_NUM_SESSION: {
							const FIT_SESSION_MESG *session = (FIT_SESSION_MESG *) mesg;
							pass_session(session);
							break;
						}

						case FIT_MESG_NUM_LAP: {
							const FIT_LAP_MESG *lap = (FIT_LAP_MESG *) mesg;
							pass_lap(lap);
							break;
						}

						case FIT_MESG_NUM_RECORD: {
							const FIT_RECORD_MESG *record = (FIT_RECORD_MESG *) mesg;
							pass_record(record);
							break;
						}

						case FIT_MESG_NUM_EVENT: {
							const FIT_EVENT_MESG *event = (FIT_EVENT_MESG *) mesg;
							pass_event(event);
							break;
						}

						case FIT_MESG_NUM_DEVICE_INFO: {
							const FIT_DEVICE_INFO_MESG *device_info = (FIT_DEVICE_INFO_MESG *) mesg;
							pass_device_info(device_info);
							break;
						}

						case FIT_MESG_NUM_WEIGHT_SCALE: {
							const FIT_WEIGHT_SCALE_MESG *weight_scale_info = (FIT_WEIGHT_SCALE_MESG *) mesg;
							pass_weight_scale_info(weight_scale_info);
							break;
						}

						default: {
							sprintf(err_msg, "Unknown message\n");
							pass_message(err_msg);
							break;
						}
					}
					break;
				}
				default:
					break;
			}
		} while (convert_return == FIT_CONVERT_MESSAGE_AVAILABLE);
	}

	if (convert_return == FIT_CONVERT_ERROR) {
		sprintf(err_msg, "Error decoding file.\n");
		pass_err_message(err_msg);
		return Qnil;
	}

	if (convert_return == FIT_CONVERT_CONTINUE) {
		sprintf(err_msg, "Unexpected end of file.\n");
		pass_err_message(err_msg);
		return Qnil;
	}

	if (convert_return == FIT_CONVERT_PROTOCOL_VERSION_NOT_SUPPORTED) {
		sprintf(err_msg, "Protocol version not supported.\n");
		pass_err_message(err_msg);
		return Qnil;
	}

	if (convert_return == FIT_CONVERT_END_OF_FILE) {
		sprintf(err_msg, "File converted successfully.\n");
		pass_message(err_msg);
	}

	return Qnil;
}