1 package FS::part_export::grandstream;
3 use vars qw(@ISA $me %info $GAPSLITE_HOME $JAVA_HOME);
8 use FS::CGI qw(rooturl);
10 @ISA = qw(FS::part_export);
11 $me = '[' . __PACKAGE__ . ']';
12 $GAPSLITE_HOME = '/usr/local/src/GS/GS_CFG_GEN/';
13 $JAVA_HOME = '/usr/lib/jvm/java-6-sun/';
14 $JAVA_HOME = '/usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/';
16 tie my %options, 'Tie::IxHash',
17 'upload' => { label=>'Enable upload to tftpserver',
20 'user' => { label=>'User name for ssh to tftp server' },
21 'tftproot' => { label=>'Directory in which to upload configuration' },
22 'java_home' => { label=>'Path to java to be used',
25 'gapslite_home' => { label=>'Path to grandstream configuration tool',
26 default=>$GAPSLITE_HOME,
28 'template' => { label=>'Configuration template',
30 notes=>'Type or paste the configuration template here',
35 'svc' => [ qw( part_device ) ], # svc_phone
36 'desc' => 'Provision phone numbers to Grandstream Networks phones',
37 'options' => \%options,
41 sub rebless { shift; }
43 sub gs_create_config {
44 my($self, $mac, %opt) = (@_);
49 warn "gs_create_config called with mac of $mac\n";
50 $mac = sprintf('%012s', lc($mac));
51 my $dir = '%%%FREESIDE_CONF%%%/cache.'. $FS::UID::datasrc;
53 my $fh = new File::Temp(
54 TEMPLATE => "grandstream.$mac.XXXXXXXX",
59 my $filename = $fh->filename;
61 #my $template = new Text::Template (
63 # SOURCE => $self->option('template'),
64 # DELIMITERS => $delimiters,
68 #$template->compile or die "Can't compile template: $Text::Template::ERROR\n";
70 #my $config = $template->fill_in( HASH => { mac_addr => $mac } );
72 print $fh $self->option('template') or die "print failed: $!";
75 system( "export GAPSLITE_HOME=$GAPSLITE_HOME; export JAVA_HOME=$JAVA_HOME; ".
76 "cd $dir; $GAPSLITE_HOME/bin/encode.sh $mac $filename $dir/cfg$mac"
78 or die "grandstream encode failed: $!";
82 open my $encoded, "$dir/cfg$mac" or die "open cfg$mac failed: $!";
87 if ($self->option('upload')) {
88 my $scp = new Net::SCP ( {
89 'host' => $self->machine,
90 'user' => $self->option('user'),
91 'cwd' => $self->option('tftproot'),
94 $scp->put( "$dir/cfg$mac" ) or die "upload failed: ". $scp->errstr;
98 $content = <$encoded>;
102 unlink "$dir/cfg$mac";
108 my($self, $mac) = (shift, shift);
110 return unless $mac; # be more alarmed? Or check upstream?
112 $self->gs_create_config($mac, 'upload' => 1);
117 my($self, $mac) = (shift, shift);
119 $mac = sprintf('%012s', lc($mac));
121 ssh_cmd( user => $self->option('user'),
122 host => $self->machine,
124 args => [ '-f', $self->option(tftproot). "/cfg$mac" ],
130 sub ssh_cmd { #subroutine, not method
132 &Net::SSH::ssh_cmd( { @_ } );
136 # my( $self, $svc_phone ) = (shift, shift);
137 # $self->gs_create($svc_phone->mac_addr);
141 sub _export_replace {
142 # my( $self, $new_svc, $old_svc ) = (shift, shift, shift);
143 # $self->gs_delete($old_svc->mac_addr);
144 # $self->gs_create($new_svc->mac_addr);
149 # my( $self, $svc_phone ) = (shift, shift);
150 # $self->gs_delete($svc_phone->mac_addr);
154 sub _export_suspend {
158 sub _export_unsuspend {
162 sub export_device_insert {
163 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
164 $self->gs_create($phone_device->mac_addr);
168 sub export_device_delete {
169 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
170 $self->gs_delete($phone_device->mac_addr);
174 sub export_device_config {
175 my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
178 # if ($phone_device) {
179 $mac = $phone_device->mac_addr;
181 # $mac = $svc_phone->mac_addr;
184 return '' unless $mac; # be more alarmed? Or check upstream?
186 $self->gs_create_config($mac);
190 sub export_device_replace {
191 my( $self, $svc_phone, $new_svc_or_device, $old_svc_or_device ) =
192 (shift, shift, shift, shift);
194 $self->gs_delete($old_svc_or_device->mac_addr);
195 $self->gs_create($new_svc_or_device->mac_addr);
201 my($self, $svc_phone, $arrayref) = (shift, shift, shift);
203 return; # remove if we actually support being an export for svc_phone;
205 my @deviceparts = map { $_->devicepart } $self->export_device;
206 my @devices = grep { my $part = $_->devicepart;
207 scalar( grep { $_ == $part } @deviceparts );
208 } $svc_phone->phone_device;
210 my $export = $self->exportnum;
211 my $fsurl = rooturl();
213 foreach my $device ( @devices ) {
214 next unless $device->mac_addr;
215 my $num = $device->devicenum;
217 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
218 qq! Phone config </A>!;
220 } elsif ($svc_phone->mac_addr) {
221 my $num = $svc_phone->svcnum;
223 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;svcnum=$num">!.
224 qq! Phone config </A>!;
229 sub export_device_links {
230 my($self, $svc_phone, $device, $arrayref) = (shift, shift, shift, shift);
232 return unless $device && $device->mac_addr;
233 my $export = $self->exportnum;
234 my $fsurl = rooturl();
235 my $num = $device->devicenum;
237 qq!<A HREF="$fsurl/misc/phone_device_config.html?exportnum=$export;devicenum=$num">!.
238 qq! Phone config </A>!;