34_ESPEasy.pm 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725
  1. # $Id: 34_ESPEasy.pm 14936 2017-08-21 04:12:53Z dev0 $
  2. ################################################################################
  3. #
  4. # 34_ESPEasy.pm is a FHEM Perl module to control ESP8266 /w ESPEasy
  5. #
  6. # Copyright 2017 by dev0
  7. # FHEM forum: https://forum.fhem.de/index.php?action=profile;u=7465
  8. #
  9. # This file is part of FHEM.
  10. #
  11. # Fhem is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # Fhem is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ################################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use Data::Dumper;
  29. use MIME::Base64;
  30. use TcpServerUtils;
  31. use HttpUtils;
  32. use Color;
  33. # ------------------------------------------------------------------------------
  34. # global/default values
  35. # ------------------------------------------------------------------------------
  36. my $module_version = "1.33"; # Version of this module
  37. my $minEEBuild = 128; # informational
  38. my $minJsonVersion = 1.02; # checked in received data
  39. my $d_Interval = 300; # interval
  40. my $d_httpReqTimeout = 10; # timeout http req
  41. my $d_colorpickerCTww = 2000; # color temp for ww (kelvin)
  42. my $d_colorpickerCTcw = 6000; # color temp for cw (kelvin)
  43. my $d_maxHttpSessions = 3; # concurrent connects to a single esp
  44. my $d_maxQueueSize = 250; # max queue size,
  45. my $d_resendFailedCmd = 0; # resend failed http requests by default?
  46. my $d_displayTextEncode = 1; # urlEncode Text for Displays
  47. my $d_displayTextWidth = 0; # display width, 0 => disable formating
  48. my $d_bridgePort = 8383;
  49. # IP ranges that are allowed to connect to ESPEasy without attr allowedIPs set.
  50. # defined as regexp beause it's quicker than check against IP ranges...
  51. my $d_allowedIPs = "192.168.0.0/16,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,"
  52. . "fe80::/10,fc00::/7,::1";
  53. my $d_localIPs = "^(127|192.168|172.(1[6-9]|2[0-9]|3[01])|10|169.254)\\.|"
  54. . "^(f(e[89ab]|[cd])|::1)";
  55. # ------------------------------------------------------------------------------
  56. # "setCmds" => "min. number of parameters"
  57. # ------------------------------------------------------------------------------
  58. my %ESPEasy_setCmds = (
  59. "gpio" => "2",
  60. "pwm" => "2",
  61. "pwmfade" => "3",
  62. "pulse" => "3",
  63. "longpulse" => "3",
  64. "servo" => "3",
  65. "lcd" => "3",
  66. "lcdcmd" => "1",
  67. "mcpgpio" => "2",
  68. "oled" => "3",
  69. "oledcmd" => "1",
  70. "pcapwm" => "2",
  71. "pcfgpio" => "2",
  72. "pcfpulse" => "3",
  73. "pcflongpulse" => "3",
  74. "irsend" => "3", #_P035_IRTX.ino
  75. "status" => "2",
  76. "raw" => "1",
  77. "reboot" => "0",
  78. "erase" => "0",
  79. "reset" => "0",
  80. "statusrequest" => "0",
  81. "clearreadings" => "0",
  82. "help" => "1",
  83. "lights" => "1",
  84. "dots" => "1",
  85. "tone" => "3",
  86. "rtttl" => "1",
  87. "dmx" => "1",
  88. "motorshieldcmd" => "5",
  89. "candle" => "0", # params are splited by ":" not " "
  90. "neopixel" => "4",
  91. "neopixelall" => "3",
  92. "neopixelline" => "5",
  93. "oledframedcmd" => "1",
  94. "serialsend" => "1", #_P020_Ser2Net.ino
  95. "buzzer" => "0",
  96. "inputswitchstate" => "0", # _0P001_Switch.ini
  97. "event" => "1",
  98. "nfx" => "1"
  99. );
  100. my @ESPEasy_maplightCmds =
  101. qw( on off toggle pct ct rgb dim line one all fade colorfade rainbow kitt
  102. comet theatre scan dualscan twinkle twinklefade sparkle wipe fire stop
  103. fadetime fadedelay count speed bgcolor
  104. );
  105. # ------------------------------------------------------------------------------
  106. # "setCmds" => "syntax", ESPEasy_paramPos() will parse for some <.*> positions
  107. # ------------------------------------------------------------------------------
  108. my %ESPEasy_setCmdsUsage = (
  109. "gpio" => "gpio <pin> <0|1|off|on>",
  110. "pwm" => "pwm <pin> <level>",
  111. "pulse" => "pulse <pin> <0|1|off|on> <duration>",
  112. "longpulse" => "longpulse <pin> <0|1|off|on> <duration>",
  113. "servo" => "servo <servoNo> <pin> <position>",
  114. "lcd" => "lcd <row> <col> <text>",
  115. "lcdcmd" => "lcdcmd <on|off|clear>",
  116. "mcpgpio" => "mcpgpio <pin> <0|1|off|on>",
  117. "oled" => "oled <row> <col> <text>",
  118. "oledcmd" => "oledcmd <on|off|clear>",
  119. "pcapwm" => "pcapwm <pin> <Level>",
  120. "pcfgpio" => "pcfgpio <pin> <0|1|off|on>",
  121. "pcfpulse" => "pcfpulse <pin> <0|1|off|on> <duration>", #missing docu
  122. "pcflongpulse" => "pcflongPulse <pin> <0|1|off|on> <duration>",#missing docu
  123. "status" => "status <device> <pin>",
  124. "pwmfade" => "pwmfade <pin> <target> <duration>", #Forum #55728.msg530220
  125. "irsend" => "irsend <RAW> <B32 raw code> <frequenz> <pulse length> <blank length> "
  126. . "| irsend <NEC|JVC|RC5|RC6|SAMSUNG|SONY|PANASONIC> <code> <bits>",
  127. "raw" => "raw <esp_comannd> <...>",
  128. "reboot" => "reboot",
  129. "erase" => "erase",
  130. "reset" => "reset",
  131. "statusrequest" => "statusRequest",
  132. "clearreadings" => "clearReadings",
  133. "help" => "help <".join("|", sort keys %ESPEasy_setCmds).">",
  134. "lights" => "lights <rgb|ct|pct|on|off|toggle> [color] [fading time] [pct]",
  135. "dots" => "dots <params>",
  136. "tone" => "tone <pin> <freq> <duration>",
  137. "rtttl" => "rtttl <RTTTL>",
  138. "dmx" => "dmx <ON|OFF|LOG|value|channel=value[,value][...]>",
  139. "motorshieldcmd" => "motorshieldcmd <DCMotor|Stepper> <Motornumber> "
  140. . "<Forward|Backward|Release> <Speed|Steps> "
  141. . "<SINGLE|DOUBLE|INTERLEAVE|MICROSTEP>",
  142. "candle" => "CANDLE:<FlameType>:<Color>:<Brightness>",
  143. "neopixel" => "NeoPixel <led_nr> <red 0-255> <green 0-255> <blue 0-255>",
  144. "neopixelall" => "NeoPixelAll <red 0-255> <green 0-255> <blue 0-255>",
  145. "neopixelline" => "NeoPixelLine <start_led_nr> <end_led_nr> <red 0-255> ".
  146. "<green 0-255> <blue 0-255>",
  147. "oledframedcmd" => "oledframedcmd <on|off>",
  148. "serialsend" => "serialsend <string>",
  149. "buzzer" => "buzzer",
  150. "inputswitchstate" => "inputswitchstate",
  151. "event" => "event <string>", #Forum #73291
  152. "nfx" => "nfx <off|on|dim|line|one|all|rgb|fade|colorfade|rainbow|kitt|comet|theatre|scan|dualscan|twinkle|twinklefade|sparkle|wipe|fire|stop> <parameter>", #Forum #73949
  153. #Lights
  154. "rgb" => "rgb <rrggbb> [fadetime] [delay +/-ms]",
  155. "pct" => "pct <pct> [fadetime]",
  156. "ct" => "ct <ct> [fadetime] [pct bri]",
  157. "on" => "on [fadetime] [delay +/-ms]",
  158. "off" => "off [fadetime] [delay +/-ms]",
  159. "toggle" => "toggle [fadetime]",
  160. # more light related commands - Forum #73949
  161. "dim" => "dim <value 0-255>",
  162. "line" => "line <startpixel> <endpixel> <rrggbb>",
  163. "one" => "one <pixel> <rrggbb>",
  164. "all" => "all <rrggbb> [fadetime] [delay +/-ms]",
  165. "fade" => "fade <rrggbb> [fadetime ms] [delay +/-ms]",
  166. "colorfade" => "colorfade <rrggbb_start> <rrggbb_end> [startpixel] [endpixel]",
  167. "rainbow" => "rainbow [speed +/- 0-50]",
  168. "kitt" => "kitt <rrggbb> [speed 0-50]",
  169. "comet" => "comet <rrggbb> [speed +/- 0-50]",
  170. "theatre" => "theatre <rrggbb> [rrggbb background] [speed +/- 0-50]",
  171. "scan" => "scan <rrggbb> [rrggbb background] [speed 0-50]",
  172. "dualscan" => "dualscan <rrggbb> [rrggbb background] [speed 0-50]",
  173. "twinkle" => "twinkle <rrggbb> [rrggbb background] [speed 0-50]",
  174. "twinklefade" => "twinklefade <rrggbb> [number of pixels] [speed 0-50]",
  175. "sparkle" => "sparkle <rrggbb> [rrggbb background] [speed 0-50]",
  176. "wipe" => "wipe <rrggbb> [rrggbb dot] [speed +/- 0-50]",
  177. "fire" => "fire [fps] [brightness 0-255] [cooling 20-100] [sparking 50-200]",
  178. "stop" => "stop",
  179. "fadetime" => "fadetime <value in ms>",
  180. "fadedelay" => "fadedelay <value in +/-ms>",
  181. "count" => "count <value>",
  182. "speed" => "speed <value 0-50>",
  183. "bgcolor" => "bgcolor <rrggbb>"
  184. );
  185. # ------------------------------------------------------------------------------
  186. # Bridge "setCmds" => "min. number of parameters"
  187. # ------------------------------------------------------------------------------
  188. my %ESPEasy_setBridgeCmds = (
  189. "user" => "0",
  190. "pass" => "0",
  191. "clearqueue" => "0",
  192. "help" => "1"
  193. );
  194. # ------------------------------------------------------------------------------
  195. # "setBridgeCmds" => "syntax", ESPEasy_paramPos() parse for some <.*> positions
  196. # ------------------------------------------------------------------------------
  197. my %ESPEasy_setBridgeCmdsUsage = (
  198. "user" => "user <username>",
  199. "pass" => "pass <password>",
  200. "clearqueue" => "clearqueue",
  201. "help" => "help <".join("|", sort keys %ESPEasy_setBridgeCmds).">"
  202. );
  203. # ------------------------------------------------------------------------------
  204. # pin names can be used instead of gpio numbers.
  205. # ------------------------------------------------------------------------------
  206. my %ESPEasy_pinMap = (
  207. "D0" => 16,
  208. "D1" => 5,
  209. "D2" => 4,
  210. "D3" => 0,
  211. "D4" => 2,
  212. "D5" => 14,
  213. "D6" => 12,
  214. "D7" => 13,
  215. "D8" => 15,
  216. "D9" => 3,
  217. "D10" => 1,
  218. "RX" => 3,
  219. "TX" => 1,
  220. "SD2" => 9,
  221. "SD3" => 10
  222. );
  223. # ------------------------------------------------------------------------------
  224. # build id
  225. # ------------------------------------------------------------------------------
  226. my %ESPEasy_build_id = (
  227. "1" => { "type" => "ESP Easy", "ver" => "STD" },
  228. "17" => { "type" => "ESP Easy Mega", "ver" => "STD" },
  229. "33" => { "type" => "ESP Easy 32", "ver" => "STD" },
  230. "65" => { "type" => "ARDUINO Easy", "ver" => "STD" },
  231. "81" => { "type" => "NANO Easy", "ver" => "STD" }
  232. );
  233. # ------------------------------------------------------------------------------
  234. #grep ^sub 34_ESPEasy.pm | awk '{print $1" "$2";"}'
  235. # ------------------------------------------------------------------------------
  236. sub ESPEasy_Initialize($)
  237. {
  238. my ($hash) = @_;
  239. #common
  240. $hash->{DefFn} = "ESPEasy_Define";
  241. $hash->{GetFn} = "ESPEasy_Get";
  242. $hash->{SetFn} = "ESPEasy_Set";
  243. $hash->{AttrFn} = "ESPEasy_Attr";
  244. $hash->{UndefFn} = "ESPEasy_Undef";
  245. $hash->{ShutdownFn} = "ESPEasy_Shutdown";
  246. $hash->{DeleteFn} = "ESPEasy_Delete";
  247. $hash->{RenameFn} = "ESPEasy_Rename";
  248. $hash->{NotifyFn} = "ESPEasy_Notify";
  249. #provider
  250. $hash->{ReadFn} = "ESPEasy_Read"; #ESP http request will be parsed here
  251. $hash->{WriteFn} = "ESPEasy_Write"; #called from logical module's IOWrite
  252. $hash->{Clients} = ":ESPEasy:"; #used by dispatch,$hash->{TYPE} of receiver
  253. my %matchList = ( "1:ESPEasy" => ".*" );
  254. $hash->{MatchList} = \%matchList;
  255. #consumer
  256. $hash->{ParseFn} = "ESPEasy_dispatchParse";
  257. $hash->{Match} = ".+";
  258. $hash->{AttrList} = "allowedIPs "
  259. ."authentication:1,0 "
  260. ."autocreate:1,0 "
  261. ."autosave:1,0 "
  262. ."colorpicker:RGB,HSV,HSVp "
  263. ."deniedIPs "
  264. ."disable:1,0 "
  265. ."displayTextEncode:1,0 "
  266. ."displayTextWidth "
  267. ."do_not_notify:0,1 "
  268. ."httpReqTimeout "
  269. ."IODev "
  270. ."Interval "
  271. ."adjustValue "
  272. ."parseCmdResponse "
  273. ."pollGPIOs "
  274. ."presenceCheck:1,0 "
  275. ."readingPrefixGPIO "
  276. ."readingSuffixGPIOState "
  277. ."readingSwitchText:1,0 "
  278. ."setState:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,100 "
  279. ."combineDevices "
  280. ."rgbGPIOs "
  281. ."maxQueueSize:10,25,50,100,250,500,1000,2500,5000,10000,25000,50000,100000 "
  282. ."maxHttpSessions:0,1,2,3,4,5,6,7,8,9 "
  283. ."resendFailedCmd:0,1 "
  284. ."mapLightCmds "
  285. ."colorpickerCTww "
  286. ."colorpickerCTcw "
  287. # ."wwcwGPIOs "
  288. # ."wwcwMaxBri:0,1 "
  289. # ."ctWW_reducedRange "
  290. # ."ctCW_reducedRange "
  291. .$readingFnAttributes;
  292. }
  293. # ------------------------------------------------------------------------------
  294. sub ESPEasy_Define($$) # only called when defined, not on reload.
  295. {
  296. my ($hash, $def) = @_;
  297. my @a = split("[ \t][ \t]*", $def);
  298. my $usg = "\nUse 'define <name> ESPEasy <bridge> <PORT>".
  299. "\nUse 'define <name> ESPEasy <ip|fqdn> <PORT> <IODev> <IDENT>";
  300. return "Wrong syntax: $usg" if(int(@a) < 3);
  301. my $name = $a[0];
  302. my $type = $a[1];
  303. my $host = $a[2];
  304. my $port;
  305. $port = $a[3] if defined $a[3];
  306. $port = 8383 if !defined $port && $host eq "bridge";
  307. my $iodev = $a[4] if defined $a[4];
  308. my $ident = $a[5] if defined $a[5];
  309. my $ipv = $port =~ m/^IPV6:/ ? 6 : 4;
  310. return "ERROR: only 1 ESPEasy bridge can be defined!"
  311. if($host eq "bridge" && $modules{ESPEasy}{defptr}{BRIDGE}{$ipv});
  312. return "ERROR: missing arguments for subtype device: $usg"
  313. if ($host ne "bridge" && !(defined $a[4]) && !(defined $a[5]));
  314. return "ERROR: too much arguments for a bridge: $usg"
  315. if ($host eq "bridge" && defined $a[4]);
  316. (ESPEasy_isIPv4($host) || ESPEasy_isFqdn($host) || $host eq "bridge")
  317. ? $hash->{HOST} = $host
  318. : return "ERROR: invalid IPv4 address, fqdn or keyword bridge: '$host'";
  319. # check fhem.pl version (internalTimer modifications are required)
  320. # https://forum.fhem.de/index.php/topic,55728.msg497094.html#msg497094
  321. AttrVal('global','version','') =~ m/^fhem.pl:(\d+)\/.*$/;
  322. return "ERROR: fhem.pl is too old to use $type module."
  323. ." Version 11000/2016-03-05 is required at least."
  324. if (not(defined $1) || $1 < 11000);
  325. $hash->{PORT} = $port if defined $port;
  326. $hash->{IDENT} = $ident if defined $ident;
  327. $hash->{VERSION} = $module_version;
  328. $hash->{NOTIFYDEV} = "global";
  329. #--- BRIDGE -------------------------------------------------
  330. if ($hash->{HOST} eq "bridge") {
  331. $hash->{SUBTYPE} = "bridge";
  332. $hash->{IPV} = $ipv;
  333. $modules{ESPEasy}{defptr}{BRIDGE}{$ipv} = $hash;
  334. Log3 $hash->{NAME}, 2, "$type $name: Opening bridge port tcp/$port (v$module_version)";
  335. ESPEasy_tcpServerOpen($hash);
  336. if ($init_done && !defined($hash->{OLDDEF})) {
  337. #if (not defined getKeyValue($type."_".$name."-firstrun")) {
  338. CommandAttr(undef,"$name room $type");
  339. CommandAttr(undef,"$name group $type Bridge");
  340. CommandAttr(undef,"$name authentication 0");
  341. CommandAttr(undef,"$name combineDevices 0");
  342. setKeyValue($type."_".$name."-firstrun","done");
  343. }
  344. $hash->{".bau"} = getKeyValue($type."_".$name."-user");
  345. $hash->{".bap"} = getKeyValue($type."_".$name."-pass");
  346. # only informational
  347. $hash->{MAX_HTTP_SESSIONS} = $d_maxHttpSessions;
  348. $hash->{MAX_QUEUE_SIZE} = $d_maxQueueSize;
  349. # Check OS IPv6 support
  350. if ($ipv == 6) {
  351. use constant HAS_AF_INET6 => defined eval { Socket::AF_INET6() };
  352. Log3 $name, 2, "$type $name: WARNING: Your system seems to have no IPv6 support."
  353. if !HAS_AF_INET6;
  354. }
  355. # Check that GIT repository is not activated
  356. ESPEasy_removeGit($hash);
  357. }
  358. #--- DEVICE -------------------------------------------------
  359. else {
  360. $hash->{INTERVAL} = $d_Interval;
  361. $hash->{SUBTYPE} = "device";
  362. AssignIoPort($hash,$iodev) if !defined $hash->{IODev};
  363. InternalTimer(gettimeofday()+5+rand(5), "ESPEasy_statusRequest", $hash);
  364. readingsSingleUpdate($hash, 'state', 'opened',1);
  365. my $io = (defined($hash->{IODev}{NAME})) ? $hash->{IODev}{NAME} : "none";
  366. Log3 $hash->{NAME}, 4, "$type $name: Opened for $ident $host:$port using bridge $io";
  367. }
  368. ESPEasy_loadRequiredModules($hash);
  369. return undef;
  370. }
  371. # ------------------------------------------------------------------------------
  372. sub ESPEasy_Get($@)
  373. {
  374. my ($hash, @a) = @_;
  375. return "argument is missing" if(int(@a) != 2);
  376. my $reading = $a[1];
  377. my $ret;
  378. if ($reading =~ m/^pinmap$/i && $hash->{SUBTYPE} eq "device") {
  379. $ret .= "\nName => GPIO\n";
  380. $ret .= "------------\n";
  381. foreach (sort keys %ESPEasy_pinMap) {
  382. $ret .= $_." " x (5-length $_ ) ."=> $ESPEasy_pinMap{$_}\n";
  383. }
  384. return $ret;
  385. } elsif ($reading =~ m/^user$/i && $hash->{SUBTYPE} eq "bridge") {
  386. return $hash->{".bau"} ? $hash->{".bau"} : "username is not defined, yet.";
  387. } elsif ($reading =~ m/^pass$/i && $hash->{SUBTYPE} eq "bridge") {
  388. return $hash->{".bap"} ? $hash->{".bap"} : "password is not defined, yet.";
  389. } elsif (lc $reading =~ m/^queueSize$/i && $hash->{SUBTYPE} eq "bridge") {
  390. foreach (keys %{ $hash->{helper}{queue} }) {
  391. $ret .= "$_:".scalar @{$hash->{helper}{queue}{"$_"}}." ";
  392. }
  393. return $ret;
  394. } elsif (exists($hash->{READINGS}{$reading})) {
  395. return defined($hash->{READINGS}{$reading})
  396. ? $hash->{READINGS}{$reading}{VAL}
  397. : "reading $reading exists but has no value defined";
  398. } else {
  399. $ret = "unknown argument $reading, choose one of";
  400. foreach my $reading (sort keys %{$hash->{READINGS}}) {
  401. $ret .= " $reading:noArg";
  402. }
  403. return ($hash->{SUBTYPE} eq "bridge")
  404. ? $ret . " user:noArg pass:noArg queueSize:noArg"
  405. : $ret . " pinMap:noArg";
  406. }
  407. }
  408. # ------------------------------------------------------------------------------
  409. sub ESPEasy_Set($$@)
  410. {
  411. my ($hash, $name, $cmd, @params) = @_;
  412. my ($type,$self) = ($hash->{TYPE},ESPEasy_whoami());
  413. $cmd = lc($cmd) if $cmd;
  414. return if (IsDisabled $name);
  415. Log3 $name, 3, "$type: set $name $cmd ".join(" ",@params)
  416. if $cmd !~ m/^(\?|user|pass)$/;
  417. # ----- BRDIGE ----------------------------------------------
  418. if ($hash->{SUBTYPE} eq "bridge") {
  419. # are there all required argumets?
  420. if($ESPEasy_setBridgeCmds{$cmd}
  421. && scalar @params < $ESPEasy_setBridgeCmds{$cmd}) {
  422. Log3 $name, 2, "$type $name: Missing argument: 'set $name $cmd "
  423. .join(" ",@params)."'";
  424. return "Missing argument: $cmd needs at least "
  425. ."$ESPEasy_setBridgeCmds{$cmd} parameter(s)\n"
  426. ."Usage: 'set $name $ESPEasy_setBridgeCmdsUsage{$cmd}'";
  427. }
  428. # handle unknown cmds
  429. if(!exists $ESPEasy_setBridgeCmds{$cmd}) {
  430. my @cList = sort keys %ESPEasy_setBridgeCmds;
  431. my $clist = join(" ", @cList);
  432. my $hlist = join(",", @cList);
  433. $clist =~ s/help/help:$hlist/; # add all cmds as params to help cmd
  434. return "Unknown argument $cmd, choose one of ". $clist;
  435. }
  436. if ($cmd eq "help") {
  437. my $usage = $ESPEasy_setBridgeCmdsUsage{$params[0]};
  438. $usage =~ s/Note:/\nNote:/g;
  439. return "Usage: set $name $usage";
  440. }
  441. elsif ($cmd =~ m/^clearqueue$/i) {
  442. delete $hash->{helper}{queue};
  443. Log3 $name, 3, "$type $name: Queues erased.";
  444. return undef;
  445. }
  446. elsif ($cmd =~ m/^user|pass$/ ) {
  447. setKeyValue($hash->{TYPE}."_".$hash->{NAME}."-".$cmd,$params[0]);
  448. $cmd eq "user" ? $hash->{".bau"} = $params[0] : $hash->{".bap"} = $params[0];
  449. }
  450. }
  451. # ----- DEVICE ----------------------------------------------
  452. else {
  453. # cmds are included in hash
  454. ESPEasy_adjustSetCmds($hash);
  455. # are there all required argumets?
  456. if($ESPEasy_setCmds{$cmd} && scalar @params < $ESPEasy_setCmds{$cmd}) {
  457. Log3 $name, 2, "$type $name: Missing argument: "
  458. ."'set $name $cmd ".join(" ",@params)."'";
  459. return "Missing argument: $cmd needs at least $ESPEasy_setCmds{$cmd} ".
  460. "parameter(s)\n"."Usage: 'set $name $ESPEasy_setCmdsUsage{$cmd}'";
  461. }
  462. #Lights Plugin
  463. my $lightCmds = join("|",@ESPEasy_maplightCmds);
  464. if (defined AttrVal($name,"mapLightCmds",undef) && $cmd =~ m/^$lightCmds$/i) {
  465. unshift @params, $cmd;
  466. $cmd = lc AttrVal($name,"mapLightCmds","");
  467. }
  468. else {
  469. # enable ct|pct commands if attr wwcwGPIOs is set
  470. if (AttrVal($name,"wwcwGPIOs",0) && $cmd =~ m/^(ct|pct)$/i) {
  471. my $ret = ESPEasy_setCT($hash,$cmd,@params);
  472. return $ret if ($ret);
  473. }
  474. # enable rgb commands if attr rgbGPIOs is set
  475. if (AttrVal($name,"rgbGPIOs",0) && $cmd =~ m/^(rgb|on|off|toggle)$/i) {
  476. my $ret = ESPEasy_setRGB($hash,$cmd,@params);
  477. return $ret if ($ret);
  478. }
  479. } #else
  480. # handle unknown cmds
  481. if (!exists $ESPEasy_setCmds{$cmd}) {
  482. my @cList = sort keys %ESPEasy_setCmds;
  483. my $clist = join(" ", @cList);
  484. my $hlist = join(",", @cList);
  485. foreach (@cList) {$clist =~ s/ $_/ $_:noArg/ if $ESPEasy_setCmds{$_} == 0}
  486. # expand ct
  487. my $ct = "ct:colorpicker,CT,"
  488. .AttrVal($name,"ctWW_reducedRange",AttrVal($name,"colorpickerCTww",$d_colorpickerCTww))
  489. .",10,"
  490. .AttrVal($name,"ctCW_reducedRange",AttrVal($name,"colorpickerCTcw",$d_colorpickerCTcw));
  491. $clist =~ s/ct /$ct /;
  492. # expand colorpicker
  493. my $cp = AttrVal($name,"colorpicker","HSVp");
  494. my $cpRepl = "rgb|bgcolor|fade|all|comet|twinkle|twinklefade|sparkle|theatre|scan|dualscan|wipe";
  495. $clist =~ s/(($cpRepl)\s)/$2:colorpicker,$cp /g; # add colorPicker if rgb cmd is available
  496. # expand pct colorpicker widget (-100)
  497. my $pct = "pct:colorpicker,BRI,0,1,100";
  498. $clist =~ s/pct /$pct /;
  499. # expand dim colorpicker widget (-255)
  500. my $dim = "dim:colorpicker,BRI,0,1,255";
  501. $clist =~ s/dim /$dim /;
  502. # slider for some values...
  503. my $fadetime = "fadetime:slider,0,100,10000";
  504. $clist =~ s/fadetime /$fadetime /;
  505. my $fadedelay = "fadedelay:slider,-5000,10,5000";
  506. $clist =~ s/fadedelay /$fadedelay /;
  507. my $speed = "speed:slider,-50,1,50";
  508. $clist =~ s/speed /$speed /;
  509. my $count = "count:slider,1,1,50";
  510. $clist =~ s/count /$count /;
  511. my $rainbow = "rainbow:slider,-10,1,10";
  512. $clist =~ s/rainbow/$rainbow/;
  513. # expand help
  514. $clist =~ s/help/help:$hlist/;
  515. Log3 $name, 2, "$type $name: Unknown set command $cmd" if $cmd ne "?";
  516. return "Unknown argument $cmd, choose one of ". $clist;
  517. }
  518. # urlEncode <text> parameter
  519. @params = ESPEasy_urlEncodeDisplayText($hash,$cmd,@params);
  520. # pin mapping (eg. D8 -> 15)
  521. my $pp = ESPEasy_paramPos($cmd,'<pin>');
  522. if ($pp && $params[$pp-1] =~ m/^[a-zA-Z]/) {
  523. Log3 $name, 5, "$type $name: Pin mapping ". uc $params[$pp-1] .
  524. " => $ESPEasy_pinMap{uc $params[$pp-1]}";
  525. $params[$pp-1] = $ESPEasy_pinMap{uc $params[$pp-1]};
  526. }
  527. # onOff mapping (on/off -> 1/0)
  528. $pp = ESPEasy_paramPos($cmd,'<0|1|off|on>');
  529. if ($pp && not($params[$pp-1] =~ m/^(0|1)$/)) {
  530. my $state;
  531. if ($params[$pp-1] =~ m/^off$/i) {
  532. $state = 0;
  533. }
  534. elsif ($params[$pp-1] =~ m/^on$/i) {
  535. $state = 1;
  536. }
  537. else {
  538. Log3 $name, 2, "$type $name: $cmd ".join(" ",@params)." => unknown argument: '$params[$pp-1]'";
  539. return undef;
  540. }
  541. Log3 $name, 5, "$type $name: onOff mapping ". $params[$pp-1]." => $state";
  542. $params[$pp-1] = $state;
  543. }
  544. if ($cmd eq "help") {
  545. my $usage = $ESPEasy_setCmdsUsage{$params[0]};
  546. $usage =~ s/Note:/\nNote:/g;
  547. return "Usage: set $name $usage";
  548. }
  549. if ($cmd eq "statusrequest") {
  550. ESPEasy_statusRequest($hash);
  551. return undef;
  552. }
  553. if ($cmd eq "clearreadings") {
  554. ESPEasy_clearReadings($hash);
  555. return undef;
  556. }
  557. Log3 $name, 5, "$type $name: IOWrite(\$defs{$hash->{NAME}}, $hash->{HOST}, $hash->{PORT}, ".
  558. "$hash->{IDENT}, $cmd, ".join(",",@params).")";
  559. Log3 $name, 2, "$type $name: Device seems to be in sleep mode, sending command nevertheless."
  560. if (defined $hash->{SLEEP} && $hash->{SLEEP} ne "0");
  561. my $parseCmd = ESPEasy_isParseCmd($hash,$cmd); # should response be parsed and dispatched
  562. IOWrite($hash, $hash->{HOST}, $hash->{PORT}, $hash->{IDENT}, $parseCmd, $cmd, @params);
  563. } # DEVICE
  564. return undef
  565. }
  566. # ------------------------------------------------------------------------------
  567. sub ESPEasy_Read($) {
  568. my ($hash) = @_; #hash of temporary child instance
  569. my $name = $hash->{NAME};
  570. my $ipv = $hash->{IPV} ? $hash->{IPV} : ($hash->{PEER} =~ m/:/ ? 6 : 4);
  571. my $bhash = $modules{ESPEasy}{defptr}{BRIDGE}{$ipv}; #hash of original instance
  572. my $bname = $bhash->{NAME};
  573. my $btype = $bhash->{TYPE};
  574. # Levering new TcpServerUtils security feature.
  575. #$attr{$name}{allowfrom} = ".*" if !$attr{$name}{allowfrom};
  576. # Accept and create a child
  577. if( $hash->{SERVERSOCKET} ) {
  578. my $aRet = ESPEasy_TcpServer_Accept($hash,"ESPEasy");
  579. return;
  580. }
  581. # use received IP instead of configured one (NAT/PAT could have modified)
  582. my $peer = $hash->{PEER};
  583. # Read 1024 byte of data
  584. my $buf;
  585. my $ret = sysread($hash->{CD}, $buf, 1024);
  586. # If there is an error in connection return
  587. if( !defined($ret ) || $ret <= 0 ) {
  588. CommandDelete( undef, $hash->{NAME} );
  589. return;
  590. }
  591. #$bhash->{SESSIONS} = scalar devspec2array("TYPE=$btype:FILTER=TEMPORARY=1")-1;
  592. # Check attr disabled
  593. return if (IsDisabled $bname);
  594. # Check allowed IPs
  595. if ( !( ESPEasy_isPeerAllowed($peer,AttrVal($bname,"allowedIPs", $d_allowedIPs)) &&
  596. !ESPEasy_isPeerAllowed($peer,AttrVal($bname,"deniedIPs",0)) ) ) {
  597. Log3 $bname, 2, "$btype $name: Peer address rejected";
  598. return;
  599. }
  600. Log3 $bname, 4, "$btype $name: Peer address accepted";
  601. my @data = split( '\R\R', $buf );
  602. my $header = ESPEasy_header2Hash($data[0]);
  603. # mask password in authorization header with ****
  604. my $logHeader = { %$header };
  605. # public IPs
  606. if (!defined $logHeader->{Authorization} && $peer !~ m/$d_localIPs/) {
  607. Log3 $bname, 2, "$btype $name: No basic auth set while using public IP "
  608. . "address. $peer rejected.";
  609. return;
  610. }
  611. $logHeader->{Authorization} =~ s/Basic\s.*\s/Basic ***** / if defined $logHeader->{Authorization};
  612. # Dump logHeader
  613. Log3 $bname, 5, "$btype $name: Received header: ".ESPEasy_dumpSingleLine($logHeader)
  614. if (defined $logHeader);
  615. # Dump content
  616. Log3 $bname, 5, "$btype $name: Received content: $data[1]" if defined $data[1];
  617. # Check content length if defined
  618. if (defined $header->{'Content-Length'}
  619. && $header->{'Content-Length'} != length($data[1])) {
  620. Log3 $bname, 2, "$btype $name: Invalid content length ".
  621. "($header->{'Content-Length'} != ".length($data[1]).")";
  622. Log3 $bname, 2, "$btype $name: Received content: $data[1]"
  623. if defined $data[1];
  624. ESPEasy_sendHttpClose($hash,"400 Bad Request","");
  625. return;
  626. }
  627. # check authorization
  628. if (!defined ESPEasy_isAuthenticated($hash,$header->{Authorization})) {
  629. ESPEasy_sendHttpClose($hash,"401 Unauthorized","");
  630. return;
  631. }
  632. # No error occurred, send http respose OK to ESP
  633. ESPEasy_sendHttpClose($hash,"200 OK",""); #if !grep(/"sleep":1/, $data[1]);
  634. # JSON received...
  635. my $json;
  636. if (defined $data[1] && $data[1] =~ m/"module":"ESPEasy"/) {
  637. # perl module JSON not installed
  638. if ( !$bhash->{helper}{pm}{JSON} ) {
  639. Log3 $bname, 2, "$btype $bname: Perl module 'JSON' is not installed. Can't process received data from $peer.";
  640. return;
  641. }
  642. # use encode_utf8 if available else replace any disturbing chars
  643. $bhash->{helper}{pm}{Encode}
  644. ? ( eval { $json = decode_json( encode_utf8($data[1]) ); 1; } )
  645. : ( eval { $json = decode_json( $data[1] =~ s/[^\x20-\x7E]/_/gr ); 1; } );
  646. if ($@) {
  647. Log3 $bname, 2, "$btype $name: WARNING: Invalid JSON received. "
  648. . "Check your ESP configuration ($peer).\n$@";
  649. return;
  650. }
  651. # check that ESPEasy software is new enough
  652. return if ESPEasy_checkVersion($bhash,$peer,$json->{data}{ESP}{build},$json->{version});
  653. # should never happen, but who knows what some JSON module versions do...
  654. $json->{data}{ESP}{name} = "" if !defined $json->{data}{ESP}{name};
  655. $json->{data}{SENSOR}{0}{deviceName} = "" if !defined $json->{data}{SENSOR}{0}{deviceName};
  656. # remove illegal chars from ESP name for further processing and assign to new var
  657. (my $espName = $json->{data}{ESP}{name}) =~ s/[^A-Za-z\d_\.]/_/g;
  658. (my $espDevName = $json->{data}{SENSOR}{0}{deviceName}) =~ s/[^A-Za-z\d_\.]/_/g;
  659. # check that 'ESP name' or 'device name' is set
  660. if ($espName eq "" && $espDevName eq "") {
  661. Log3 $bname, 2, "$btype $name: WARNIING 'ESP name' and 'device name' "
  662. ."missing ($peer). Check your ESP config. Skip processing data.";
  663. Log3 $bname, 2, "$btype $name: Data: $data[1]";
  664. return;
  665. }
  666. my $ident = ESPEasy_isCombineDevices($peer,$espName,AttrVal($bname,"combineDevices",0))
  667. ? $espName ne "" ? $espName : $peer
  668. : $espName.($espName ne "" && $espDevName ne "" ? "_" : "").$espDevName;
  669. # push internals in @values
  670. my @values;
  671. my @intVals = qw(unit sleep build build_git build_notes version node_type_id);
  672. foreach my $intVal (@intVals) {
  673. next if !defined $json->{data}{ESP}{$intVal} || $json->{data}{ESP}{$intVal} eq "";
  674. push(@values,"i||".$intVal."||".$json->{data}{ESP}{$intVal}."||0");
  675. }
  676. # push sensor value in @values
  677. foreach my $vKey (keys %{$json->{data}{SENSOR}}) {
  678. if(ref $json->{data}{SENSOR}{$vKey} eq ref {}
  679. && exists $json->{data}{SENSOR}{$vKey}{value}) {
  680. # remove illegal chars
  681. $json->{data}{SENSOR}{$vKey}{valueName} =~ s/[^A-Za-z\d_\.\-\/]/_/g;
  682. my $dmsg = "r||".$json->{data}{SENSOR}{$vKey}{valueName}
  683. ."||".$json->{data}{SENSOR}{$vKey}{value}
  684. ."||".$json->{data}{SENSOR}{$vKey}{type};
  685. if ($dmsg =~ m/(\|\|\|\|)|(\|\|$)/) { #detect an empty value
  686. Log3 $bname, 2, "$btype $name: WARNING: value name or value is "
  687. ."missing ($peer). Skip processing this value.";
  688. Log3 $bname, 2, "$btype $name: Data: $data[1]";
  689. next; #skip further processing for this value only
  690. }
  691. push(@values,$dmsg);
  692. }
  693. }
  694. ESPEasy_dispatch($hash,$ident,$peer,@values);
  695. } #$data[1] =~ m/"module":"ESPEasy"/
  696. else {
  697. Log3 $bname, 2, "$btype $name: WARNING: Wrong controller configured or "
  698. ."ESPEasy Version is too old.";
  699. Log3 $bname, 2, "$btype $name: WARNING: ESPEasy version R"
  700. .$minEEBuild." or later required.";
  701. }
  702. # session will not be close immediately if ESP goes to sleep after http send
  703. # needs further investigation?
  704. if ($hash->{TEMPORARY} && $json->{data}{ESP}{sleep}) {
  705. CommandDelete(undef, $name);
  706. }
  707. return;
  708. }
  709. # ------------------------------------------------------------------------------
  710. sub ESPEasy_Write($$$$$@) #called from logical's IOWrite (end of SetFn)
  711. {
  712. my ($hash,$ip,$port,$ident,$parseCmd,$cmd,@params) = @_;
  713. my ($name,$type,$self) = ($hash->{NAME},$hash->{TYPE},ESPEasy_whoami()."()");
  714. if ($cmd eq "cleanup") {
  715. delete $hash->{helper}{received};
  716. return undef;
  717. }
  718. elsif ($cmd eq "statusrequest") {
  719. ESPEasy_statusRequest($hash);
  720. return undef;
  721. }
  722. ESPEasy_httpReq($hash, $ip, $port, $ident, $parseCmd, $cmd, @params);
  723. }
  724. # ------------------------------------------------------------------------------
  725. sub ESPEasy_Notify($$)
  726. {
  727. my ($hash,$dev) = @_;
  728. my $name = $hash->{NAME};
  729. my $type = $hash->{TYPE};
  730. # $hash->{NOTIFYDEV} = "global" set in DefineFn
  731. return if(!grep(m/^(DELETE)?ATTR $name /, @{$dev->{CHANGED}}));
  732. foreach (@{$dev->{CHANGED}}) {
  733. if (m/^(DELETE)?ATTR ($name) (\w+)\s?(\d+)?$/) {
  734. Log3 $name, 5, "$type $name: received event: $_";
  735. if ($3 eq "disable") {
  736. if (defined $1 || (defined $4 && $4 eq "0")) {
  737. Log3 $name, 4,"$type $name: Device enabled";
  738. ESPEasy_resetTimer($hash) if ($hash->{SUBTYPE} eq "device");
  739. readingsSingleUpdate($hash, 'state', 'opened',1);
  740. }
  741. else {
  742. Log3 $name, 3,"$type $name: Device disabled";
  743. ESPEasy_clearReadings($hash) if $hash->{SUBTYPE} eq "device";
  744. ESPEasy_resetTimer($hash,"stop");
  745. readingsSingleUpdate($hash, "state", "disabled",1)
  746. }
  747. }
  748. elsif ($3 eq "Interval") {
  749. if (defined $1) {
  750. $hash->{INTERVAL} = $d_Interval;
  751. }
  752. elsif (defined $4 && $4 eq "0") {
  753. $hash->{INTERVAL} = "disabled";
  754. ESPEasy_resetTimer($hash,"stop");
  755. CommandDeleteReading(undef, "$name presence")
  756. if defined $hash->{READINGS}{presence};
  757. }
  758. else { # Interval > 0
  759. $hash->{INTERVAL} = $4;
  760. ESPEasy_resetTimer($hash);
  761. }
  762. }
  763. elsif ($3 eq "setState") {
  764. if (defined $1 || (defined $4 && $4 > 0)) {
  765. ESPEasy_setState($hash);
  766. }
  767. else { #setState == 0
  768. CommandSetReading(undef,"$name state opened");
  769. }
  770. }
  771. else {
  772. #Log 5, "$type $name: Attribute $3 not handeled by NotifyFn ";
  773. }
  774. } #main if
  775. else { #should never be reached
  776. #Log 5, "$type $name: WARNING: unexpected event received by NotifyFn: $_";
  777. }
  778. }
  779. return undef;
  780. }
  781. # ------------------------------------------------------------------------------
  782. sub ESPEasy_Rename() {
  783. my ($new,$old) = @_;
  784. my $i = 0;
  785. my $type = $defs{"$new"}->{TYPE};
  786. my $name = $defs{"$new"}->{NAME};
  787. my $subtype = $defs{"$new"}->{SUBTYPE};
  788. my @am;
  789. # copy values from old to new device
  790. setKeyValue($type."_".$new."-user",getKeyValue($type."_".$old."-user"));
  791. setKeyValue($type."_".$new."-pass",getKeyValue($type."_".$old."-pass"));
  792. setKeyValue($type."_".$new."-firstrun",getKeyValue($type."_".$old."-firstrun"));
  793. # delete old entries
  794. setKeyValue($type."_".$old."-user",undef);
  795. setKeyValue($type."_".$old."-pass",undef);
  796. setKeyValue($type."_".$old."-firstrun",undef);
  797. # replace IDENT in devices if bridge name changed
  798. if ($subtype eq "bridge") {
  799. foreach my $ldev (devspec2array("TYPE=$type")) {
  800. my $dhash = $defs{$ldev};
  801. my $dsubtype = $dhash->{SUBTYPE};
  802. next if ($dsubtype eq "bridge");
  803. my $dname = $dhash->{NAME};
  804. my $ddef = $dhash->{DEF};
  805. my $oddef = $dhash->{DEF};
  806. $ddef =~ s/ $old / $new /;
  807. if ($oddef ne $ddef){
  808. $i = $i+2;
  809. CommandModify(undef, "$dname $ddef");
  810. CommandAttr(undef,"$dname IODev $new");
  811. push (@am,$dname);
  812. }
  813. }
  814. }
  815. Log3 $name, 2, "$type $name: Device $old renamed to $new";
  816. Log3 $name, 2, "$type $name: Attribute IODev set to '$name' in these "
  817. ."devices: ".join(", ",@am) if $subtype eq "bridge";
  818. if (AttrVal($name,"autosave",AttrVal("global","autosave",1)) && $i>0) {
  819. CommandSave(undef,undef);
  820. Log3 $type, 2, "$type $name: $i structural changes saved "
  821. ."(autosave is enabled)";
  822. }
  823. elsif ($i>0) {
  824. Log3 $type, 2, "$type $name: There are $i structural changes. "
  825. ."Don't forget to save chages.";
  826. }
  827. return undef;
  828. }
  829. # ------------------------------------------------------------------------------
  830. sub ESPEasy_Attr(@)
  831. {
  832. my ($cmd,$name,$aName,$aVal) = @_;
  833. my $hash = $defs{$name};
  834. my $type = $hash->{TYPE};
  835. my $ret;
  836. if ($cmd eq "set" && !defined $aVal) {
  837. Log3 $name, 2, "$type $name: attr $name $aName '': value must not be empty";
  838. return "$name: attr $aName: value must not be empty";
  839. }
  840. # device attributes
  841. if (defined $hash->{SUBTYPE} && $hash->{SUBTYPE} eq "bridge"
  842. && ($aName =~ m/^(Interval|pollGPIOs|IODev|setState|readingSwitchText)$/
  843. || $aName =~ m/^(readingPrefixGPIO|readingSuffixGPIOState|adjustValue)$/
  844. || $aName =~ m/^(displayTextEncode|displayTextWidth)$/
  845. || $aName =~ m/^(presenceCheck|parseCmdResponse|rgbGPIOs|colorpicker)$/
  846. || $aName =~ m/^(wwcwGPIOs|colorpickerCTww|colorpickerCTcw|mapLightCmds)$/)) {
  847. Log3 $name, 2, "$type $name: Attribut '$aName' can not be used by bridge";
  848. return "$type: attribut '$aName' cannot be used by bridge device";
  849. }
  850. # bridge attributes
  851. elsif (defined $hash->{SUBTYPE} && $hash->{SUBTYPE} eq "device"
  852. && ($aName =~ m/^(autocreate|autosave|authentication|httpReqTimeout)$/
  853. || $aName =~ m/^(maxHttpSessions|maxQueueSize|resendFailedCmd)$/
  854. || $aName =~ m/^(allowedIPs|deniedIPs|combineDevices)$/ )) {
  855. Log3 $name, 2, "$type $name: Attribut '$aName' can be used with "
  856. ."bridge device, only";
  857. return "$type: attribut '$aName' can be used with the bridge device, only";
  858. }
  859. elsif ($aName =~ m/^(autosave|autocreate|authentication|disable)$/
  860. || $aName =~ m/^(presenceCheck|readingSwitchText|resendFailedCmd)$/
  861. || $aName =~ m/^(displayTextEncode)$/) {
  862. $ret = "0,1" if ($cmd eq "set" && not $aVal =~ m/^(0|1)$/)}
  863. elsif ($aName eq "combineDevices") {
  864. $ret = "0 | 1 | ESPname | ip[/netmask][,ip[/netmask]][,...]"
  865. if $cmd eq "set" && !(ESPEasy_isAttrCombineDevices($aVal) || $aVal =~ m/^[01]$/ )}
  866. elsif ($aName =~ m/^(allowedIPs|deniedIPs)$/) {
  867. $ret = "[comma separated list of] ip[/netmask] or a regexp"
  868. if $cmd eq "set" && !ESPEasy_isIPv64Range($aVal,"regexp")}
  869. elsif ($aName =~ m/^(pollGPIOs|rgbGPIOs|wwcwGPIOs)$/) {
  870. $ret = "GPIO_No[,GPIO_No][...]"
  871. if $cmd eq "set" && $aVal !~ m/^[a-zA-Z]{0,2}[0-9]+(,[a-zA-Z]{0,2}[0-9]+)*$/}
  872. elsif ($aName eq "colorpicker") {
  873. $ret = "RGB | HSV | HSVp"
  874. if ($cmd eq "set" && not $aVal =~ m/^(RGB|HSV|HSVp)$/)}
  875. elsif ($aName =~ m/^(colorpickerCTww|colorpickerCTcw)$/) {
  876. $ret = "1000..10000"
  877. if $cmd eq "set" && ($aVal < 1000 || $aVal > 10000)}
  878. elsif ($aName eq "parseCmdResponse") {
  879. my $cmds = lc join("|",keys %ESPEasy_setCmdsUsage);
  880. $ret = "cmd[,cmd][...]"
  881. if $cmd eq "set" && lc($aVal) !~ m/^($cmds){1}(,($cmds))*$/}
  882. elsif ($aName eq "mapLightCmds") {
  883. my $cmds = lc join("|",keys %ESPEasy_setCmdsUsage);
  884. $ret = "ESPEasy cmd"
  885. if $cmd eq "set" && lc($aVal) !~ m/^($cmds){1}(,($cmds))*$/}
  886. elsif ($aName eq "setState") {
  887. $ret = "integer"
  888. if ($cmd eq "set" && not $aVal =~ m/^(\d+)$/)}
  889. elsif ($aName eq "displayTextWidth") {
  890. $ret = "number of charaters per line"
  891. if ($cmd eq "set" && not $aVal =~ m/^(\d+)$/)}
  892. elsif ($aName eq "readingPrefixGPIO") {
  893. $ret = "[a-zA-Z0-9._-/]+"
  894. if ($cmd eq "set" && $aVal !~ m/^[A-Za-z\d_\.\-\/]+$/)}
  895. elsif ($aName eq "readingSuffixGPIOState") {
  896. $ret = "[a-zA-Z0-9._-/]+"
  897. if ($cmd eq "set" && $aVal !~ m/^[A-Za-z\d_\.\-\/]+$/)}
  898. elsif ($aName eq "httpReqTimeout") {
  899. $ret = "3..60 (default: $d_httpReqTimeout)"
  900. if $cmd eq "set" && ($aVal < 3 || $aVal > 60)}
  901. elsif ($aName eq "maxHttpSessions") {
  902. ($cmd eq "set" && ($aVal !~ m/^[0-9]+$/))
  903. ? ($ret = ">= 0 (default: $d_maxHttpSessions, 0: disable queuing)")
  904. : ($hash->{MAX_HTTP_SESSIONS} = $aVal);
  905. if ($cmd eq "del") {$hash->{MAX_HTTP_SESSIONS} = $d_maxHttpSessions}
  906. }
  907. elsif ($aName eq "maxQueueSize") {
  908. ($cmd eq "set" && ($aVal !~ m/^[1-9][0-9]+$/))
  909. ? ($ret = ">=10 (default: $d_maxQueueSize)")
  910. : ($hash->{MAX_QUEUE_SIZE} = $aVal);
  911. if ($cmd eq "del") {$hash->{MAX_QUEUE_SIZE} = $d_maxQueueSize}
  912. }
  913. elsif ($aName eq "Interval") {
  914. ($cmd eq "set" && ($aVal !~ m/^(\d)+$/ || $aVal <10 && $aVal !=0))
  915. ? ($ret = "0 or >=10")
  916. : ($hash->{INTERVAL} = $aVal)
  917. }
  918. if (!$init_done) {
  919. if ($aName =~ /^disable$/ && $aVal == 1) {
  920. readingsSingleUpdate($hash, "state", "disabled",1);
  921. }
  922. }
  923. if (defined $ret) {
  924. Log3 $name, 2, "$type $name: attr $name $aName '$aVal' != '$ret'";
  925. return "$name: $aName must be: $ret";
  926. }
  927. return undef;
  928. }
  929. # ------------------------------------------------------------------------------
  930. #UndefFn: called while deleting device (delete-command) or while rereadcfg
  931. sub ESPEasy_Undef($$)
  932. {
  933. my ($hash, $arg) = @_;
  934. my ($name,$type,$port) = ($hash->{NAME},$hash->{TYPE},$hash->{PORT});
  935. # close server and return if it is a child process for incoming http requests
  936. if (defined $hash->{TEMPORARY} && $hash->{TEMPORARY} == 1) {
  937. my $ipv = $hash->{PEER} =~ m/:/ ? 6 : 4;
  938. my $bhash = $modules{ESPEasy}{defptr}{BRIDGE}{$ipv};
  939. Log3 $bhash->{NAME}, 4, "$type $name: Closing tcp session.";
  940. TcpServer_Close($hash);
  941. return undef
  942. };
  943. HttpUtils_Close($hash);
  944. RemoveInternalTimer($hash);
  945. if($hash->{SUBTYPE} && $hash->{SUBTYPE} eq "bridge") {
  946. my $ipv = $hash->{IPV};
  947. delete $modules{ESPEasy}{defptr}{BRIDGE}{$ipv}
  948. if(defined($modules{ESPEasy}{defptr}{BRIDGE}{$ipv}));
  949. TcpServer_Close( $hash );
  950. Log3 $name, 2, "$type $name: Socket on port tcp/$port closed";
  951. }
  952. else {
  953. IOWrite($hash, $hash->{HOST}, undef, undef, undef, "cleanup", undef );
  954. }
  955. return undef;
  956. }
  957. # ------------------------------------------------------------------------------
  958. #ShutdownFn: called before fhem's shutdown command
  959. sub ESPEasy_Shutdown($)
  960. {
  961. my ($hash) = @_;
  962. HttpUtils_Close($hash);
  963. Log3 $hash->{NAME}, 4, "$hash->{TYPE} $hash->{NAME}: Shutdown requested";
  964. return undef;
  965. }
  966. # ------------------------------------------------------------------------------
  967. #DeleteFn: called while deleting device (delete-command) but after UndefFn
  968. sub ESPEasy_Delete($$)
  969. {
  970. my ($hash, $arg) = @_;
  971. #return if it is a child process for incoming http requests
  972. if (not defined $hash->{TEMPORARY}) {
  973. setKeyValue($hash->{TYPE}."_".$hash->{NAME}."-user",undef);
  974. setKeyValue($hash->{TYPE}."_".$hash->{NAME}."-pass",undef);
  975. setKeyValue($hash->{TYPE}."_".$hash->{NAME}."-firstrun",undef);
  976. Log3 $hash->{NAME}, 4, "$hash->{TYPE} $hash->{NAME}: $hash->{NAME} deleted";
  977. }
  978. return undef;
  979. }
  980. # ------------------------------------------------------------------------------
  981. sub ESPEasy_dispatch($$$@) #called by bridge -> send to logical devices
  982. {
  983. my($hash,$ident,$host,@values) = @_;
  984. my $name = $hash->{NAME};
  985. return if (IsDisabled $name);
  986. my $type = $hash->{TYPE};
  987. my $ipv = $host =~ m/:/ ? 6 : 4;
  988. my $bhash = $modules{ESPEasy}{defptr}{BRIDGE}{$ipv};
  989. my $bname = $bhash->{NAME};
  990. my $ui = 1; #can be removed later
  991. my $as = (AttrVal($bname,"autosave",AttrVal("global","autosave",1))) ? 1 : 0;
  992. my $ac = (AttrVal($bname,"autocreate",AttrVal("global","autoload_undefined_devices",1))) ? 1 : 0;
  993. my $msg = $ident."::".$host."::".$ac."::".$as."::".$ui."::".join("|||",@values);
  994. Log3 $bname, 5, "$type $name: Dispatch: $msg";
  995. Dispatch($bhash, $msg, undef);
  996. return undef;
  997. }
  998. # ------------------------------------------------------------------------------
  999. sub ESPEasy_dispatchParse($$$) # called by logical device (defined by
  1000. { # $hash->{ParseFn})
  1001. # we are called from dispatch() from the ESPEasy bridge device
  1002. # we never come here if $msg does not match $hash->{MATCH} in the first place
  1003. my ($IOhash, $msg) = @_; # IOhash points to the ESPEasy bridge, not device
  1004. my $IOname = $IOhash->{NAME};
  1005. my $type = $IOhash->{TYPE};
  1006. # 1:ident 2:ip 3:autocreate 4:autosave 5:uniqIDs 6:value(s)
  1007. my ($ident,$ip,$ac,$as,$ui,$v) = split("::",$msg);
  1008. # return undef if !$ident || $ident eq "";
  1009. return "" if !$ident || $ident eq "";
  1010. my $name;
  1011. my @v = split("\\|\\|\\|",$v);
  1012. # look in each $defs{$d}{IDENT} for $ident to get device name.
  1013. foreach my $d (keys %defs) {
  1014. next if($defs{$d}{TYPE} ne "ESPEasy");
  1015. if (InternalVal($defs{$d}{NAME},"IDENT","") eq "$ident") {
  1016. $name = $defs{$d}{NAME} ;
  1017. last;
  1018. }
  1019. }
  1020. # autocreate device if no device has $ident asigned.
  1021. if (!($name) && $ac eq "1") {
  1022. $name = ESPEasy_autocreate($IOhash,$ident,$ip,$as);
  1023. # cleanup helper
  1024. delete $IOhash->{helper}{autocreate}{$ident}
  1025. if defined $IOhash->{helper}{autocreate}{$ident};
  1026. delete $IOhash->{helper}{autocreate}
  1027. if scalar keys %{$IOhash->{helper}{autocreate}} == 0;
  1028. }
  1029. # autocreate is disabled
  1030. elsif (!($name) && $ac eq "0") {
  1031. Log3 $IOname, 2, "$type $IOname: autocreate is disabled (ident: $ident)"
  1032. if not defined $IOhash->{helper}{autocreate}{$ident};
  1033. $IOhash->{helper}{autocreate}{$ident} = "disabled";
  1034. return $ident;
  1035. }
  1036. return $name if (IsDisabled $name);
  1037. my $hash = $defs{$name};
  1038. Log3 $name, 5, "$type $name: Received: $msg";
  1039. if (defined $hash && $hash->{TYPE} eq "ESPEasy" && $hash->{SUBTYPE} eq "device") {
  1040. my @logInternals;
  1041. foreach (@v) {
  1042. my ($cmd,$reading,$value,$vType) = split("\\|\\|",$_);
  1043. # reading prefix replacement (useful if we poll values)
  1044. my $replace = '"'.AttrVal($name,"readingPrefixGPIO","GPIO").'"';
  1045. $reading =~ s/^GPIO/$replace/ee;
  1046. # --- setReading ----------------------------------------------
  1047. if ($cmd eq "r") {
  1048. # reading suffix replacement only for setreading
  1049. $replace = '"'.AttrVal($name,"readingSuffixGPIOState","").'"';
  1050. $reading =~ s/_state$/$replace/ee;
  1051. # map value to on/off if device is a switch
  1052. $value = ($value eq "1") ? "on" : "off"
  1053. if ($vType == 10 && AttrVal($name,"readingSwitchText",1) && !AttrVal($name,"rgbGPIOs",0)
  1054. && $value =~ /^(0|1)$/);
  1055. # delete ignored reading and helper
  1056. if (defined ReadingsVal($name,".ignored_$reading",undef)) {
  1057. delete $hash->{READINGS}{".ignored_$reading"};
  1058. delete $hash->{helper}{received}{".ignored_$reading"};
  1059. }
  1060. # delete warning if there is any (send from httpRequestParse before)
  1061. if (exists ($hash->{"WARNING"})) {
  1062. if (defined $hash->{"WARNING"}) {
  1063. Log3 $name, 2, "$type $name: RESOLVED: ".$hash->{"WARNING"};
  1064. }
  1065. delete $hash->{"WARNING"};
  1066. }
  1067. # attr adjustValue
  1068. my $orgVal = $value;
  1069. $value = ESPEasy_adjustValue($hash,$reading,$value);
  1070. if (!defined $value) {
  1071. Log3 $name, 4, "$type $name: $reading: $orgVal [ignored]";
  1072. $reading = ".ignored_$reading";
  1073. $value = $orgVal;
  1074. }
  1075. readingsSingleUpdate($hash, $reading, $value, 1);
  1076. my $adj = ($orgVal ne $value) ? " [adjusted]" : "";
  1077. Log3 $name, 4, "$type $name: $reading: $value".$adj
  1078. if defined $value && $reading !~ m/^\./; #no leading dot
  1079. # used for presence detection
  1080. $hash->{helper}{received}{$reading} = time();
  1081. # recalc RGB reading if a PWM channel has changed
  1082. if (AttrVal($name,"rgbGPIOs",0) && $reading =~ m/\d$/i) {
  1083. my ($r,$g,$b) = ESPEasy_gpio2RGB($hash);
  1084. # if (($r ne "" && uc ReadingsVal($name,"rgb","") ne uc $r.$g.$b) || ReadingsAge($name,"rgb",0) > 5 ) {
  1085. if (($r ne "" && uc ReadingsVal($name,"rgb","") ne uc $r.$g.$b) ) {
  1086. readingsSingleUpdate($hash, "rgb", $r.$g.$b, 1);
  1087. }
  1088. }
  1089. }
  1090. # --- setInternal ---------------------------------------------
  1091. elsif ($cmd eq "i") {
  1092. # add human readable text to node_type_id
  1093. $value .= defined $ESPEasy_build_id{$value}{type}
  1094. ? ": " . $ESPEasy_build_id{$value}{type}
  1095. : ": unknown node type id"
  1096. if $reading eq "node_type_id";
  1097. # no value given
  1098. $value = "not defined" if !defined $value || $value eq "";
  1099. # set internal
  1100. $hash->{"ESP_".uc($reading)} = $value;
  1101. # add to log
  1102. push(@logInternals,"$reading:$value");
  1103. }
  1104. # --- Error ---------------------------------------------------
  1105. elsif ($cmd eq "e") {
  1106. if (!defined $hash->{"WARNING"} || $hash->{"WARNING"} ne $value) {
  1107. Log3 $name, 2, "$type $name: WARNING: $value";
  1108. $hash->{"WARNING"} = $value;
  1109. # CommandTrigger(undef, "$name ....");
  1110. }
  1111. #readingsSingleUpdate($hash, $reading, $value, 1);
  1112. }
  1113. # --- DeleteReading -------------------------------------------
  1114. elsif ($cmd eq "dr") {
  1115. CommandDeleteReading(undef, "$name $reading");
  1116. Log3 $name, 4, "$type $name: Reading $reading deleted";
  1117. }
  1118. else {
  1119. Log3 $name, 2, "$type $name: Unknown command received via dispatch";
  1120. }
  1121. } # foreach @v
  1122. Log3 $name, 5, "$type $name: Internals: ".join(" ",@logInternals)
  1123. if scalar @logInternals > 0;
  1124. ESPEasy_checkPresence($hash) if ReadingsVal($name,"presence","") ne "present";
  1125. ESPEasy_setState($hash);
  1126. }
  1127. else { #autocreate failed
  1128. Log3 undef, 2, "ESPEasy: Device $name not defined";
  1129. }
  1130. return $name; # must be != undef. else msg will processed further -> help me!
  1131. }
  1132. # ------------------------------------------------------------------------------
  1133. sub ESPEasy_autocreate($$$$)
  1134. {
  1135. my ($IOhash,$ident,$ip,$autosave) = @_;
  1136. my $IOname = $IOhash->{NAME};
  1137. my $IOtype = $IOhash->{TYPE};
  1138. my $devname = "ESPEasy_".$ident;
  1139. my $define = "$devname ESPEasy $ip 80 $IOhash->{NAME} $ident";
  1140. Log3 undef, 2, "$IOtype $IOname: Autocreate $define";
  1141. my $cmdret= CommandDefine(undef,$define);
  1142. if(!$cmdret) {
  1143. $cmdret= CommandAttr(undef, "$devname room $IOhash->{TYPE}");
  1144. $cmdret= CommandAttr(undef, "$devname group $IOhash->{TYPE} Device");
  1145. $cmdret= CommandAttr(undef, "$devname setState 3");
  1146. $cmdret= CommandAttr(undef, "$devname Interval $d_Interval");
  1147. $cmdret= CommandAttr(undef, "$devname presenceCheck 1");
  1148. $cmdret= CommandAttr(undef, "$devname readingSwitchText 1");
  1149. if (AttrVal($IOname,"autosave",AttrVal("global","autosave",1))) {
  1150. CommandSave(undef,undef);
  1151. Log3 undef, 2, "$IOtype $IOname: Structural changes saved.";
  1152. }
  1153. else {
  1154. Log3 undef, 2, "$IOtype $IOname: Autosave is disabled: "
  1155. ."Do not forget to save changes.";
  1156. }
  1157. }
  1158. else {
  1159. Log3 undef, 1, "$IOtype $IOname: WARNING: an error occurred "
  1160. ."while creating device for $ident: $cmdret";
  1161. }
  1162. return $devname;
  1163. }
  1164. # ------------------------------------------------------------------------------
  1165. sub ESPEasy_httpReq($$$$$$@)
  1166. {
  1167. my ($hash, $host, $port, $ident, $parseCmd, $cmd, @params) = @_;
  1168. my ($name,$type,$self) = ($hash->{NAME},$hash->{TYPE},ESPEasy_whoami()."()");
  1169. # command queue (in)
  1170. return undef if ESPEasy_httpReqQueue(@_);
  1171. # increment http session counter
  1172. $hash->{helper}{sessions}{$host}++;
  1173. my $orgParams = join(",",@params);
  1174. my $orgCmd = $cmd;
  1175. # raw is used for command not implemented right now
  1176. if ($cmd eq "raw") {
  1177. $cmd = $params[0];
  1178. splice(@params,0,1);
  1179. }
  1180. $params[0] = ",".$params[0] if defined $params[0];
  1181. my $plist = join(",",@params);
  1182. my $path = ($cmd =~ m/(reboot|reset|erase)/i) ? "/?cmd=" : "/control?cmd=";
  1183. my $url = "http://".$host.":".$port.$path.$cmd.$plist;
  1184. # there is already a log entry with verbose 3 from device
  1185. Log3 $name, 4, "$type $name: Send $cmd$plist to $host for ident $ident" if ($cmd !~ m/^(status)/);
  1186. my $timeout = AttrVal($name,"httpReqTimeout",$d_httpReqTimeout);
  1187. my $httpParams = {
  1188. url => $url,
  1189. timeout => $timeout,
  1190. keepalive => 0,
  1191. httpversion => "1.0",
  1192. hideurl => 0,
  1193. method => "GET",
  1194. hash => $hash,
  1195. cmd => $orgCmd, # passthrought to parseFn
  1196. plist => $orgParams, # passthrought to parseFn
  1197. host => $host, # passthrought to parseFn
  1198. port => $port, # passthrought to parseFn
  1199. ident => $ident, # passthrought to parseFn
  1200. parseCmd => $parseCmd, # passthrought to parseFn (attr parseCmdResponse => (0)|1)
  1201. callback => \&ESPEasy_httpReqParse
  1202. };
  1203. Log3 $name, 5, "$type $name: NonblockingGet for ident:$ident => $url";
  1204. HttpUtils_NonblockingGet($httpParams);
  1205. return undef;
  1206. }
  1207. # ------------------------------------------------------------------------------
  1208. sub ESPEasy_httpReqParse($$$)
  1209. {
  1210. my ($param, $err, $data) = @_;
  1211. my $hash = $param->{hash};
  1212. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  1213. my @values;
  1214. # command queue
  1215. $hash->{helper}{sessions}{$param->{host}}--;
  1216. if ($err ne "") {
  1217. #dispatch $err to logical device
  1218. @values = ("e||_lastError||$err||0");
  1219. # keep in helper for support reason
  1220. $hash->{"WARNING_$param->{host}"} = $err;
  1221. # logqueue
  1222. Log3 $name, 2, "$type $name: httpReq failed: $param->{host} $param->{ident} "
  1223. ."'$param->{cmd} $param->{plist}' ";
  1224. # unshift command back to queue (resend)
  1225. if (AttrVal($name,"resendFailedCmd",$d_resendFailedCmd)
  1226. && $hash->{MAX_HTTP_SESSIONS}) {
  1227. my @p = ($param->{hash}, $param->{host}, $param->{port}, $param->{ident},
  1228. $param->{parseCmd}, $param->{cmd}, $param->{plist});
  1229. unshift @{$hash->{helper}{queue}{$param->{host}}}, \@p;
  1230. # logqueue
  1231. Log3 $name, 5, "$type $name: Requeuing: $param->{host} $param->{ident} "
  1232. ."'$param->{cmd} $param->{plist}' "
  1233. ."(".scalar @{$hash->{helper}{queue}{$param->{host}}}.")";
  1234. }
  1235. }
  1236. # check that response from cmd should be parsed (client attr parseCmdResponse)
  1237. elsif ($data ne "" && !$param->{parseCmd}) {
  1238. ESPEasy_httpReqDequeue($hash, $param->{host});
  1239. return undef;
  1240. }
  1241. elsif ($data ne "") { # no error occurred
  1242. # command queue
  1243. delete $hash->{"WARNING_$param->{host}"};
  1244. (my $logData = $data) =~ s/\n//sg;
  1245. Log3 $name, 5, "$type $name: http response for ident:$param->{ident} "
  1246. ."cmd:'$param->{cmd},$param->{plist}' => '$logData'";
  1247. if ($data =~ m/^{/) { #it could be json...
  1248. my $res;
  1249. if ( !$hash->{helper}{pm}{JSON} ) {
  1250. Log3 $name, 2, "$type $name: Perl module JSON missing, can't process data.";
  1251. return undef;
  1252. }
  1253. # use encode_utf8 if available else replace any disturbing chars
  1254. $hash->{helper}{pm}{Encode}
  1255. ? ( eval { $res = decode_json( encode_utf8($data) ); 1; } )
  1256. : ( eval { $res = decode_json( $data =~ s/[^\x20-\x7E]/_/gr ); 1; } );
  1257. if ($@) {
  1258. Log3 $name, 2, "$type $name: WARNING: deformed JSON data received "
  1259. ."from $param->{host} requested by $param->{ident}.";
  1260. Log3 $name, 2, "$type $name: $@";
  1261. @values = ("e||_lastError||$@||0");
  1262. return undef;
  1263. }
  1264. # maps plugin type (answer for set state/gpio) to SENSOR_TYPE_SWITCH
  1265. # 10 = SENSOR_TYPE_SWITCH
  1266. my $vType = (defined $res->{plugin} && $res->{plugin} eq "1") ? "10" : "0";
  1267. # Plugin lights:123 nfx:124
  1268. if (defined $res->{plugin} && $res->{plugin} =~ m/^(123|124)$/) {
  1269. foreach (keys %{ $res }) {
  1270. push @values, "r||$_||".$res->{$_}."||".$vType
  1271. if $res->{$_} ne "" && $_ ne "plugin";
  1272. }
  1273. }
  1274. else {
  1275. # push values/cmds in @values
  1276. push @values, "r||GPIO".$res->{pin}."_mode||".$res->{mode}."||".$vType;
  1277. push @values, "r||GPIO".$res->{pin}."_state||".$res->{state}."||".$vType;
  1278. push @values, "r||_lastAction"."||".$res->{log}."||".$vType if $res->{log} ne "";
  1279. }
  1280. } #it is json...
  1281. else { # no json returned => unknown state
  1282. Log3 $name, 5, "$type $name: No json fmt: ident:$param->{ident} ".
  1283. "$param->{cmd} $param->{plist} => $data";
  1284. if ($param->{cmd} eq "status" && $param->{plist} =~ m/^gpio,(\d+)$/i) {
  1285. # push values/cmds in @values
  1286. if (defined $1) {
  1287. push @values, "r||GPIO".$1."_mode||"."?"."||0";
  1288. push @values, "r||GPIO".$1."_state||".$data."||0";
  1289. }
  1290. }
  1291. }
  1292. } # ($data ne "")
  1293. ESPEasy_dispatch($hash,$param->{ident},$param->{host},@values);
  1294. ESPEasy_httpReqDequeue($hash, $param->{host});
  1295. return undef;
  1296. }
  1297. # ------------------------------------------------------------------------------
  1298. sub ESPEasy_httpReqQueue(@)
  1299. {
  1300. my ($hash, $host, $port, $ident, $parseCmd, $cmd, @params) = @_;
  1301. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  1302. $hash->{helper}{sessions}{$host} = 0 if !defined $hash->{helper}{sessions}{$host};
  1303. # is queueing enabled?
  1304. if ($hash->{MAX_HTTP_SESSIONS}) {
  1305. # do queueing if max sessions are already in use
  1306. if ($hash->{helper}{sessions}{$host} >= $hash->{MAX_HTTP_SESSIONS} ) {
  1307. # max queue size reached
  1308. if (!defined $hash->{helper}{queue}{"$host"}
  1309. || scalar @{$hash->{helper}{queue}{"$host"}} < $hash->{MAX_QUEUE_SIZE}) {
  1310. push(@{$hash->{helper}{queue}{"$host"}}, \@_);
  1311. # logqueue
  1312. Log3 $name, 5, "$type $name: Queuing: $host $ident '$cmd ".join(",",@params)."' (". scalar @{$hash->{helper}{queue}{"$host"}} .")";
  1313. return 1;
  1314. }
  1315. else {
  1316. # logqueue
  1317. Log3 $name, 2, "$type $name: set $cmd ".join(",",@params)." (skipped "
  1318. ."due to queue size exceeded: $hash->{MAX_QUEUE_SIZE})";
  1319. # if ($cmd ne "status");
  1320. # ESPEasy_httpReqDequeue($hash,$host);
  1321. return 1;
  1322. }
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. # ------------------------------------------------------------------------------
  1328. sub ESPEasy_httpReqDequeue($$)
  1329. {
  1330. my ($hash,$host) = @_;
  1331. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  1332. if (defined $hash->{helper}{queue}{"$host"} && scalar @{$hash->{helper}{queue}{"$host"}}) {
  1333. my $p = shift @{$hash->{helper}{queue}{"$host"}};
  1334. my ($dhash, $dhost, $port, $ident, $parseCmd, $cmd, @params) = @{ $p };
  1335. # logqueue
  1336. Log3 $name, 5, "$type $name: Dequeuing: $host $ident '$cmd ".join(",",@params)."' (". scalar @{$hash->{helper}{queue}{"$host"}} .")";
  1337. ESPEasy_httpReq($dhash, $dhost, $port, $ident, $parseCmd, $cmd, @params);
  1338. }
  1339. return undef;
  1340. }
  1341. # ------------------------------------------------------------------------------
  1342. sub ESPEasy_statusRequest($) #called by device
  1343. {
  1344. my ($hash) = @_;
  1345. my ($name, $type) = ($hash->{NAME},$hash->{TYPE});
  1346. unless (IsDisabled $name) {
  1347. Log3 $name, 4, "$type $name: set statusRequest";
  1348. ESPEasy_pollGPIOs($hash);
  1349. ESPEasy_checkPresence($hash);
  1350. ESPEasy_setState($hash);
  1351. }
  1352. ESPEasy_resetTimer($hash);
  1353. return undef;
  1354. }
  1355. # ------------------------------------------------------------------------------
  1356. sub ESPEasy_pollGPIOs($) #called by device
  1357. {
  1358. my ($hash) = @_;
  1359. my $name = $hash->{NAME};
  1360. my $type = $hash->{TYPE};
  1361. my $sleep = $hash->{SLEEP};
  1362. my $a = AttrVal($name,'pollGPIOs',undef);
  1363. if (!defined $a) {
  1364. # do nothing, just return
  1365. }
  1366. elsif (defined $sleep && $sleep eq "1") {
  1367. Log3 $name, 2, "$type $name: Polling of GPIOs is not possible as long as deep sleep mode is active.";
  1368. }
  1369. else {
  1370. my @gpios = split(",",$a);
  1371. foreach my $gpio (@gpios) {
  1372. if ($gpio =~ m/^[a-zA-Z]/) { # pin mapping (eg. D8 -> 15)
  1373. Log3 $name, 5, "$type $name: Pin mapping ".uc $gpio." => $ESPEasy_pinMap{uc $gpio}";
  1374. $gpio = $ESPEasy_pinMap{uc $gpio};
  1375. }
  1376. Log3 $name, 5, "$type $name: IOWrite(\$defs{$name}, $hash->{HOST}, $hash->{PORT}, $hash->{IDENT}, 1, status, gpio,".$gpio.")";
  1377. IOWrite($hash, $hash->{HOST}, $hash->{PORT}, $hash->{IDENT}, 1, "status", "gpio,".$gpio);
  1378. } #foreach
  1379. } #else
  1380. return undef;
  1381. }
  1382. # ------------------------------------------------------------------------------
  1383. sub ESPEasy_resetTimer($;$)
  1384. {
  1385. my ($hash,$sig) = @_;
  1386. my $name = $hash->{NAME};
  1387. my $type = $hash->{TYPE};
  1388. $sig = "" if !$sig;
  1389. if ($init_done == 1) {
  1390. RemoveInternalTimer($hash, "ESPEasy_statusRequest");
  1391. }
  1392. if ($sig eq "stop") {
  1393. Log3 $name, 5, "$type $name: internalTimer stopped";
  1394. return undef;
  1395. }
  1396. return undef if AttrVal($name,"Interval",$d_Interval) == 0;
  1397. unless(IsDisabled($name)) {
  1398. my $s = AttrVal($name,"Interval",$d_Interval) + rand(5);
  1399. my $ts = $s + gettimeofday();
  1400. Log3 $name, 5, "$type $name: Start internalTimer +".int($s)." => ".FmtDateTime($ts);
  1401. InternalTimer($ts, "ESPEasy_statusRequest", $hash);
  1402. }
  1403. return undef;
  1404. }
  1405. # ------------------------------------------------------------------------------
  1406. sub ESPEasy_tcpServerOpen($) {
  1407. my ($hash) = @_;
  1408. my $name = $hash->{NAME};
  1409. my $type = $hash->{TYPE};
  1410. my $port = ($hash->{PORT}) ? $hash->{PORT} : 8383;
  1411. my $ret = TcpServer_Open( $hash, $port, "global" );
  1412. exit(1) if ($ret && !$init_done);
  1413. readingsSingleUpdate ( $hash, "state", "initialized", 1 );
  1414. return $ret;
  1415. }
  1416. # ------------------------------------------------------------------------------
  1417. # Duplicated sub from TcpServerUtils as a workaround for new security feature:
  1418. # https://forum.fhem.de/index.php/topic,72717.0.html
  1419. sub ESPEasy_TcpServer_Accept($$)
  1420. {
  1421. my ($hash, $type) = @_;
  1422. my $name = $hash->{NAME};
  1423. my @clientinfo = $hash->{SERVERSOCKET}->accept();
  1424. if(!@clientinfo) {
  1425. Log3 $name, 1, "Accept failed ($name: $!)" if($! != EAGAIN);
  1426. return undef;
  1427. }
  1428. $hash->{CONNECTS}++;
  1429. my ($port, $iaddr) = $hash->{IPV6} ?
  1430. sockaddr_in6($clientinfo[1]) :
  1431. sockaddr_in($clientinfo[1]);
  1432. my $caddr = $hash->{IPV6} ?
  1433. inet_ntop(AF_INET6(), $iaddr) :
  1434. inet_ntoa($iaddr);
  1435. # ------------------------------------------------------------------------------
  1436. # Removed from sub because we have our own access control system that works in
  1437. # a more readable and flexible way (network ranges with allow/deny and regexps).
  1438. # Our new allowed ranges default are also now:
  1439. # 127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,fc00::/7,fe80::/10,::1
  1440. # ------------------------------------------------------------------------------
  1441. #
  1442. # my $af = $attr{$name}{allowfrom};
  1443. # if(!$af) {
  1444. # my $re = "^(127|192.168|172.(1[6-9]|2[0-9]|3[01])|10|169.254)\\.|".
  1445. # "^(fe[89ab]|::1)";
  1446. # if($caddr !~ m/$re/) {
  1447. # my %empty;
  1448. # $hash->{SNAME} = $hash->{NAME};
  1449. # my $auth = Authenticate($hash, \%empty);
  1450. # delete $hash->{SNAME};
  1451. # if($auth == 0) {
  1452. # Log3 $name, 1,
  1453. # "Connection refused from the non-local address $caddr:$port, ".
  1454. # "as there is no working allowed instance defined for it";
  1455. # close($clientinfo[0]);
  1456. # return undef;
  1457. # }
  1458. # }
  1459. # }
  1460. #
  1461. # if($af) {
  1462. # if($caddr !~ m/$af/) {
  1463. # my $hostname = gethostbyaddr($iaddr, AF_INET);
  1464. # if(!$hostname || $hostname !~ m/$af/) {
  1465. # Log3 $name, 1, "Connection refused from $caddr:$port";
  1466. # close($clientinfo[0]);
  1467. # return undef;
  1468. # }
  1469. # }
  1470. # }
  1471. #$clientinfo[0]->blocking(0); # Forum #24799
  1472. if($hash->{SSL}) {
  1473. # Forum #27565: SSLv23:!SSLv3:!SSLv2', #35004: TLSv12:!SSLv3
  1474. my $sslVersion = AttrVal($hash->{NAME}, "sslVersion",
  1475. AttrVal("global", "sslVersion", "TLSv12:!SSLv3"));
  1476. # Certs directory must be in the modpath, i.e. at the same level as the
  1477. # FHEM directory
  1478. my $mp = AttrVal("global", "modpath", ".");
  1479. my $ret = IO::Socket::SSL->start_SSL($clientinfo[0], {
  1480. SSL_server => 1,
  1481. SSL_key_file => "$mp/certs/server-key.pem",
  1482. SSL_cert_file => "$mp/certs/server-cert.pem",
  1483. SSL_version => $sslVersion,
  1484. SSL_cipher_list => 'HIGH:!RC4:!eNULL:!aNULL',
  1485. Timeout => 4,
  1486. });
  1487. my $err = $!;
  1488. if( !$ret
  1489. && $err != EWOULDBLOCK
  1490. && $err ne "Socket is not connected") {
  1491. $err = "" if(!$err);
  1492. $err .= " ".($SSL_ERROR ? $SSL_ERROR : IO::Socket::SSL::errstr());
  1493. Log3 $name, 1, "$type SSL/HTTPS error: $err"
  1494. if($err !~ m/error:00000000:lib.0.:func.0.:reason.0./); #Forum 56364
  1495. close($clientinfo[0]);
  1496. return undef;
  1497. }
  1498. }
  1499. my $cname = "${name}_${caddr}_${port}";
  1500. my %nhash;
  1501. $nhash{NR} = $devcount++;
  1502. $nhash{NAME} = $cname;
  1503. $nhash{PEER} = $caddr;
  1504. $nhash{PORT} = $port;
  1505. $nhash{FD} = $clientinfo[0]->fileno();
  1506. $nhash{CD} = $clientinfo[0]; # sysread / close won't work on fileno
  1507. $nhash{TYPE} = $type;
  1508. $nhash{SSL} = $hash->{SSL};
  1509. $nhash{STATE} = "Connected";
  1510. $nhash{SNAME} = $name;
  1511. $nhash{TEMPORARY} = 1; # Don't want to save it
  1512. $nhash{BUF} = "";
  1513. $attr{$cname}{room} = "hidden";
  1514. $defs{$cname} = \%nhash;
  1515. $selectlist{$nhash{NAME}} = \%nhash;
  1516. my $ret = $clientinfo[0]->setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1);
  1517. Log3 $name, 4, "Connection accepted from $nhash{NAME}";
  1518. return \%nhash;
  1519. }
  1520. # ------------------------------------------------------------------------------
  1521. sub ESPEasy_header2Hash($) {
  1522. my ($string) = @_;
  1523. my %header = ();
  1524. foreach my $line (split("\r\n", $string)) {
  1525. my ($key,$value) = split(": ", $line,2);
  1526. next if !$value;
  1527. $value =~ s/^ //;
  1528. $header{$key} = $value;
  1529. }
  1530. return \%header;
  1531. }
  1532. # ------------------------------------------------------------------------------
  1533. sub ESPEasy_isAuthenticated($$)
  1534. {
  1535. my ($hash,$ah) = @_;
  1536. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  1537. my $ipv = $hash->{PEER} =~ m/:/ ? 6 : 4;
  1538. my $bhash = $modules{ESPEasy}{defptr}{BRIDGE}{$ipv};
  1539. my ($bname,$btype) = ($bhash->{NAME},$bhash->{TYPE});
  1540. my $u = $bhash->{".bau"};
  1541. my $p = $bhash->{".bap"};
  1542. my $attr = AttrVal($bname,"authentication",0);
  1543. if (!defined $u || !defined $p || $attr == 0) {
  1544. if (defined $ah){
  1545. Log3 $bname, 2, "$type $name: No basic authentication active but ".
  1546. "credentials received";
  1547. }
  1548. else {
  1549. Log3 $bname, 4, "$type $name: No basic authentication required";
  1550. }
  1551. return "not required";
  1552. }
  1553. elsif (defined $ah) {
  1554. my ($a,$v) = split(" ",$ah);
  1555. if ($a eq "Basic" && decode_base64($v) eq $u.":".$p) {
  1556. Log3 $bname, 4, "$type $name: Basic authentication accepted";
  1557. return "accepted";
  1558. }
  1559. else {
  1560. Log3 $bname, 2, "$type $name: Basic authentication rejected";
  1561. }
  1562. }
  1563. else {
  1564. Log3 $bname, 2, "$type $name: Basic authentication active but ".
  1565. "no credentials received";
  1566. }
  1567. return undef;
  1568. }
  1569. # ------------------------------------------------------------------------------
  1570. sub ESPEasy_isParseCmd($$) #called by device
  1571. {
  1572. my ($hash,$cmd) = @_;
  1573. my $name = $hash->{NAME};
  1574. my $doParse = 0;
  1575. my @cmds = split(",",AttrVal($name,"parseCmdResponse","status"));
  1576. foreach (@cmds) {
  1577. if (lc($_) eq lc($cmd)) {
  1578. $doParse = 1;
  1579. last;
  1580. }
  1581. }
  1582. return $doParse;
  1583. }
  1584. # ------------------------------------------------------------------------------
  1585. sub ESPEasy_sendHttpClose($$$) {
  1586. my ($hash,$code,$response) = @_;
  1587. my ($name,$type,$con) = ($hash->{NAME},$hash->{TYPE},$hash->{CD});
  1588. my $ipv = $hash->{PEER} =~ m/:/ ? 6 : 4;
  1589. my $bhash = $modules{ESPEasy}{defptr}{BRIDGE}{$ipv};
  1590. my $bname = $bhash->{NAME};
  1591. print $con "HTTP/1.1 ".$code."\r\n",
  1592. "Content-Type: text/plain\r\n",
  1593. "Connection: close\r\n",
  1594. "Content-Length: ".length($response)."\r\n\r\n",
  1595. $response;
  1596. Log3 $bname, 4, "$type $name: Send http close '$code'";
  1597. return undef;
  1598. }
  1599. # ------------------------------------------------------------------------------
  1600. sub ESPEasy_paramPos($$)
  1601. {
  1602. my ($cmd,$search) = @_;
  1603. my @usage = split(" ",$ESPEasy_setCmdsUsage{$cmd});
  1604. my $pos = 0;
  1605. my $i = 0;
  1606. foreach (@usage) {
  1607. if ($_ eq $search) {
  1608. $pos = $i;
  1609. last;
  1610. }
  1611. $i++;
  1612. }
  1613. return $pos; # return 0 if no match, else position
  1614. }
  1615. # ------------------------------------------------------------------------------
  1616. sub ESPEasy_paramCount($)
  1617. {
  1618. return () = $_[0] =~ m/\s/g # count \s in a string
  1619. }
  1620. # ------------------------------------------------------------------------------
  1621. sub ESPEasy_clearReadings($)
  1622. {
  1623. my ($hash) = @_;
  1624. my $name = $hash->{NAME};
  1625. my $type = $hash->{TYPE};
  1626. my @dr;
  1627. foreach (keys %{$hash->{READINGS}}) {
  1628. # next if $_ =~ m/^(presence)$/;
  1629. CommandDeleteReading(undef, "$name $_");
  1630. # fhem("deletereading $name $_");
  1631. push(@dr,$_);
  1632. }
  1633. if (scalar @dr >= 1) {
  1634. delete $hash->{helper}{received};
  1635. delete $hash->{helper}{fpc}; # used in checkPresence
  1636. Log3 $name, 3, "$type $name: Readings [".join(",",@dr)."] wiped out";
  1637. }
  1638. ESPEasy_setState($hash);
  1639. return undef
  1640. }
  1641. # ------------------------------------------------------------------------------
  1642. sub ESPEasy_checkVersion($$$$)
  1643. {
  1644. my ($hash,$dev,$ve,$vj) = @_;
  1645. my ($type,$name) = ($hash->{TYPE},$hash->{NAME});
  1646. my $ov = "_OUTDATED_ESP_VER_$dev";
  1647. if ($vj < $minJsonVersion) {
  1648. $hash->{$ov} = "R".$ve."/J".$vj;
  1649. Log3 $name, 2, "$type $name: WARNING: no data processed. ESPEasy plugin "
  1650. ."'FHEM HTTP' is too old [$dev: R".$ve." J".$vj."]. ".
  1651. "Use ESPEasy R$minEEBuild at least.";
  1652. return 1;
  1653. }
  1654. else{
  1655. delete $hash->{$ov} if exists $hash->{$ov};
  1656. return 0;
  1657. }
  1658. }
  1659. # ------------------------------------------------------------------------------
  1660. sub ESPEasy_checkPresence($)
  1661. {
  1662. my ($hash,$isPresent) = @_;
  1663. my $name = $hash->{NAME};
  1664. my $type = $hash->{TYPE};
  1665. my $interval = AttrVal($name,'Interval',$d_Interval);
  1666. my $addTime = 10; # if there is extreme heavy system load
  1667. return undef if AttrVal($name,'presenceCheck',1) == 0;
  1668. return undef if $interval == 0;
  1669. my $presence = "absent";
  1670. # check each received reading
  1671. foreach my $reading (keys %{$hash->{helper}{received}}) {
  1672. if (ReadingsAge($name,$reading,0) < $interval+$addTime) {
  1673. #dev is present if any reading is newer than INTERVAL+$addTime
  1674. $presence = "present";
  1675. last;
  1676. }
  1677. }
  1678. # update presence only if FirstPrecenceCheck is $interval seconds ago.
  1679. $hash->{helper}{fpc} = time() if (!defined $hash->{helper}{fpc});
  1680. if ($presence eq "present" || (time() - $hash->{helper}{fpc}) > $interval) {
  1681. readingsSingleUpdate($hash,"presence",$presence,1);
  1682. Log3 $name, 4, "$type $name: presence: $presence";
  1683. }
  1684. return undef;
  1685. }
  1686. # ------------------------------------------------------------------------------
  1687. sub ESPEasy_setState($)
  1688. {
  1689. my ($hash) = @_;
  1690. my $name = $hash->{NAME};
  1691. my $type = $hash->{TYPE};
  1692. return undef if not AttrVal($name,"setState",1);
  1693. if (AttrVal($name,"rgbGPIOs",0)) {
  1694. my ($r,$g,$b) = ESPEasy_gpio2RGB($hash);
  1695. if ($r ne "") {
  1696. readingsSingleUpdate($hash,"state", "R: $r G: $g B: $b", 1)
  1697. }
  1698. }
  1699. else {
  1700. my $interval = AttrVal($name,"Interval",$d_Interval);
  1701. my $addTime = 3;
  1702. my @ret;
  1703. foreach my $reading (sort keys %{$hash->{helper}{received}}) {
  1704. next if $reading =~ m/^(\.ignored_.*|state|presence|_lastAction|_lastError|\w+_mode)$/;
  1705. next if $interval && ReadingsAge($name,$reading,1) > $interval+$addTime;
  1706. push(@ret, substr($reading,0,AttrVal($name,"setState",3))
  1707. .": ".ReadingsVal($name,$reading,""));
  1708. }
  1709. my $oState = ReadingsVal($name, "state", "");
  1710. my $presence = ReadingsVal($name, "presence", "opened");
  1711. if ($presence eq "absent" && $oState ne "absent") {
  1712. readingsSingleUpdate($hash,"state","absent", 1 );
  1713. delete $hash->{helper}{received};
  1714. }
  1715. else {
  1716. my $nState = (scalar @ret >= 1) ? join(" ",@ret) : $presence;
  1717. readingsSingleUpdate($hash,"state",$nState, 1 ); # if ($oState ne $nState);
  1718. }
  1719. }
  1720. return undef;
  1721. }
  1722. # ------------------------------------------------------------------------------
  1723. sub ESPEasy_setRGB($$@)
  1724. {
  1725. my ($hash,$cmd,@p) = @_;
  1726. my ($type,$name) = ($hash->{TYPE},$hash->{NAME});
  1727. my ($rg,$gg,$bg) = split(",",AttrVal($name,"rgbGPIOs",""));
  1728. my ($r,$g,$b);
  1729. my $rgb = $p[0] if $cmd =~ m/^rgb$/i;
  1730. # return undef if !defined $rgb;
  1731. $rg = $ESPEasy_pinMap{uc $rg} if defined $ESPEasy_pinMap{uc $rg};
  1732. $gg = $ESPEasy_pinMap{uc $gg} if defined $ESPEasy_pinMap{uc $gg};
  1733. $bg = $ESPEasy_pinMap{uc $bg} if defined $ESPEasy_pinMap{uc $bg};
  1734. if ($cmd =~ m/^(1|on)$/ || ($cmd =~ m/^rgb$/i && $rgb =~ m/^(1|on)$/)) {
  1735. $rgb = "FFFFFF" }
  1736. elsif ($cmd =~ m/^(0|off)$/ || ($cmd =~ m/^rgb$/i && $rgb =~ m/^(0|off)$/)) {
  1737. $rgb = "000000" }
  1738. elsif ($cmd =~ m/^toggle$/i || ($cmd =~ m/^rgb$/i && $rgb =~ m/^toggle$/i)) {
  1739. $rgb = ReadingsVal($name,"rgb","000000") ne "000000" ? "000000" : "FFFFFF"
  1740. }
  1741. if ($rgb =~ m/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/) {
  1742. ($r,$g,$b) = (hex($1), hex($2), hex($3));
  1743. }
  1744. else {
  1745. Log3 $name, 2, "$type $name: set $name $cmd $rgb: "
  1746. ."'$rgb' is not a valid RGB value.";
  1747. return "'$rgb' is not a valid RGB value.";
  1748. }
  1749. ESPEasy_Set($hash, $name, "pwm", ("$rg", $r*4));
  1750. ESPEasy_Set($hash, $name, "pwm", ("$gg", $g*4));
  1751. ESPEasy_Set($hash, $name, "pwm", ("$bg", $b*4));
  1752. readingsSingleUpdate($hash, "rgb", uc $rgb, 1);
  1753. return undef;
  1754. }
  1755. # ------------------------------------------------------------------------------
  1756. sub ESPEasy_setCT($$@)
  1757. {
  1758. my ($hash,$cmd,@p) = @_;
  1759. my ($type,$name) = ($hash->{TYPE},$hash->{NAME});
  1760. my ($gww,$gcw) = split(",",AttrVal($name,"wwcwGPIOs",""));
  1761. my ($ww,$cw);
  1762. my ($pct,$ct);
  1763. my $ctWW = AttrVal($name,"colorpickerCTww",$d_colorpickerCTww);
  1764. my $ctCW = AttrVal($name,"colorpickerCTcw",$d_colorpickerCTcw);
  1765. my $ctWW_lim = AttrVal($name,"ctWW_reducedRange",undef);
  1766. my $ctCW_lim = AttrVal($name,"ctCW_reducedRange",undef);
  1767. $gww = $ESPEasy_pinMap{uc $gww} if defined $ESPEasy_pinMap{uc $gww};
  1768. $gcw = $ESPEasy_pinMap{uc $gcw} if defined $ESPEasy_pinMap{uc $gcw};
  1769. readingsSingleUpdate($hash, $cmd, $p[0], 1);
  1770. if ($cmd eq "ct") {
  1771. $ct = $p[0];
  1772. $pct = ReadingsVal($name,"pct",50);
  1773. }
  1774. elsif ($cmd eq "pct") {
  1775. $pct = $p[0];
  1776. $ct = ReadingsVal($name,"ct",3000);
  1777. }
  1778. # are we out of range?
  1779. $pct = 100 if $pct > 100;
  1780. $pct = 0 if $pct < 0;
  1781. $ct = $ctWW if $ct < $ctWW;
  1782. $ct = $ctCW if $ct > $ctCW;
  1783. #Log 1, "pct:$pct ct:$ct ctWW:$ctWW ctCW:$ctCW ctWW_lim:$ctWW_lim ctCW_lim:$ctCW_lim";
  1784. my $wwcwMaxBri = AttrVal($name,"wwcwMaxBri",0);
  1785. my ($fww,$fcw) = ESPEasy_ct2wwcw($ct, $ctWW, $ctCW, $wwcwMaxBri, $ctWW_lim, $ctCW_lim);
  1786. ESPEasy_Set($hash, $name, "pwm", ($gww, int $pct*10.23*$fww));
  1787. ESPEasy_Set($hash, $name, "pwm", ($gcw, int $pct*10.23*$fcw));
  1788. return undef;
  1789. }
  1790. # ------------------------------------------------------------------------------
  1791. # ct2wwcw with constant brightness over temp range (or max bri if $maxBri == 1).
  1792. # "used range" can be set to reduce temp range to get a lighter leds with constant
  1793. # bri over reduced temp range.
  1794. # 1: temp to set 2:led-ww-temp 3:led-cw-temp 4:maxBri 5:used range ww 6:used range cw
  1795. sub ESPEasy_ct2wwcw($$$;$$$)
  1796. {
  1797. my ($t,$tww,$tcw,$maxBri,$tww_ur,$tcw_ur) = @_;
  1798. my $maxBriFactor;
  1799. $tcw -= $tww;
  1800. $t -= $tww;
  1801. my $fcw = $t / $tcw;
  1802. my $fww = 1 - $fcw;
  1803. if ($maxBri // $maxBri) {
  1804. $maxBriFactor = ($fcw > $fww) ? 1/$fcw : 1/$fww;
  1805. #Log 1, "maxBriFactor: $maxBriFactor (maxBri)";
  1806. }
  1807. else {
  1808. $tww_ur = $tww if !(defined $tww_ur) || $tww_ur < $tww || $tww_ur >= $tcw;
  1809. $tcw_ur = $tcw if !(defined $tcw_ur) || $tcw_ur > $tcw || $tcw_ur <= $tww;
  1810. $tww_ur -= $tww;
  1811. $tcw_ur -= $tww;
  1812. my $t = ($tww_ur < $tcw - $tcw_ur) ? $tww_ur : $tcw - $tcw_ur;
  1813. my $fcw = $t / $tcw;
  1814. my $fww = 1 - $fcw;
  1815. $maxBriFactor = ($fcw > $fww) ? 1/$fcw : 1/$fww;
  1816. #Log 1, "maxBriFactor: $maxBriFactor (constBri)";
  1817. }
  1818. return ( $fww * $maxBriFactor, $fcw * $maxBriFactor );
  1819. }
  1820. # ------------------------------------------------------------------------------
  1821. sub ESPEasy_gpio2RGB($)
  1822. {
  1823. my ($hash) = @_;
  1824. my $name = $hash->{NAME};
  1825. my ($r,$g,$b,$rgb);
  1826. my $a = AttrVal($name,"rgbGPIOs",undef);
  1827. return undef if !defined $a;
  1828. my ($gr,$gg,$gb) = split(",",AttrVal($name,"rgbGPIOs",""));
  1829. $gr = $ESPEasy_pinMap{uc $gr} if defined $ESPEasy_pinMap{uc $gr};
  1830. $gg = $ESPEasy_pinMap{uc $gg} if defined $ESPEasy_pinMap{uc $gg};
  1831. $gb = $ESPEasy_pinMap{uc $gb} if defined $ESPEasy_pinMap{uc $gb};
  1832. my $rr = AttrVal($name,"readingPrefixGPIO","GPIO").$gr;
  1833. my $rg = AttrVal($name,"readingPrefixGPIO","GPIO").$gg;
  1834. my $rb = AttrVal($name,"readingPrefixGPIO","GPIO").$gb;
  1835. $r = ReadingsVal($name,$rr,undef);
  1836. $g = ReadingsVal($name,$rg,undef);
  1837. $b = ReadingsVal($name,$rb,undef);
  1838. return ("","","") if !defined $r || !defined $g || !defined $b
  1839. || $r !~ m/^\d+$/ || $g !~ m/^\d+$/i || $b !~ m/^\d+$/i;
  1840. return (sprintf("%2.2X",$r/4), sprintf("%2.2X",$g/4), sprintf("%2.2X",$b/4));
  1841. }
  1842. # ------------------------------------------------------------------------------
  1843. sub ESPEasy_adjustSetCmds($)
  1844. {
  1845. my ($hash) = @_;
  1846. my $name = $hash->{NAME};
  1847. delete $ESPEasy_setCmds{rgb};
  1848. delete $ESPEasy_setCmds{ct};
  1849. delete $ESPEasy_setCmds{pct};
  1850. delete $ESPEasy_setCmds{on};
  1851. delete $ESPEasy_setCmds{off};
  1852. delete $ESPEasy_setCmds{toggle};
  1853. delete $ESPEasy_setCmds{dim};
  1854. delete $ESPEasy_setCmds{line};
  1855. delete $ESPEasy_setCmds{one};
  1856. delete $ESPEasy_setCmds{all};
  1857. delete $ESPEasy_setCmds{fade};
  1858. delete $ESPEasy_setCmds{colorfade};
  1859. delete $ESPEasy_setCmds{rainbow};
  1860. delete $ESPEasy_setCmds{kitt};
  1861. delete $ESPEasy_setCmds{comet};
  1862. delete $ESPEasy_setCmds{theatre};
  1863. delete $ESPEasy_setCmds{scan};
  1864. delete $ESPEasy_setCmds{dualscan};
  1865. delete $ESPEasy_setCmds{twinkle};
  1866. delete $ESPEasy_setCmds{twinklefade};
  1867. delete $ESPEasy_setCmds{sparkle};
  1868. delete $ESPEasy_setCmds{wipe};
  1869. delete $ESPEasy_setCmds{fire};
  1870. delete $ESPEasy_setCmds{stop};
  1871. delete $ESPEasy_setCmds{fadetime};
  1872. delete $ESPEasy_setCmds{fadedelay};
  1873. delete $ESPEasy_setCmds{count};
  1874. delete $ESPEasy_setCmds{speed};
  1875. delete $ESPEasy_setCmds{bgcolor};
  1876. if (defined AttrVal($name,"mapLightCmds",undef)) {
  1877. $ESPEasy_setCmds{rgb} = 1;
  1878. $ESPEasy_setCmds{ct} = 1;
  1879. $ESPEasy_setCmds{pct} = 1;
  1880. $ESPEasy_setCmds{on} = 0;
  1881. $ESPEasy_setCmds{off} = 0;
  1882. $ESPEasy_setCmds{toggle} = 0;
  1883. # used by nfx plugin
  1884. $ESPEasy_setCmds{dim} = 1;
  1885. $ESPEasy_setCmds{line} = 3;
  1886. $ESPEasy_setCmds{one} = 2;
  1887. $ESPEasy_setCmds{all} = 1;
  1888. $ESPEasy_setCmds{fade} = 1;
  1889. $ESPEasy_setCmds{colorfade}= 2;
  1890. $ESPEasy_setCmds{rainbow} = 0;
  1891. $ESPEasy_setCmds{kitt} = 1;
  1892. $ESPEasy_setCmds{comet} = 1;
  1893. $ESPEasy_setCmds{theatre} = 1;
  1894. $ESPEasy_setCmds{scan} = 1;
  1895. $ESPEasy_setCmds{dualscan} = 1;
  1896. $ESPEasy_setCmds{twinkle} = 1;
  1897. $ESPEasy_setCmds{twinklefade} = 1;
  1898. $ESPEasy_setCmds{sparkle} = 1;
  1899. $ESPEasy_setCmds{wipe} = 1;
  1900. $ESPEasy_setCmds{fire} = 0;
  1901. $ESPEasy_setCmds{stop} = 0;
  1902. $ESPEasy_setCmds{fadetime} = 1;
  1903. $ESPEasy_setCmds{fadedelay}= 1;
  1904. $ESPEasy_setCmds{speed} = 1;
  1905. $ESPEasy_setCmds{count} = 1;
  1906. $ESPEasy_setCmds{bgcolor} = 1;
  1907. }
  1908. if (defined AttrVal($name,"rgbGPIOs",undef)) {
  1909. $ESPEasy_setCmds{rgb} = 1;
  1910. $ESPEasy_setCmds{on} = 0;
  1911. $ESPEasy_setCmds{off} = 0;
  1912. $ESPEasy_setCmds{toggle} = 0;
  1913. }
  1914. if (defined AttrVal($name,"wwcwGPIOs",undef)) {
  1915. $ESPEasy_setCmds{ct} = 1;
  1916. $ESPEasy_setCmds{pct} = 1;
  1917. $ESPEasy_setCmds{on} = 0;
  1918. $ESPEasy_setCmds{off} = 0;
  1919. $ESPEasy_setCmds{toggle} = 0;
  1920. }
  1921. return undef;
  1922. }
  1923. # ------------------------------------------------------------------------------
  1924. # attr <dev> devStateIcon { ESPEasy_devStateIcon($name) }
  1925. sub ESPEasy_devStateIcon($)
  1926. {
  1927. my $ret = Color::devStateIcon($_[0],"rgb","rgb");
  1928. $ret =~ m/^.*:on@#(..)(..)(..):toggle$/;
  1929. return undef if !defined $1;
  1930. my $symP = int((hex($1)+hex($2)+hex($3))/76.5)*10;
  1931. $symP = "00" if $symP == 0;
  1932. my $icon = "light_light_dim_".$symP;
  1933. $ret =~ s/:on@#/:$icon@#/;
  1934. return $ret;
  1935. }
  1936. # ------------------------------------------------------------------------------
  1937. sub ESPEasy_adjustValue($$$)
  1938. {
  1939. my ($hash,$r,$v) = @_;
  1940. my $name = $hash->{NAME};
  1941. my $type = $hash->{TYPE};
  1942. my $a = AttrVal($name,"adjustValue",undef);
  1943. return $v if !defined $a;
  1944. my ($VALUE,$READING,$NAME) = ($v,$r,$name); #capital vars for use in attribute
  1945. my @a = split(" ",$a);
  1946. foreach (@a) {
  1947. my ($regex,$formula) = split(":",$_);
  1948. if ($r =~ m/^$regex$/) {
  1949. no warnings;
  1950. my $adjVal = $formula =~ m/\$VALUE/ ? eval($formula) : eval($v.$formula);
  1951. use warnings;
  1952. if ($@) {
  1953. Log3 $name, 2, "$type $name: WARNING: attribute 'adjustValue': "
  1954. ."mad expression '$formula'";
  1955. Log3 $name, 2, "$type $name: $@";
  1956. }
  1957. else {
  1958. my $rText = (defined $adjVal) ? $adjVal : "'undef'";
  1959. Log3 $name, 5, "$type $name: Adjusted reading $r: $v => $formula = $rText";
  1960. return $adjVal;
  1961. }
  1962. #last; #disabled to be able to match multiple readings
  1963. }
  1964. }
  1965. return $v;
  1966. }
  1967. # ------------------------------------------------------------------------------
  1968. sub ESPEasy_urlEncodeDisplayText($$@)
  1969. {
  1970. my ($hash, $cmd, @params) = @_;
  1971. my $name = $hash->{NAME};
  1972. my $enc = AttrVal($name, "displayTextEncode", $d_displayTextEncode);
  1973. my $pp = ESPEasy_paramPos($cmd,'<text>');
  1974. if ($enc && $pp) {
  1975. my (@p, @t);
  1976. my $c = scalar @params;
  1977. # leading parameters
  1978. for (my $i=0; $i<$pp-1; $i++) {
  1979. push( @p, $params[$i] )
  1980. }
  1981. # collect all texts parameters
  1982. for (my $i=$pp-1; $i<$c; $i++) {
  1983. $params[$i] =~ s/,/./g; # comma is ESPEasy parameter splitter, can't be used
  1984. push @t, $params[$i];
  1985. }
  1986. my $text = join(" ", @t);
  1987. # fill line with leading/trailing spaces
  1988. my $width = AttrVal($name,"displayTextWidth", $d_displayTextWidth);
  1989. if ($width) {
  1990. $text = " " x ($p[1]-1) .$text. " " x ($width - length($text) - $p[1]+1);
  1991. $text = substr($text, 0, $width);
  1992. $p[1] = 1;
  1993. }
  1994. push(@p, urlEncode($text));
  1995. return @p;
  1996. }
  1997. return @params;
  1998. }
  1999. # ------------------------------------------------------------------------------
  2000. sub ESPEasy_loadRequiredModules($)
  2001. {
  2002. my ($hash) = @_;
  2003. foreach ("JSON", "Encode") {
  2004. eval "use $_; 1;";
  2005. if (!$@) {
  2006. $hash->{helper}{pm}{$_} = 1;
  2007. }
  2008. else {
  2009. $hash->{helper}{pm}{$_} = 0;
  2010. if ($init_done || $hash->{SUBTYPE} eq "bridge") {
  2011. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  2012. Log3 $name, 1, "$type $name: WARNING: Perl module $_ is not installed. "
  2013. . "Reduced functionality!";
  2014. Log3 $name, 2, "$type $name: $@" if $init_done;
  2015. }
  2016. }
  2017. }
  2018. return undef;
  2019. }
  2020. # ------------------------------------------------------------------------------
  2021. sub ESPEasy_isAttrCombineDevices($)
  2022. {
  2023. return 0 if !defined $_[0];
  2024. my @ranges = split(/,| /,$_[0]);
  2025. foreach (@ranges) {
  2026. if (!($_ =~ m/^([A-Za-z0-9_\.]|[A-Za-z0-9_\.][A-Za-z0-9_\.]*[A-Za-z0-9\._])$/
  2027. || ESPEasy_isIPv64Range($_)))
  2028. {
  2029. return 0
  2030. }
  2031. }
  2032. return 1;
  2033. }
  2034. # ------------------------------------------------------------------------------
  2035. # check if $peer is covered by $allowed (eg. 10.1.2.3 is included in 10.0.0.0/8)
  2036. # 1:peer address 2:allowed range
  2037. # ------------------------------------------------------------------------------
  2038. sub ESPEasy_isCombineDevices($$$)
  2039. {
  2040. my ($peer,$espName,$allowed) = @_;
  2041. return $allowed if $allowed =~ m/^[01]$/;
  2042. my @allowed = split(/,| /,$allowed);
  2043. foreach (@allowed) { return 1 if $espName eq $_ }
  2044. return 1 if ESPEasy_isPeerAllowed($peer,$allowed);
  2045. return 0;
  2046. }
  2047. # ------------------------------------------------------------------------------
  2048. # check param to be a valid ip64 address or fqdn or hostname
  2049. # ------------------------------------------------------------------------------
  2050. sub ESPEasy_isValidPeer($)
  2051. {
  2052. my ($addr) = @_;
  2053. return 0 if !defined $addr;
  2054. my @ranges = split(/,| /,$addr);
  2055. foreach (@ranges) {
  2056. return 0 if !( ESPEasy_isIPv64Range($_)
  2057. || ESPEasy_isFqdn($_) || ESPEasy_isHostname($_) );
  2058. }
  2059. return 1;
  2060. }
  2061. # ------------------------------------------------------------------------------
  2062. # check if given ip or ip range is guilty
  2063. # argument can be:
  2064. # - ipv4, ipv4/CIDR, ipv4/dotted, ipv6, ipv6/CIDR (or a regexp if opt. argument
  2065. # $regexChk is set)
  2066. # - space or comma separated list of above.
  2067. # ------------------------------------------------------------------------------
  2068. sub ESPEasy_isIPv64Range($;$)
  2069. {
  2070. my ($addr,$regexChk) = @_;
  2071. return 0 if !defined $addr;
  2072. my @ranges = split(/,| /,$addr);
  2073. foreach (@ranges) {
  2074. my ($ip,$nm) = split("/",$_);
  2075. if (ESPEasy_isIPv4($ip)) {
  2076. return 0 if defined $nm && !( ESPEasy_isNmDotted($nm)
  2077. || ESPEasy_isNmCIDRv4($nm) );
  2078. }
  2079. elsif (ESPEasy_isIPv6($ip)) {
  2080. return 0 if defined $nm && !ESPEasy_isNmCIDRv6($nm);
  2081. }
  2082. elsif (defined $regexChk && !defined $nm) {
  2083. return 0 if $ip =~ m/^\*/ || $ip =~ m/^\d+\.\d+\.\d+\.\d+$/; # faulty regexp/ip
  2084. eval { "Hallo" =~ m/^$ip$/ };
  2085. return $@ ? 0 : 1;
  2086. }
  2087. else {
  2088. return 0;
  2089. }
  2090. }
  2091. return 1;
  2092. }
  2093. # ------------------------------------------------------------------------------
  2094. # check if $peer is covered by $allowed (eg. 10.1.2.3 is included in 10.0.0.0/8)
  2095. # 1:peer address 2:allowed range
  2096. # ------------------------------------------------------------------------------
  2097. sub ESPEasy_isPeerAllowed($$)
  2098. {
  2099. my ($peer,$allowed) = @_;
  2100. return $allowed if $allowed =~ m/^[01]$/;
  2101. #return 1 if $allowed =~ /^0.0.0.0\/0(.0.0.0)?$/; # not necessary but faster
  2102. my $binPeer = ESPEasy_ip2bin($peer);
  2103. my @a = split(/,| /,$allowed);
  2104. foreach (@a) {
  2105. return 1 if $peer =~ m/^$_$/; # a regexp is been used
  2106. next if !ESPEasy_isIPv64Range($_); # needed for combinedDevices
  2107. my ($addr,$ip,$mask) = ESPEasy_addrToCIDR($_);
  2108. return 0 if !defined $ip || !defined $mask; # return if ip or mask !guilty
  2109. my $binAllowed = ESPEasy_ip2bin($addr);
  2110. my $binPeerCut = substr($binPeer,0,$mask);
  2111. return 1 if ($binAllowed eq $binPeerCut);
  2112. }
  2113. return 0;
  2114. }
  2115. # ------------------------------------------------------------------------------
  2116. # convert IPv64 address to binary format and return network part of binary, only
  2117. # ------------------------------------------------------------------------------
  2118. sub ESPEasy_ip2bin($)
  2119. {
  2120. my ($addr) = @_;
  2121. my ($ip,$mask) = split("/",$addr);
  2122. my @bin;
  2123. if (ESPEasy_isIPv4($ip)) {
  2124. $mask = 32 if !defined $mask;
  2125. @bin = map substr(unpack("B32",pack("N",$_)),-8), split(/\./,$ip);
  2126. }
  2127. elsif (ESPEasy_isIPv6($ip)) {
  2128. $ip = ESPEasy_expandIPv6($ip);
  2129. $mask = 128 if !defined $mask;
  2130. @bin = map {unpack('B*',pack('H*',$_))} split(/:/, $ip);
  2131. }
  2132. else {
  2133. return undef;
  2134. }
  2135. my $bin = join('', @bin);
  2136. my $binMask = substr($bin, 0, $mask);
  2137. return $binMask; # return network part of $bin
  2138. }
  2139. # ------------------------------------------------------------------------------
  2140. # expand IPv6 address to 8 full blocks
  2141. # Advantage of IO::Socket : already installed and it seems to be the fastest way
  2142. # http://stackoverflow.com/questions/4800691/perl-ipv6-address-expansion-parsing
  2143. # ------------------------------------------------------------------------------
  2144. sub ESPEasy_expandIPv6($)
  2145. {
  2146. my ($ipv6) = @_;
  2147. use Socket qw(inet_pton AF_INET6);
  2148. return join(":", unpack("H4H4H4H4H4H4H4H4",inet_pton(AF_INET6, $ipv6)));
  2149. }
  2150. # ------------------------------------------------------------------------------
  2151. # convert IPv64 address or range into CIDR notion
  2152. # return undef if addreess or netmask is not valid
  2153. # ------------------------------------------------------------------------------
  2154. sub ESPEasy_addrToCIDR($)
  2155. {
  2156. my ($addr) = @_;
  2157. my ($ip,$mask) = split("/",$addr);
  2158. # no nm specified
  2159. return (ESPEasy_isIPv4($ip) ? ("$ip/32",$ip,32) : ("$ip/128",$ip,128)) if !defined $mask;
  2160. # netmask is already in CIDR format and all values are valid
  2161. return ("$ip/$mask",$ip,$mask)
  2162. if (ESPEasy_isIPv4($ip) && ESPEasy_isNmCIDRv4($mask))
  2163. || (ESPEasy_isIPv6($ip) && ESPEasy_isNmCIDRv6($mask));
  2164. $mask = ESPEasy_dottedNmToCIDR($mask);
  2165. return (undef,undef,undef) if !defined $mask;
  2166. return ("$ip/$mask",$ip,$mask);
  2167. }
  2168. # ------------------------------------------------------------------------------
  2169. # convert dotted decimal netmask to CIDR format
  2170. # return undef if nm is not in dotted decimal format
  2171. # ------------------------------------------------------------------------------
  2172. sub ESPEasy_dottedNmToCIDR($)
  2173. {
  2174. my ($mask) = @_;
  2175. return undef if !ESPEasy_isNmDotted($mask);
  2176. # dotted decimal to CIDR
  2177. my ($byte1, $byte2, $byte3, $byte4) = split(/\./, $mask);
  2178. my $num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
  2179. my $bin = unpack("B*", pack("N", $num));
  2180. my $count = ($bin =~ tr/1/1/);
  2181. return $count; # return number of netmask bits
  2182. }
  2183. # ------------------------------------------------------------------------------
  2184. sub ESPEasy_isIPv4($)
  2185. {
  2186. return 0 if !defined $_[0];
  2187. return 1 if($_[0]
  2188. =~ m/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/);
  2189. return 0;
  2190. }
  2191. # ------------------------------------------------------------------------------
  2192. sub ESPEasy_isIPv6($)
  2193. {
  2194. return 0 if !defined $_[0];
  2195. return 1 if ($_[0]
  2196. =~ m/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/);
  2197. return 0;
  2198. }
  2199. # ------------------------------------------------------------------------------
  2200. sub ESPEasy_isIPv64($)
  2201. {
  2202. return 0 if !defined $_[0];
  2203. return 1 if ESPEasy_isIPv4($_[0]) || ESPEasy_isIPv6($_[0]);
  2204. return 0;
  2205. }
  2206. # ------------------------------------------------------------------------------
  2207. sub ESPEasy_isNmDotted($)
  2208. {
  2209. return 0 if !defined $_[0];
  2210. return 1 if ($_[0]
  2211. =~ m/^(255|254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(255|254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(255|254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(255|254|252|248|240|224|192|128|0)$/);
  2212. return 0;
  2213. }
  2214. # ------------------------------------------------------------------------------
  2215. sub ESPEasy_isNmCIDRv4($)
  2216. {
  2217. return 0 if !defined $_[0];
  2218. return 1 if ($_[0] =~ m/^([0-2]?[0-9]|3[0-2])$/);
  2219. return 0;
  2220. }
  2221. # ------------------------------------------------------------------------------
  2222. sub ESPEasy_isNmCIDRv6($)
  2223. {
  2224. return 0 if !defined $_[0];
  2225. return 1 if ($_[0] =~ m/^([0-9]?[0-9]|1([0-1][0-9]|2[0-8]))$/);
  2226. return 0;
  2227. }
  2228. # ------------------------------------------------------------------------------
  2229. sub ESPEasy_isFqdn($)
  2230. {
  2231. return 0 if !defined $_[0];
  2232. return 1 if ($_[0]
  2233. =~ m/^(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)$/);
  2234. return 0;
  2235. }
  2236. # ------------------------------------------------------------------------------
  2237. sub ESPEasy_isHostname($)
  2238. {
  2239. return 0 if !defined $_[0];
  2240. return 1 if ($_[0] =~ m/^([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/)
  2241. && !(ESPEasy_isIPv4($_[0]) || ESPEasy_isIPv6($_[0]));
  2242. return 0;
  2243. }
  2244. # ------------------------------------------------------------------------------
  2245. sub ESPEasy_whoami() {return (split('::',(caller(1))[3]))[1] || '';}
  2246. # ------------------------------------------------------------------------------
  2247. sub ESPEasy_dumpSingleLine($)
  2248. {
  2249. my $saveIndent = $Data::Dumper::Indent; my $saveTerse = $Data::Dumper::Terse ;
  2250. $Data::Dumper::Indent = 0; $Data::Dumper::Terse = 1;
  2251. my $ret = Dumper($_[0]);
  2252. $Data::Dumper::Indent = $saveIndent; $Data::Dumper::Terse = $saveTerse;
  2253. return $ret;
  2254. }
  2255. # ------------------------------------------------------------------------------
  2256. sub ESPEasy_removeGit($)
  2257. {
  2258. my ($hash) = @_;
  2259. my $file = $attr{global}{modpath}."/ESPEasy.txt";
  2260. if (-f $file) {
  2261. unlink $file;
  2262. my $controls = $attr{global}{modpath}."/FHEM/controls.txt";
  2263. open(FH, $controls) || return "Can't open $controls: $!";
  2264. my $ret = join("", <FH>);
  2265. close(FH);
  2266. if ($ret =~ m/controls_ESPEasy.txt/) {
  2267. my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
  2268. Log3 $name, 1, "";
  2269. Log3 $name, 1, "================================================================================";
  2270. Log3 $name, 1, "";
  2271. Log3 $name, 1, "ESPEasy is part of the official FHEM distribution.";
  2272. Log3 $name, 1, "";
  2273. Log3 $name, 1, "Please remove ESPEasy Github repository from FHEM update by using the following";
  2274. Log3 $name, 1, "FHEM command: ";
  2275. Log3 $name, 1, "";
  2276. Log3 $name, 1, "update delete https://raw.githubusercontent.com/ddtlabs/ESPEasy/master/controls_ESPEasy.txt";
  2277. Log3 $name, 1, "";
  2278. Log3 $name, 1, "================================================================================";
  2279. Log3 $name, 1, "";
  2280. }
  2281. }
  2282. return undef;
  2283. }
  2284. 1;
  2285. =pod
  2286. =item device
  2287. =item summary Control and access to ESPEasy (Espressif ESP8266 WLAN-SoC)
  2288. =item summary_DE Steuerung und Zugriff auf ESPEasy (Espressif ESP8266 WLAN-SoC)
  2289. =begin html
  2290. <a name="ESPEasy"></a>
  2291. <h3>ESPEasy</h3>
  2292. <ul>
  2293. <p>Provides access and control to Espressif ESP8266 WLAN-SoC w/ ESPEasy</p>
  2294. Notes:
  2295. <ul>
  2296. <li>You have to define a bridge device before any logical device can be
  2297. defined.
  2298. </li>
  2299. <li>You have to configure your ESP to use "FHEM HTTP" controller protocol.
  2300. Furthermore ESP controller IP must match FHEM's IP address. ESP controller
  2301. port and the FHEM ESPEasy bridge port must be the same.
  2302. </li>
  2303. <li>
  2304. Max. 2 ESPEasy bridges can be defined at the same time: 1 for IPv4 and 1 for IPv6
  2305. </li>
  2306. <li>Further information about this module is available here:
  2307. <a href="https://forum.fhem.de/index.php/topic,55728.0.html">Forum #55728</a>
  2308. </li>
  2309. <br>
  2310. <li><b>UPDATE:
  2311. </li>
  2312. <li></b>For security reasons: if one or more of your ESPEasy device uses a
  2313. public IP address then you have to enable this explicitly or the device(s)
  2314. will be ignored/rejected:
  2315. </li>
  2316. <ul>
  2317. <li>
  2318. Enable all ESPEasy device IP addresses/subnets/regexs with the help of
  2319. bridge attributes
  2320. <a href="#ESPEasy_bridge_attr_allowedips">allowedIPs</a> /
  2321. <a href="#ESPEasy_bridge_attr_deniedips">deniedIPs</a>.
  2322. </li>
  2323. <li>
  2324. Enable authentication: see attribute
  2325. <a href="#ESPEasy_bridge_attr_authentication">authentication</a> and
  2326. bridge set <a href="#ESPEasy_bridge_set_user">user</a>
  2327. / <a href="#ESPEasy_bridge_set_pass">pass</a> commands.
  2328. </li>
  2329. </ul>
  2330. <br>
  2331. </ul>
  2332. Requirements:
  2333. <ul>
  2334. <li>
  2335. ESPEasy build &gt;= <a href="https://github.com/ESP8266nu/ESPEasy"
  2336. target="_new">R128</a> (self compiled) or an ESPEasy precompiled image
  2337. &gt;= <a href="http://www.letscontrolit.com/wiki/index.php/ESPEasy#Loading_firmware" target="_new">R140_RC3</a><br>
  2338. </li>
  2339. <li>perl module JSON<br>
  2340. Use "cpan install JSON" or operating system's package manager to install
  2341. Perl JSON Modul. Depending on your os the required package is named:
  2342. libjson-perl or perl-JSON.
  2343. </li>
  2344. </ul>
  2345. <h3>ESPEasy Bridge</h3>
  2346. <a name="ESPEasy_bridge_define"></a>
  2347. <b>Define </b>(bridge)<br><br>
  2348. <ul>
  2349. <code>define &lt;name&gt; ESPEasy bridge &lt;[IPV6:]port&gt;</code><br><br>
  2350. <li>
  2351. <a name=""><code>&lt;name&gt;</code></a><br>
  2352. Specifies a device name of your choise.<br>
  2353. example: <code>ESPBridge</code>
  2354. </li><br>
  2355. <li>
  2356. <a name=""><code>&lt;port&gt;</code></a><br>
  2357. Specifies TCP port for incoming ESPEasy http requests. This port must
  2358. <u>not</u> be used by any other application or daemon on your system and
  2359. must be in the range 1024..65535 unless you run your FHEM installation
  2360. with root permissions (not recommanded).<br>
  2361. If you want to define an IPv4 and an IPv6 bridge on the same TCP port
  2362. (recommanded) then it might be necessary on (some?) Linux
  2363. distributions to activate IPV6_V6ONLY socket option. Use <code>"echo
  2364. 1>/proc/sys/net/ipv6/bindv6only"</code> or systemctl for that purpose.<br>
  2365. eg. <code>8383</code><br>
  2366. eg. <code>IPV6:8383</code><br>
  2367. Example:<br>
  2368. <code>define ESPBridge ESPEasy bridge 8383</code></li><br>
  2369. </ul>
  2370. <br><a name="ESPEasy_bridge_get"></a>
  2371. <b>Get </b>(bridge)<br><br>
  2372. <ul>
  2373. <li><a name="ESPEasy_bridge_get_reading">&lt;reading&gt;</a><br>
  2374. returns the value of the specified reading</li>
  2375. <br>
  2376. <li><a name="ESPEasy_bridge_get_queueSize">queueSize</a><br>
  2377. returns number of entries for each queue (&lt;ip&gt;:number
  2378. [&lt;ip&gt;:number] [...]).
  2379. </li><br>
  2380. <li><a name="ESPEasy_bridge_get_user">user</a><br>
  2381. returns username used by basic authentication for incoming requests.
  2382. </li><br>
  2383. <li><a name="ESPEasy_bridge_get_pass">pass</a><br>
  2384. returns password used by basic authentication for incoming requests.
  2385. </li><br>
  2386. </ul>
  2387. <br><a name="ESPEasy_bridge_set"></a>
  2388. <b>Set </b>(bridge)<br><br>
  2389. <ul>
  2390. <li><a name="ESPEasy_bridge_set_help">help</a><br>
  2391. Shows set command usage<br>
  2392. required values: <code>help|pass|user|clearQueue</code></li><br>
  2393. <li><a name="ESPEasy_bridge_set_clearqueue">clearQueue</a><br>
  2394. Used to erase all command queues.<br>
  2395. required value: <code>&lt;none&gt;</code><br>
  2396. eg. : <code>set ESPBridge clearQueue</code></li><br>
  2397. <li><a name="ESPEasy_bridge_set_pass">pass</a><br>
  2398. Specifies password used by basic authentication for incoming requests.<br>
  2399. Note that attribute <a href="#ESPEasy_bridge_attr_authentication">authentication</a>
  2400. must be set to enable basic authentication, too.<br>
  2401. required value: <code>&lt;password&gt;</code><br>
  2402. eg. : <code>set ESPBridge pass secretpass</code></li><br>
  2403. <li><a name="ESPEasy_bridge_set_user">user</a><br>
  2404. Specifies username used by basic authentication for incoming requests.<br>
  2405. Note that attribute <a href="#ESPEasy_bridge_attr_authentication">authentication</a>
  2406. must be set to enable basic authentication, too.<br>
  2407. required value: <code>&lt;username&gt;</code><br>
  2408. eg. : <code>set ESPBridge user itsme</code></li><br>
  2409. </ul>
  2410. <br><a name="ESPEasy_bridge_attr"></a>
  2411. <b>Attributes </b>(bridge)<br><br>
  2412. <ul>
  2413. <li><a name="ESPEasy_bridge_attr_allowedips">allowedIPs</a><br>
  2414. Used to limit IPs or IP ranges of ESPs which are allowed to commit data.
  2415. <br>
  2416. Specify IP, IP/netmask, regexp or a comma separated list of these values.
  2417. Netmask can be written as bitmask or dotted decimal. Domain names for dns
  2418. lookups are not supported.<br>
  2419. Possible values: IPv64 address, IPv64/netmask, regexp<br>
  2420. Default: 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16,
  2421. fe80::/10, fc00::/7, ::1
  2422. <br><br>
  2423. Examles:<br>
  2424. <table><tr><td>
  2425. 10.68.30.147
  2426. </td><td>
  2427. =&gt; IPv4 single address
  2428. </td></tr><tr><td>
  2429. 10.68.30.0/25
  2430. </td><td>
  2431. =&gt; IPv4 CIDR network 192.168.30.0-127
  2432. </td></tr><tr><td>
  2433. 10.68.30.8/255.255.248.0
  2434. </td><td>
  2435. =&gt; IPv4 CIDR network 192.168.30.8-15
  2436. </td></tr><tr><td>
  2437. 192.168.30.1([0-4][0-9]|50)
  2438. </td><td>
  2439. =&gt; IPv4 range w/ regexp: 192.168.30.100-150
  2440. </td></tr><tr><td>
  2441. 2001:1a59:50a9::aaaa
  2442. </td><td>
  2443. =&gt; IPv6 single address
  2444. </td></tr><tr><td>
  2445. 2001:1a59:50a9::/48
  2446. </td><td>
  2447. =&gt; IPv6 network 2001:1a59:50a9::/48
  2448. </td></tr><tr><td>
  2449. 2001:1a59:50a9::01[0-4][0-9]
  2450. </td><td>
  2451. =&gt; IPv6 range w/ regexp: 2001:1a59:50a9::0100-0149
  2452. </tr></td>
  2453. </table>
  2454. <span style="font-size:small;">Note that short IPv6 notation (::) must be
  2455. used in conjunction with regexps.</span>
  2456. </li><br>
  2457. <li><a name="ESPEasy_bridge_attr_authentication">authentication</a><br>
  2458. Used to enable basic authentication for incoming requests.<br>
  2459. Note that user, pass and authentication attribute must be set to activate
  2460. basic authentication<br>
  2461. Possible values: 0,1<br>
  2462. Default: 0</li><br>
  2463. <li><a name="ESPEasy_bridge_attr_autocreate">autocreate</a><br>
  2464. Used to overwrite global autocreate setting.<br>
  2465. Global autocreate setting will be detected by global attribut
  2466. 'autoload_undefined_devices'<br>
  2467. Possible values: 0,1<br>
  2468. Default: 0 (disabled)</li><br>
  2469. <li><a name="ESPEasy_bridge_attr_autosave">autosave</a><br>
  2470. Used to overwrite global autosave setting.<br>
  2471. Global autosave setting will be detected by global attribut 'autosave'.
  2472. <br>
  2473. Possible values: 0,1<br>
  2474. Default: 0 (disabled)</li><br>
  2475. <li><a name="ESPEasy_bridge_attr_combinedevices">combineDevices</a><br>
  2476. Used to gather all ESP devices of a single ESP into 1 FHEM device even if
  2477. different ESP devices names are used.<br>
  2478. Possible values: 0, 1, IPv64 address, IPv64/netmask, ESPname or a comma
  2479. separated list consisting of these values.<br>
  2480. Netmasks can be written as bitmask or dotted decimal. Domain names for dns
  2481. lookups are not supported.<br>
  2482. Default: 0 (disabled for all ESPs)<br>
  2483. Eg. 1 (globally enabled)<br>
  2484. Eg. ESP01,ESP02<br>
  2485. Eg. 10.68.30.1,10.69.0.0/16,ESP01,2002:1a59:50a9::/48</li><br>
  2486. <li><a name="ESPEasy_bridge_attr_deniedips">deniedIPs</a><br>
  2487. Used to define IPs or IP ranges of ESPs which are denied to commit data.
  2488. <br>
  2489. Syntax see <a href="#ESPEasy_bridge_attr_allowedips">allowedIPs</a>.<br>
  2490. This attribute will overwrite any IP or range defined by
  2491. <a href="#ESPEasy_bridge_attr_allowedips">allowedIPs</a>.<br>
  2492. Default: none (no IPs are denied)</li><br>
  2493. <li><a name="ESPEasy_bridge_attr_disable">disable</a><br>
  2494. Used to disable device.<br>
  2495. Possible values: 0,1<br>
  2496. Default: 0 (eanble)</li><br>
  2497. <li><a name="ESPEasy_bridge_attr_httpreqtimeout">httpReqTimeout</a><br>
  2498. Specifies seconds to wait for a http answer from ESP8266 device.<br>
  2499. Possible values: 4..60<br>
  2500. Default: 10 seconds</li><br>
  2501. <li><a name="ESPEasy_bridge_attr_maxhttpsessions">maxHttpSessions</a><br>
  2502. Limit maximal concurrent outgoing http sessions to a single ESP.<br>
  2503. Set to 0 to disable this feature. At the moment (ESPEasy R147) it seems
  2504. to be possible to send 5 "concurrent" requests if nothing else keeps the
  2505. esp working.<br>
  2506. Possible values: 0..9<br>
  2507. Default: 3</li><br>
  2508. <li><a name="ESPEasy_bridge_attr_maxqueuesize">maxQueueSize</a><br>
  2509. Limit maximal queue size (number of commands in queue) for outgoing http
  2510. requests.<br>
  2511. If command queue size is reached (eg. ESP is offline) any further
  2512. command will be ignored and discard.<br>
  2513. Possible values: >10<br>
  2514. Default: 250</li><br>
  2515. <li><a name="ESPEasy_bridge_attr_resendfailedcmd">resendFailedCmd</a><br>
  2516. Used to resend commands when http request returned an error<br>
  2517. Possible values: 0,1<br>
  2518. Default: 0 (disabled)</li><br>
  2519. <li><a name="ESPEasy_bridge_attr_uniqids">uniqIDs</a><br>
  2520. This attribute has been removed.</li><br>
  2521. <li><a href="#readingFnAttributes">readingFnAttributes</a>
  2522. </li><br>
  2523. </ul>
  2524. <h3>ESPEasy Device</h3>
  2525. <a name="ESPEasy_device_define"></a>
  2526. <b>Define </b>(logical device)<br><br>
  2527. <ul>
  2528. Notes: Logical devices will be created automatically if any values are
  2529. received by the bridge device and autocreate is not disabled. If you
  2530. configured your ESP in a way that no data is send independently then you
  2531. have to define logical devices. At least wifi rssi value could be defined
  2532. to use autocreate and presence detection.<br><br>
  2533. <code>define &lt;name&gt; ESPEasy &lt;ip|fqdn&gt; &lt;port&gt; &lt;IODev&gt; &lt;identifier&gt;</code><br><br>
  2534. <li>
  2535. <a name=""><code>&lt;name&gt;</code></a><br>
  2536. Specifies a device name of your choise.<br>
  2537. example: <code>ESPxx</code>
  2538. </li><br>
  2539. <li>
  2540. <a name=""><code>&lt;ip|fqdn&gt;</code></a><br>
  2541. Specifies ESP IP address or hostname.<br>
  2542. example: <code>172.16.4.100</code><br>
  2543. example: <code>espxx.your.domain.net</code>
  2544. </li><br>
  2545. <li>
  2546. <a name=""><code>&lt;port&gt;</code></a><br>
  2547. Specifies http port to be used for outgoing request to your ESP. Should be 80<br>
  2548. example: <code>80</code>
  2549. </li><br>
  2550. <li>
  2551. <a name=""><code>&lt;IODev&gt;</code></a><br>
  2552. Specifies your ESP bridge device. See above.<br>
  2553. example: <code>ESPBridge</code>
  2554. </li><br>
  2555. <li>
  2556. <a name=""><code>&lt;identifier&gt;</code></a><br>
  2557. Specifies an identifier that will bind your ESP to this device.<br>
  2558. This identifier must be specified in this form:<br>
  2559. &lt;esp name&gt;_&lt;esp device name&gt;.<br>
  2560. If bridge attribute <a href="#ESPEasy_bridge_attr_combinedevices">combineDevices</a> is used then &lt;esp name&gt; is used, only.<br>
  2561. ESP name and device name can be found here:<br>
  2562. &lt;esp name&gt;: =&gt; ESP GUI =&gt; Config =&gt; Main Settings =&gt; Name<br>
  2563. &lt;esp device name&gt;: =&gt; ESP GUI =&gt; Devices =&gt; Edit =&gt; Task Settings =&gt; Name<br>
  2564. example: <code>ESPxx_DHT22</code><br>
  2565. example: <code>ESPxx</code>
  2566. </li><br>
  2567. <li>Example:<br>
  2568. <code>define ESPxx ESPEasy 172.16.4.100 80 ESPBridge EspXX_SensorXX</code>
  2569. </li><br>
  2570. </ul>
  2571. <br><a name="ESPEasy_device_get"></a>
  2572. <b>Get </b>(logical device)<br><br>
  2573. <ul>
  2574. <li><a name="ESPEasy_device_get_reading">&lt;reading&gt;</a><br>
  2575. returns the value of the specified reading
  2576. </li><br>
  2577. <li><a name="ESPEasy_bridge_get_pinmap">pinMap</a><br>
  2578. returns possible alternative pin names that can be used in commands
  2579. </li><br>
  2580. </ul>
  2581. <br><a name="ESPEasy_device_set"></a>
  2582. <b>Set </b>(logical device)
  2583. <br><br>
  2584. <ul>
  2585. Notes:<br>
  2586. - Commands are case insensitive.<br>
  2587. - Users of Wemos D1 mini or NodeMCU can use Arduino pin names instead of
  2588. GPIO numbers.<br>
  2589. &nbsp;&nbsp;D1 =&gt; GPIO5, D2 =&gt; GPIO4, ...,TX =&gt; GPIO1 (see: get
  2590. <a href="#ESPEasy_bridge_get_pinmap">pinMap</a>)<br>
  2591. - low/high state can be written as 0/1 or on/off
  2592. <br>
  2593. <br>
  2594. <li><a name="ESPEasy_device_set_clearreadings">clearReadings</a><br>
  2595. Delete all readings that are auto created by received sensor values
  2596. since last FHEM restart.<br>
  2597. <ul>
  2598. <li>arguments: <code>none</code></li>
  2599. <li>example: set &lt;esp&gt; clearReadings</li>
  2600. </ul>
  2601. </li><br>
  2602. <li><a name="ESPEasy_device_set_help">help</a><br>
  2603. Shows set command usage.<br>
  2604. <ul>
  2605. <li>arguments: <code>a valid set command</code></li>
  2606. <li>example: <code>set &lt;esp&gt; help gpio</code></li>
  2607. </ul>
  2608. </li><br>
  2609. <li><a name="ESPEasy_device_set_raw">raw</a><br>
  2610. Can be used for own ESP plugins or new ESPEasy commands that are not
  2611. considered by this module at the moment. Any argument will be sent
  2612. directly to the ESP.
  2613. <ul>
  2614. <li>arguments: raw &lt;cmd&gt; [&lt;arg1&gt;] [&lt;arg2&gt;] [&lt;...&gt;]</li>
  2615. <li>example: set &lt;esp&gt; raw myCommand p1 p2 p3</li>
  2616. </ul>
  2617. </li><br>
  2618. <li><a name="ESPEasy_device_set_statusrequest">statusRequest</a><br>
  2619. Trigger a statusRequest for configured GPIOs (see attribut pollGPIOs)
  2620. and do a presence check<br>
  2621. <ul>
  2622. <li>arguments: <code>n/a</code></li>
  2623. <li>example: <code>set &lt;esp&gt; statusRequest</code></li>
  2624. </ul><br>
  2625. </li><br>
  2626. </ul>
  2627. <i><b>Note:</b> The following commands are built-in ESPEasy Software commands
  2628. that are send directly to the ESP after passing a syntax check. A detailed
  2629. description can be found here:
  2630. <a href="http://www.letscontrolit.com/wiki/index.php/ESPEasy_Command_Reference"
  2631. target="_NEW">ESPEasy Command Reference</a></i><br><br>
  2632. <ul>
  2633. </ul>
  2634. <u>ESPEasy generic I/O commands:</u><br><br>
  2635. <ul>
  2636. <li><a name="ESPEasy_device_set_gpio">GPIO</a><br>
  2637. Switch output pins to high/low<br>
  2638. <ul>
  2639. <li>arguments: <code>&lt;pin&gt; &lt;0|1|off|on&gt;</code></li>
  2640. <li>example: <code>set &lt;esp&gt; gpio 14 on</code></li>
  2641. </ul>
  2642. </li><br>
  2643. <li><a name="ESPEasy_device_set_pwm">PWM</a><br>
  2644. Direct PWM control of output pins<br>
  2645. <ul>
  2646. <li>arguments: <code>&lt;pin&gt; &lt;level&gt;</code></li>
  2647. <li>example: <code>set &lt;esp&gt; pwm 14 512</code></li>
  2648. </ul>
  2649. </li><br>
  2650. <li><a name="ESPEasy_device_set_pwmfade">PWMFADE</a><br>
  2651. Fade output pins to a pwm value<br>
  2652. <ul>
  2653. <li>arguments: <code>&lt;pin&gt; &lt;target pwm&gt; &lt;duration 1-30s&gt;</code></li>
  2654. <li>example: <code>set &lt;esp&gt; pwmfade 14 1023 10</code></li>
  2655. </ul>
  2656. </li><br>
  2657. <li><a name="ESPEasy_device_set_pulse">Pulse</a><br>
  2658. Direct pulse control of output pins<br>
  2659. <ul>
  2660. <li>arguments: <code>&lt;pin&gt; &lt;0|1|off|on&gt; &lt;duration&gt;</code></li>
  2661. <li>example: <code>set &lt;esp&gt; pulse 14 on 10</code></li>
  2662. </ul>
  2663. </li><br>
  2664. <li><a name="ESPEasy_device_set_logpulse">LongPulse</a><br>
  2665. Direct pulse control of output pins<br>
  2666. <ul>
  2667. <li>arguments: <code>&lt;pin&gt; &lt;0|1|off|on&gt; &lt;duration&gt;</code></li>
  2668. <li>example: <code>set &lt;esp&gt; longpulse 14 on 10</code></li>
  2669. </ul>
  2670. </li><br>
  2671. <li><a name="ESPEasy_device_set_pcfgpio">PCFGpio</a><br>
  2672. Control PCF8574 (8-bit I/O expander for I2C-bus)<br>
  2673. <ul>
  2674. <li>arguments: <code>&lt;port&gt; &lt;0|1|off|on&gt;</code></li>
  2675. <li>example: <code>set &lt;esp&gt; PCFGpio 128 on</code></li>
  2676. </ul>
  2677. Port numbering see:
  2678. <a href="https://www.letscontrolit.com/wiki/index.php/PCF8574#Input">
  2679. ESPEasy Wiki PCF8574</a>
  2680. </li><br>
  2681. <li><a name="ESPEasy_device_set_pcfpulse">PCFPulse</a><br>
  2682. Control PCF8574 (8-bit I/O expander for I2C-bus)
  2683. <ul>
  2684. <li>arguments: <code>&lt;port&gt; &lt;0|1|off|on&gt; &lt;duration&gt;</code></li>
  2685. <li>example: <code>set &lt;esp&gt; PCFPulse 128 on 10</code></li>
  2686. </ul>
  2687. Port numbering see:
  2688. <a href="https://www.letscontrolit.com/wiki/index.php/PCF8574#Input">
  2689. ESPEasy Wiki PCF8574</a>
  2690. </li><br>
  2691. <li><a name="ESPEasy_device_set_pcflongpulse">PCFLongPulse</a><br>
  2692. Control on PCF8574 (8-bit I/O expander for I2C-bus)
  2693. <ul>
  2694. <li>arguments: <code>&lt;port&gt; &lt;0|1|off|on&gt; &lt;duration&gt;</code></li>
  2695. <li>example: <code>set &lt;esp&gt; PCFLongPulse 128 on 10</code></li>
  2696. </ul>
  2697. Port numbering see:
  2698. <a href="https://www.letscontrolit.com/wiki/index.php/PCF8574#Input">
  2699. ESPEasy Wiki PCF8574</a>
  2700. </li><br>
  2701. <li><a name="ESPEasy_device_set_mcpgpio">mcpgpio</a><br>
  2702. Control MCP23017 output pins (16-Bit I/O Expander with Serial Interface)<br>
  2703. <ul>
  2704. <li>arguments: <code>&lt;port&gt; &lt;0|1|off|on&gt;</code></li>
  2705. <li>example: <code>set &lt;esp&gt; mcpgpio 48 on</code></li>
  2706. </ul>
  2707. Port numbering see:
  2708. <a href="https://www.letscontrolit.com/wiki/index.php/MCP23017#Input">
  2709. ESPEasy Wiki MCP23017</a>
  2710. </li><br>
  2711. <li><a name="ESPEasy_device_set_pcapwm">pcapwm</a><br>
  2712. Control PCA9685 (16-channel / 12-bit PWM I2C-bus controller)<br>
  2713. <ul>
  2714. <li>arguments: <code>&lt;pin 0-15&gt; &lt;level 0-4095&gt;</code></li>
  2715. <li>example: <code>set &lt;esp&gt; pcapwm 15 4095</code></li>
  2716. </ul>
  2717. </li><br>
  2718. </ul>
  2719. <u>ESPEasy motor control commands:</u><br><br>
  2720. <ul>
  2721. <li><a name="ESPEasy_device_set_servo">Servo</a><br>
  2722. Direct control of servo motors<br>
  2723. <ul>
  2724. <li>arguments: <code>&lt;servoNo&gt; &lt;pin&gt; &lt;position&gt;</code></li>
  2725. <li>example: <code>set &lt;esp&gt; servo 1 14 100</code></li>
  2726. </ul>
  2727. </li><br>
  2728. <li><a name="ESPEasy_device_set_motorshieldcmd">MotorShieldCMD</a><br>
  2729. Control a DC motor or stepper<br>
  2730. <ul>
  2731. <li>
  2732. arguments: <code>DCMotor &lt;motornumber&gt; &lt;forward|backward|release&gt; &lt;speed&gt;</code><br>
  2733. arguments: <code>Stepper &lt;motornumber&gt; &lt;forward|backward|release&gt; &lt;steps&gt; &lt;single|double|interleave|microstep&gt;</code>
  2734. </li>
  2735. <li>
  2736. example: <code>set &lt;esp&gt; MotorShieldCMD DCMotor 1 forward 10</code><br>
  2737. example: <code>set &lt;esp&gt; MotorShieldCMD Stepper 1 backward 25 single</code>
  2738. </li>
  2739. </ul>
  2740. </li><br>
  2741. </ul>
  2742. <u>ESPEasy display related commands:</u><br>
  2743. <ul><br>
  2744. <li><a name="ESPEasy_device_set_lcd">lcd</a><br>
  2745. Write text messages to LCD screen<br>
  2746. Pay attention to attributes
  2747. <a href="#ESPEasy_device_attr_displaytextencode">displayTextEncode</a> and
  2748. <a href="#ESPEasy_device_attr_displaytextwidth">displayTextWidth</a>.<br>
  2749. <ul>
  2750. <li>arguments: <code>&lt;row&gt; &lt;col&gt; &lt;text&gt;</code></li>
  2751. <li>example: <code>set &lt;esp&gt; lcd 1 1 Test a b c</code></li>
  2752. </ul>
  2753. </li><br>
  2754. <li><a name="ESPEasy_device_set_lcdcmd">lcdcmd</a><br>
  2755. Control LCD screen<br>
  2756. <ul>
  2757. <li>arguments: <code>&lt;on|off|clear&gt;</code></li>
  2758. <li>example: <code>set &lt;esp&gt; lcdcmd clear</code></li>
  2759. </ul>
  2760. </li><br>
  2761. <li><a name="ESPEasy_device_set_oled">oled</a><br>
  2762. Write text messages to OLED screen<br>
  2763. Pay attention to attributes
  2764. <a href="#ESPEasy_device_attr_displaytextencode">displayTextEncode</a> and
  2765. <a href="#ESPEasy_device_attr_displaytextwidth">displayTextWidth</a>.<br>
  2766. <ul>
  2767. <li>arguments: <code>&lt;row&gt; &lt;col&gt; &lt;text&gt;</code></li>
  2768. <li>example: <code>set &lt;esp&gt; oled 1 1 Test a b c</code></li>
  2769. </ul>
  2770. </li><br>
  2771. <li><a name="ESPEasy_device_set_oledcmd">oledcmd</a><br>
  2772. Control OLED screen<br>
  2773. <ul>
  2774. <li>arguments: <code>&lt;on|off|clear&gt;</code></li>
  2775. <li>example: <code>set &lt;esp&gt; oledcmd clear</code></li>
  2776. </ul>
  2777. </li><br>
  2778. <li><a name="ESPEasy_device_set_oledframedcmd">oledframedcmd</a><br>
  2779. Switch oledframed on/off<br>
  2780. <ul>
  2781. <li>arguments: <code>&lt;on|off&gt;</code></li>
  2782. <li>example: <code>set &lt;esp&gt; oledframedcmd on</code></li>
  2783. </ul>
  2784. </li><br>
  2785. </ul>
  2786. <u>ESPEasy DMX related commands:</u>
  2787. <ul><br>
  2788. <li><a name="ESPEasy_device_set_dmx">dmx</a><br>
  2789. Send DMX commands to a device<br>
  2790. <ul>
  2791. <li>arguments: <code>&lt;on|off|log|value|channel=value[,value][...]&gt;</code></li>
  2792. <li>example: <code>set &lt;esp&gt; dmx 1=255,2=127</code></li>
  2793. </ul>
  2794. </li><br>
  2795. </ul>
  2796. <u>ESPEasy LED/Lights related commands:</u>
  2797. <ul><br>
  2798. <li><a name="ESPEasy_device_set_lights">Lights</a> (plugin can be found <a
  2799. href="https://github.com/ddtlabs/ESPEasy-Plugin-Lights target="_NEW">here</a>)<br>
  2800. Control a rgb or ct light<br>
  2801. <ul>
  2802. <li>arguments: <code>&lt;rgb|ct|pct|on|off|toggle&gt; [&lt;hex-rgb|color-temp|pct-value&gt;] [&lt;fading time&gt;]</code></li>
  2803. <li>examples:<br>
  2804. <code>set &lt;esp&gt; lights rgb aa00aa</code><br>
  2805. <code>set &lt;esp&gt; lights rgb aa00aa 10</code><br>
  2806. <code>set &lt;esp&gt; lights ct 3200</code><br>
  2807. <code>set &lt;esp&gt; lights ct 3200 10</code><br>
  2808. <code>set &lt;esp&gt; lights pct 50</code><br>
  2809. <code>set &lt;esp&gt; lights on</code><br>
  2810. <code>set &lt;esp&gt; lights off</code><br>
  2811. <code>set &lt;esp&gt; lights toggle</code><br>
  2812. </li>
  2813. </ul>
  2814. </li><br>
  2815. <li><a name="ESPEasy_device_set_nfx">nfx</a> (plugin can be found
  2816. <a target="_blank" href="https://github.com/djcysmic/NeopixelBusFX">here</a>)<br>
  2817. Control nfx plugin<br>
  2818. Note: To use FHEMWEB's colorpicker and slider widgets you have to set
  2819. Attribut <a href="ESPEasy_device_attr_maplightscmds">mapLightCmds</a>.
  2820. <ul>
  2821. <li>arguments: <code>&lt;off|on|dim|line|one|all|rgb|fade|colorfade|rainbow|kitt|comet|theatre|scan|dualscan|twinkle|twinklefade|sparkle|wipe|fire|stop|statusrequest|fadetime|fadedelay|speed|count|bgcolor&gt; [&lt;rgb&gt;] [&lt;fadeing time&gt;] [&lt;delay time&gt;]</code></li>
  2822. <li>examples:<br>
  2823. <code>
  2824. set &lt;esp&gt; nfx all 00ff00 100<br>
  2825. set &lt;esp&gt; nfx rgb aa00ff 1000 10<br>
  2826. set &lt;esp&gt; nfx line 0 100 f0f0f0c<br>
  2827. </code>
  2828. </li>
  2829. </ul>
  2830. </li><br>
  2831. <li><a name="ESPEasy_device_set_candle">candle</a><br>
  2832. Control candle rgb plugin<br>
  2833. <ul>
  2834. <li>arguments:
  2835. <code>CANDLE:&lt;FlameType&gt;:&lt;Color&gt;:&lt;Brightness&gt;</code></li>
  2836. <li>example: <code>set &lt;esp&gt; CANDLE:4:FF0000:200</code></li>
  2837. </ul>
  2838. </li><br>
  2839. <li><a name="ESPEasy_device_set_neopixel">neopixel</a><br>
  2840. Control neopixel plugin (single LED)<br>
  2841. <ul>
  2842. <li>arguments: <code>&lt;led nr&gt; &lt;red 0-255&gt; &lt;green 0-255&gt; &lt;blue 0-255&gt;</code></li>
  2843. <li>example: <code>set &lt;esp&gt; neopixel 1 255 255 255</code></li>
  2844. </ul>
  2845. </li><br>
  2846. <li><a name="ESPEasy_device_set_neopixelall">neopixelall</a><br>
  2847. Control neopixel plugin (all together)<br>
  2848. <ul>
  2849. <li>arguments: <code>&lt;red 0-255&gt; &lt;green 0-255&gt; &lt;blue 0-255&gt;</code></li>
  2850. <li>example: <code>set &lt;esp&gt; neopixelall 255 255 255</code></li>
  2851. </ul>
  2852. </li><br>
  2853. <li><a name="ESPEasy_device_set_neopixelline">neopixelline</a><br>
  2854. Control neopixel plugin (line)<br>
  2855. <ul>
  2856. <li>arguments: <code>&lt;start led no&gt; &lt;stop led no&gt; &lt;red 0-255&gt; &lt;green 0-255&gt; &lt;blue 0-255&gt;</code></li>
  2857. <li>example: <code>set &lt;esp&gt; neopixelline 1 5 0 127 255</code></li>
  2858. </ul>
  2859. </li><br>
  2860. </ul>
  2861. <u>ESPEasy sound related commands:</u>
  2862. <ul><br>
  2863. <li><a name="ESPEasy_device_set_tone">tone</a><br>
  2864. Play a tone on a pin via a speaker or piezo element (ESPEasy &gt;=
  2865. 2.0.0-dev6)
  2866. <br>
  2867. <ul>
  2868. <li>arguments: <code>&lt;pin&gt; &lt;freq Hz&gt; &lt;duration s&gt;</code></li>
  2869. <li>example: <code>set &lt;esp&gt; tone 14 4000 1</code></li>
  2870. </ul>
  2871. </li><br>
  2872. <li><a name="ESPEasy_device_set_rtttl">rtttl</a><br>
  2873. Play melodies via <a target="_NEW"
  2874. href="https://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language#Technical_specification">RTTTL</a>
  2875. (ESPEasy &gt;= 2.0.0-dev6)
  2876. <br>
  2877. <ul>
  2878. <li>arguments: &lt;rtttl&gt; &lt;pin&gt;:&lt;rtttl codes&gt;</li>
  2879. <li>example: <code>set &lt;esp&gt; rtttl 14:d=10,o=6,b=180,c,e,g</code></li>
  2880. </ul>
  2881. </li><br>
  2882. <li><a name="ESPEasy_device_set_buzzer">buzzer</a><br>
  2883. Beep a short time<br>
  2884. <ul>
  2885. <li>arguments: <code>none</code></li>
  2886. <li>example: <code>set &lt;esp&gt; buzzer</code></li>
  2887. </ul>
  2888. </li><br>
  2889. </ul>
  2890. <u>ESPEasy miscellaneous commands:</u>
  2891. <ul><br>
  2892. <li><a name="ESPEasy_device_set_event">Event</a><br>
  2893. Trigger an ESP event. Such events can be used in ESP Easy rules.<br>
  2894. <ul>
  2895. <li>arguments: <code>&lt;string&gt;</code></li>
  2896. <li>example: <code>set &lt;esp&gt; event testevent</code></li>
  2897. </ul>
  2898. </li><br>
  2899. <li><a name="ESPEasy_device_set_irsend">irsend</a><br>
  2900. Send ir codes via "Infrared Transmit" Plugin<br>
  2901. Supported protocols are: NEC, JVC, RC5, RC6, SAMSUNG, SONY, PANASONIC at
  2902. the moment. As long as official documentation is missing you can find
  2903. some details here:
  2904. <a href="http://www.letscontrolit.com/forum/viewtopic.php?f=5&t=328" target="_NEW">
  2905. IR Transmitter thread #1</a> and
  2906. <a
  2907. href="https://www.letscontrolit.com/forum/viewtopic.php?t=328&start=61" target="_NEW">
  2908. IR Transmitter thread #61</a>.<br>
  2909. <ul>
  2910. <li>
  2911. arguments: <code>&lt;NEC|JVC|RC5|RC6|SAMSUNG|SONY|PANASONIC&gt; &lt;hex code&gt; &lt;bit length&gt;</code><br>
  2912. arguments: <code>&lt;RAW&gt; &lt;B32 raw&gt; &lt;frequenz&gt; &lt;pulse length&gt; &lt;blank length&gt;</code>
  2913. </li>
  2914. <li>
  2915. example: <code>set &lt;esp&gt; irsend NEC 7E81542B 32</code><br>
  2916. example: <code>set &lt;esp&gt; irsend RAW 3U0GGL8AGGK588A22K58ALALALAGL1A22LAK45ALALALALALALALALAL1AK5 38 512 256</code>
  2917. </li>
  2918. </ul>
  2919. </li><br>
  2920. <li><a name="ESPEasy_device_set_serialsend">serialsend</a><br>
  2921. Used for ser2net plugin<br>
  2922. <ul>
  2923. <li>arguments: <code>&lt;string&gt;</code></li>
  2924. <li>example: <code>set &lt;esp&gt; serialsend test</code></li>
  2925. </ul>
  2926. </li><br>
  2927. </ul>
  2928. <u>ESPEasy administrative commands</u> (be careful !!!):
  2929. <ul><br>
  2930. <li><a name="ESPEasy_device_set_erase">erase</a><br>
  2931. Wipe out ESP flash memory<br>
  2932. <ul>
  2933. <li>arguments: <code>none</code></li>
  2934. <li>example: <code>set &lt;esp&gt; erase</code></li>
  2935. </ul>
  2936. </li><br>
  2937. <li><a name="ESPEasy_device_set_reboot">reboot</a><br>
  2938. Used to reboot your ESP<br>
  2939. <ul>
  2940. <li>arguments: <code>none</code></li>
  2941. <li>example: <code>set &lt;esp&gt; reboot</code></li>
  2942. </ul>
  2943. </li><br>
  2944. <li><a name="ESPEasy_device_set_reset">reset</a><br>
  2945. Do a factory reset on the ESP<br>
  2946. <ul>
  2947. <li>arguments: <code>none</code></li>
  2948. <li>example: <code>set &lt;esp&gt; reset</code></li>
  2949. </ul>
  2950. </li><br>
  2951. </ul>
  2952. <u>ESPEasy experimental commands:</u> (The following commands can be changed or removed at any time)
  2953. <ul><br>
  2954. <li><a name="ESPEasy_device_set_rgb">rgb</a><br>
  2955. Used to control a rgb light wo/ an ESPEasy plugin.<br>
  2956. You have to set attribute <a href="#ESPEasy_device_attr_rgbgpios">rgbGPIOs</a> to
  2957. enable this feature. Default colorpicker mode is HSVp but can be adjusted
  2958. with help of attribute <a href="#ESPEasy_device_attr_colorpicker">colorpicker</a>
  2959. to HSV or RGB. Set attribute <a href="#webCmd">webCmd</a> to rgb to
  2960. display a colorpicker in FHEMWEB room view and on detail page.<br>
  2961. <ul>
  2962. <li>
  2963. arguments: <code>&lt;rrggbb&gt;|on|off|toggle</code>
  2964. </li>
  2965. <li>
  2966. examples:<br>
  2967. <code>set &lt;esp&gt; rgb 00FF00</code><br>
  2968. <code>set &lt;esp&gt; rgb on</code><br>
  2969. <code>set &lt;esp&gt; rgb off</code><br>
  2970. <code>set &lt;esp&gt; rgb toggle</code><br>
  2971. </li>
  2972. <li>Full featured example:<br>
  2973. attr &lt;ESP&gt; colorpicker HSVp<br>
  2974. attr &lt;ESP&gt; devStateIcon { ESPEasy_devStateIcon($name) }<br>
  2975. attr &lt;ESP&gt; Interval 30<br>
  2976. attr &lt;ESP&gt; parseCmdResponse status,pwm<br>
  2977. attr &lt;ESP&gt; pollGPIOs D6,D7,D8<br>
  2978. attr &lt;ESP&gt; rgbGPIOs D6,D7,D8<br>
  2979. attr &lt;ESP&gt; webCmd rgb:rgb ff0000:rgb 00ff00:rgb 0000ff:toggle:on:off
  2980. </li>
  2981. </ul>
  2982. </li><br>
  2983. </ul>
  2984. <u>ESPEasy deprecated commands:</u> (will be removed in a later version)
  2985. <ul><br>
  2986. <li><a name="ESPEasy_device_set_status">status</a><br>
  2987. Request esp device status (eg. gpio)<br>
  2988. See attributes: parseCmdResponse, readingPrefixGPIO, readingSuffixGPIOState
  2989. <ul>
  2990. <li>arguments: <code>&lt;pin&gt;</code></li>
  2991. <li>example: <code>set &lt;esp&gt; status 14</code></li>
  2992. </ul>
  2993. </li><br>
  2994. </ul>
  2995. <br><a name="ESPEasy_device_attr"></a>
  2996. <b>Attributes</b> (logical device)<br><br>
  2997. <ul>
  2998. <li><a name="ESPEasy_device_attr_adjustvalue">adjustValue</a><br>
  2999. Used to adjust sensor values<br>
  3000. Must be a space separated list of &lt;reading&gt;:&lt;formula&gt;.
  3001. Reading can be a regexp. Formula can be an arithmetic expression like
  3002. 'round(($VALUE-32)*5/9,2)'.
  3003. If $VALUE is omitted in formula then it will be added to the beginning of
  3004. the formula. So you can simple write 'temp:+20' or '.*:*4'<br>
  3005. Modified or ignored values are marked in the system log (verbose 4). Use
  3006. verbose 5 logging to see more details.<br>
  3007. If the used sub function returns 'undef' then the value will be ignored.
  3008. <br>
  3009. The following variables can be used if necessary:
  3010. <ul>
  3011. <li>$VALUE contains the original value</li>
  3012. <li>$READING contains the reading name</li>
  3013. <li>$NAME contains the device name</li>
  3014. </ul>
  3015. Default: none<br>
  3016. Eg. <code>attr ESPxx adjustValue humidity:+0.1
  3017. temperature*:($VALUE-32)*5/9</code><br>
  3018. Eg. <code>attr ESPxx adjustValue
  3019. .*:my_OwnFunction($NAME,$READING,$VALUE)</code><br>
  3020. <br>
  3021. Sample function to ignore negative values:<br>
  3022. <code>
  3023. sub my_OwnFunction($$$) {<br>
  3024. &nbsp;&nbsp;my ($name,$reading,$value) = @_;<br>
  3025. &nbsp;&nbsp;return ($value < 0) ? undef : $value;<br>
  3026. }<br>
  3027. </code>
  3028. </li><br>
  3029. <li><a name="ESPEasy_device_attr_colorpicker">colorpicker</a><br>
  3030. Used to select colorpicker mode<br>
  3031. Possible values: RGB,HSV,HSVp<br>
  3032. Default: HSVp
  3033. </li><br>
  3034. <li><a name="ESPEasy_device_attr_colorpickerctcw">colorpickerCTcw</a><br>
  3035. Used to select ct colorpicker's cold white color temperature in Kelvin<br>
  3036. Possible values: &gt; colorpickerCTww<br>
  3037. Default: 6000
  3038. </li><br>
  3039. <li><a name="ESPEasy_device_attr_colorpickerctww">colorpickerCTww</a><br>
  3040. Used to select ct colorpicker's warm white color temperature in Kelvin<br>
  3041. Possible values: &lt; colorpickerCTcw<br>
  3042. Default: 2000
  3043. </li><br>
  3044. <li><a name="ESPEasy_device_attr_disable">disable</a><br>
  3045. Used to disable device<br>
  3046. Possible values: 0,1<br>
  3047. Default: 0
  3048. </li><br>
  3049. <li><a name="ESPEasy_device_attr_displaytextencode">displayTextEncode</a><br>
  3050. Used to disable url encoding for text that is send to oled/lcd displays.
  3051. Useful if you want to encode the text by yourself.<br>
  3052. Possible values: 0,1<br>
  3053. Default: 1 (enabled)
  3054. </li><br>
  3055. <li><a name="ESPEasy_device_attr_displaytextwidth">displayTextWidth</a><br>
  3056. Used to specify number of characters per display line.<br>
  3057. If set then all characters before and after the text on the same line will
  3058. be overwritten with spaces. Attribute
  3059. <a href="#ESPEasy_device_attr_displaytextencode">displayTextEncode</a> must not be
  3060. disabled to use this feature. A 128x64px display has 16 characters per
  3061. line if you are using a 8px font.<br>
  3062. Possible values: integer<br>
  3063. Default: 0 (disabled)
  3064. </li><br>
  3065. <li><a name="ESPEasy_device_attr_interval">Interval</a><br>
  3066. Used to set polling interval for presence check and GPIOs polling in
  3067. seconds. 0 will disable this feature.<br>
  3068. Possible values: secs &gt; 10.<br>
  3069. Default: 300
  3070. </li><br>
  3071. <li><a href="#IODev">IODev</a><br>
  3072. Used to select I/O device (ESPEasy Bridge).
  3073. </li><br>
  3074. <li><a name="ESPEasy_device_attr_maplightscmds">mapLightCmds</a><br>
  3075. Enable the following commands and map them to the specified ESPEasy
  3076. command: rgb, ct, pct, on, off, toggle, dim, line, one, all, fade,
  3077. colorfade, rainbow, kitt, comet, theatre, scan, dualscan, twinkle,
  3078. twinklefade, sparkle, wipe, fire, stop, fadetime, fadedelay, count, speed,
  3079. bgcolor. Ask the ESPEasy maintainer to add more if required.<br>
  3080. Needed to use FHEM's colorpicker or slider widgets to control a
  3081. rgb/ct/effect/... plugin.<br>
  3082. required values: <code>a valid set command</code><br>
  3083. eg. <code>attr &lt;esp&gt; mapLightCmds Lights</code>
  3084. </li><br>
  3085. <li><a name="ESPEasy_device_attr_presencecheck">presenceCheck</a><br>
  3086. Used to enable/disable presence check for ESPs<br>
  3087. Presence check determines the presence of a device by readings age. If any
  3088. reading of a device is newer than <a href="#ESPEasy_device_attr_interval">interval</a>
  3089. seconds then it is marked as being present. This kind of check works for
  3090. ESP devices in deep sleep too but require at least 1 reading that is
  3091. updated regularly. Therefore the ESP must send the corresponding data
  3092. regularly (ESP device option "delay").<br>
  3093. Possible values: 0,1<br>
  3094. Default: 1 (enabled)
  3095. </li><br>
  3096. <li><a name="ESPEasy_device_attr_readingswitchtext">readingSwitchText</a><br>
  3097. Use on,off instead of 1,0 for readings if ESP device is a switch.<br>
  3098. Possible values: 0,1<br>
  3099. Default: 1 (enabled)
  3100. </li><br>
  3101. <li><a name="ESPEasy_device_attr_setstate">setState</a><br>
  3102. Summarize received values in state reading.<br>
  3103. A positive number determines the number of characters used for abbreviated
  3104. reading names. Only readings with an age less than
  3105. <a href="#ESPEasy_device_attr_interval">interval</a> will be considered. If your are
  3106. not satisfied with format or behavior of setState then disable this
  3107. attribute (set to 0) and use global attributes userReadings and/or
  3108. stateFormat to get what you want.<br>
  3109. Possible values: integer &gt;=0<br>
  3110. Default: 3 (enabled with 3 characters abbreviation)
  3111. </li><br>
  3112. The following two attributes should only be use in cases where ESPEasy
  3113. software do not send data on status changes and no rule/dummy can be used
  3114. to do that. Useful for commands like PWM, STATUS, ...
  3115. <br><br>
  3116. <li><a name="ESPEasy_device_attr_parsecmdresponse">parseCmdResponse</a><br>
  3117. Used to parse response of commands like GPIO, PWM, STATUS, ...<br>
  3118. Specify a module command or comma separated list of commands as argument.
  3119. Commands are case insensitive.<br>
  3120. Only necessary if ESPEasy software plugins do not send their data
  3121. independently. Useful for commands like STATUS, PWM, ...<br>
  3122. Possible values: &lt;set cmd&gt;[,&lt;set cmd&gt;][,...]<br>
  3123. Default: status<br>
  3124. Eg. <code>attr ESPxx parseCmdResponse status,pwm</code>
  3125. </li><br>
  3126. <li><a name="ESPEasy_device_attr_pollgpios">pollGPIOs</a><br>
  3127. Used to enable polling for GPIOs status. This polling will do same as
  3128. command 'set ESPxx status &lt;device&gt; &lt;pin&gt;'<br>
  3129. Possible values: GPIO number or comma separated GPIO number list<br>
  3130. Default: none<br>
  3131. Eg. <code>attr ESPxx pollGPIOs 13,D7,D2</code>
  3132. </li>
  3133. <br>
  3134. The following two attributes control naming of readings that are
  3135. generated by help of parseCmdResponse and pollGPIOs (see above)
  3136. <br><br>
  3137. <li><a name="ESPEasy_device_attr_readingprefixgpio">readingPrefixGPIO</a><br>
  3138. Specifies a prefix for readings based on GPIO numbers. For example:
  3139. "set ESPxx pwm 13 512" will switch GPIO13 into pwm mode and set pwm to
  3140. 512. If attribute readingPrefixGPIO is set to PIN and attribut
  3141. <a href="#ESPEasy_device_attr_parsecmdresponse">parseCmdResponse</a> contains pwm
  3142. command then the reading name will be PIN13.<br>
  3143. Possible Values: <code>string</code><br>
  3144. Default: GPIO
  3145. </li><br>
  3146. <li><a name="ESPEasy_device_attr_readingsuffixgpiostate">readingSuffixGPIOState</a><br>
  3147. Specifies a suffix for the state-reading of GPIOs (see Attribute
  3148. <a href="#ESPEasy_device_attr_pollgpios">pollGPIOs</a>)<br>
  3149. Possible Values: <code>string</code><br>
  3150. Default: no suffix<br>
  3151. Eg. attr ESPxx readingSuffixGPIOState _state
  3152. </li><br>
  3153. <li>
  3154. <a href="#readingFnAttributes">readingFnAttributes</a>
  3155. </li><br>
  3156. <b>Experimental</b> (The following attributes can be changed or removed at
  3157. any time):<br><br>
  3158. <li><a name="ESPEasy_device_attr_rgbgpios">rgbGPIOs</a><br>
  3159. Use to define GPIOs your lamp is conneted to. Must be set to be able to
  3160. use <a href="#ESPEasy_device_set_rgb">rgb</a> set command.<br>
  3161. Possible values: Comma separated tripple of ESP pin numbers or arduino pin
  3162. names<br>
  3163. Eg: 12,13,15<br>
  3164. Eg: D6,D7,D8<br>
  3165. Default: none
  3166. </li><br>
  3167. </ul>
  3168. </ul>
  3169. =end html
  3170. =cut