Skip to content

Commit

Permalink
Merge pull request #23 from jduda/android_updates
Browse files Browse the repository at this point in the history
Merged it with my clone and tested basic functionality. I have no time for further in-depth testing but since the code is running fine I merged the pull request into master.
  • Loading branch information
hollie committed Dec 5, 2012
2 parents 4630cfb + f7ca3f0 commit bc59de1
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 59 deletions.
93 changes: 74 additions & 19 deletions code/common/android_server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,69 @@

my (%androidClients);

###########################################################################
# THIS SECTION CONTAINS API CODE AND DEMONSTRATES ALL THAT CAN BE DONE
# FROM USER CODE MODULES
###########################################################################

# Instantiate the Android_Item. The Android_Item class demonstrates all
# the Android type things which can be done with objects which inherit from
# Generic_Item class.
$android = new Android_Item( );

# Example speak event to androids
$v_test_android_speak = new Voice_Cmd("test android speak");
if (my $state = said $v_test_android_speak) {
&speak ( "hello from jim duda");
}

# Example play event to androids
$v_test_android_play = new Voice_Cmd("test android play");
if (my $state = said $v_test_android_play) {
&play ( "../sounds/hello_from_bruce.wav");
}

# Example push of caller ID event to androids
$v_test_android_callerid = new Voice_Cmd("test android caller id");
if (my $state = said $v_test_android_callerid) {
&android_callerid ( "Jim Duda", "7813545048");
}

# Example push of notification event to androids
$v_test_android_notification = new Voice_Cmd("test android notification");
if (my $state = said $v_test_android_notification) {
&android_notification ( "This is notiication LINE 1", "And LINE 2");
}

# Dump the inventory of connected android devices to log file
$v_test_android_inventory = new Voice_Cmd("dump android inventory to log file");
if (my $state = said $v_test_android_inventory) {
foreach my $client_ip (keys %androidClients) {
my $room = lc $androidClients{$client_ip}{room};
my $device = $androidClients{$client_ip}{device};
my $version = $androidClients{$client_ip}{version};
my $model = $androidClients{$client_ip}{model};
my $serial = $androidClients{$client_ip}{serialNumber};
&print_log ("android_inventory::");
&print_log ("room: $room");
&print_log ("ip: $client_ip");
&print_log ("device: $device");
&print_log ("version: $version");
&print_log ("model: $model");
&print_log ("serial: $serial");
}
}

$v_android_volume = new Voice_Cmd("Set Android Volume to [0,10,20,30,40,50,60,70,80,90,100]");
if (defined said $v_android_volume) {
my $volume = $v_android_volume->state( );
&android_volume("all", $volume);
}

###########################################################################
# CODE BELOW HERE IS CORE CODE FOR ANDROID SUPPORT
###########################################################################

#Tell MH to call our routine each time something is spoken
if ($Startup or $Reload) {
&Speak_parms_add_hook(\&pre_speak_to_android);
Expand Down Expand Up @@ -268,7 +329,7 @@ sub android_callerid {
}
}

# Call this method to provide a 2 line notificaiton message for the Android.
# Call this method to provide a 2 line notification message for the Android.
sub android_notification {
my ($line1, $line2) = @_;
print_log "android_notification: $line1 $line2" if $Debug{android};
Expand All @@ -280,24 +341,18 @@ sub android_notification {
}
}

$v_test_android_speak = new Voice_Cmd("test android speak");
if (my $state = said $v_test_android_speak) {
&speak ( "hello from jim duda");
}

$v_test_android_play = new Voice_Cmd("test android play");
if (my $state = said $v_test_android_play) {
&play ( "../sounds/hello_from_bruce.wav");
}

$v_test_android_callerid = new Voice_Cmd("test android caller id");
if (my $state = said $v_test_android_callerid) {
&android_callerid ( "Jim Duda", "7813545048");
}

$v_test_android_notification = new Voice_Cmd("test android notification");
if (my $state = said $v_test_android_notification) {
&android_notification ( "This is notiication LINE 1", "And LINE 2");
# Call this method to control the volume for the Android
sub android_volume {
my ($room, $volume) = @_;
$room = "all" unless defined $room;
&print_log ("android_volume: $volume $room") if $Debug{android};
my %data;
$data{volume} = $volume;
foreach my $client_ip (keys %androidClients) {
if (($room eq "all") or ($androidClients{$client_ip}{room} eq $room)) {
&android_send_message ( $client_ip, "volume", %data );
}
}
}

sub android_xml {
Expand Down
122 changes: 105 additions & 17 deletions lib/Android_Item.pm
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ sub new {
&::MainLoop_pre_add_hook(\&Android_Item::state_machine, 'persistent', $self);

$self->{toggle_item} = new Generic_Item( );
$self->{toggle_item}->set_states('off','on');
$self->{toggle_item}->set_states('on','off');
$self->{toggle_item}->android_set_name("toggle_item");

$self->{spinner_item} = new Generic_Item( );
$self->{spinner_item}->set_states('one', 'two', 'three');
$self->{spinner_item}->android_set_name("spinner_item");

$self->{text_item} = "Simple Text";

return $self;
}
Expand All @@ -68,9 +72,60 @@ sub print_log {
return @last_printed;
}

sub set {
my ($self, $value) = @_;
&main::print_log ("set: $value");

# Check for JSON command
if ($value =~ /^{/) {
$value =~ s/\'/"/g;
&main::print_log ("Android_Item:: json: $value") if ($::Debug{android});
my $json = JSON->new->allow_nonref;
my $ref = $json->decode( $value );
if (ref $ref eq 'HASH') {
if ($ref->{"toggle_item"}) {
my $value = $ref->{"toggle_item"};
&main::print_log ("Android_Item:: toggle_item: $value") if ($::Debug{android});
$self->{toggle_item}->set($value);
}
if ($ref->{"spinner_item"}) {
my $value = $ref->{"spinner_item"};
&main::print_log ("Android_Item:: spinner_item: $value") if ($::Debug{android});
$self->{spinner_item}->set($value);
}
if ($ref->{"image_button"} eq "true") {
&main::print_log ("Android_Item:: image_button activated!") if ($::Debug{android});
}
} else {
&main::print_log ("Android_Item:: json decode failed!") if ($::Debug{android});
}
}
}

sub get_array_example ( ) {
my ($self) = @_;
my @array;
push @array, 'Array Value 1';
push @array, 'Array Value 2';
push @array, 'Array Value 3';
push @array, 'Array Value N';
return @array;
}

sub get_hash_example ( ) {
my ($self) = @_;
my %hash;
$hash{"Key 1"} = "Data 1";
$hash{"Key 2"} = "Data 2";
$hash{"Key 3"} = "Data 3";
$hash{"Key 4"} = "Data 4";
$hash{"Key N"} = "Data N";
return %hash;
}

sub android_xml {
my ($self, $depth, $fields, $num_tags, $attributes) = @_;
my @f = qw( speech_log print_log toggle_item spinner_item);
my @f = qw( speech_log print_log text_item toggle_item spinner_item image_button array_example hash_example);

# Avoid filter due to no state
$attributes->{noFilterState} = "true";
Expand All @@ -93,6 +148,12 @@ sub android_xml {
} elsif ( $f eq 'print_log' ) {
my @a = $self->$method;
$value = \@a;
} elsif ( $f eq 'array_example' ) {
my @a = $self->$method;
$value = \@a;
} elsif ( $f eq 'hash_example' ) {
my %a = $self->$method;
$value = \%a;
} else {
$value = $self->$method;
$value = encode_entities( $value, "\200-\377&<>" );
Expand All @@ -105,30 +166,55 @@ sub android_xml {
}

# Add alias to change display name on android list
if ($f eq 'text_item') {
$attributes->{alias} = "Text Item";
}

# Display an example toggle
if ($f eq 'toggle_item') {
$attributes->{alias} = "Toggle Item";
}

# Display an example spinner
if ($f eq 'spinner_item') {
$attributes->{alias} = "Spinner Item";
}

# Display an example image button
if ($f eq 'image_button') {
$attributes->{alias} = "Image Button";
$attributes->{type} = "image";
}

# Display an example array
if ($f eq 'array_example') {
$attributes->{alias} = "Array Example";
}

# Display an example hash
if ($f eq 'hash_example') {
$attributes->{alias} = "Hash Example";
}

# Insert the values for each key into xml structure
if ( ref $value eq 'Generic_Item') {
if ($value->can('android_xml')) {
my $attributes = {};
$attributes->{type} = ref $value;
$xml_objects .= $value->android_xml($depth+1, $fields, 0, $attributes);
} else {
$xml_objects .= $prefix . "<object>\n";
}
$xml_objects .= $prefix . "</object>\n";
#my $attributes = {};
$attributes->{object} = $value->{object_name};
$attributes->{type} = ref $value;
$attributes->{memberObject} = "true";
$xml_objects .= $value->android_xml($depth+1, $fields, 0, $attributes);
$xml_objects .= $prefix . "</$value->{object_name}>\n";
}

elsif ( ref $value eq 'ARRAY' ) {
$attributes->{type} = "arrayList";
$xml_objects .= $self->android_xml_tag ( $prefix, $f, $attributes );
$prefix = ' ' x ($depth+1);
foreach (@{$value}) {
$_ = 'undef' unless defined $_;
$value = $_;
$value = encode_entities( $value, "\200-\377&<>" );
$xml_objects .= $prefix . " <value>$value</value>\n";
$_ = "" unless defined $_;
my $val = $_;
$val = encode_entities( $val, "\200-\377&<>" );
$xml_objects .= $self->android_xml_tag ( $prefix, "value", $attributes, $val );
}
$prefix = ' ' x $depth;
$xml_objects .= $prefix . "</$f>\n";
Expand All @@ -140,18 +226,20 @@ sub android_xml {
$prefix = ' ' x ($depth+1);
foreach my $key (keys %{$value}) {
my $val = $value->{$key};
$val = "" unless defined $val;
$val = encode_entities( $val, "\200-\377&<>" );
$key = encode_entities( $key, "\200-\377&<>" );
$key =~ s/ /_/g;
$key =~ s/[\[\]]/_/g;
$val = encode_entities( $val, "\200-\377&<>" );
$xml_objects .= $prefix . "<$key>$val</$key>\n";
$xml_objects .= $self->android_xml_tag ( $prefix, $key, $attributes, $val );
}
$prefix = ' ' x $depth;
$xml_objects .= $prefix . "</$f>\n";
}

else {
$xml_objects .= $self->android_xml_tag ( $prefix, $f, $attributes );
$value = "" unless defined $value;
$xml_objects .= $self->android_xml_tag ( $prefix, $f, $attributes, $value );
}
}
return $xml_objects;
Expand Down
Loading

0 comments on commit bc59de1

Please sign in to comment.