34_ESPEasy.pm 162 KB

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