-
Notifications
You must be signed in to change notification settings - Fork 1
/
99_RHASSPY_Utils_Demo.pm
138 lines (113 loc) · 4.51 KB
/
99_RHASSPY_Utils_Demo.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
##############################################
# $Id: RHASSPY_Utils_Demo.pm 2021-04-21 Beta-User $
#
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;
my $type = shift;
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 = decode_json($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;
my $rawd = shift;
my $hash = $defs{$name} // return;
my $data;
if ( !eval { $data = decode_json($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"] };
$rets[0] = $reaction;
$rets[2] = 30; #timeout to replace default timeout
return \@rets;
}
1;
__END__
=pod
=encoding utf8
=item summary demonstrate how to hand over parameters from RHASSPY
=item summary_DE Beispiele wie man Parameter aus RHASSPY heraus übergibt
=begin html
<a id="RHASSPY_Utils_Demo"></a>
<h3>RHASSPY_Utils_Demo</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 <rhasspyDevice> 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 <rhasspyDevice> 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>
Example: <code>attr <rhasspyDevice> rhasspyIntents SetCustomIntentsDialogueTest=RHASSPY::Demo::DialogueTest(NAME,DATA)</code></p>
</ul>
=end html
=cut