################################################################################ # 95_webViewControl.pm # Modul for FHEM # # Modul for communication between WebViewControl Android App and FHEM # # contributed by Dirk Hoffmann 01/2013 # $Id: # ################################################################################ package main; use Data::Dumper; # for debugging only use strict; use warnings; use URI::Escape; use vars qw {%data %attr %defs %modules $FW_RET}; #supress errors in Eclipse EPIC use constant { webViewControl_Version => '0.5.1_beta', }; ######################### # Forward declaration sub webViewControl_Initialize($); # Initialize sub webViewControl_Define($$); # define WEBVIEWCONTROL sub webViewControl_Undef($$); # delete sub webViewControl_modifyJsInclude($); # include js parts sub webViewControl_Set($@); # set sub webViewControl_Get($@); # get sub webViewControl_Cgi(); # analyze and parse URL sub webViewControl_Attr(@); ######################### # Global variables my $fhemUrl = '/webviewcontrol' ; my %sets = ( 'screenBrightness' => 'screenBrightness', # slider,1,1,100', 'volume' => 'volume', # slider,1,1,100', 'keepScreenOn' => 'keepScreenOn', 'toastMessage' => 'toastMessage', 'reload' => 'reload', 'audioPlay' => 'audioPlay', 'audioStop' => 'audioStop', 'ttsSay' => 'ttsSay', 'voiceRec' => 'voiceRec', 'newUrl' => 'newUrl', 'reload' => 'reload', ); my %gets = ( 'powerLevel' => 1, 'powerPlugged' => 1, 'voiceRecognitionLastError' => 1, 'voiceRecognitionLastResult'=> 1, ); my $FW_encoding="UTF-8"; # like in FHEMWEB: encoding hardcoded ################################################################################ # Implements Initialize function # # @param hash $hash hash of device addressed # ################################################################################ sub webViewControl_Initialize($) { my ($hash) = @_; $hash->{DefFn} = 'webViewControl_Define'; $hash->{UndefFn} = 'webViewControl_Undef'; $hash->{SetFn} = 'webViewControl_Set'; $hash->{GetFn} = 'webViewControl_Get'; $hash->{AttrFn} = "webViewControl_Attr"; $hash->{AttrList} = 'loglevel:0,1,2,3,4,5,6 model userJsFile userCssFile ' . $readingFnAttributes; # CGI $data{FWEXT}{$fhemUrl}{FUNC} = 'webViewControl_Cgi'; } ################################################################################ # Implements DefFn function # # @param hash $hash hash of device addressed # @param string $def definition string # # @return string # ################################################################################ sub webViewControl_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t][ \t]*", $def); my $name = $hash->{NAME}; return "wrong syntax: define WEBVIEWCONTROL APP-ID" if int(@a)!=3; $hash->{appID} = $a[2]; $modules{webViewControl}{defptr}{$name} = $hash; webViewControl_modifyJsInclude($hash); $hash->{VERSION} = webViewControl_Version; return undef; } ############################# sub webViewControl_Undef($$) { my ($hash, $name) = @_; delete($modules{webViewControl}{defptr}{$name}); webViewControl_modifyJsInclude($hash); return undef; } sub webViewControl_Attr (@) { my (undef, $name, $attr, $val) = @_; my $hash = $defs{$name}; my $msg = ''; if ($attr eq 'userJsFile' || $attr eq 'userCssFile') { $attr{$name}{$attr} = $val; webViewControl_modifyJsInclude($hash); } return undef; } sub webViewControl_modifyJsInclude($) { my ($hash) = @_; my $name = $hash->{NAME}; my @appsArray; foreach my $appName (keys %{ $modules{webViewControl}{defptr} } ) { push(@appsArray, '\'' . $modules{webViewControl}{defptr}{$appName}->{appID} . '\': \'' . $appName . '\''); } my $vars = 'var wvcDevices = {' . join(', ', @appsArray) . '}'; my $userJs = AttrVal($name, 'userJsFile', ''); $userJs = $userJs ? '' : ''; my $userCss = AttrVal($name, 'userCssFile', ''); if ($userCss) { $vars.= '; var wvcUserCssFile="' . $userCss . '"'; } $data{FWEXT}{$fhemUrl}{SCRIPT} = 'cordova-2.3.0.js">' . '' . $userJs . '' . '