48 |
) |
) |
49 |
); |
); |
50 |
|
|
51 |
|
foreach my $init ( qw/ state session / ) { |
52 |
|
$self->db->put( $init => {} ) unless $self->db->get( $init ); |
53 |
|
} |
54 |
|
|
55 |
return $self; |
return $self; |
56 |
} |
} |
57 |
|
|
69 |
confess "need ID" unless $ID; |
confess "need ID" unless $ID; |
70 |
confess "need state" unless $state; |
confess "need state" unless $state; |
71 |
|
|
72 |
|
if ( my $uid = $self->ID_to_uid( $ID, $state ) ) { |
73 |
|
if ( my $o = $self->db->get('state')->get( $uid ) ) { |
74 |
|
warn "## update state of $uid [$ID]\n" if $self->debug; |
75 |
|
$o->import( $state ); |
76 |
|
} else { |
77 |
|
warn "## create new state for $uid [$ID]\n" if $self->debug; |
78 |
|
$self->db->get('state')->put( $uid => $state ); |
79 |
|
} |
80 |
|
} else { |
81 |
|
warn "## no uid for $ID, first seen?\n" if $self->debug; |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
=head2 state |
86 |
|
|
87 |
|
my $state = $store->state( $ID ); |
88 |
|
|
89 |
|
Returns normal unblessed hash (actually, in-memory copy of state in database). |
90 |
|
|
91 |
|
=cut |
92 |
|
|
93 |
if ( my $o = $self->db->get( $ID ) ) { |
sub state { |
94 |
warn "## update state of $ID\n" if $self->debug; |
my $self = shift; |
95 |
$o->import( $state ); |
my ( $ID ) = @_; |
96 |
|
confess "need ID" unless $ID; |
97 |
|
if ( my $uid = $self->ID_to_uid( $ID ) ) { |
98 |
|
if ( my $state = $self->db->get('state')->get( $uid ) ) { |
99 |
|
return $state->export; |
100 |
|
} else { |
101 |
|
return; |
102 |
|
} |
103 |
} else { |
} else { |
104 |
warn "## create new state for $ID\n" if $self->debug; |
warn "## no uid for $ID so no state!\n" if $self->debug; |
105 |
$self->db->put( $ID => $state ); |
return; |
106 |
} |
} |
|
|
|
107 |
} |
} |
108 |
|
|
109 |
|
=head2 known_CPE |
110 |
|
|
111 |
|
my @cpe = $store->known_CPE; |
112 |
|
|
113 |
|
=cut |
114 |
|
|
115 |
|
sub known_CPE { |
116 |
|
my $self = shift; |
117 |
|
my @cpes = keys %{ $self->db->{state} }; |
118 |
|
warn "all CPE: ", dump( @cpes ), "\n" if $self->debug; |
119 |
|
return @cpes; |
120 |
|
} |
121 |
|
|
122 |
|
=head2 ID_to_uid |
123 |
|
|
124 |
|
my $CPE_uid = $store->ID_to_uid( $ID, $state ); |
125 |
|
|
126 |
|
It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID |
127 |
|
for each CPE. |
128 |
|
|
129 |
|
=cut |
130 |
|
|
131 |
|
sub ID_to_uid { |
132 |
|
my $self = shift; |
133 |
|
my ( $ID, $state ) = @_; |
134 |
|
|
135 |
|
confess "need ID" unless $ID; |
136 |
|
|
137 |
|
warn "ID_to_uid",dump( $ID, $state ) if $self->debug; |
138 |
|
|
139 |
|
$self->db->{session}->{ $ID }->{last_seen} = time(); |
140 |
|
|
141 |
|
my $uid; |
142 |
|
|
143 |
|
if ( $uid = $self->db->{session}->{ $ID }->{ID_to_uid} ) { |
144 |
|
return $uid; |
145 |
|
} elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) { |
146 |
|
warn "## created new session for $uid session $ID\n" if $self->debug; |
147 |
|
$self->db->{session}->{ $ID } = { |
148 |
|
last_seen => time(), |
149 |
|
ID_to_uid => $uid, |
150 |
|
}; |
151 |
|
return $uid; |
152 |
|
} else { |
153 |
|
warn "## can't find uid for ID $ID, first seen?\n"; |
154 |
|
return; |
155 |
|
} |
156 |
|
|
157 |
|
# TODO: expire sessions longer than 30m |
158 |
|
|
159 |
|
return; |
160 |
|
} |
161 |
|
|
162 |
1; |
1; |