-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiabetesPilot.pm
263 lines (190 loc) · 6.07 KB
/
DiabetesPilot.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
# Palm::DiabetesPilot.pm
#
# Palm::PDB helper for handling Diabetes Pilot databases
#
# Copyright (C) 2003 Christophe Beauregard
#
# $Id: DiabetesPilot.pm,v 1.8 2004/09/08 23:23:00 cpb Exp $
use strict;
package Palm::DiabetesPilot;
use Palm::PDB;
use Palm::Raw();
use Palm::StdAppInfo();
use vars qw( $VERSION @ISA );
$VERSION = do { my @r = (q$Revision: 1.8 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
@ISA = qw( Palm::StdAppInfo Palm::Raw );
=head1 NAME
Palm::DiabetesPilot - Handler for Diabetes Pilot databases
=head1 SYNOPSIS
use Palm::DiabetesPilot;
=head1 DESCRIPTION
Helper for reading Diabetes Pilot (www.diabetespilot.com) databases.
=head2 AppInfo block
The AppInfo block begins with standard category support. See
L<Palm::StdAppInfo> for details. Diabetes Pilot doesn't have any
application-specific extensions here.
=head2 Records
$record = $pdb->{records}{$i}
$record->{year}
$record->{month}
$record->{day}
$record->{hour}
$record->{minute}
The time of the record entry.
$record->{type}
The type of record. This will be one of C<gluc>, C<meal>, C<med>,
C<exer>, or C<note>.
$record->{quantity}
The quantity associated with the record. For a glucose reading, this is the
level (in the appropriate units). For a meal, it's a carb value. For the
medication, it's whatever units are appropriate. For the exercise, it's
associated with the specific exercise selection.
$record->{note}
Any record type can have a note associated with it.
$record->{med}
In a C<med> record, this indicates the type of medication taken. Meds are
just text strings.
$record->{exer}
In an C<exercise> record, this is a comment describing the type of
exercise and the quantity associated with it.
$record->{items}
In a C<meal> record, this is a reference to an array of individual meal
items. Each item is a hash reference containing the following fields:
C<servings>, C<carbs>, C<fat>, C<protein>, C<fiber>, C<calories>, C<name>.
C<name> is the textual description of the item and also generally includes
the serving size and units.
=cut
#'
sub import
{
&Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ "DGA1", "DATA" ], );
}
sub new
{
die( "Palm::DiabetesPilot does not support new databases" );
}
sub new_Record
{
die( "Palm::DiabetesPilot does not support new records" );
}
sub ParseAppInfoBlock($$)
{
my ($self,$data) = @_;
$self->{'appinfo'} = {};
&Palm::StdAppInfo::parse_StdAppInfo($self->{'appinfo'}, $data);
return $self->{'appinfo'};
}
sub PackAppInfoBlock
{
die( "Palm::DiabetesPilot does not support writing appinfo" );
}
sub ParseRecord
{
my $self = shift;
my %record = @_;
# catch empty records
return \%record unless length $record{'data'} >= 20;
my ($sec,$min,$hour,$day,$mon,$year,$type,$quantity,$data)
= unpack( "nnnnnn x2 C x3 n a*", $record{'data'} );
# quantities are always multiplied by ten for storage
$quantity /= 10.0;
# notes are NUL terminated and follow quantities
my $note = (split /\0/, $data)[0];
chomp($note);
$record{'second'} = $sec;
$record{'minute'} = $min;
$record{'hour'} = $hour;
$record{'day'} = $day;
$record{'month'} = $mon;
$record{'year'} = $year;
$record{'quantity'} = $quantity;
$record{'note'} = $note if $note ne "";
# type-specific structures seem to be appended, word aligned, right after
# the note ends. We've already extracted what we need from $data.
my $nl = length($note)+1;
$data = substr( $data, $nl + $nl % 2 );
# type is a bitmask.
if( $type & 0x1 ) {
$record{'type'} = 'meal';
# we think it's the size of the data section in bytes, although it
# doesn't always jive.
my ($dlen,$items) = unpack( "n n", $data );
my @servings = unpack( "n$items", substr($data,4) );
@servings = map { $_/10.0 } @servings;
# skip the 4+2*items header
$data = substr( $data, 4+($items*2) );
my @items = ();
for( my ($i,$pos) = (0,0); $i < $items; $i ++ ) {
# records are 34 bytes, followed by a text description. There's
# a lot in the records we don't know about, although some will
# probably be food classification (as per the database), some
# might be extended nutritional info, etc. None exactly relevant
# at the moment.
# there's some really odd record alignment, too. All records are
# word aligned, but there's always going to be at least one
# non-data byte between consecutive records (the NUL string
# terminator counts as data).
my $item = substr( $data, $pos, 34 );
last if length $item < 34;
my ($calories,$fat,$carbs,$fiber,$protein)
= unpack( "x6 n x2 n x6 n n x2 n x8", $item );
$fat /= 10.0;
$carbs /= 10.0;
$fiber /= 10.0;
$protein /= 10.0;
$calories /= 10.0;
my $name = substr( $data, 34 + $pos );
$name = (split /\0/, $name)[0];
push @items,
{ 'servings' => $servings[$i],
'carbs' => $carbs,
'fat' => $fat,
'protein' => $protein,
'fiber' => $fiber,
'calories' => $calories,
'name' => $name,
};
my $nl = length($name)+1;
# word aligned, but if the string ends on a word boundary the
# following word is skipped.
$nl += ($nl%2) ? 1 : 2;
$pos += 34 + $nl;
}
$record{'items'} = \@items;
} elsif( $type & 0x2 ) {
$record{'type'} = 'gluc';
} elsif( $type & 0x4 ) {
# dword length indicates the med string
$record{'med'} = substr( $data, 2, unpack( "n", $data )-1 );
chomp( $record{'med'} );
$record{'type'} = 'med';
} elsif( $type & 0x8 ) {
$record{'exercise'} = substr( $data, 2, unpack( "n", $data )-1 );
chomp( $record{'exercise'} );
$record{'type'} = 'exer';
} elsif( $type & 0x10 ) {
delete $record{'quantity'}; # notes don't have valid quantities
$record{'type'} = 'note';
} else {
return undef;
}
delete $record{'offset'};
delete $record{'data'};
return \%record;
}
sub PackRecord
{
die( "Palm::DiabetesPilot does not support writing records" );
}
1;
__END__
=head1 BUGS
Not strictly a bug, but writing databases is unsupported. This is an incomplete
reverse-engineering of medical journalling software. As such, it's unlikely
that we'll ever handle writing.
=head1 AUTHOR
Christophe Beauregard E<lt>cpb@cpan.orgE<gt>
=head1 SEE ALSO
Palm::PDB(3)
Palm::StdAppInfo(3)
=cut