2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00
fhem-mirror/fhem/contrib/RHASSPY/99_RHASSPY_Utils_Demo.pm
Beta-User aa8efed2b2 speechcontrol.template: fix typo
git-svn-id: https://svn.fhem.de/fhem/trunk@29295 2b470e98-0d58-463d-a4d8-8e2adae1ed80
2024-10-26 04:30:03 +00:00

137 lines
4.8 KiB
Perl

##############################################
# $Id$
#
package RHASSPY::Demo; ## no critic 'Package declaration'
use strict;
use warnings;
use JSON;
use Encode;
use List::Util qw(max min);
use GPUtils qw(GP_Import);
## Import der FHEM Funktionen
#-- Run before package compilation
BEGIN {
# Import from main context
GP_Import(
qw(
AttrVal
InternalVal
ReadingsVal
ReadingsNum
ReadingsAge
fhem
Log3
decode
defs
round
)
);
}
sub ::RHASSPY_Utils_Demo_Initialize { goto &Initialize }
# initialize ##################################################################
sub Initialize {
my $hash = shift;
return;
}
# Enter you functions below _this_ line.
sub BasicTest {
my $site = shift // return 'No site argument provided!';
my $type = shift // return 'No type argument provided!';
Log3('rhasspy',3 , "RHASSPY: Site $site, Type $type");
return "RHASSPY: Site $site, Type $type";
}
sub DataTest {
my $name = shift;
my $rawd = shift;
my $hash = $defs{$name} // return;
my $data;
if ( !eval { $data = JSON->new->decode($rawd) ; 1 } ) {
Log3($hash->{NAME}, 1, "JSON decoding error, $rawd seems not to be valid JSON data: $@");
return "Error! $rawd seems not to be valid JSON data!";
}
my $site = $data->{siteId};
my $type = $data->{Type};
Log3('rhasspy',3 , "RHASSPY: Site $site, Type $type");
my @devices = sort keys %{$hash->{helper}{devicemap}->{devices}};
while (@devices > 2) {
shift @devices; # limit triggered devices two to minimize unwanted side effects
}
my @rets;
$rets[0] = "RHASSPY: Site $site, Type $type";
$rets[1] = join q{,}, @devices;
return \@rets;
}
sub DialogueTest{
my $name = shift // return 'No RHASSPY name argument provided!';
my $rawd = shift // return 'No JSON data argument provided!';
my $hash = $defs{$name} // return "RHASSPY name doesn't exist!";
my $data;
if ( !eval { $data = JSON->new->decode($rawd) ; 1 } ) {
Log3($hash->{NAME}, 1, "JSON decoding error, $rawd seems not to be valid JSON data: $@");
return "Error! $rawd seems not to be valid JSON data!";
}
my $site = $data->{siteId};
my $type = $data->{Type};
Log3('rhasspy',3 , "RHASSPY: Site $site, Type $type");
my @devices = sort keys %{$hash->{helper}{devicemap}->{devices}};
while (@devices > 2) {
shift @devices; # limit triggered devices two to minimize unwanted side effects
}
my @rets;
#interactive dialogue as described in https://rhasspy.readthedocs.io/en/latest/reference/#dialoguemanager_continuesession and https://docs.snips.ai/articles/platform/dialog/multi-turn-dialog
#This example here just lets you confirm the action, as intent filter is limited to ConfirmAction
my $response = "RHASSPY asking for OK or cancellation: Site $site, Type $type";
my $ca_string = qq{$hash->{LANGUAGE}.$hash->{fhemId}:ConfirmAction};
my $reaction = { text => $response,
intentFilter => ["$ca_string"],
customData => $data->{customData} #hand over existing reference
};
$rets[0] = $reaction;
$rets[1] = 30; #timeout to replace default timeout
return \@rets;
}
1;
__END__
=pod
=begin html
<a name="RHASSPY_Demo_Utils"></a>
<h3>RHASSPY_Demo_Utils</h3>
<ul>
<b>Routines to demonstrate how to handle function calls from within Custom intents in RHASSPY context</b><br>
<li>BasicTest</li>
This is to demonstrate how to get single elements from the message hash just by their name.</p>
Example: <code>attr &lt;rhasspyDevice&gt; rhasspyIntents SetCustomIntentsBasicTest=RHASSPY::Demo::BasicTest(siteId,Type)</code></p>
<li>DataTest</li>
This is to demonstrate how to get NAME and entire message hash and return a list of devices that may have been triggered. This is just a showcase to extended options and might be a good starting point for developing own complex Custom Intent functions.</p>
Example: <code>attr &lt;rhasspyDevice&gt; rhasspyIntents SetCustomIntentsDataTest=RHASSPY::Demo::DataTest(NAME,DATA)</code></p>
<li>DialogueTest</li>
This is to demonstrate how to keep dialogue open for some time and dynamically fill a slot for possible answer values to the dialoge. This is not fully tested jet and might somewhen in time be a good starting point for developing own complex Custom Intent functions including dialogues.</p>
<b>NOTE</b>: The format of the returned data might be subject to changes, see https://forum.fhem.de/index.php?topic=139337.0 for details!</p>
Example: <code>attr &lt;rhasspyDevice&gt; rhasspyIntents SetCustomIntentsDialogueTest=RHASSPY::Demo::DialogueTest(NAME,DATA)</code></p>
</ul>
=end html
=cut