75_MSG.pm 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. # $Id: 75_MSG.pm 13281 2017-01-30 01:37:46Z loredo $
  2. ##############################################################################
  3. #
  4. # 75_MSG.pm
  5. # Dynamic message and notification routing for FHEM
  6. #
  7. # Copyright by Julian Pawlowski
  8. # e-mail: julian.pawlowski at gmail.com
  9. #
  10. # This file is part of fhem.
  11. #
  12. # Fhem is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation, either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # Fhem is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  24. #
  25. ##############################################################################
  26. package main;
  27. use strict;
  28. use warnings;
  29. use Time::HiRes qw(time);
  30. use Data::Dumper;
  31. no if $] >= 5.017011, warnings => 'experimental';
  32. sub CommandMsg($$;$$);
  33. ########################################
  34. sub MSG_Initialize($$) {
  35. my %hash = (
  36. Fn => "CommandMsg",
  37. Hlp =>
  38. "[<type>] [<\@device>|<e-mail address>] [<priority>] [|<title>|] <message-text>",
  39. );
  40. $cmds{msg} = \%hash;
  41. require "$attr{global}{modpath}/FHEM/msgSchema.pm";
  42. }
  43. ########################################
  44. sub CommandMsg($$;$$) {
  45. my ( $cl, $msg, $testMode ) = @_;
  46. my $return = "";
  47. if ( $featurelevel >= 5.7 ) {
  48. my %dummy;
  49. my ( $err, @a ) = ReplaceSetMagic( \%dummy, 0, ($msg) );
  50. $msg = join( " ", @a )
  51. unless ($err);
  52. }
  53. # find existing msgConfig device or create a new instance
  54. my $globalDevName = "globalMsg";
  55. if ( defined( $modules{msgConfig}{defptr} ) ) {
  56. $globalDevName = $modules{msgConfig}{defptr}{NAME};
  57. }
  58. else {
  59. fhem "define $globalDevName msgConfig";
  60. $return .=
  61. "Global configuration device $globalDevName was created.\n\n";
  62. }
  63. if ( $msg eq "" || $msg =~ /^\?[\s\t]*$/ || $msg eq "help" ) {
  64. return $return
  65. . "Usage: msg [<type>] [<\@device>|<e-mail address>] [<priority>] [|<title>|] <message>";
  66. }
  67. # default settings
  68. my $cmdSchema = msgSchema::get();
  69. my $settings = {
  70. 'audio' => {
  71. 'typeEscalation' => {
  72. 'gwUnavailable' => 'text',
  73. 'emergency' => 'text',
  74. 'residentGone' => 'text',
  75. 'residentAbsent' => 'text',
  76. },
  77. },
  78. 'light' => {
  79. 'typeEscalation' => {
  80. 'gwUnavailable' => 'audio',
  81. 'emergency' => 'audio',
  82. 'residentGone' => 'audio',
  83. 'residentAbsent' => 'audio',
  84. },
  85. },
  86. 'push' => {
  87. 'typeEscalation' => {
  88. 'gwUnavailable' => 'mail',
  89. 'emergency' => 'mail',
  90. },
  91. },
  92. 'screen' => {
  93. 'typeEscalation' => {
  94. 'gwUnavailable' => 'light',
  95. 'emergency' => 'light',
  96. 'residentGone' => 'light',
  97. 'residentAbsent' => 'light',
  98. },
  99. },
  100. };
  101. ################################################################
  102. ### extract message details
  103. ###
  104. my $types = "";
  105. my $recipients = "";
  106. my $priority = "";
  107. my $title = "-";
  108. my $advanced = "";
  109. my $priorityCat = "";
  110. # check for message types
  111. if ( $msg =~
  112. s/^[\s\t]*([a-z,]*!?(screen|light|audio|text|push|mail)[a-z,!|]*)[\s\t]+//
  113. )
  114. {
  115. Log3 $globalDevName, 5, "msg: found types=$1";
  116. $types = $1;
  117. }
  118. # programatic exception:
  119. # e.g. recipients were given automatically from empty readings
  120. if ( $msg =~ s/^[\s\t]*([!]?(([A-Za-z0-9%+._-])*@([,\-:|]+)))[\s\t]+// ) {
  121. Log3 $globalDevName, 4,
  122. "msg: message won't be sent - recipient '$1' contains special"
  123. . " characters like ',-:|' or behind the @ character is simply"
  124. . " emptiness. This might be okay, e.g. if you are using something"
  125. . " like a reading from RESIDENTS/ROOMMATE/GUEST to address present"
  126. . " or absent residents and this list is simply empty at this time."
  127. . " ($msg)";
  128. return;
  129. }
  130. # check for given recipients
  131. if ( $msg =~
  132. s/^[\s\t]*([!]?(([A-Za-z0-9%+._-])*@([%+a-z0-9A-Z.-]+))[\w,@.!|:]*)[\s\t]+//
  133. )
  134. {
  135. Log3 $globalDevName, 5, "msg: found recipient=$1";
  136. $recipients = $1;
  137. }
  138. # check for given priority
  139. if ( $msg =~ s/^[\s\t]*([-+]{0,1}\d+[.\d]*)[\s\t]*// ) {
  140. Log3 $globalDevName, 5, "msg: found priority=$1";
  141. $priority = $1;
  142. }
  143. # check for given message title
  144. if ( $msg =~ s/^[\s\t]*\|(.*?)\|[\s\t]*// ) {
  145. Log3 $globalDevName, 5, "msg: found title=$1";
  146. $title = $1;
  147. }
  148. # check for advanced options
  149. if ( $msg =~ s/[\s\t]*O(\[\{.*\}\])[\s\t]*$// ) {
  150. Log3 $globalDevName, 5, "msg: found options=$1";
  151. # Use JSON module if possible
  152. eval {
  153. require JSON;
  154. import JSON qw( decode_json );
  155. };
  156. if ( !$@ ) {
  157. eval '$advanced = decode_json( Encode::encode_utf8($1) ); 1';
  158. if ( !$@ ) {
  159. Log3 $globalDevName, 5,
  160. "msg: Decoded advanced options\n" . Dumper($advanced);
  161. }
  162. else {
  163. Log3 $globalDevName, 5,
  164. "msg: Error decoding JSON for advanced options";
  165. $advanced = "";
  166. }
  167. }
  168. else {
  169. Log3 $globalDevName, 3,
  170. "msg: To use advanced options, please install Perl JSON.";
  171. }
  172. }
  173. ################################################################
  174. ### command queue
  175. ###
  176. $types = AttrVal( "msgType", $globalDevName, "text" )
  177. if ( $types eq "" );
  178. my $msgSent = 0;
  179. my $forwarded = "";
  180. my %sentTypesPerDevice;
  181. my $sentCounter = 0;
  182. my $msgID = time();
  183. my $isTypeOr = 1;
  184. my $isRecipientOr = 1;
  185. my $hasTypeOr = 0;
  186. my $hasRecipientOr = 0;
  187. $recipients = "\@" . $globalDevName if ( $recipients eq "" );
  188. my @typesOr = split( /\|/, $types );
  189. $hasTypeOr = 1 if ( scalar( grep { defined $_ } @typesOr ) > 1 );
  190. Log3 $globalDevName, 5,
  191. "msg: typeOr total is " . scalar( grep { defined $_ } @typesOr )
  192. if ( $testMode ne "1" );
  193. for (
  194. my $iTypesOr = 0 ;
  195. $iTypesOr < scalar( grep { defined $_ } @typesOr ) ;
  196. $iTypesOr++
  197. )
  198. {
  199. Log3 $globalDevName, 5,
  200. "msg: start typeOr loop for type(s) $typesOr[$iTypesOr]"
  201. if ( $testMode ne "1" );
  202. my @type = split( /,/, $typesOr[$iTypesOr] );
  203. for ( my $i = 0 ; $i < scalar( grep { defined $_ } @type ) ; $i++ ) {
  204. Log3 $globalDevName, 5, "msg: running loop for type $type[$i]"
  205. if ( $testMode ne "1" );
  206. last if ( !defined( $type[$i] ) );
  207. my $forceType = 0;
  208. if ( $type[$i] =~ s/(.*)![\s\t]*$// ) {
  209. $type[$i] = $1;
  210. $forceType = 1;
  211. }
  212. # check for correct type
  213. my @msgCmds =
  214. ( "screen", "light", "audio", "text", "push", "mail" );
  215. if ( !( $type[$i] ~~ @msgCmds ) ) {
  216. $return .= "Unknown message type $type[$i]\n";
  217. next;
  218. }
  219. ################################################################
  220. ### recipient loop
  221. ###
  222. my @recipientsOr = split( /\|/, $recipients );
  223. $hasRecipientOr = 1
  224. if ( scalar( grep { defined $_ } @recipientsOr ) > 1 );
  225. Log3 $globalDevName, 5,
  226. "msg: recipientOr total is "
  227. . scalar( grep { defined $_ } @recipientsOr )
  228. if ( $testMode ne "1" );
  229. for (
  230. my $iRecipOr = 0 ;
  231. $iRecipOr < scalar( grep { defined $_ } @recipientsOr ) ;
  232. $iRecipOr++
  233. )
  234. {
  235. Log3 $globalDevName, 5,
  236. "msg: start recipientsOr loop for recipient(s) $recipientsOr[$iRecipOr]"
  237. if ( $testMode ne "1" );
  238. my @recipient = split( /,/, $recipientsOr[$iRecipOr] );
  239. foreach my $device (@recipient) {
  240. Log3 $globalDevName, 5,
  241. "msg: running loop for device $device"
  242. if ( $testMode ne "1" );
  243. my $msgSentDev = 0;
  244. my $gatewayDevs = "";
  245. my $forceDevice = 0;
  246. # for device type
  247. my $deviceType = "device";
  248. if ( $device =~
  249. /^(([A-Za-z0-9%+._-])+[@]+([%+a-z0-9A-Z.-]*))$/ )
  250. {
  251. $gatewayDevs = $1;
  252. $deviceType = "email";
  253. }
  254. elsif ( $device =~ s/^@?(.*)![\s\t]*$// ) {
  255. $device = $1;
  256. $forceDevice = 1;
  257. }
  258. elsif ( $device =~ s/^@(.*)// ) {
  259. $device = $1;
  260. }
  261. # sub-recipient
  262. my $subRecipient = "";
  263. my $termRecipient = "";
  264. if ( $device =~
  265. m/^@?([A-Za-z0-9._]+):([A-Za-z0-9._\-\/@+]*):?([A-Za-z0-9._\-\/@+]*)$/
  266. )
  267. {
  268. $device = $1;
  269. $subRecipient = $2;
  270. $termRecipient = $3;
  271. }
  272. # FATAL ERROR: device does not exist
  273. if ( !defined( $defs{$device} )
  274. && $deviceType eq "device" )
  275. {
  276. $return .= "Device $device does not exist\n";
  277. Log3 $globalDevName, 5,
  278. "msg $device: Device does not exist"
  279. if ( $testMode ne "1" );
  280. my $regex1 =
  281. "\\s*!?@?" . $device . "[,|]"; # at the beginning
  282. my $regex2 = "[,|]!?@?" . $device . "\\s*"; # at the end
  283. my $regex3 =
  284. ",!?@?" . $device . ","; # in the middle with comma
  285. my $regex4 =
  286. "[\|,]!?@?"
  287. . $device
  288. . "[\|,]"; # in the middle with pipe and/or comma
  289. $recipients =~ s/^$regex1//gi;
  290. $recipients =~ s/$regex2$/|/gi;
  291. $recipients =~ s/$regex3/,/gi;
  292. $recipients =~ s/$regex4/|/gi;
  293. next;
  294. }
  295. # next type loop if device is an email address and this is not the mail type loop run
  296. if ( $deviceType eq "email"
  297. && $type[$i] ne "mail"
  298. && $type[$i] ne "text" )
  299. {
  300. Log3 $globalDevName, 5,
  301. "msg $device: Skipping loop for device type 'email' with unmatched message type '"
  302. . $type[$i] . "'";
  303. next;
  304. }
  305. my $typeUc = ucfirst( $type[$i] );
  306. my $catchall = 0;
  307. my $useLocation = 0;
  308. my $logDevice;
  309. $logDevice = $globalDevName;
  310. $logDevice = $device
  311. if (
  312. # look for direct
  313. AttrVal(
  314. $device, "verbose",
  315. #look for indirect
  316. AttrVal(
  317. AttrVal( $device, "msgRecipient$typeUc", "" ),
  318. "verbose",
  319. #look for indirect general
  320. AttrVal(
  321. AttrVal( $device, "msgRecipient", "" ),
  322. "verbose",
  323. # no verbose found
  324. ""
  325. )
  326. )
  327. ) ne ""
  328. );
  329. ################################################################
  330. ### get target information from device location
  331. ###
  332. # search for location references
  333. my @locationDevs;
  334. @locationDevs = split(
  335. /,/,
  336. # look for direct
  337. AttrVal(
  338. $device, "msgLocationDevs",
  339. #look for indirect
  340. AttrVal(
  341. AttrVal( $device, "msgRecipient$typeUc", "" ),
  342. "msgLocationDevs",
  343. # look for indirect general
  344. AttrVal(
  345. AttrVal( $device, "msgRecipient", "" ),
  346. "msgLocationDevs",
  347. # look for global direct
  348. AttrVal(
  349. $globalDevName, "msgLocationDevs",
  350. #look for global indirect
  351. AttrVal(
  352. AttrVal(
  353. $globalDevName,
  354. "msgRecipient$typeUc",
  355. ""
  356. ),
  357. "msgLocationDevs",
  358. # look for global indirect general
  359. AttrVal(
  360. AttrVal(
  361. $globalDevName,
  362. "msgRecipient",
  363. ""
  364. ),
  365. "msgLocationDevs",
  366. # no locations defined
  367. ""
  368. )
  369. )
  370. )
  371. )
  372. )
  373. )
  374. );
  375. if ( $deviceType eq "device" ) {
  376. # get device location
  377. my $deviceLocation =
  378. # look for direct
  379. ReadingsVal(
  380. $device, "location",
  381. # look for indirect
  382. ReadingsVal(
  383. AttrVal( $device, "msgRecipient$typeUc", "" ),
  384. "location",
  385. # look for indirect general
  386. ReadingsVal(
  387. AttrVal( $device, "msgRecipient", "" ),
  388. "location",
  389. # no location found
  390. ""
  391. )
  392. )
  393. );
  394. my $locationDev = "";
  395. if ( $deviceLocation ne "" && $deviceType eq "device" )
  396. {
  397. # lookup matching location
  398. foreach (@locationDevs) {
  399. if ( $featurelevel >= 5.7 ) {
  400. my %dummy;
  401. my ( $err, @a ) =
  402. ReplaceSetMagic( \%dummy, 0, ($_) );
  403. $_ = join( " ", @a )
  404. unless ($err);
  405. }
  406. my $lName =
  407. AttrVal( $_, "msgLocationName", "" );
  408. if ( $lName ne "" && $lName eq $deviceLocation )
  409. {
  410. $locationDev = $_;
  411. last;
  412. }
  413. }
  414. if ( $featurelevel >= 5.7 ) {
  415. my %dummy;
  416. my ( $err, @a ) =
  417. ReplaceSetMagic( \%dummy, 0, ($locationDev) );
  418. $locationDev = join( " ", @a )
  419. unless ($err);
  420. }
  421. # look for gateway device
  422. $gatewayDevs =
  423. # look for direct
  424. AttrVal(
  425. $locationDev, "msgContact$typeUc",
  426. # look for indirect
  427. AttrVal(
  428. AttrVal(
  429. $locationDev, "msgRecipient$typeUc",
  430. ""
  431. ),
  432. "msgContact$typeUc",
  433. # look for indirect general
  434. AttrVal(
  435. AttrVal(
  436. $locationDev, "msgRecipient",
  437. ""
  438. ),
  439. "msgContact$typeUc",
  440. # no contact found
  441. ""
  442. )
  443. )
  444. );
  445. # at least one of the location gateways needs to
  446. # be available. Otherwise we fall back to
  447. # non-location contacts
  448. if ( $gatewayDevs ne "" ) {
  449. if ( $featurelevel >= 5.7 ) {
  450. my %dummy;
  451. my ( $err, @a ) =
  452. ReplaceSetMagic( \%dummy, 0,
  453. ($gatewayDevs) );
  454. $gatewayDevs = join( " ", @a )
  455. unless ($err);
  456. }
  457. foreach
  458. my $gatewayDevOr ( split /\|/, $gatewayDevs )
  459. {
  460. foreach my $gatewayDev ( split /,/,
  461. $gatewayDevOr )
  462. {
  463. my $tmpSubRecipient;
  464. if ( $gatewayDev =~ s/:(.*)// ) {
  465. $tmpSubRecipient = $1;
  466. }
  467. if ( $type[$i] ne "mail"
  468. && !defined( $defs{$gatewayDev} )
  469. && $deviceType eq "device" )
  470. {
  471. $useLocation = 2
  472. if ( $useLocation == 0 );
  473. }
  474. elsif ( $type[$i] ne "mail"
  475. && IsDisabled($gatewayDev) )
  476. {
  477. $useLocation = 2
  478. if ( $useLocation == 0 );
  479. }
  480. elsif (
  481. $type[$i] ne "mail"
  482. && (
  483. ReadingsVal(
  484. $gatewayDev, "presence",
  485. "present"
  486. ) =~
  487. m/^(0|false|absent|disappeared|unauthorized|disconnected|unreachable)$/i
  488. || ReadingsVal(
  489. $gatewayDev, "state",
  490. "present"
  491. ) =~
  492. m/^(absent|disappeared|unauthorized|disconnected|unreachable)$/i
  493. || ( $defs{$gatewayDev}{STATE}
  494. && $defs{$gatewayDev}{STATE}
  495. =~ m/^(absent|disappeared|unauthorized|disconnected|unreachable)$/i
  496. )
  497. || ReadingsVal(
  498. $gatewayDev, "available",
  499. "yes"
  500. ) =~ m/^(0|no|false)$/i
  501. || ReadingsVal(
  502. $gatewayDev, "reachable",
  503. "yes"
  504. ) =~ m/^(0|no|false)$/i
  505. )
  506. )
  507. {
  508. $useLocation = 2
  509. if ( $useLocation == 0 );
  510. }
  511. else {
  512. $useLocation = 1;
  513. }
  514. }
  515. }
  516. # use gatewayDevs from location only
  517. # if it has been confirmed to be available
  518. if ( $useLocation == 1 ) {
  519. Log3 $logDevice, 4,
  520. "msg $device: Matching location definition found.";
  521. }
  522. else {
  523. $gatewayDevs = "";
  524. }
  525. }
  526. }
  527. }
  528. ################################################################
  529. ### given device name is already a gateway device itself
  530. ###
  531. my $deviceType2 =
  532. defined( $defs{$device} ) ? $defs{$device}{TYPE} : "";
  533. if (
  534. $gatewayDevs eq ""
  535. && $deviceType eq "device"
  536. && $deviceType2 ne ""
  537. && (
  538. (
  539. $type[$i] eq "audio" && defined(
  540. $cmdSchema->{ $type[$i] }{$deviceType2}
  541. )
  542. )
  543. || (
  544. $type[$i] eq "light"
  545. && defined(
  546. $cmdSchema->{ $type[$i] }{$deviceType2}
  547. )
  548. )
  549. || (
  550. $type[$i] eq "push"
  551. && defined(
  552. $cmdSchema->{ $type[$i] }{$deviceType2}
  553. )
  554. )
  555. || (
  556. $type[$i] eq "screen"
  557. && defined(
  558. $cmdSchema->{ $type[$i] }{$deviceType2}
  559. )
  560. )
  561. )
  562. )
  563. {
  564. Log3 $logDevice, 4,
  565. "msg $device: Recipient type $deviceType2 is a gateway device itself for message type "
  566. . $type[$i]
  567. . ". Still checking for any delegates ..."
  568. if ( $testMode ne "1" );
  569. $gatewayDevs =
  570. # look for direct
  571. AttrVal(
  572. $device,
  573. "msgContact$typeUc",
  574. # look for indirect
  575. AttrVal(
  576. AttrVal( $device, "msgRecipient$typeUc", "" ),
  577. "msgContact$typeUc",
  578. # look for indirect general
  579. AttrVal(
  580. AttrVal( $device, "msgRecipient", "" ),
  581. "msgContact$typeUc",
  582. # self
  583. $device
  584. )
  585. )
  586. );
  587. }
  588. ################################################################
  589. ### get target information from device
  590. ###
  591. elsif ( $deviceType eq "device" && $gatewayDevs eq "" ) {
  592. # look for gateway device
  593. $gatewayDevs =
  594. # look for direct
  595. AttrVal(
  596. $device, "msgContact$typeUc",
  597. #look for indirect
  598. AttrVal(
  599. AttrVal( $device, "msgRecipient$typeUc", "" ),
  600. "msgContact$typeUc",
  601. #look for indirect general
  602. AttrVal(
  603. AttrVal( $device, "msgRecipient", "" ),
  604. "msgContact$typeUc",
  605. # no contact found
  606. ""
  607. )
  608. )
  609. );
  610. # fallback/catchall
  611. if ( $gatewayDevs eq "" ) {
  612. $catchall = 1
  613. if ( $device ne $globalDevName );
  614. Log3 $logDevice, 5,
  615. "msg $device: (No $typeUc contact defined, trying global instead)"
  616. if ( $catchall == 1 );
  617. $gatewayDevs =
  618. # look for direct
  619. AttrVal(
  620. $globalDevName, "msgContact$typeUc",
  621. #look for indirect
  622. AttrVal(
  623. AttrVal(
  624. $globalDevName, "msgRecipient$typeUc",
  625. ""
  626. ),
  627. "msgContact$typeUc",
  628. #look for indirect general
  629. AttrVal(
  630. AttrVal(
  631. $globalDevName, "msgRecipient",
  632. ""
  633. ),
  634. "msgContact$typeUc",
  635. # no contact found
  636. ""
  637. )
  638. )
  639. );
  640. }
  641. }
  642. # Find priority if none was explicitly specified
  643. my $loopPriority = $priority;
  644. $loopPriority =
  645. # look for direct
  646. AttrVal(
  647. $device, "msgPriority$typeUc",
  648. #look for indirect
  649. AttrVal(
  650. AttrVal( $device, "msgRecipient$typeUc", "" ),
  651. "msgPriority$typeUc",
  652. #look for indirect general
  653. AttrVal(
  654. AttrVal( $device, "msgRecipient", "" ),
  655. "msgPriority$typeUc",
  656. # look for global direct
  657. AttrVal(
  658. $globalDevName, "msgPriority$typeUc",
  659. #look for global indirect
  660. AttrVal(
  661. AttrVal(
  662. $globalDevName,
  663. "msgRecipient$typeUc",
  664. ""
  665. ),
  666. "msgPriority$typeUc",
  667. #look for global indirect general
  668. AttrVal(
  669. AttrVal(
  670. $globalDevName,
  671. "msgRecipient",
  672. ""
  673. ),
  674. "msgPriority$typeUc",
  675. # default
  676. 0
  677. )
  678. )
  679. )
  680. )
  681. )
  682. ) if ( !$priority );
  683. # check for available routes
  684. #
  685. my %routes;
  686. $routes{screen} = 0;
  687. $routes{light} = 0;
  688. $routes{audio} = 0;
  689. $routes{text} = 0;
  690. $routes{push} = 0;
  691. $routes{mail} = 0;
  692. if (
  693. !defined($testMode)
  694. || ( $testMode ne "1"
  695. && $testMode ne "2" )
  696. )
  697. {
  698. Log3 $logDevice, 5,
  699. "msg $device: Checking for available routes (triggered by type $type[$i])";
  700. $routes{screen} = 1
  701. if (
  702. $deviceType eq "device"
  703. && CommandMsg( "screen",
  704. "screen \@$device $priority Routing Test", 1 )
  705. eq "ROUTE_AVAILABLE"
  706. );
  707. $routes{light} = 1
  708. if (
  709. $deviceType eq "device"
  710. && CommandMsg( "light",
  711. "light \@$device $priority Routing Test", 1 )
  712. eq "ROUTE_AVAILABLE"
  713. );
  714. $routes{audio} = 1
  715. if (
  716. $deviceType eq "device"
  717. && CommandMsg( "audio",
  718. "audio \@$device $priority Routing Test", 1 )
  719. eq "ROUTE_AVAILABLE"
  720. );
  721. if (
  722. $deviceType eq "device"
  723. && CommandMsg( "push",
  724. "push \@$device $priority Routing Test", 1 ) eq
  725. "ROUTE_AVAILABLE"
  726. )
  727. {
  728. $routes{push} = 1;
  729. $routes{text} = 1;
  730. }
  731. if (
  732. CommandMsg( "mail",
  733. "mail \@$device $priority Routing Test", 1 ) eq
  734. "ROUTE_AVAILABLE"
  735. )
  736. {
  737. $routes{mail} = 1;
  738. $routes{text} = 1;
  739. }
  740. $routes{mail} = 1
  741. if ( $deviceType eq "email" );
  742. Log3 $logDevice, 4,
  743. "msg $device: Available routes: screen="
  744. . $routes{screen}
  745. . " light="
  746. . $routes{light}
  747. . " audio="
  748. . $routes{audio}
  749. . " text="
  750. . $routes{text}
  751. . " push="
  752. . $routes{push}
  753. . " mail="
  754. . $routes{mail};
  755. }
  756. ##################################################
  757. ### dynamic routing for text (->push, ->mail)
  758. ###
  759. if ( $type[$i] eq "text" ) {
  760. # user selected emergency priority text threshold
  761. my $prioThresTextEmg =
  762. # look for direct
  763. AttrVal(
  764. $device, "msgThPrioTextEmergency",
  765. #look for indirect audio
  766. AttrVal(
  767. AttrVal( $device, "msgRecipient$typeUc", "" ),
  768. "msgThPrioTextEmergency",
  769. #look for indirect general
  770. AttrVal(
  771. AttrVal( $device, "msgRecipient", "" ),
  772. "msgThPrioTextEmergency",
  773. # look for global direct
  774. AttrVal(
  775. $globalDevName,
  776. "msgThPrioTextEmergency",
  777. #look for global indirect type
  778. AttrVal(
  779. AttrVal(
  780. $globalDevName,
  781. "msgRecipient$typeUc",
  782. ""
  783. ),
  784. "msgThPrioTextEmergency",
  785. #look for global indirect general
  786. AttrVal(
  787. AttrVal(
  788. $globalDevName,
  789. "msgRecipient",
  790. ""
  791. ),
  792. "msgThPrioTextEmergency",
  793. # default
  794. "2"
  795. )
  796. )
  797. )
  798. )
  799. )
  800. );
  801. # user selected low priority text threshold
  802. my $prioThresTextNormal =
  803. # look for direct
  804. AttrVal(
  805. $device, "msgThPrioTextNormal",
  806. #look for indirect audio
  807. AttrVal(
  808. AttrVal( $device, "msgRecipient$typeUc", "" ),
  809. "msgThPrioTextNormal",
  810. #look for indirect general
  811. AttrVal(
  812. AttrVal( $device, "msgRecipient", "" ),
  813. "msgThPrioTextNormal",
  814. # look for global direct
  815. AttrVal(
  816. $globalDevName, "msgThPrioTextNormal",
  817. #look for global indirect type
  818. AttrVal(
  819. AttrVal(
  820. $globalDevName,
  821. "msgRecipient$typeUc",
  822. ""
  823. ),
  824. "msgThPrioTextNormal",
  825. #look for global indirect general
  826. AttrVal(
  827. AttrVal(
  828. $globalDevName,
  829. "msgRecipient",
  830. ""
  831. ),
  832. "msgThPrioTextNormal",
  833. # default
  834. "-2"
  835. )
  836. )
  837. )
  838. )
  839. )
  840. );
  841. # Decide push and/or e-mail destination based on priorities
  842. if ( $loopPriority >= $prioThresTextEmg
  843. && $routes{push} == 1
  844. && $routes{mail} == 1 )
  845. {
  846. Log3 $logDevice, 4,
  847. "msg $device: Text routing decision: push+mail(1)";
  848. $forwarded .= ","
  849. if ( $forwarded ne "" );
  850. $forwarded .= "text>push+mail";
  851. push @type, "push" if !( "push" ~~ @type );
  852. push @type, "mail" if !( "mail" ~~ @type );
  853. }
  854. elsif ($loopPriority >= $prioThresTextEmg
  855. && $routes{push} == 1
  856. && $routes{mail} == 0 )
  857. {
  858. Log3 $logDevice, 4,
  859. "msg $device: Text routing decision: push(2)";
  860. $forwarded .= ","
  861. if ( $forwarded ne "" );
  862. $forwarded .= "text>push";
  863. push @type, "push" if !( "push" ~~ @type );
  864. }
  865. elsif ($loopPriority >= $prioThresTextEmg
  866. && $routes{push} == 0
  867. && $routes{mail} == 1 )
  868. {
  869. Log3 $logDevice, 4,
  870. "msg $device: Text routing decision: mail(3)";
  871. $forwarded .= ","
  872. if ( $forwarded ne "" );
  873. $forwarded .= "text>mail";
  874. push @type, "mail" if !( "mail" ~~ @type );
  875. }
  876. elsif ($loopPriority >= $prioThresTextNormal
  877. && $routes{push} == 1 )
  878. {
  879. Log3 $logDevice, 4,
  880. "msg $device: Text routing decision: push(4)";
  881. $forwarded .= ","
  882. if ( $forwarded ne "" );
  883. $forwarded .= "text>push";
  884. push @type, "push" if !( "push" ~~ @type );
  885. }
  886. elsif ($loopPriority >= $prioThresTextNormal
  887. && $routes{mail} == 1 )
  888. {
  889. Log3 $logDevice, 4,
  890. "msg $device: Text routing decision: mail(5)";
  891. $forwarded .= ","
  892. if ( $forwarded ne "" );
  893. $forwarded .= "text>mail";
  894. push @type, "mail" if !( "mail" ~~ @type );
  895. }
  896. elsif ( $routes{mail} == 1 ) {
  897. Log3 $logDevice, 4,
  898. "msg $device: Text routing decision: mail(6)";
  899. $forwarded .= ","
  900. if ( $forwarded ne "" );
  901. $forwarded .= "text>mail";
  902. push @type, "mail" if !( "mail" ~~ @type );
  903. }
  904. elsif ( $routes{push} == 1 ) {
  905. Log3 $logDevice, 4,
  906. "msg $device: Text routing decision: push(7)";
  907. $forwarded .= ","
  908. if ( $forwarded ne "" );
  909. $forwarded .= "text>push";
  910. push @type, "push" if !( "push" ~~ @type );
  911. }
  912. # FATAL ERROR: routing decision failed
  913. else {
  914. Log3 $logDevice, 4,
  915. "msg $device: Text routing FAILED - priority=$loopPriority push="
  916. . $routes{push}
  917. . " mail="
  918. . $routes{mail};
  919. $return .=
  920. "ERROR: Could not find any Push or Mail contact for device $device - set attributes: msgContactPush | msgContactMail | msgContactText | msgRecipientPush | msgRecipientMail | msgRecipientText | msgRecipient\n";
  921. }
  922. next;
  923. }
  924. # FATAL ERROR: we could not find any targets for
  925. # user specified device...
  926. if ( $gatewayDevs eq ""
  927. && $device ne $globalDevName )
  928. {
  929. $return .=
  930. "ERROR: Could not find any $typeUc contact for device $device - set attributes: msgContact$typeUc | msgRecipient$typeUc | msgRecipient\n";
  931. }
  932. # FATAL ERROR: we could not find any targets at all
  933. elsif ( $gatewayDevs eq "" ) {
  934. $return .=
  935. "ERROR: Could not find any general $typeUc contact. Please specify a destination device or set attributes in general msg configuration device $globalDevName : msgContact$typeUc | msgRecipient$typeUc | msgRecipient\n";
  936. }
  937. #####################
  938. # return if we are in routing target test mode
  939. #
  940. if ( defined($testMode) && $testMode eq "1" ) {
  941. Log3 $logDevice, 5,
  942. "msg $device: $type[$i] route check result: ROUTE_AVAILABLE"
  943. if ( $return eq "" );
  944. Log3 $logDevice, 5,
  945. "msg $device: $type[$i] route check result: ROUTE_UNAVAILABLE"
  946. if ( $return ne "" );
  947. return "ROUTE_AVAILABLE" if ( $return eq "" );
  948. return "ROUTE_UNAVAILABLE" if ( $return ne "" );
  949. }
  950. # user selected audio-visual announcement state
  951. my $annState = ReadingsVal(
  952. # look for direct
  953. AttrVal(
  954. $device, "msgSwitcherDev",
  955. #look for indirect audio
  956. AttrVal(
  957. AttrVal( $device, "msgRecipient$typeUc", "" ),
  958. "msgSwitcherDev",
  959. #look for indirect general
  960. AttrVal(
  961. AttrVal( $device, "msgRecipient", "" ),
  962. "msgSwitcherDev",
  963. # look for global direct
  964. AttrVal(
  965. $globalDevName, "msgSwitcherDev",
  966. #look for global indirect type
  967. AttrVal(
  968. AttrVal(
  969. $globalDevName,
  970. "msgRecipient$typeUc",
  971. ""
  972. ),
  973. "msgSwitcherDev",
  974. #look for global indirect general
  975. AttrVal(
  976. AttrVal(
  977. $globalDevName,
  978. "msgRecipient",
  979. ""
  980. ),
  981. "msgSwitcherDev",
  982. # default
  983. ""
  984. )
  985. )
  986. )
  987. )
  988. )
  989. ),
  990. "state",
  991. "long"
  992. );
  993. # user selected emergency priority audio threshold
  994. my $prioThresAudioEmg =
  995. # look for direct
  996. AttrVal(
  997. $device, "msgThPrioAudioEmergency",
  998. #look for indirect audio
  999. AttrVal(
  1000. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1001. "msgThPrioAudioEmergency",
  1002. #look for indirect general
  1003. AttrVal(
  1004. AttrVal( $device, "msgRecipient", "" ),
  1005. "msgThPrioAudioEmergency",
  1006. # look for global direct
  1007. AttrVal(
  1008. $globalDevName, "msgThPrioAudioEmergency",
  1009. #look for global indirect type
  1010. AttrVal(
  1011. AttrVal(
  1012. $globalDevName,
  1013. "msgRecipient$typeUc",
  1014. ""
  1015. ),
  1016. "msgThPrioAudioEmergency",
  1017. #look for global indirect general
  1018. AttrVal(
  1019. AttrVal(
  1020. $globalDevName,
  1021. "msgRecipient",
  1022. ""
  1023. ),
  1024. "msgThPrioAudioEmergency",
  1025. # default
  1026. "2"
  1027. )
  1028. )
  1029. )
  1030. )
  1031. )
  1032. );
  1033. # user selected high priority audio threshold
  1034. my $prioThresAudioHigh =
  1035. # look for direct
  1036. AttrVal(
  1037. $device, "msgThPrioAudioHigh",
  1038. #look for indirect audio
  1039. AttrVal(
  1040. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1041. "msgThPrioAudioHigh",
  1042. #look for indirect general
  1043. AttrVal(
  1044. AttrVal( $device, "msgRecipient", "" ),
  1045. "msgThPrioAudioHigh",
  1046. # look for global direct
  1047. AttrVal(
  1048. $globalDevName, "msgThPrioAudioHigh",
  1049. #look for global indirect type
  1050. AttrVal(
  1051. AttrVal(
  1052. $globalDevName,
  1053. "msgRecipient$typeUc",
  1054. ""
  1055. ),
  1056. "msgThPrioAudioHigh",
  1057. #look for global indirect general
  1058. AttrVal(
  1059. AttrVal(
  1060. $globalDevName,
  1061. "msgRecipient",
  1062. ""
  1063. ),
  1064. "msgThPrioAudioHigh",
  1065. # default
  1066. "1"
  1067. )
  1068. )
  1069. )
  1070. )
  1071. )
  1072. );
  1073. # user selected high priority threshold
  1074. my $prioThresHigh =
  1075. # look for direct
  1076. AttrVal(
  1077. $device, "msgThPrioHigh",
  1078. #look for indirect audio
  1079. AttrVal(
  1080. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1081. "msgThPrioHigh",
  1082. #look for indirect general
  1083. AttrVal(
  1084. AttrVal( $device, "msgRecipient", "" ),
  1085. "msgThPrioHigh",
  1086. # look for global direct
  1087. AttrVal(
  1088. $globalDevName, "msgThPrioHigh",
  1089. #look for global indirect type
  1090. AttrVal(
  1091. AttrVal(
  1092. $globalDevName,
  1093. "msgRecipient$typeUc",
  1094. ""
  1095. ),
  1096. "msgThPrioHigh",
  1097. #look for global indirect general
  1098. AttrVal(
  1099. AttrVal(
  1100. $globalDevName,
  1101. "msgRecipient",
  1102. ""
  1103. ),
  1104. "msgThPrioHigh",
  1105. # default
  1106. "2"
  1107. )
  1108. )
  1109. )
  1110. )
  1111. )
  1112. );
  1113. # user selected normal priority threshold
  1114. my $prioThresNormal =
  1115. # look for direct
  1116. AttrVal(
  1117. $device, "msgThPrioNormal",
  1118. #look for indirect audio
  1119. AttrVal(
  1120. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1121. "msgThPrioNormal",
  1122. #look for indirect general
  1123. AttrVal(
  1124. AttrVal( $device, "msgRecipient", "" ),
  1125. "msgThPrioNormal",
  1126. # look for global direct
  1127. AttrVal(
  1128. $globalDevName, "msgThPrioNormal",
  1129. #look for global indirect type
  1130. AttrVal(
  1131. AttrVal(
  1132. $globalDevName,
  1133. "msgRecipient$typeUc",
  1134. ""
  1135. ),
  1136. "msgThPrioNormal",
  1137. #look for global indirect general
  1138. AttrVal(
  1139. AttrVal(
  1140. $globalDevName,
  1141. "msgRecipient",
  1142. ""
  1143. ),
  1144. "msgThPrioNormal",
  1145. # default
  1146. "0"
  1147. )
  1148. )
  1149. )
  1150. )
  1151. )
  1152. );
  1153. if ( $type[$i] eq "audio" ) {
  1154. if ( $annState eq "long"
  1155. || $forceType == 1
  1156. || $forceDevice == 1
  1157. || $loopPriority >= $prioThresAudioEmg )
  1158. {
  1159. $priorityCat = "";
  1160. }
  1161. elsif ( $loopPriority >= $prioThresAudioHigh ) {
  1162. $priorityCat = "ShortPrio";
  1163. }
  1164. else {
  1165. $priorityCat = "Short";
  1166. }
  1167. }
  1168. else {
  1169. if ( $loopPriority >= $prioThresHigh ) {
  1170. $priorityCat = "High";
  1171. }
  1172. elsif ( $loopPriority >= $prioThresNormal ) {
  1173. $priorityCat = "";
  1174. }
  1175. else {
  1176. $priorityCat = "Low";
  1177. }
  1178. }
  1179. # get resident presence information
  1180. #
  1181. my $residentDevState = "";
  1182. my $residentDevPresence = "";
  1183. # device
  1184. if ( ReadingsVal( $device, "presence", "-" ) ne "-" ) {
  1185. $residentDevState = ReadingsVal( $device, "state", "" );
  1186. $residentDevPresence =
  1187. ReadingsVal( $device, "presence", "" );
  1188. }
  1189. # device indirect
  1190. if (
  1191. (
  1192. $residentDevState eq ""
  1193. || $residentDevPresence eq ""
  1194. )
  1195. && ReadingsVal(
  1196. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1197. "presence", "-" ) ne "-"
  1198. )
  1199. {
  1200. $residentDevState =
  1201. ReadingsVal(
  1202. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1203. "state", "" )
  1204. if ( $residentDevState eq "" );
  1205. $residentDevPresence =
  1206. ReadingsVal(
  1207. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1208. "presence", "" )
  1209. if ( $residentDevPresence eq "" );
  1210. }
  1211. # device indirect general
  1212. if (
  1213. (
  1214. $residentDevState eq ""
  1215. || $residentDevPresence eq ""
  1216. )
  1217. && ReadingsVal( AttrVal( $device, "msgRecipient", "" ),
  1218. "presence", "-" ) ne "-"
  1219. )
  1220. {
  1221. $residentDevState =
  1222. ReadingsVal( AttrVal( $device, "msgRecipient", "" ),
  1223. "state", "" )
  1224. if ( $residentDevState eq "" );
  1225. $residentDevPresence =
  1226. ReadingsVal( AttrVal( $device, "msgRecipient", "" ),
  1227. "presence", "" )
  1228. if ( $residentDevPresence eq "" );
  1229. }
  1230. # device explicit
  1231. if (
  1232. (
  1233. $residentDevState eq ""
  1234. || $residentDevPresence eq ""
  1235. )
  1236. && ReadingsVal(
  1237. AttrVal( $device, "msgResidentsDev", "" ),
  1238. "presence", "-" ) ne "-"
  1239. )
  1240. {
  1241. $residentDevState =
  1242. ReadingsVal(
  1243. AttrVal( $device, "msgResidentsDev", "" ),
  1244. "state", "" )
  1245. if ( $residentDevState eq "" );
  1246. $residentDevPresence =
  1247. ReadingsVal(
  1248. AttrVal( $device, "msgResidentsDev", "" ),
  1249. "presence", "" )
  1250. if ( $residentDevPresence eq "" );
  1251. }
  1252. # global indirect
  1253. if (
  1254. (
  1255. $residentDevState eq ""
  1256. || $residentDevPresence eq ""
  1257. )
  1258. && ReadingsVal(
  1259. AttrVal(
  1260. $globalDevName, "msgRecipient$typeUc", ""
  1261. ),
  1262. "presence",
  1263. "-"
  1264. ) ne "-"
  1265. )
  1266. {
  1267. $residentDevState = ReadingsVal(
  1268. AttrVal(
  1269. $globalDevName, "msgRecipient$typeUc", ""
  1270. ),
  1271. "state", ""
  1272. ) if ( $residentDevState eq "" );
  1273. $residentDevPresence = ReadingsVal(
  1274. AttrVal(
  1275. $globalDevName, "msgRecipient$typeUc", ""
  1276. ),
  1277. "presence",
  1278. ""
  1279. ) if ( $residentDevPresence eq "" );
  1280. }
  1281. # global indirect general
  1282. if (
  1283. (
  1284. $residentDevState eq ""
  1285. || $residentDevPresence eq ""
  1286. )
  1287. && ReadingsVal(
  1288. AttrVal( $globalDevName, "msgRecipient", "" ),
  1289. "presence", "-" ) ne "-"
  1290. )
  1291. {
  1292. $residentDevState =
  1293. ReadingsVal(
  1294. AttrVal( $globalDevName, "msgRecipient", "" ),
  1295. "state", "" )
  1296. if ( $residentDevState eq "" );
  1297. $residentDevPresence =
  1298. ReadingsVal(
  1299. AttrVal( $globalDevName, "msgRecipient", "" ),
  1300. "presence", "" )
  1301. if ( $residentDevPresence eq "" );
  1302. }
  1303. # global explicit
  1304. if (
  1305. (
  1306. $residentDevState eq ""
  1307. || $residentDevPresence eq ""
  1308. )
  1309. && ReadingsVal(
  1310. AttrVal( $globalDevName, "msgResidentsDev", "" ),
  1311. "presence", "-" ) ne "-"
  1312. )
  1313. {
  1314. $residentDevState =
  1315. ReadingsVal(
  1316. AttrVal( $globalDevName, "msgResidentsDev", "" ),
  1317. "state", "" )
  1318. if ( $residentDevState eq "" );
  1319. $residentDevPresence =
  1320. ReadingsVal(
  1321. AttrVal( $globalDevName, "msgResidentsDev", "" ),
  1322. "presence", "" )
  1323. if ( $residentDevPresence eq "" );
  1324. }
  1325. ################################################################
  1326. ### Send message
  1327. ###
  1328. # user selected emergency priority text threshold
  1329. my $prioThresGwEmg =
  1330. # look for direct
  1331. AttrVal(
  1332. $device, "msgThPrioGwEmergency",
  1333. #look for indirect type
  1334. AttrVal(
  1335. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1336. "msgThPrioGwEmergency",
  1337. #look for indirect general
  1338. AttrVal(
  1339. AttrVal( $device, "msgRecipient", "" ),
  1340. "msgThPrioGwEmergency",
  1341. # look for global direct
  1342. AttrVal(
  1343. $globalDevName, "msgThPrioGwEmergency",
  1344. #look for global indirect type
  1345. AttrVal(
  1346. AttrVal(
  1347. $globalDevName,
  1348. "msgRecipient$typeUc",
  1349. ""
  1350. ),
  1351. "msgThPrioGwEmergency",
  1352. #look for global indirect general
  1353. AttrVal(
  1354. AttrVal(
  1355. $globalDevName,
  1356. "msgRecipient",
  1357. ""
  1358. ),
  1359. "msgThPrioGwEmergency",
  1360. # default
  1361. "2"
  1362. )
  1363. )
  1364. )
  1365. )
  1366. )
  1367. );
  1368. if ( $featurelevel >= 5.7 ) {
  1369. my %dummy;
  1370. my ( $err, @a ) =
  1371. ReplaceSetMagic( \%dummy, 0, ($gatewayDevs) );
  1372. $gatewayDevs = join( " ", @a )
  1373. unless ($err);
  1374. }
  1375. my %gatewaysStatus;
  1376. foreach my $gatewayDevOr ( split /\|/, $gatewayDevs ) {
  1377. foreach my $gatewayDev ( split /,/, $gatewayDevOr ) {
  1378. if ( $gatewayDev =~
  1379. m/^@?([A-Za-z0-9._]+):([A-Za-z0-9._\-\/@+]*):?([A-Za-z0-9._\-\/@+]*)$/
  1380. )
  1381. {
  1382. $gatewayDev = $1;
  1383. $subRecipient = $2 if ( $subRecipient eq "" );
  1384. $termRecipient = $3 if ( $termRecipient eq "" );
  1385. }
  1386. my $logMsg =
  1387. "msg $device: Trying to send message via gateway $gatewayDev";
  1388. $logMsg .= " to recipient $subRecipient"
  1389. if ( $subRecipient ne "" );
  1390. $logMsg .= ", terminal device $termRecipient"
  1391. if ( $termRecipient ne "" );
  1392. Log3 $logDevice, 5, $logMsg;
  1393. ##############
  1394. # check for gateway availability and set route status
  1395. #
  1396. my $routeStatus = "OK";
  1397. if ( $type[$i] ne "mail"
  1398. && !defined( $defs{$gatewayDev} )
  1399. && $deviceType eq "device" )
  1400. {
  1401. $routeStatus = "UNDEFINED";
  1402. }
  1403. elsif ( $type[$i] ne "mail"
  1404. && IsDisabled($gatewayDev) )
  1405. {
  1406. $routeStatus = "DISABLED";
  1407. }
  1408. elsif (
  1409. $type[$i] ne "mail"
  1410. && (
  1411. ReadingsVal( $gatewayDev, "presence",
  1412. "present" ) =~
  1413. m/^(0|false|absent|disappeared|unauthorized|disconnected|unreachable)$/i
  1414. || ReadingsVal( $gatewayDev, "state",
  1415. "present" ) =~
  1416. m/^(absent|disappeared|unauthorized|disconnected|unreachable)$/i
  1417. || ( $defs{$gatewayDev}{STATE}
  1418. && $defs{$gatewayDev}{STATE} =~
  1419. m/^(absent|disappeared|unauthorized|disconnected|unreachable)$/i
  1420. )
  1421. || ReadingsVal( $gatewayDev, "available",
  1422. "yes" ) =~ m/^(0|no|off|false)$/i
  1423. || ReadingsVal( $gatewayDev, "reachable",
  1424. "yes" ) =~ m/^(0|no|off|false)$/i
  1425. )
  1426. )
  1427. {
  1428. $routeStatus = "UNAVAILABLE";
  1429. }
  1430. elsif ( $type[$i] eq "screen"
  1431. && ReadingsVal( $gatewayDev, "power", "on" ) =~
  1432. m/^(0|off)$/i )
  1433. {
  1434. $routeStatus = "OFF";
  1435. }
  1436. elsif ($type[$i] eq "audio"
  1437. && $annState ne "long"
  1438. && $annState ne "short" )
  1439. {
  1440. $routeStatus = "USER_DISABLED";
  1441. }
  1442. elsif ( $type[$i] eq "light" && $annState eq "off" )
  1443. {
  1444. $routeStatus = "USER_DISABLED";
  1445. }
  1446. elsif ($type[$i] ne "push"
  1447. && $type[$i] ne "mail"
  1448. && $residentDevPresence eq "absent" )
  1449. {
  1450. $routeStatus = "USER_ABSENT";
  1451. }
  1452. elsif ($type[$i] ne "push"
  1453. && $type[$i] ne "mail"
  1454. && $residentDevState eq "asleep" )
  1455. {
  1456. $routeStatus = "USER_ASLEEP";
  1457. }
  1458. # enforce by user request
  1459. if (
  1460. (
  1461. $routeStatus eq "USER_DISABLED"
  1462. || $routeStatus eq "USER_ABSENT"
  1463. || $routeStatus eq "USER_ASLEEP"
  1464. )
  1465. && ( $forceType == 1 || $forceDevice == 1 )
  1466. )
  1467. {
  1468. $routeStatus = "OK_ENFORCED";
  1469. }
  1470. # enforce by priority
  1471. if (
  1472. (
  1473. $routeStatus eq "USER_DISABLED"
  1474. || $routeStatus eq "USER_ABSENT"
  1475. || $routeStatus eq "USER_ASLEEP"
  1476. )
  1477. && $loopPriority >= $prioThresGwEmg
  1478. )
  1479. {
  1480. $routeStatus = "OK_EMERGENCY";
  1481. }
  1482. # add location status
  1483. if ( $useLocation == 2 ) {
  1484. $routeStatus .= "+LOCATION-UNAVAILABLE";
  1485. }
  1486. elsif ( $useLocation == 1 ) {
  1487. $routeStatus .= "+LOCATION";
  1488. }
  1489. my $gatewayType = (
  1490. $type[$i] eq "mail" ? "fhemMsgMail"
  1491. : (
  1492. $defs{$gatewayDev}{TYPE}
  1493. ? $defs{$gatewayDev}{TYPE}
  1494. : "UNDEFINED"
  1495. )
  1496. );
  1497. my $defTitle = "";
  1498. $defTitle =
  1499. $cmdSchema->{ $type[$i] }{$gatewayType}
  1500. {defaultValues}{$priorityCat}{TITLE}
  1501. if (
  1502. defined(
  1503. $cmdSchema->{ $type[$i] }{$gatewayType}
  1504. {defaultValues}{$priorityCat}{TITLE}
  1505. )
  1506. && $priorityCat ne ""
  1507. );
  1508. $defTitle =
  1509. $cmdSchema->{ $type[$i] }{$gatewayType}
  1510. {defaultValues}{Normal}{TITLE}
  1511. if (
  1512. defined(
  1513. $cmdSchema->{ $type[$i] }{$gatewayType}
  1514. {defaultValues}{Normal}{TITLE}
  1515. )
  1516. && $priorityCat eq ""
  1517. );
  1518. Log3 $logDevice, 5,
  1519. "msg $device: Determined default title: $defTitle";
  1520. # use title from device, global or internal default
  1521. my $loopTitle = $title;
  1522. $loopTitle =
  1523. # look for direct high
  1524. AttrVal(
  1525. $device, "msgTitle$typeUc$priorityCat",
  1526. # look for indirect high
  1527. AttrVal(
  1528. AttrVal(
  1529. $device, "msgRecipient$typeUc", ""
  1530. ),
  1531. "msgTitle$typeUc$priorityCat",
  1532. #look for indirect general high
  1533. AttrVal(
  1534. AttrVal( $device, "msgRecipient", "" ),
  1535. "msgTitle$typeUc$priorityCat",
  1536. # look for global direct high
  1537. AttrVal(
  1538. $globalDevName,
  1539. "msgTitle$typeUc$priorityCat",
  1540. # look for global indirect high
  1541. AttrVal(
  1542. AttrVal(
  1543. $globalDevName,
  1544. "msgRecipient$typeUc",
  1545. ""
  1546. ),
  1547. "msgTitle$typeUc$priorityCat",
  1548. #look for global indirect general high
  1549. AttrVal(
  1550. AttrVal(
  1551. $globalDevName,
  1552. "msgRecipient",
  1553. ""
  1554. ),
  1555. "msgTitle$typeUc$priorityCat",
  1556. # default
  1557. $defTitle
  1558. )
  1559. )
  1560. )
  1561. )
  1562. )
  1563. ) if ( $title eq "-" );
  1564. $loopTitle = ""
  1565. if ( $loopTitle eq "none"
  1566. || $loopTitle eq "-" );
  1567. my $loopMsg = $msg;
  1568. if ( $catchall == 1 ) {
  1569. $loopTitle = "Fw: $loopTitle"
  1570. if ( $loopTitle
  1571. && $type[$i] !~ /^(audio|screen)$/ );
  1572. $loopMsg = "Forwarded Message: $loopMsg"
  1573. if ( !$loopTitle );
  1574. if ( $type[$i] eq "mail" ) {
  1575. $loopMsg .=
  1576. "\n\n-- \nMail catched from device $device";
  1577. }
  1578. elsif ( $type[$i] !~ /^(audio|screen)$/ ) {
  1579. $loopMsg .=
  1580. " ### (Catched from device $device)";
  1581. }
  1582. }
  1583. # correct message format
  1584. #
  1585. # Remove Sonos Speak commands
  1586. $loopMsg =~ s/(\s*\|\w+\|\s*)/\\n\\n/gi
  1587. if ( $type[$i] ne "audio" );
  1588. # Replace new line with HTML break
  1589. # for e-mails
  1590. $loopMsg =~ s/\n/<br \/>\n/gi
  1591. if ( $type[$i] eq "mail" );
  1592. # use command from device, global or internal default
  1593. my $defCmd = "";
  1594. $defCmd =
  1595. $cmdSchema->{ $type[$i] }{$gatewayType}
  1596. {$priorityCat}
  1597. if (
  1598. defined(
  1599. $cmdSchema->{ $type[$i] }{$gatewayType}
  1600. {$priorityCat}
  1601. )
  1602. && $priorityCat ne ""
  1603. );
  1604. $defCmd =
  1605. $cmdSchema->{ $type[$i] }{$gatewayType}{Normal}
  1606. if (
  1607. defined(
  1608. $cmdSchema->{ $type[$i] }{$gatewayType}
  1609. {Normal}
  1610. )
  1611. && $priorityCat eq ""
  1612. );
  1613. my $cmd =
  1614. # gateway device
  1615. AttrVal(
  1616. $gatewayDev, "msgCmd$typeUc$priorityCat",
  1617. # look for direct
  1618. AttrVal(
  1619. $device, "msgCmd$typeUc$priorityCat",
  1620. # look for indirect
  1621. AttrVal(
  1622. AttrVal(
  1623. $device, "msgRecipient$typeUc",
  1624. ""
  1625. ),
  1626. "msgCmd$typeUc$priorityCat",
  1627. #look for indirect general
  1628. AttrVal(
  1629. AttrVal(
  1630. $device, "msgRecipient", ""
  1631. ),
  1632. "msgCmd$typeUc$priorityCat",
  1633. # look for global direct
  1634. AttrVal(
  1635. $globalDevName,
  1636. "msgCmd$typeUc$priorityCat",
  1637. # look for global indirect
  1638. AttrVal(
  1639. AttrVal(
  1640. $globalDevName,
  1641. "msgRecipient$typeUc",
  1642. ""
  1643. ),
  1644. "msgCmd$typeUc$priorityCat",
  1645. #look for global indirect general
  1646. AttrVal(
  1647. AttrVal(
  1648. $globalDevName,
  1649. "msgRecipient",
  1650. ""
  1651. ),
  1652. "msgCmd$typeUc$priorityCat",
  1653. # internal
  1654. $defCmd
  1655. )
  1656. )
  1657. )
  1658. )
  1659. )
  1660. )
  1661. );
  1662. if ( $cmd eq "" ) {
  1663. Log3 $logDevice, 4,
  1664. "$gatewayDev: Unknown command schema for gateway device type $gatewayType. Use manual definition by userattr msgCmd*";
  1665. $return .=
  1666. "$gatewayDev: Unknown command schema for gateway device type $gatewayType. Use manual definition by userattr msgCmd*\n";
  1667. next;
  1668. }
  1669. # ReplaceSetMagic
  1670. #
  1671. my $replaceError;
  1672. if ( $featurelevel >= 5.7 ) {
  1673. my %dummy;
  1674. my ( $err, @a );
  1675. # TITLE
  1676. ( $err, @a ) =
  1677. ReplaceSetMagic( \%dummy, 0, ($loopTitle) );
  1678. $replaceError .=
  1679. "ReplaceSetMagic failed for TITLE: $err\n"
  1680. if ($err);
  1681. $loopTitle = join( " ", @a )
  1682. unless ($err);
  1683. # RECIPIENT
  1684. if ( $subRecipient ne "" ) {
  1685. ( $err, @a ) =
  1686. ReplaceSetMagic( \%dummy, 0,
  1687. ($subRecipient) );
  1688. $replaceError .=
  1689. "ReplaceSetMagic failed "
  1690. . "for RECIPIENT: $err\n"
  1691. if ($err);
  1692. $subRecipient = join( " ", @a )
  1693. unless ($err);
  1694. }
  1695. # TERMINAL
  1696. if ( $termRecipient ne "" ) {
  1697. ( $err, @a ) =
  1698. ReplaceSetMagic( \%dummy, 0,
  1699. ($termRecipient) );
  1700. $replaceError .=
  1701. "ReplaceSetMagic failed "
  1702. . "for TERMINAL: $err\n"
  1703. if ($err);
  1704. $termRecipient = join( " ", @a )
  1705. unless ($err);
  1706. }
  1707. }
  1708. $cmd =~ s/%DEVICE%/$gatewayDev/gi;
  1709. $cmd =~ s/%PRIORITY%/$loopPriority/gi;
  1710. $cmd =~ s/%TITLE%/$loopTitle/gi;
  1711. $cmd =~ s/%MSG%/$loopMsg/gi;
  1712. $cmd =~ s/%RECIPIENT%/$subRecipient/gi
  1713. if ( $subRecipient ne "" );
  1714. $cmd =~ s/%TERMINAL%/$termRecipient/gi
  1715. if ( $termRecipient ne "" );
  1716. # advanced options from message
  1717. if ( ref($advanced) eq "ARRAY" ) {
  1718. for my $item (@$advanced) {
  1719. for my $key ( keys(%$item) ) {
  1720. my $val = $item->{$key};
  1721. $cmd =~ s/%$key%/$val/gi;
  1722. }
  1723. }
  1724. }
  1725. # advanced options from command schema hash
  1726. if (
  1727. $priorityCat ne ""
  1728. && defined(
  1729. $cmdSchema->{ $type[$i] }{$gatewayType}
  1730. {defaultValues}{$priorityCat}
  1731. )
  1732. )
  1733. {
  1734. for my $item (
  1735. $cmdSchema->{ $type[$i] }{$gatewayType}
  1736. {defaultValues}{$priorityCat} )
  1737. {
  1738. for my $key ( keys(%$item) ) {
  1739. my $val = $item->{$key};
  1740. $cmd =~ s/%$key%/$val/gi;
  1741. }
  1742. }
  1743. }
  1744. elsif (
  1745. $priorityCat eq ""
  1746. && defined(
  1747. $cmdSchema->{ $type[$i] }{$gatewayType}
  1748. {defaultValues}{Normal}
  1749. )
  1750. )
  1751. {
  1752. for my $item (
  1753. $cmdSchema->{ $type[$i] }{$gatewayType}
  1754. {defaultValues}{Normal} )
  1755. {
  1756. for my $key ( keys(%$item) ) {
  1757. my $val = $item->{$key};
  1758. $cmd =~ s/%$key%/$val/gi;
  1759. }
  1760. }
  1761. }
  1762. $sentCounter++;
  1763. if ( $routeStatus =~ /^OK\w*/ ) {
  1764. my $error = 0;
  1765. # ReplaceSetMagic
  1766. #
  1767. if ( $featurelevel >= 5.7 && !$replaceError ) {
  1768. my %dummy;
  1769. my ( $err, @a ) =
  1770. ReplaceSetMagic( \%dummy, 0, ($cmd) );
  1771. $replaceError .=
  1772. "ReplaceSetMagic failed for CMD: $err\n"
  1773. if ($err);
  1774. $cmd = join( " ", @a )
  1775. unless ($err);
  1776. }
  1777. # run command
  1778. if ($replaceError) {
  1779. $error = 2;
  1780. $return .= $replaceError;
  1781. }
  1782. elsif ( $cmd =~ /^\s*\{.*\}\s*$/ ) {
  1783. Log3 $logDevice, 5,
  1784. "msg $device: $type[$i] route command (Perl): $cmd";
  1785. eval $cmd;
  1786. if ($@) {
  1787. $error = 1;
  1788. $return .= "$gatewayDev: $@\n";
  1789. }
  1790. }
  1791. else {
  1792. Log3 $logDevice, 5,
  1793. "msg $device: $type[$i] route command (fhem): $cmd";
  1794. fhem $cmd, 1;
  1795. if ($@) {
  1796. $error = 1;
  1797. $return .= "$gatewayDev: $@\n";
  1798. }
  1799. }
  1800. $routeStatus = "ERROR"
  1801. if ( $error == 1 );
  1802. $routeStatus = "ERROR_EVAL"
  1803. if ( $error == 2 );
  1804. Log3 $logDevice, 3,
  1805. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev RECIPIENT=$subRecipient STATUS=$routeStatus PRIORITY=$loopPriority($priorityCat) TITLE='$loopTitle' MSG='$loopMsg'"
  1806. if ( $priorityCat ne ""
  1807. && $subRecipient ne "" );
  1808. Log3 $logDevice, 3,
  1809. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev RECIPIENT=$subRecipient STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' MSG='$loopMsg'"
  1810. if ( $priorityCat eq ""
  1811. && $subRecipient ne "" );
  1812. Log3 $logDevice, 3,
  1813. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev STATUS=$routeStatus PRIORITY=$loopPriority($priorityCat) TITLE='$loopTitle' MSG='$loopMsg'"
  1814. if ( $priorityCat ne ""
  1815. && $subRecipient eq "" );
  1816. Log3 $logDevice, 3,
  1817. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' MSG='$loopMsg'"
  1818. if ( $priorityCat eq ""
  1819. && $subRecipient eq "" );
  1820. $msgSent = 1 if ( $error == 0 );
  1821. $msgSentDev = 1 if ( $error == 0 );
  1822. if ( $subRecipient ne "" ) {
  1823. $gatewaysStatus{"$gatewayDev:$subRecipient"}
  1824. = $routeStatus
  1825. if ( $globalDevName ne $gatewayDev );
  1826. $gatewaysStatus{"$device:$subRecipient"} =
  1827. $routeStatus
  1828. if ( $globalDevName eq $gatewayDev );
  1829. }
  1830. else {
  1831. $gatewaysStatus{$gatewayDev} = $routeStatus
  1832. if ( $globalDevName ne $gatewayDev );
  1833. $gatewaysStatus{$device} = $routeStatus
  1834. if ( $globalDevName eq $gatewayDev );
  1835. }
  1836. }
  1837. elsif ($routeStatus eq "UNAVAILABLE"
  1838. || $routeStatus eq "UNDEFINED" )
  1839. {
  1840. Log3 $logDevice, 3,
  1841. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev RECIPIENT=$subRecipient STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' '$loopMsg'"
  1842. if ( $subRecipient ne "" );
  1843. Log3 $logDevice, 3,
  1844. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' '$loopMsg'"
  1845. if ( $subRecipient eq "" );
  1846. $gatewaysStatus{$gatewayDev} = $routeStatus
  1847. if ( $globalDevName ne $gatewayDev );
  1848. $gatewaysStatus{$device} = $routeStatus
  1849. if ( $globalDevName eq $gatewayDev );
  1850. }
  1851. else {
  1852. Log3 $logDevice, 3,
  1853. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev RECIPIENT=$subRecipient STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' '$loopMsg'"
  1854. if ( $subRecipient ne "" );
  1855. Log3 $logDevice, 3,
  1856. "msg $device: ID=$msgID.$sentCounter TYPE=$type[$i] ROUTE=$gatewayDev STATUS=$routeStatus PRIORITY=$loopPriority TITLE='$loopTitle' '$loopMsg'"
  1857. if ( $subRecipient eq "" );
  1858. $msgSent = 2 if ( $msgSent != 1 );
  1859. $msgSentDev = 2 if ( $msgSentDev != 1 );
  1860. $gatewaysStatus{$gatewayDev} = $routeStatus
  1861. if ( $globalDevName ne $gatewayDev );
  1862. $gatewaysStatus{$device} = $routeStatus
  1863. if ( $globalDevName eq $gatewayDev );
  1864. }
  1865. }
  1866. last if ( $msgSentDev == 1 );
  1867. }
  1868. if ( $catchall == 0 ) {
  1869. if ( !defined( $sentTypesPerDevice{$device} ) ) {
  1870. $sentTypesPerDevice{$device} = "";
  1871. }
  1872. else {
  1873. $sentTypesPerDevice{$device} .= " ";
  1874. }
  1875. $sentTypesPerDevice{$device} .=
  1876. $type[$i] . ":" . $msgSentDev;
  1877. }
  1878. else {
  1879. if ( !defined( $sentTypesPerDevice{$device} ) ) {
  1880. $sentTypesPerDevice{$globalDevName} = "";
  1881. }
  1882. else {
  1883. $sentTypesPerDevice{$globalDevName} .= " ";
  1884. }
  1885. $sentTypesPerDevice{$globalDevName} .=
  1886. $type[$i] . ":" . $msgSentDev;
  1887. }
  1888. # update device readings
  1889. my $readingsDev = $defs{$device};
  1890. $readingsDev = $defs{$globalDevName}
  1891. if ( $catchall == 1 || $deviceType eq "email" );
  1892. readingsBeginUpdate($readingsDev);
  1893. readingsBulkUpdate( $readingsDev, "fhemMsg" . $typeUc,
  1894. $msg );
  1895. readingsBulkUpdate( $readingsDev,
  1896. "fhemMsg" . $typeUc . "Title", $title );
  1897. readingsBulkUpdate( $readingsDev,
  1898. "fhemMsg" . $typeUc . "Prio",
  1899. $loopPriority );
  1900. my $gwStates = "-";
  1901. while ( ( my $gwName, my $gwState ) = each %gatewaysStatus )
  1902. {
  1903. $gwStates = "" if $gwStates eq "-";
  1904. $gwStates .= " " if $gwStates ne "-";
  1905. $gwStates .= "$gwName:$gwState";
  1906. }
  1907. readingsBulkUpdate( $readingsDev,
  1908. "fhemMsg" . $typeUc . "Gw", $gwStates );
  1909. readingsBulkUpdate( $readingsDev,
  1910. "fhemMsg" . $typeUc . "State", $msgSentDev );
  1911. ################################################################
  1912. ### Implicit forwards based on priority or presence
  1913. ###
  1914. # no implicit escalations for type mail
  1915. next if ( $type[$i] eq "mail" );
  1916. # Skip if typeOr is defined
  1917. # and this is not the last type entry
  1918. # TODO: bei mehreren gleichzeitigen Typen (and-Definition)?
  1919. if ( $msgSentDev != 1
  1920. && $hasTypeOr == 1
  1921. && $isTypeOr < scalar( grep { defined $_ } @typesOr ) )
  1922. {
  1923. Log3 $logDevice, 4,
  1924. "msg $device: Skipping implicit forward due to typesOr definition";
  1925. # remove recipient from list to avoid
  1926. # other interaction when using recipientOr in parallel
  1927. if ( $hasRecipientOr == 1
  1928. && $isRecipientOr <
  1929. scalar( grep { defined $_ } @recipientsOr ) )
  1930. {
  1931. my $regex1 =
  1932. "\\s*!?@?" . $device . "[,|]"; # at the beginning
  1933. my $regex2 =
  1934. "[,|]!?@?" . $device . "\\s*"; # at the end
  1935. my $regex3 =
  1936. ",!?@?"
  1937. . $device
  1938. . ","; # in the middle with comma
  1939. my $regex4 =
  1940. "[\|,]!?@?"
  1941. . $device
  1942. . "[\|,]"; # in the middle with pipe and/or comma
  1943. $recipients =~ s/^$regex1//;
  1944. $recipients =~ s/$regex2$/|/gi;
  1945. $recipients =~ s/$regex3/,/gi;
  1946. $recipients =~ s/$regex4/|/gi;
  1947. }
  1948. next;
  1949. }
  1950. # Skip if recipientOr is defined
  1951. # and this is not the last device entry
  1952. # TODO: bei mehreren gleichzeitigen Empfängern
  1953. # (and-Definition)?
  1954. if ( $msgSentDev != 1
  1955. && $hasRecipientOr == 1
  1956. && $isRecipientOr <
  1957. scalar( grep { defined $_ } @recipientsOr ) )
  1958. {
  1959. Log3 $logDevice, 4,
  1960. "msg $device: Skipping implicit forward due to recipientOr definition";
  1961. next;
  1962. }
  1963. # priority forward thresholds
  1964. #
  1965. ### emergency
  1966. my $msgFwPrioEmergency =
  1967. # look for direct
  1968. AttrVal(
  1969. $device, "msgFwPrioEmergency$typeUc",
  1970. #look for indirect
  1971. AttrVal(
  1972. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1973. "msgFwPrioEmergency$typeUc",
  1974. #look for indirect general
  1975. AttrVal(
  1976. AttrVal( $device, "msgRecipient", "" ),
  1977. "msgFwPrioEmergency$typeUc",
  1978. # default
  1979. 2
  1980. )
  1981. )
  1982. );
  1983. ### absent
  1984. my $msgFwPrioAbsent =
  1985. # look for direct
  1986. AttrVal(
  1987. $device, "msgFwPrioAbsent$typeUc",
  1988. #look for indirect
  1989. AttrVal(
  1990. AttrVal( $device, "msgRecipient$typeUc", "" ),
  1991. "msgFwPrioAbsent$typeUc",
  1992. #look for indirect general
  1993. AttrVal(
  1994. AttrVal( $device, "msgRecipient", "" ),
  1995. "msgFwPrioAbsent$typeUc",
  1996. # default
  1997. 0
  1998. )
  1999. )
  2000. );
  2001. ### gone
  2002. my $msgFwPrioGone =
  2003. # look for direct
  2004. AttrVal(
  2005. $device, "msgFwPrioGone$typeUc",
  2006. #look for indirect
  2007. AttrVal(
  2008. AttrVal( $device, "msgRecipient$typeUc", "" ),
  2009. "msgFwPrioGone$typeUc",
  2010. #look for indirect general
  2011. AttrVal(
  2012. AttrVal( $device, "msgRecipient", "" ),
  2013. "msgFwPrioGone$typeUc",
  2014. # default
  2015. 1
  2016. )
  2017. )
  2018. );
  2019. Log3 $logDevice, 5,
  2020. "msg $device: Implicit forwards: recipient presence=$residentDevPresence state=$residentDevState"
  2021. if ( $residentDevPresence ne ""
  2022. || $residentDevState ne "" );
  2023. my $fw_gwUnavailable =
  2024. defined(
  2025. $settings->{ $type[$i] }{typeEscalation}{gwUnavailable}
  2026. )
  2027. ? $settings->{ $type[$i] }{typeEscalation}{gwUnavailable}
  2028. : "";
  2029. my $fw_emergency =
  2030. defined(
  2031. $settings->{ $type[$i] }{typeEscalation}{emergency} )
  2032. ? $settings->{ $type[$i] }{typeEscalation}{emergency}
  2033. : "";
  2034. my $fw_residentAbsent =
  2035. defined(
  2036. $settings->{ $type[$i] }{typeEscalation}{residentAbsent}
  2037. )
  2038. ? $settings->{ $type[$i] }{typeEscalation}{residentAbsent}
  2039. : "";
  2040. my $fw_residentGone =
  2041. defined(
  2042. $settings->{ $type[$i] }{typeEscalation}{residentGone} )
  2043. ? $settings->{ $type[$i] }{typeEscalation}{residentGone}
  2044. : "";
  2045. # Forward message
  2046. # if no gateway device for this type was available
  2047. if ( $msgSentDev == 0
  2048. && $fw_gwUnavailable ne ""
  2049. && !( $fw_gwUnavailable ~~ @type )
  2050. && $routes{$fw_gwUnavailable} == 1 )
  2051. {
  2052. Log3 $logDevice, 4,
  2053. "msg $device: Implicit forwards: No $type[$i] gateway device available for recipient $device ($gatewayDevs). Trying alternative message type "
  2054. . $fw_gwUnavailable;
  2055. push @type, $fw_gwUnavailable;
  2056. $forwarded .= "," . $type[$i] . ">" . $fw_gwUnavailable
  2057. if ( $forwarded ne "" );
  2058. $forwarded .= $type[$i] . ">" . $fw_gwUnavailable
  2059. if ( $forwarded eq "" );
  2060. }
  2061. # Forward message
  2062. # if emergency priority
  2063. if ( $loopPriority >= $msgFwPrioEmergency
  2064. && $fw_emergency ne ""
  2065. && !( $fw_emergency ~~ @type )
  2066. && $routes{$fw_emergency} == 1 )
  2067. {
  2068. Log3 $logDevice, 4,
  2069. "msg $device: Implicit forwards: Escalating high priority $type[$i] message via "
  2070. . $fw_emergency;
  2071. push @type, $fw_emergency;
  2072. $forwarded .= "," . $type[$i] . ">" . $fw_emergency
  2073. if ( $forwarded ne "" );
  2074. $forwarded .= $type[$i] . ">" . $fw_emergency
  2075. if ( $forwarded eq "" );
  2076. }
  2077. # Forward message
  2078. # if high priority and residents are constantly not at home
  2079. if ( $residentDevPresence eq "absent"
  2080. && $loopPriority >= $msgFwPrioGone
  2081. && $fw_residentGone ne ""
  2082. && !( $fw_residentGone ~~ @type )
  2083. && $routes{$fw_residentGone} == 1 )
  2084. {
  2085. Log3 $logDevice, 4,
  2086. "msg $device: Implicit forwards: Escalating high priority $type[$i] message via "
  2087. . $fw_residentGone;
  2088. push @type, $fw_residentGone;
  2089. $forwarded .= "," . $type[$i] . ">" . $fw_residentGone
  2090. if ( $forwarded ne "" );
  2091. $forwarded .= $type[$i] . ">" . $fw_residentGone
  2092. if ( $forwarded eq "" );
  2093. }
  2094. # Forward message
  2095. # if priority is normal or higher and residents
  2096. # are not at home but nearby
  2097. if ( $residentDevState eq "absent"
  2098. && $loopPriority >= $msgFwPrioAbsent
  2099. && $fw_residentAbsent ne ""
  2100. && !( $fw_residentAbsent ~~ @type )
  2101. && $routes{$fw_residentAbsent} == 1 )
  2102. {
  2103. Log3 $logDevice, 4,
  2104. "msg $device: Implicit forwards: Escalating $type[$i] message via "
  2105. . $fw_residentAbsent
  2106. . " due to absence";
  2107. push @type, $fw_residentAbsent;
  2108. $forwarded .=
  2109. "," . $type[$i] . ">" . $fw_residentAbsent
  2110. if ( $forwarded ne "" );
  2111. $forwarded .= $type[$i] . ">" . $fw_residentAbsent
  2112. if ( $forwarded eq "" );
  2113. }
  2114. }
  2115. last if ( $msgSent == 1 );
  2116. $isRecipientOr++;
  2117. }
  2118. }
  2119. last if ( $msgSent == 1 );
  2120. $isTypeOr++;
  2121. }
  2122. # finalize device readings
  2123. while ( ( my $device, my $types ) = each %sentTypesPerDevice ) {
  2124. $device = $globalDevName
  2125. if ( $device =~ /^(([A-Za-z0-9%+._-])+[@]+([%+a-z0-9A-Z.-]*))$/ );
  2126. readingsBulkUpdate( $defs{$device}, "fhemMsgStateTypes", $types )
  2127. if ( $forwarded eq "" );
  2128. readingsBulkUpdate( $defs{$device}, "fhemMsgStateTypes",
  2129. $types . " forwards:" . $forwarded )
  2130. if ( $forwarded ne "" );
  2131. readingsBulkUpdate( $defs{$device}, "fhemMsgState", $msgSent );
  2132. readingsEndUpdate( $defs{$device}, 1 );
  2133. }
  2134. if ( $msgSent == 1 && $return ne "" ) {
  2135. $return .= "However, message was still sent to some recipients!";
  2136. }
  2137. if ( $msgSent == 2 ) {
  2138. $return .=
  2139. "FATAL ERROR: Message NOT sent. No gateway device was available.";
  2140. }
  2141. return $return;
  2142. }
  2143. 1;
  2144. =pod
  2145. =item command
  2146. =item summary dynamic routing of messages to FHEM devices and modules
  2147. =item summary_DE dynamisches Routing f&uuml;r Nachrichten an FHEM Ger&auml;te und Module
  2148. =begin html
  2149. <a name="MSG"></a>
  2150. <h3>msg</h3>
  2151. <ul>
  2152. <code>msg [&lt;type&gt;] [&lt;@device&gt;|&lt;e-mail address&gt;] [&lt;priority&gt;] [|&lt;title&gt;|] &lt;message&gt;</code>
  2153. <br>
  2154. <br>
  2155. No documentation here yet, sorry.<br>
  2156. <a href="http://forum.fhem.de/index.php/topic,39983.0.html">FHEM Forum</a>
  2157. </ul>
  2158. =end html
  2159. =begin html_DE
  2160. <a name="MSG"></a>
  2161. <h3>msg</h3>
  2162. <ul>
  2163. <code>msg [&lt;type&gt;] [&lt;@device&gt;|&lt;e-mail address&gt;] [&lt;priority&gt;] [|&lt;title&gt;|] &lt;message&gt;</code>
  2164. <br>
  2165. <br>
  2166. Bisher keine Dokumentation hier, sorry.<br>
  2167. <a href="http://forum.fhem.de/index.php/topic,39983.0.html">FHEM Forum</a>
  2168. </ul>
  2169. =end html_DE
  2170. =cut