98_BOSEST.pm 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  1. #############################################################
  2. #
  3. # BOSEST.pm (c) by Dominik Karall, 2016-2017
  4. # dominik karall at gmail dot com
  5. # $Id: 98_BOSEST.pm 14882 2017-08-12 12:44:25Z dominik $
  6. #
  7. # FHEM module to communicate with BOSE SoundTouch system
  8. # API as defined in BOSE SoundTouchAPI_WebServices_v1.0.1.pdf
  9. #
  10. # Version: 2.1.1
  11. #
  12. #############################################################
  13. #
  14. # v2.1.1 - 20170812
  15. # - CHANGE: changed reading name type to model
  16. #
  17. # v2.1.0 - 20170129
  18. # - NEW REQUIREMENT: TTS: sox, libsox-fmt-mp3 (only required for TTS)
  19. # - FEATURE: TTS: add 1 second silence before TTS message for speak to
  20. # prevent low volume on first words
  21. # - FEATURE: TTS: support "unlimited" characters in TTS speak.
  22. # Text is split in sentences which are afterwards
  23. # merged with sox. Same sentences are downloaded
  24. # only once a month to reduce requests to Google.
  25. # - FEATURE: TTS: remove ttsDlnaServer attribut, it will be automatically discovered
  26. # - BUGFIX: TTS: support pause/stop after speak if previous state was paused/stopped
  27. # - BUGFIX: TTS: fix resume after speak when spotify running
  28. # - BUGFIX: TTS: fix speakChannel for spotify presets
  29. # - BUGFIX: TTS: use pause on TTS instead of stop to allow proper resume
  30. # - BUGFIX: TTS: improved check after TTS play to restore previous state
  31. # - BUGFIX: TTS: if state was invalid before TTS it will be set to standby
  32. # - BUGFIX: fix save spotify to channel_7-20
  33. # - BUGFIX: fix list of arguments
  34. # - FEATURE: add $readingFnAttributes
  35. # - FEATURE: add playPause toggle command
  36. #
  37. # v2.0.1 - 20161203
  38. # - FEATURE: support shuffle/repeat (thx@rockyou)
  39. # - BUGFIX: support special characters for TTS (thx@hschuett)
  40. # - BUGFIX: module didn't work if you had only 1 preset active
  41. # - CHANGE: add link to commandref on how to setup TTS
  42. # - BUGFIX: remove "unknown event" log entry for ping/pong messages
  43. #
  44. # v2.0.0 - 20160718
  45. # - CHANGE: first official release within fhem repository
  46. #
  47. # v1.5.7 - 20160623
  48. # - BUGFIX: fix off command if zone-play is active
  49. # - BUGFIX: fix auto-zone if zone-play is already active
  50. # - BUGFIX: do not create auto-zone if both players play nothing
  51. # - BUGFIX: update most readings only on change (reduces number of events)
  52. # - BUGFIX: fix autoAddDLNAServers functionality in main device
  53. #
  54. # v1.5.6 - 20160611
  55. # - FEATURE: auto-zone, start zone-play if speakers are playing the same (contentItemLocation)
  56. # attr <name> autoZone on (default: off)
  57. # - BUGFIX: set zone only from master device as slave devices might not always report the truth (known bug at BOSE)
  58. # - CHANGE: set zonemember_X to UDN instead of player name
  59. # - CHANGE: delete TTS files after 30 days
  60. # - CHANGE: reduce number of http calls after each discovery process
  61. # - CHANGE: reduce number of http calls for key presses
  62. #
  63. # v1.5.5 - 20160510
  64. # - BUGFIX: fix unitiliazed value in handleDeviceByIp
  65. #
  66. # v1.5.4 - 20160509
  67. # - FEATURE: restore volume when speaker goes online
  68. # allows to power off the box completely without loosing
  69. # previous volume settings
  70. # - BUGFIX: fix possible unitialized value
  71. # - BUGFIX: fix next which should be return
  72. # - BUGFIX: sometimes double-tap feature wasn't working due to BOSE not
  73. # updating zones for slave speakers
  74. #
  75. # v1.5.3 - 20160425
  76. # - FEATURE: support static IPs (should only be used if device can't be discovered)
  77. # attr bose_system staticIPs 192.168.1.52,192.168.1.53
  78. # - FEATURE: support speak channel name (useful for SoundTouch w/o display)
  79. # attr <name> speakChannel 1-6
  80. # attr <name> speakChannel 2,3,5,6
  81. # - BUGFIX: retry websocket setup every 5s if it fails
  82. # - BUGFIX: update supportClockDisplay reading only on reconnect
  83. # - CHANGE: remove user attr from main device
  84. #
  85. # v1.5.2 - 20160403
  86. # - FEATURE: support clock display (SoundTouch 20/30)
  87. # set <name> clock enable/disable
  88. #
  89. # v1.5.1 - 20160330
  90. # - CHANGE: updated documentation (again many thx to Miami!)
  91. # - FEATURE: support triple-tap (currently no function implemented: any ideas? :))
  92. # - CHANGE: change back channel even after speakOff
  93. # - BUGFIX: unitialized value fixed
  94. #
  95. # v1.5.0 - 20160306
  96. # - FEATURE: support SetExtensions (on-for-timer,...)
  97. # - FEATURE: support TTS (TextToSpeach) via Google Translate
  98. # set <name> speak "This is a test message"
  99. # - FEATURE: support volume control for TTS
  100. # set <name> speak "This message has different volume" 30
  101. # - FEATURE: support different languages for TTS
  102. # set <name> speak "Das ist ein deutscher Test" de
  103. # set <name> speak "Das ist ein deutscher Test" 30 de
  104. # - FEATURE: support off (instead of resume) after TTS messages (restores only volume settings)
  105. # set <name> speakOff "Music is going to switch off now. Good night." 30 en
  106. # - FEATURE: speak "not available" text on Google Captcha
  107. # can be disabled by ttsSpeakOnError = 0
  108. # - FEATURE: set default TTS language via ttsLanguage attribute
  109. # - FEATURE: automatically add DLNA server running on the same
  110. # server as FHEM to the BOSE library
  111. # - FEATURE: automatically add all DLNA servers to BOSE library
  112. # requires autoAddDLNAServers = 1 attribute for "main" (not players!)
  113. # - FEATURE: reuse cached TTS files for 30 days
  114. # - FEATURE: set DLNA TTS directory via ttsDirectory attribute
  115. # - FEATURE: set DLNA TTS server via ttsDLNAServer attribute
  116. # only needed if the DLNA server is not the FHEM server
  117. # - FEATURE: support ttsVolume for speak
  118. # ttsVolume = 20 (set volume 20 for speak)
  119. # ttsVolume = +20 (increase volume by 20 from current level)
  120. # - FEATURE: add html documentation (provided by Miami)
  121. # - FEATURE: support relative volume settings with +/-
  122. # set <name> volume +3
  123. # set <name> speak "This is a louder message" +10
  124. # - FEATURE: new reading "connectedDLNAServers" (blanks are replaced by "-")
  125. # - FEATURE: support add/remove DLNA servers to the BOSE library
  126. # set <name> addDLNAServer RPi
  127. # set <name> removeDLNAServer RPi
  128. # - FEATURE: add readings for channel_07-20
  129. # - FEATURE: support saveChannel to save current channel to channel_07-20
  130. # - FEATURE: support bass settings only if available (/bassCapabilities)
  131. # - FEATURE: support bluetooth only if available (/sources)
  132. # - FEATURE: support switch source to airplay (untested)
  133. # - BUGFIX: update zone on Player discovery
  134. # - BUGFIX: fixed some uninitialized variables
  135. # - CHANGE: limit recent_X readings to 15 max
  136. #
  137. # v1.0.0 - 20160219
  138. # - FEATURE: support multi-room (playEverywhere, stopPlayEverywhere)
  139. # - FEATURE: show current zone members in readings
  140. # - FEATURE: support createZone <deviceID1>,<deviceID2>,...
  141. # - FEATURE: support addToZone <deviceID1>,<deviceID2>,...
  142. # - FEATURE: support removeFromZone <deviceID1>,<deviceID2>,...
  143. # - FEATURE: add "double-tap" multi-room feature
  144. # double-tap (<1s) a hardware preset button to
  145. # enable or disable the multi-room feature
  146. # - FEATURE: support bass settings
  147. # - FEATURE: support infoUpdated (e.g. deviceName change)
  148. # - FEATURE: support mute on/off/toggle
  149. # - FEATURE: support recent channel list
  150. # set name recent X
  151. # names for recent list entries are shown in readings
  152. # - FEATURE: support channel_07-20 by attribute
  153. # format:name|location|source|sourceAccount or
  154. # name|location|source| if no sourceAccount
  155. # - FEATURE: support bluetooth/bt-discover/aux mode
  156. # - FEATURE: support ignoreDeviceIDs for main define
  157. # format:B23C23FF,A2EC81EF
  158. # - CHANGE: reading channel_X => channel_0X (e.g. channel_02)
  159. #
  160. # v0.9.7 - 20160214
  161. # - FEATURE: print module version on startup of main module
  162. # - FEATURE: support device rename (e.g. BOSE_... => wz_BOSE)
  163. # - FEATURE: show preset itemNames in channel_X reading
  164. # - FEATURE: automatically update preset readings on change
  165. # - FEATURE: add description reading (could be very long)
  166. # - CHANGE: change log level for not implemented events to 4
  167. # - CHANGE: use only one processXml function for websocket and http
  168. # - BUGFIX: fix set off/on more than once within 1 second
  169. # - BUGFIX: fix warnings during setup process
  170. # - BUGFIX: support umlauts in all readings
  171. # - BUGFIX: handle XMLin errors with eval
  172. # - BUGFIX: handle "set" when speaker wasn't found yet
  173. #
  174. # v0.9.6 - 20160210
  175. # - FEATURE: support prev/next track
  176. #
  177. # v0.9.5 - 20160210
  178. # - FEATURE: update channel based on websocket events
  179. # - BUGFIX: specify minimum libmojolicious-perl version
  180. # - BUGFIX: reconnect websocket if handshake fails
  181. # - BUGFIX: presence reading fixed
  182. # - CHANGE: websocket request timeout changed to 10s (prev. 5s)
  183. # - CHANGE: clockDisplayUpdated message handled now
  184. #
  185. # v0.9.4 - 20160206
  186. # - CHANGE: completely drop ithreads (reduces memory usage)
  187. # - CHANGE: search for new devices every 60s (BlockingCall)
  188. # - CHANGE: check presence status based on websocket connection
  189. # - BUGFIX: removed arguments and readings for main BOSEST
  190. # - FEATURE: read volume on startup
  191. #
  192. # v0.9.3 - 20160125
  193. # - BUGFIX: fix "EV does not work with ithreads."
  194. #
  195. # v0.9.2 - 20160123
  196. # - BUGFIX: fix memory leak
  197. # - BUGFIX: use select instead of usleep
  198. #
  199. # v0.9.1 - 20160122
  200. # - BUGFIX: bugfix for on/off support
  201. #
  202. # v0.9 - 20160121
  203. # - autodiscover BOSE SoundTouch players
  204. # - add alias for newly created devices
  205. # - update IP if the player IP changes
  206. # - automatically re-connect websocket
  207. # - support UTF-8 names with umlauts
  208. # - reconnect websocket when connection closed
  209. # - add firmware version & IP readings
  210. # - automatically update /info on IP update
  211. # - state: offline,playing,stopped,paused,online (online means standby)
  212. # - support on/off commands based on current state
  213. # - support more readings for now_playing
  214. #
  215. # v0.2 - 20160110
  216. # - support stop/play/pause/power
  217. # - change preset to channel according to DevGuidelinesAV
  218. # - read /info on startup
  219. # - connect to websocket to receive speaker events
  220. #
  221. # v0.1 - 20160105
  222. # - define BOSE Soundtouch based on fixed IP
  223. # - change volume via /volume
  224. # - change preset via /key
  225. #
  226. # TODO
  227. # - set title/album/artist for TTS files (--comment "Title=Title..")
  228. # - check if Mojolicious should be used for HTTPGET/HTTPPOST
  229. # - ramp up/down volume support in SetExtensions
  230. #
  231. #############################################################
  232. BEGIN {
  233. $ENV{MOJO_REACTOR} = "Mojo::Reactor::Poll";
  234. }
  235. package main;
  236. use strict;
  237. use warnings;
  238. use Blocking;
  239. use Encode;
  240. use SetExtensions;
  241. use Data::Dumper;
  242. use Digest::MD5 qw(md5_hex);
  243. use File::stat;
  244. use IO::Socket::INET;
  245. use LWP::UserAgent;
  246. use Mojolicious 5.54;
  247. use Net::Bonjour;
  248. use Scalar::Util qw(looks_like_number);
  249. use XML::Simple;
  250. use URI::Escape;
  251. my $BOSEST_GOOGLE_NOT_AVAILABLE_TEXT = "Hello, I'm sorry, but Google Translate is currently not available.";
  252. my $BOSEST_GOOGLE_NOT_AVAILABLE_LANG = "en";
  253. my $BOSEST_READ_CMDREF_TEXT = "Hello, I'm sorry, but you need to install new libraries, please read command reference.";
  254. my $BOSEST_READ_CMDREF_LANG = "en";
  255. sub BOSEST_Initialize($) {
  256. my ($hash) = @_;
  257. $hash->{DefFn} = 'BOSEST_Define';
  258. $hash->{UndefFn} = 'BOSEST_Undef';
  259. $hash->{GetFn} = 'BOSEST_Get';
  260. $hash->{SetFn} = 'BOSEST_Set';
  261. $hash->{AttrFn} = 'BOSEST_Attribute';
  262. $hash->{AttrList} = $readingFnAttributes;
  263. return undef;
  264. }
  265. sub BOSEST_Define($$) {
  266. my ($hash, $def) = @_;
  267. my @a = split("[ \t]+", $def);
  268. my $name = $a[0];
  269. $hash->{DEVICEID} = "0";
  270. $hash->{STATE} = "initialized";
  271. if (int(@a) > 3) {
  272. return 'BOSEST: Wrong syntax, must be define <name> BOSEST [<deviceID>]';
  273. } elsif(int(@a) == 3) {
  274. my $param = $a[2];
  275. #set device id from parameter
  276. $hash->{DEVICEID} = $param;
  277. #set IP to unknown
  278. $hash->{helper}{IP} = "unknown";
  279. readingsSingleUpdate($hash, "IP", "unknown", 1);
  280. #allow on/off commands (updateIP?)
  281. $hash->{helper}{sent_on} = 0;
  282. $hash->{helper}{sent_off} = 0;
  283. #no websockets connected
  284. $hash->{helper}{wsconnected} = 0;
  285. #create mojo useragent
  286. $hash->{helper}{useragent} = Mojo::UserAgent->new() if(!defined($hash->{helper}{useragent}));
  287. #init statecheck
  288. $hash->{helper}{stateCheck}{enabled} = 0;
  289. $hash->{helper}{stateCheck}{actionActive} = 0;
  290. #init switchSource
  291. $hash->{helper}{switchSource} = "";
  292. #init speak channel functionality
  293. $hash->{helper}{lastSpokenChannel} = "";
  294. my $attrList = "channel_07 channel_08 channel_09 channel_10 channel_11 ".
  295. "channel_12 channel_13 channel_14 channel_15 channel_16 ".
  296. "channel_17 channel_18 channel_19 channel_20 ignoreDeviceIDs ".
  297. "ttsDirectory ttsLanguage ttsSpeakOnError ttsVolume ".
  298. "speakChannel autoZone";
  299. my @attrListArr = split(" ", $attrList);
  300. foreach my $attrname (@attrListArr) {
  301. addToDevAttrList($name, $attrname);
  302. }
  303. BOSEST_deleteOldTTSFiles($hash);
  304. #FIXME reset all recent_$i entries on startup (must be done here, otherwise readings are displayed when player wasn't found)
  305. }
  306. #init dlnaservers
  307. $hash->{helper}{dlnaServers} = "";
  308. #init supported source commands
  309. $hash->{helper}{supportedSourcesCmds} = "";
  310. $hash->{helper}{supportedBassCmds} = "";
  311. if (int(@a) < 3) {
  312. Log3 $hash, 3, "BOSEST: BOSE SoundTouch v2.1.1";
  313. #start discovery process 30s delayed
  314. InternalTimer(gettimeofday()+30, "BOSEST_startDiscoveryProcess", $hash, 0);
  315. foreach my $attrname (qw(staticIPs autoAddDLNAServers)) {
  316. addToDevAttrList($name, $attrname);
  317. }
  318. }
  319. return undef;
  320. }
  321. sub BOSEST_Attribute($$$$) {
  322. my ($mode, $devName, $attrName, $attrValue) = @_;
  323. if($mode eq "set") {
  324. if(substr($attrName, 0, 8) eq "channel_") {
  325. #check if there are 3 | in the attrValue
  326. my @value = split("\\|", $attrValue);
  327. return "BOSEST: wrong format" if(!defined($value[2]));
  328. #update reading for channel_X
  329. readingsSingleUpdate($main::defs{$devName}, $attrName, $value[0], 1);
  330. }
  331. } elsif($mode eq "del") {
  332. if(substr($attrName, 0, 8) eq "channel_") {
  333. #update reading for channel_X
  334. readingsSingleUpdate($main::defs{$devName}, $attrName, "-", 1);
  335. }
  336. }
  337. return undef;
  338. }
  339. sub BOSEST_Set($@) {
  340. my ($hash, $name, @params) = @_;
  341. my $workType = shift(@params);
  342. #get quoted text from params
  343. my $blankParams = join(" ", @params);
  344. my @params2;
  345. while($blankParams =~ /"?((?<!")\S+(?<!")|[^"]+)"?\s*/g) {
  346. push(@params2, $1);
  347. }
  348. @params = @params2;
  349. my $list = "on:noArg off:noArg power:noArg play:noArg ".
  350. "playPause:noArg ".
  351. "mute:on,off,toggle recent source:".$hash->{helper}{supportedSourcesCmds}.
  352. "shuffle:on,off repeat:all,one,off ".
  353. "nextTrack:noArg prevTrack:noArg playTrack speak speakOff ".
  354. "playEverywhere:noArg stopPlayEverywhere:noArg createZone addToZone removeFromZone ".
  355. "clock:enable,disable ".
  356. "stop:noArg pause:noArg channel:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 ".
  357. "volume:slider,0,1,100 ".$hash->{helper}{supportedBassCmds}." ".
  358. "saveChannel:07,08,09,10,11,12,13,14,15,16,17,18,19,20 ".
  359. "addDLNAServer:".$hash->{helper}{dlnaServers}." ".
  360. "removeDLNAServer:".ReadingsVal($hash->{NAME}, "connectedDLNAServers", "noArg");
  361. # check parameters for set function
  362. #DEVELOPNEWFUNCTION-1
  363. if($workType eq "?") {
  364. if($hash->{DEVICEID} eq "0") {
  365. return ""; #no arguments for server
  366. } else {
  367. return SetExtensions($hash, $list, $name, $workType, @params);
  368. }
  369. }
  370. if($hash->{helper}{IP} eq "unknown") {
  371. return "Searching for BOSE SoundTouch, try again later...";
  372. }
  373. if($workType eq "volume") {
  374. return "BOSEST: volume requires volume as additional parameter" if(int(@params) < 1);
  375. #params[0] = volume value
  376. BOSEST_setVolume($hash, $params[0]);
  377. } elsif($workType eq "zoneVolume") {
  378. BOSEST_setZoneVolume($hash, $params[0]);
  379. } elsif($workType eq "channel") {
  380. return "BOSEST: channel requires preset id as additional parameter" if(int(@params) < 1);
  381. #params[0] = preset channel
  382. BOSEST_setPreset($hash, $params[0]);
  383. } elsif($workType eq "saveChannel") {
  384. return "BOSEST: saveChannel requires channel number as additional parameter" if(int(@params) < 1);
  385. #params[09 = channel number (07-20)
  386. BOSEST_saveChannel($hash, $params[0]);
  387. } elsif($workType eq "bass") {
  388. return "BOSEST: bass requires bass (1-10) as additional parameter" if(int(@params) < 1);
  389. #params[0] = bass value
  390. BOSEST_setBass($hash, $params[0]);
  391. } elsif($workType eq "mute") {
  392. return "BOSEST: mute requires on/off/toggle as additional parameter" if(int(@params) < 1);
  393. #params[0] = mute value
  394. BOSEST_setMute($hash, $params[0]);
  395. } elsif($workType eq "shuffle") {
  396. return "BOSEST: shuffle requires on/off as additional parameter" if(int(@params) < 1);
  397. #params[0] = shuffle value
  398. BOSEST_setShuffle($hash, $params[0]);
  399. } elsif($workType eq "repeat") {
  400. return "BOSEST: repeat requires all/one/off as additional parameter" if(int(@params) < 1);
  401. return "BOSEST: repeat one not supported by spotify" if ($params[0] eq "one" && ReadingsVal($hash->{NAME}, "source", "") eq "SPOTIFY");
  402. #params[0] = repeat value
  403. BOSEST_setRepeat($hash, $params[0]);
  404. } elsif($workType eq "recent") {
  405. return "BOSEST: recebt requires number as additional parameter" if(int(@params) < 1);
  406. #params[0] = recent value
  407. BOSEST_setRecent($hash, $params[0]);
  408. } elsif($workType eq "source") {
  409. return "BOSEST: source requires bluetooth/aux as additional parameter" if(int(@params) < 1);
  410. #params[0] = source value
  411. BOSEST_setSource($hash, $params[0]);
  412. } elsif($workType eq "addDLNAServer") {
  413. return "BOSEST: addDLNAServer requires DLNA friendly name as additional parameter" if(int(@params) < 1);
  414. #params[0] = friendly name
  415. BOSEST_addDLNAServer($hash, $params[0]);
  416. } elsif($workType eq "removeDLNAServer") {
  417. return "BOSEST: removeDLNAServer requires DLNA friendly name as additional parameter" if(int(@params) < 1);
  418. #params[0] = friendly name
  419. BOSEST_removeDLNAServer($hash, $params[0]);
  420. } elsif($workType eq "clock") {
  421. return "BOSEST: clock requires enable/disable as additional parameter" if(int(@params) < 1);
  422. #check if supported
  423. return "BOSEST: clock not supported." if(ReadingsVal($hash->{NAME}, "supportClockDisplay", "false") eq "false");
  424. BOSEST_clockSettings($hash, $params[0]);
  425. } elsif($workType eq "play") {
  426. BOSEST_play($hash);
  427. } elsif($workType eq "stop") {
  428. BOSEST_stop($hash);
  429. } elsif($workType eq "pause") {
  430. BOSEST_pause($hash);
  431. } elsif($workType eq "playPause") {
  432. BOSEST_playPause($hash);
  433. } elsif($workType eq "power") {
  434. BOSEST_power($hash);
  435. } elsif($workType eq "on") {
  436. BOSEST_on($hash);
  437. } elsif($workType eq "off") {
  438. BOSEST_off($hash);
  439. InternalTimer(gettimeofday()+2, "BOSEST_off", $hash, 0);
  440. } elsif($workType eq "nextTrack") {
  441. BOSEST_next($hash);
  442. } elsif($workType eq "prevTrack") {
  443. BOSEST_prev($hash);
  444. } elsif($workType eq "playTrack") {
  445. return "BOSEST: playTrack requires track name as additional parameters" if(int(@params) < 1);
  446. #params[0] = track name for search
  447. BOSEST_playTrack($hash, $params[0]);
  448. } elsif($workType eq "speak" or $workType eq "speakOff") {
  449. return "BOSEST: speak requires quoted text as additional parameters" if(int(@params) < 1);
  450. return "BOSEST: speak requires quoted text" if(substr($blankParams, 0, 1) ne "\"");
  451. if(AttrVal($hash->{NAME}, "ttsDirectory", "") eq "") {
  452. return "BOSEST: Please set ttsDirectory attribute first.
  453. FHEM user needs permissions to write to that directory.
  454. It is also recommended to set ttsLanguage (default: en).";
  455. }
  456. #set text (must be within quotes)
  457. my $text = $params[0];
  458. my $volume = "";
  459. if(looks_like_number($params[1])) {
  460. #set volume (default current volume)
  461. $volume = $params[1] if(defined($params[1]));
  462. } else {
  463. #parameter is language
  464. $params[2] = $params[1];
  465. }
  466. #set language (default English)
  467. my $lang = "";
  468. $lang = $params[2] if(defined($params[2]));
  469. #stop after speak?
  470. my $stopAfterSpeak = 0;
  471. if($workType eq "speakOff") {
  472. $stopAfterSpeak = 1;
  473. }
  474. BOSEST_speak($hash, $text, $volume, $lang, $stopAfterSpeak);
  475. } elsif($workType eq "playEverywhere") {
  476. BOSEST_playEverywhere($hash);
  477. } elsif($workType eq "stopPlayEverywhere") {
  478. BOSEST_stopPlayEverywhere($hash);
  479. } elsif($workType eq "createZone") {
  480. return "BOSEST: createZone requires deviceIDs as additional parameter" if(int(@params) < 1);
  481. #params[0] = deviceID channel
  482. BOSEST_createZone($hash, $params[0]);
  483. } elsif($workType eq "addToZone") {
  484. return "BOSEST: addToZone requires deviceID as additional parameter" if(int(@params) < 1);
  485. #params[0] = deviceID channel
  486. BOSEST_addToZone($hash, $params[0]);
  487. } elsif($workType eq "removeFromZone") {
  488. return "BOSEST: removeFromZone requires deviceID as additional parameter" if(int(@params) < 1);
  489. #params[0] = deviceID channel
  490. BOSEST_removeFromZone($hash, $params[0]);
  491. } else {
  492. return SetExtensions($hash, $list, $name, $workType, @params);
  493. }
  494. return undef;
  495. }
  496. #DEVELOPNEWFUNCTION-2 (create own function)
  497. sub BOSEST_setZoneVolume {
  498. my ($hash, $targetVolume) = @_;
  499. #FIXME
  500. # #change volume of this device
  501. # DLNARenderer_volume($hash, $targetVolume);
  502. # #handle volume for all devices in the current group
  503. # #iterate through group and change volume relative to the current volume of this device
  504. # my $mainVolumeDiff = DLNARenderer_convertVolumeToAbsolute($hash, $targetVolume) - ReadingsVal($hash->{NAME}, "volume", 0);
  505. # my $multiRoomUnits = ReadingsVal($hash->{NAME}, "multiRoomUnits", "");
  506. # my @multiRoomUnitsArray = split("," $multiRoomUnits);
  507. # foreach my $unit (@multiRoomUnitsArray) {
  508. # my $devHash = DLNARenderer_getHashByFriendlyName($hash, $unit);
  509. # my $newVolume = ReadingsVal($devHash->{NAME}, "volume", 0) + $mainVolumeDiff);
  510. # if($newVolume > 100) {
  511. # $newVolume = 100;
  512. # } elsif($newVolume < 0) {
  513. # $newVolume = 0;
  514. # }
  515. # DLNARenderer_volume($devHash, $newVolume);
  516. # }
  517. return undef;
  518. }
  519. sub BOSEST_clockSettings($$) {
  520. my ($hash, $val) = @_;
  521. if($val eq "disable") {
  522. $val = "false";
  523. } else {
  524. $val = "true";
  525. }
  526. my $postXml = "<clockDisplay><clockConfig userEnable=\"$val\"/></clockDisplay>";
  527. if(BOSEST_HTTPPOST($hash, '/clockDisplay', $postXml)) {
  528. }
  529. #FIXME error handling
  530. return undef;
  531. }
  532. sub BOSEST_addDLNAServer($$) {
  533. my ($hash, $friendlyName) = @_;
  534. #retrieve uuid for friendlyname
  535. my $listMediaServers = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/listMediaServers");
  536. foreach my $mediaServer (@{ $listMediaServers->{ListMediaServersResponse}->{media_server} }) {
  537. $mediaServer->{friendly_name} =~ s/\ /_/g;
  538. if($mediaServer->{friendly_name} eq $friendlyName) {
  539. BOSEST_setMusicServiceAccount($hash, $friendlyName, $mediaServer->{id});
  540. }
  541. }
  542. return undef;
  543. }
  544. sub BOSEST_removeDLNAServer($$) {
  545. my ($hash, $friendlyName) = @_;
  546. #retrieve uuid for friendlyname
  547. my $sources = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/sources");
  548. foreach my $source (@{ $sources->{sources}->{sourceItem} }) {
  549. next if($source->{source} ne "STORED_MUSIC");
  550. $source->{content} =~ s/\ /_/g;
  551. if($source->{content} eq $friendlyName) {
  552. BOSEST_removeMusicServiceAccount($hash, $friendlyName, $source->{sourceAccount});
  553. }
  554. }
  555. return undef;
  556. }
  557. sub BOSEST_saveChannel($$) {
  558. my ($hash, $channel) = @_;
  559. if(ReadingsVal($hash->{NAME}, "contentItemLocation", "") eq "") {
  560. return "BOSEST: No active channel.";
  561. }
  562. #itemname, type, location, source, sourceaccount
  563. my $itemName = ReadingsVal($hash->{NAME}, "contentItemItemName", "");
  564. my $location = ReadingsVal($hash->{NAME}, "contentItemLocation", "");
  565. my $type = ReadingsVal($hash->{NAME}, "contentItemType", "");
  566. my $source = ReadingsVal($hash->{NAME}, "contentItemSource", "");
  567. my $sourceAccount = ReadingsVal($hash->{NAME}, "contentItemSourceAccount", "");
  568. fhem("attr $hash->{NAME} channel_$channel $itemName|$type|$location|$source|$sourceAccount");
  569. return undef;
  570. }
  571. sub BOSEST_stopPlayEverywhere($) {
  572. my ($hash) = @_;
  573. my $postXmlHeader = "<zone master=\"$hash->{DEVICEID}\">";
  574. my $postXmlFooter = "</zone>";
  575. my $postXml = "";
  576. my @players = BOSEST_getAllBosePlayers($hash);
  577. foreach my $playerHash (@players) {
  578. if($playerHash->{DEVICEID} ne $hash->{DEVICEID}) {
  579. $postXml .= "<member ipaddress=\"".$playerHash->{helper}{IP}."\">".$playerHash->{DEVICEID}."</member>" if($playerHash->{helper}{IP} ne "unknown");
  580. }
  581. }
  582. $postXml = $postXmlHeader.$postXml.$postXmlFooter;
  583. if(BOSEST_HTTPPOST($hash, '/removeZoneSlave', $postXml)) {
  584. #ok
  585. }
  586. }
  587. sub BOSEST_playEverywhere($) {
  588. my ($hash) = @_;
  589. my $postXmlHeader = "<zone master=\"$hash->{DEVICEID}\" senderIPAddress=\"$hash->{helper}{IP}\">";
  590. my $postXmlFooter = "</zone>";
  591. my $postXml = "";
  592. my @players = BOSEST_getAllBosePlayers($hash);
  593. foreach my $playerHash (@players) {
  594. #don't add myself as member, I'm the master
  595. if($playerHash->{DEVICEID} ne $hash->{DEVICEID}) {
  596. $postXml .= "<member ipaddress=\"".$playerHash->{helper}{IP}."\">".$playerHash->{DEVICEID}."</member>" if($playerHash->{helper}{IP} ne "unknown");
  597. }
  598. }
  599. $postXml = $postXmlHeader.$postXml.$postXmlFooter;
  600. if(BOSEST_HTTPPOST($hash, '/setZone', $postXml)) {
  601. #ok
  602. }
  603. return undef;
  604. }
  605. sub BOSEST_createZone($$) {
  606. my ($hash, $deviceIds) = @_;
  607. my @devices = split(",", $deviceIds);
  608. my $postXmlHeader = "<zone master=\"$hash->{DEVICEID}\" senderIPAddress=\"$hash->{helper}{IP}\">";
  609. my $postXmlFooter = "</zone>";
  610. my $postXml = "";
  611. foreach my $deviceId (@devices) {
  612. my $playerHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  613. return undef if(!defined($playerHash));
  614. $postXml .= "<member ipaddress=\"".$playerHash->{helper}{IP}."\">".$playerHash->{DEVICEID}."</member>" if($playerHash->{helper}{IP} ne "unknown");
  615. }
  616. $postXml = $postXmlHeader.$postXml.$postXmlFooter;
  617. if(BOSEST_HTTPPOST($hash, '/setZone', $postXml)) {
  618. #ok
  619. }
  620. return undef;
  621. }
  622. sub BOSEST_addToZone($$) {
  623. my ($hash, $deviceIds) = @_;
  624. my @devices = split(",", $deviceIds);
  625. my $postXmlHeader = "<zone master=\"$hash->{DEVICEID}\" senderIPAddress=\"$hash->{helper}{IP}\">";
  626. my $postXmlFooter = "</zone>";
  627. my $postXml = "";
  628. foreach my $deviceId (@devices) {
  629. my $playerHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  630. return undef if(!defined($playerHash));
  631. $postXml .= "<member ipaddress=\"".$playerHash->{helper}{IP}."\">".$playerHash->{DEVICEID}."</member>" if($playerHash->{helper}{IP} ne "unknown");
  632. }
  633. $postXml = $postXmlHeader.$postXml.$postXmlFooter;
  634. if(BOSEST_HTTPPOST($hash, '/addZoneSlave', $postXml)) {
  635. #ok
  636. }
  637. return undef;
  638. }
  639. sub BOSEST_removeFromZone($$) {
  640. my ($hash, $deviceIds) = @_;
  641. my @devices = split(",", $deviceIds);
  642. my $postXmlHeader = "<zone master=\"$hash->{DEVICEID}\">";
  643. my $postXmlFooter = "</zone>";
  644. my $postXml = "";
  645. foreach my $deviceId (@devices) {
  646. my $playerHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  647. return undef if(!defined($playerHash));
  648. $postXml .= "<member ipaddress=\"".$playerHash->{helper}{IP}."\">".$playerHash->{DEVICEID}."</member>" if($playerHash->{helper}{IP} ne "unknown");
  649. }
  650. $postXml = $postXmlHeader.$postXml.$postXmlFooter;
  651. if(BOSEST_HTTPPOST($hash, '/removeZoneSlave', $postXml)) {
  652. #ok
  653. }
  654. return undef;
  655. }
  656. sub BOSEST_on($) {
  657. my ($hash) = @_;
  658. if(!$hash->{helper}{sent_on}) {
  659. my $sourceState = ReadingsVal($hash->{NAME}, "source", "STANDBY");
  660. if($sourceState eq "STANDBY") {
  661. BOSEST_power($hash);
  662. }
  663. $hash->{helper}{sent_on} = 1;
  664. }
  665. }
  666. sub BOSEST_off($) {
  667. my ($hash) = @_;
  668. if(!$hash->{helper}{sent_off}) {
  669. my $sourceState = ReadingsVal($hash->{NAME}, "source", "STANDBY");
  670. if($sourceState ne "STANDBY") {
  671. BOSEST_power($hash);
  672. }
  673. $hash->{helper}{sent_off} = 1;
  674. }
  675. }
  676. sub BOSEST_setRecent($$) {
  677. my ($hash, $nr) = @_;
  678. if(!defined($hash->{helper}{recents}{$nr}{itemName})) {
  679. #recent entry not found
  680. return undef;
  681. }
  682. BOSEST_setContentItem($hash,
  683. $hash->{helper}{recents}{$nr}{itemName},
  684. $hash->{helper}{recents}{$nr}{type},
  685. $hash->{helper}{recents}{$nr}{location},
  686. $hash->{helper}{recents}{$nr}{source},
  687. $hash->{helper}{recents}{$nr}{sourceAccount});
  688. return undef;
  689. }
  690. sub BOSEST_setContentItem {
  691. my ($hash, $itemName, $type, $location, $source, $sourceAccount) = @_;
  692. $type = "" if(!defined($type));
  693. my $postXml = "<ContentItem source=\"".
  694. $source.
  695. "\" sourceAccount=\"".
  696. $sourceAccount.
  697. "\" location=\"".
  698. $location.
  699. "\" type=\"".
  700. $type.
  701. "\">".
  702. "<itemName>".
  703. $itemName.
  704. "</itemName>".
  705. "</ContentItem>";
  706. if(BOSEST_HTTPPOST($hash, "/select", $postXml)) {
  707. #ok
  708. }
  709. return undef;
  710. }
  711. sub BOSEST_setBass($$) {
  712. my ($hash, $bass) = @_;
  713. $bass = $bass - 10;
  714. my $postXml = "<bass>$bass</bass>";
  715. if(BOSEST_HTTPPOST($hash, '/bass', $postXml)) {
  716. }
  717. #FIXME error handling
  718. return undef;
  719. }
  720. sub BOSEST_setVolume($$) {
  721. my ($hash, $volume) = @_;
  722. if(substr($volume, 0, 1) eq "+" or
  723. substr($volume, 0, 1) eq "-") {
  724. $volume = ReadingsVal($hash->{NAME}, "volume", 0) + $volume;
  725. }
  726. my $postXml = '<volume>'.$volume.'</volume>';
  727. if(BOSEST_HTTPPOST($hash, '/volume', $postXml)) {
  728. }
  729. #FIXME error handling
  730. return undef;
  731. }
  732. sub BOSEST_setMute($$) {
  733. my ($hash, $mute) = @_;
  734. if(($mute eq "on" && $hash->{READINGS}{mute}{VAL} eq "false") or
  735. ($mute eq "off" && $hash->{READINGS}{mute}{VAL} eq "true") or
  736. ($mute eq "toggle")) {
  737. BOSEST_sendKey($hash, "MUTE");
  738. }
  739. return undef;
  740. }
  741. sub BOSEST_setShuffle($$) {
  742. my ($hash, $shuffle) = @_;
  743. if($shuffle eq "on") {
  744. BOSEST_sendKey($hash, "SHUFFLE_ON");
  745. }
  746. if ($shuffle eq "off") {
  747. BOSEST_sendKey($hash, "SHUFFLE_OFF");
  748. }
  749. return undef;
  750. }
  751. sub BOSEST_setRepeat($$) {
  752. my ($hash, $repeat) = @_;
  753. if($repeat eq "one") {
  754. BOSEST_sendKey($hash, "REPEAT_ONE");
  755. }
  756. if ($repeat eq "all") {
  757. BOSEST_sendKey($hash, "REPEAT_ALL");
  758. }
  759. if ($repeat eq "off") {
  760. BOSEST_sendKey($hash, "REPEAT_OFF");
  761. }
  762. return undef;
  763. }
  764. sub BOSEST_setSource($$) {
  765. my ($hash, $source) = @_;
  766. $hash->{helper}{switchSource} = uc $source;
  767. if($hash->{helper}{switchSource} eq "") {
  768. return undef;
  769. }
  770. if($hash->{helper}{switchSource} eq "BT-DISCOVER" &&
  771. ReadingsVal($hash->{NAME}, "connectionStatusInfo", "") eq "DISCOVERABLE") {
  772. $hash->{helper}{switchSource} = "";
  773. return undef;
  774. }
  775. if($hash->{helper}{switchSource} eq ReadingsVal($hash->{NAME}, "source", "") &&
  776. ReadingsVal($hash->{NAME}, "connectionStatusInfo", "") ne "DISCOVERABLE") {
  777. $hash->{helper}{switchSource} = "";
  778. return undef;
  779. }
  780. #source is not switchSource yet
  781. BOSEST_sendKey($hash, "AUX_INPUT");
  782. return undef;
  783. }
  784. sub BOSEST_setPreset($$) {
  785. my ($hash, $preset) = @_;
  786. if($preset > 0 && $preset < 7) {
  787. BOSEST_sendKey($hash, "PRESET_".$preset);
  788. } else {
  789. #set channel based on AttrVal
  790. my $channelVal = AttrVal($hash->{NAME}, sprintf("channel_%02d", $preset), "0");
  791. return undef if($channelVal eq "0");
  792. my @channel = split("\\|", $channelVal);
  793. $channel[1] = "" if(!defined($channel[1]));
  794. $channel[2] = "" if(!defined($channel[2]));
  795. $channel[3] = "" if(!defined($channel[3]));
  796. $channel[4] = "" if(!defined($channel[4]));
  797. Log3 $hash, 5, "BOSEST: AttrVal: $channel[0], $channel[1], $channel[2], $channel[3], $channel[4]";
  798. #format: itemName|location|source|sourceAccount
  799. BOSEST_setContentItem($hash, $channel[0], $channel[1], $channel[2], $channel[3], $channel[4]);
  800. }
  801. return undef;
  802. }
  803. sub BOSEST_play($) {
  804. my ($hash) = @_;
  805. BOSEST_sendKey($hash, "PLAY");
  806. return undef;
  807. }
  808. sub BOSEST_playPause($) {
  809. my ($hash) = @_;
  810. BOSEST_sendKey($hash, "PLAY_PAUSE");
  811. return undef;
  812. }
  813. sub BOSEST_stop($) {
  814. my ($hash) = @_;
  815. BOSEST_sendKey($hash, "STOP");
  816. return undef;
  817. }
  818. sub BOSEST_pause($) {
  819. my ($hash) = @_;
  820. BOSEST_sendKey($hash, "PAUSE");
  821. return undef;
  822. }
  823. sub BOSEST_power($) {
  824. my ($hash) = @_;
  825. BOSEST_sendKey($hash, "POWER");
  826. return undef;
  827. }
  828. sub BOSEST_next($) {
  829. my ($hash) = @_;
  830. BOSEST_sendKey($hash, "NEXT_TRACK");
  831. return undef;
  832. }
  833. sub BOSEST_prev($) {
  834. my ($hash) = @_;
  835. BOSEST_sendKey($hash, "PREV_TRACK");
  836. return undef;
  837. }
  838. sub BOSEST_Undef($) {
  839. my ($hash) = @_;
  840. #remove internal timer
  841. RemoveInternalTimer($hash);
  842. #kill blocking
  843. BlockingKill($hash->{helper}{DISCOVERY_PID}) if(defined($hash->{helper}{DISCOVERY_PID}));
  844. return undef;
  845. }
  846. sub BOSEST_Get($$) {
  847. return undef;
  848. }
  849. sub BOSEST_speakChannel {
  850. my ($hash) = @_;
  851. my $speakChannel = AttrVal($hash->{NAME}, "speakChannel", "");
  852. if($speakChannel ne "") {
  853. my $channelNr = ReadingsVal($hash->{NAME}, "channel", "");
  854. Log3 $hash, 5, "BOSEST: speakChannel, $channelNr is in $speakChannel range?";
  855. if($channelNr =~ /[$speakChannel]/g) {
  856. my $channelName = ReadingsVal($hash->{NAME}, "contentItemItemName", "");
  857. Log3 $hash, 5, "BOSEST: speakChannel, start speak for channl $channelName";
  858. if($channelNr ne "" && $channelName ne "" && $hash->{helper}{lastSpokenChannel} ne $channelName) {
  859. #speak channel name
  860. $hash->{helper}{lastSpokenChannel} = $channelName;
  861. BOSEST_speak($hash, $channelName, "", "", 0);
  862. }
  863. } else {
  864. if($channelNr ne "") {
  865. #delete lastSpokenChannel
  866. $hash->{helper}{lastSpokenChannel} = "";
  867. }
  868. }
  869. }
  870. }
  871. sub BOSEST_speak($$$$$) {
  872. my ($hash, $text, $volume, $lang, $stopAfterSpeak) = @_;
  873. my $ttsDir = AttrVal($hash->{NAME}, "ttsDirectory", "");
  874. $lang = AttrVal($hash->{NAME}, "ttsLanguage", "en") if($lang eq "");
  875. $volume = AttrVal($hash->{NAME}, "ttsVolume", ReadingsVal($hash->{NAME}, "volume", 20)) if($volume eq "");
  876. my $sox = qx(which sox);
  877. chomp $sox;
  878. if(!-x $sox) {
  879. BOSEST_playGoogleTTS($hash, $ttsDir, $BOSEST_READ_CMDREF_TEXT, $volume, $BOSEST_READ_CMDREF_LANG, $stopAfterSpeak);
  880. return undef;
  881. }
  882. #download file and play
  883. BOSEST_playGoogleTTS($hash, $ttsDir, $text, $volume, $lang, $stopAfterSpeak);
  884. return undef;
  885. }
  886. sub BOSEST_saveCurrentState($) {
  887. my ($hash) = @_;
  888. $hash->{helper}{savedState}{volume} = ReadingsVal($hash->{NAME}, "volume", 20);
  889. $hash->{helper}{savedState}{source} = ReadingsVal($hash->{NAME}, "source", "");
  890. $hash->{helper}{savedState}{bass} = ReadingsVal($hash->{NAME}, "bass", "");
  891. $hash->{helper}{savedState}{playStatus} = ReadingsVal($hash->{NAME}, "playStatus", "STOP_STATE");
  892. $hash->{helper}{savedState}{contentItemItemName} = ReadingsVal($hash->{NAME}, "contentItemItemName", "");
  893. $hash->{helper}{savedState}{contentItemType} = ReadingsVal($hash->{NAME}, "contentItemType", "");
  894. $hash->{helper}{savedState}{contentItemLocation} = ReadingsVal($hash->{NAME}, "contentItemLocation", "");
  895. $hash->{helper}{savedState}{contentItemSource} = ReadingsVal($hash->{NAME}, "contentItemSource", "");
  896. $hash->{helper}{savedState}{contentItemSourceAccount} = ReadingsVal($hash->{NAME}, "contentItemSourceAccount", "");
  897. return undef;
  898. }
  899. sub BOSEST_restoreSavedState($) {
  900. my ($hash) = @_;
  901. BOSEST_setVolume($hash, $hash->{helper}{savedState}{volume});
  902. BOSEST_setBass($hash, $hash->{helper}{savedState}{bass});
  903. #bose off when source was off
  904. if($hash->{helper}{savedState}{source} eq "STANDBY" or $hash->{helper}{savedState}{source} eq "INVALID_SOURCE") {
  905. BOSEST_off($hash);
  906. } else {
  907. BOSEST_setContentItem($hash, $hash->{helper}{savedState}{contentItemItemName},
  908. $hash->{helper}{savedState}{contentItemType},
  909. $hash->{helper}{savedState}{contentItemLocation},
  910. $hash->{helper}{savedState}{contentItemSource},
  911. $hash->{helper}{savedState}{contentItemSourceAccount});
  912. if($hash->{helper}{savedState}{playStatus} eq "STOP_STATE") {
  913. InternalTimer(gettimeofday()+0.8, "BOSEST_stop", $hash, 0);
  914. } elsif($hash->{helper}{savedState}{playStatus} eq "PAUSE_STATE") {
  915. InternalTimer(gettimeofday()+0.8, "BOSEST_pause", $hash, 0);
  916. }
  917. }
  918. return undef;
  919. }
  920. sub BOSEST_restoreVolumeAndOff($) {
  921. my ($hash) = @_;
  922. BOSEST_setVolume($hash, $hash->{helper}{savedState}{volume});
  923. BOSEST_setBass($hash, $hash->{helper}{savedState}{bass});
  924. BOSEST_setContentItem($hash, $hash->{helper}{savedState}{contentItemItemName},
  925. $hash->{helper}{savedState}{contentItemType},
  926. $hash->{helper}{savedState}{contentItemLocation},
  927. $hash->{helper}{savedState}{contentItemSource},
  928. $hash->{helper}{savedState}{contentItemSourceAccount});
  929. BOSEST_off($hash);
  930. }
  931. sub BOSEST_downloadGoogleNotAvailable($) {
  932. my ($hash) = @_;
  933. my $text = $BOSEST_GOOGLE_NOT_AVAILABLE_TEXT;
  934. my $lang = $BOSEST_GOOGLE_NOT_AVAILABLE_LANG;
  935. my $ttsDir = AttrVal($hash->{NAME}, "ttsDirectory", "");
  936. my $md5 = md5_hex($lang.$text);
  937. my $filename = $ttsDir."/".$md5.".mp3";
  938. if (! -f $filename) {
  939. BOSEST_retrieveGooglTTSFile($hash, $filename, $md5, $text, $lang);
  940. }
  941. return undef;
  942. }
  943. sub BOSEST_retrieveGooglTTSFile($$$$$;$) {
  944. my ($hash, $filename, $md5, $text, $lang, $callback) = @_;
  945. my $uri_text = uri_escape($text);
  946. $hash->{helper}{useragent}->get("http://translate.google.com/translate_tts?ie=UTF-8&tl=$lang&client=tw-ob&q=$uri_text" => sub {
  947. my ($ua, $tx) = @_;
  948. my $downloadOk = 0;
  949. if($tx->res->headers->content_type eq "audio/mpeg") {
  950. $tx->res->content->asset->move_to($filename);
  951. $downloadOk = 1;
  952. }
  953. if(defined($callback)) {
  954. $callback->($hash, $filename, $md5, $downloadOk);
  955. }
  956. });
  957. return undef;
  958. }
  959. sub BOSEST_generateSilence {
  960. my ($hash) = @_;
  961. my $ttsDir = AttrVal($hash->{NAME}, "ttsDirectory", "");
  962. my $silenceFile = $ttsDir."/BOSEST_silence.mp3";
  963. my $soxCmd;
  964. if(!-f $silenceFile) {
  965. #generate silence file
  966. $soxCmd = "sox -n -r 24000 -c 1 $silenceFile trim 0.0 1";
  967. qx($soxCmd);
  968. }
  969. return undef;
  970. }
  971. sub BOSEST_joinAudioFilesBlocking {
  972. my ($string) = @_;
  973. my ($name, $outputFile, @inputFiles) = split("\\|", $string);
  974. my $ttsDir = AttrVal($name, "ttsDirectory", "");
  975. my $hash = $main::defs{$name};
  976. my $inputF = join(" ", map { $ttsDir."/".$_ } @inputFiles);
  977. my $outputF = $ttsDir."/".$outputFile;
  978. my $outputFileTmp = $ttsDir."/tmp_".$outputFile;
  979. BOSEST_generateSilence($hash);
  980. my $soxCmd = "sox $inputF $outputFileTmp";
  981. Log3 $hash, 5, "SOX: $soxCmd";
  982. my $soxRes = qx($soxCmd);
  983. qx(mv $outputFileTmp $outputF);
  984. return $name;
  985. }
  986. sub BOSEST_playMessageStringArg {
  987. my ($name) = @_;
  988. my $hash = $main::defs{$name};
  989. BOSEST_playMessage($hash, "v1_".$hash->{helper}{tts}{fulltextmd5}, $hash->{helper}{tts}{volume}, $hash->{helper}{tts}{stopAfterSpeak});
  990. return undef;
  991. }
  992. sub BOSEST_playMessage($$$$) {
  993. my ($hash, $trackname, $volume, $stopAfterSpeak) = @_;
  994. Log3 $hash, 4, "BOSEST: playMessage $trackname, $volume, $stopAfterSpeak";
  995. BOSEST_saveCurrentState($hash);
  996. if($volume ne ReadingsVal($hash->{NAME}, "volume", 0)) {
  997. BOSEST_pause($hash);
  998. BOSEST_setVolume($hash, $volume);
  999. }
  1000. BOSEST_playTrack($hash, $trackname);
  1001. $hash->{helper}{stateCheck}{enabled} = 1;
  1002. #after play the speaker changes contentItemItemName
  1003. $hash->{helper}{stateCheck}{actionContentItemItemName} = $trackname;
  1004. #check if we need to stop after speak
  1005. if(defined($stopAfterSpeak) && $stopAfterSpeak eq "1") {
  1006. $hash->{helper}{stateCheck}{function} = \&BOSEST_restoreVolumeAndOff;
  1007. } else {
  1008. $hash->{helper}{stateCheck}{function} = \&BOSEST_restoreSavedState;
  1009. }
  1010. return undef;
  1011. }
  1012. sub BOSEST_deleteOldTTSFiles {
  1013. my ($hash) = @_;
  1014. my ($err, $val) = getKeyValue("BOSEST_tts_files");
  1015. my @ttsFiles = split(",", $val);
  1016. my $ttsDir = AttrVal($hash->{NAME}, "ttsDirectory", "");
  1017. return undef if($ttsDir eq "");
  1018. InternalTimer(gettimeofday()+86500, "BOSEST_deleteOldTTSFiles", $hash, 0);
  1019. foreach my $ttsFile (@ttsFiles) {
  1020. ($err, $val) = getKeyValue($ttsFile);
  1021. my $now = gettimeofday();
  1022. if($now - $val > 2592000) {
  1023. #delete file
  1024. unlink $ttsDir."/".$ttsFile.".mp3";;
  1025. #remove $ttsFile from BOSEST_tts_files array
  1026. @ttsFiles = grep { $_ != $ttsFile } @ttsFiles;
  1027. #remove key
  1028. $err = setKeyValue($ttsFile, undef);
  1029. }
  1030. }
  1031. $err = setKeyValue("BOSEST_tts_files", join(",", @ttsFiles));
  1032. }
  1033. sub BOSEST_playGoogleTTS {
  1034. my ($hash, $ttsDir, $text, $volume, $lang, $stopAfterSpeak) = @_;
  1035. $hash->{helper}{tts}{volume} = $volume;
  1036. $hash->{helper}{tts}{stopAfterSpeak} = $stopAfterSpeak;
  1037. $hash->{helper}{tts}{fulltextmd5} = md5_hex($lang.$text);
  1038. my $filename = $ttsDir."/v1_".$hash->{helper}{tts}{fulltextmd5}.".mp3";
  1039. if(-f $filename) {
  1040. my $timestamp = (stat($filename))->mtime(); #last modification timestamp
  1041. my $now = time();
  1042. if($now-$timestamp < 2592000) {
  1043. #file is not older than 30 days
  1044. Log3 $hash, 5, "BOSEST: File $filename found. No new download required.";
  1045. BOSEST_playMessageStringArg($hash->{NAME});
  1046. return undef;
  1047. }
  1048. }
  1049. my @sentences = split (/(?<=[.?!])/, $text);
  1050. $hash->{helper}{tts}{downloads}{all} = "";
  1051. foreach my $sentence (@sentences) {
  1052. my $md5 = md5_hex($lang.$sentence);
  1053. $hash->{helper}{tts}{downloads}{$md5} = 0;
  1054. $hash->{helper}{tts}{downloads}{all} .= $md5.",";
  1055. BOSEST_downloadGoogleTTS($hash, $ttsDir, $sentence, $lang);
  1056. }
  1057. InternalTimer(gettimeofday()+1, "BOSEST_checkTTSDownloadFinished", $hash, 0);
  1058. return undef;
  1059. }
  1060. sub BOSEST_checkTTSDownloadFinished {
  1061. my ($hash) = @_;
  1062. my @allMd5 = split(",", $hash->{helper}{tts}{downloads}{all});
  1063. my $msgStatus = 1;
  1064. foreach my $md5 (@allMd5) {
  1065. if($hash->{helper}{tts}{downloads}{$md5} == 10) {
  1066. $msgStatus = 10;
  1067. } elsif($hash->{helper}{tts}{downloads}{$md5} == 0) {
  1068. $msgStatus = 0;
  1069. }
  1070. }
  1071. if($msgStatus == 10) {
  1072. if(AttrVal($hash->{NAME}, "ttsSpeakOnError", "1") eq "1") {
  1073. my $md5 = md5_hex($BOSEST_GOOGLE_NOT_AVAILABLE_LANG.$BOSEST_GOOGLE_NOT_AVAILABLE_TEXT);
  1074. BOSEST_playMessage($hash, $md5, $hash->{helper}{tts}{volume}, $hash->{helper}{tts}{stopAfterSpeak});
  1075. } else {
  1076. Log3 $hash, 3, "BOSEST: Google translate download failed.";
  1077. }
  1078. } elsif($msgStatus == 0) {
  1079. #check again in 1s
  1080. InternalTimer(gettimeofday()+1, "BOSEST_checkTTSDownloadFinished", $hash, 0);
  1081. } else {
  1082. BlockingCall("BOSEST_joinAudioFilesBlocking", $hash->{NAME}."|v1_".$hash->{helper}{tts}{fulltextmd5}.".mp3|BOSEST_silence.mp3|".join(".mp3|", @allMd5).".mp3", "BOSEST_playMessageStringArg");
  1083. }
  1084. return undef;
  1085. }
  1086. sub BOSEST_downloadGoogleTTS {
  1087. my ($hash, $ttsDir, $text, $lang) = @_;
  1088. BOSEST_downloadGoogleNotAvailable($hash);
  1089. my $md5 = md5_hex($lang.$text);
  1090. my $filename = $ttsDir."/".$md5.".mp3";
  1091. if(-f $filename) {
  1092. my $timestamp = (stat($filename))->mtime(); #last modification timestamp
  1093. my $now = time();
  1094. if($now-$timestamp < 2592000) {
  1095. #file is not older than 30 days
  1096. $hash->{helper}{tts}{downloads}{$md5} = 1;
  1097. return undef;
  1098. }
  1099. }
  1100. BOSEST_retrieveGooglTTSFile($hash, $filename, $md5, $text, $lang, sub {
  1101. my ($hash, $filename, $md5, $downloadOk) = @_;
  1102. if($downloadOk) {
  1103. my ($err, $val) = getKeyValue("BOSEST_tts_files");
  1104. if(!defined($val)) {
  1105. $val = "";
  1106. } else {
  1107. $val .= ",";
  1108. }
  1109. $err = setKeyValue("BOSEST_tts_files", $val.$md5);
  1110. $err = setKeyValue($md5, gettimeofday());
  1111. $hash->{helper}{tts}{downloads}{$md5} = 1;
  1112. #add silence and play message afterwards
  1113. } else {
  1114. $hash->{helper}{tts}{downloads}{$md5} = 10; #download error
  1115. }
  1116. });
  1117. return undef;
  1118. }
  1119. sub BOSEST_setMusicServiceAccount($$$) {
  1120. my ($hash, $friendlyName, $uuid) = @_;
  1121. my $postXml = '<credentials source="STORED_MUSIC" displayName="'.
  1122. $friendlyName.
  1123. '"><user>'.
  1124. $uuid.'/0'.
  1125. '</user><pass/></credentials>';
  1126. if(BOSEST_HTTPPOST($hash, '/setMusicServiceAccount', $postXml)) {
  1127. #ok
  1128. }
  1129. return undef;
  1130. }
  1131. sub BOSEST_removeMusicServiceAccount($$$) {
  1132. my ($hash, $friendlyName, $uuid) = @_;
  1133. my $postXml = '<credentials source="STORED_MUSIC" displayName="'.
  1134. $friendlyName.
  1135. '"><user>'.
  1136. $uuid.
  1137. '</user><pass/></credentials>';
  1138. if(BOSEST_HTTPPOST($hash, '/removeMusicServiceAccount', $postXml)) {
  1139. #ok
  1140. }
  1141. return undef;
  1142. }
  1143. sub BOSEST_playTrack($$) {
  1144. my ($hash, $trackName) = @_;
  1145. my $ttsDlnaServer = $hash->{helper}{ttsdlnaserver};
  1146. if(defined($ttsDlnaServer) && $ttsDlnaServer ne "") {
  1147. Log3 $hash, 4, "BOSEST: Search for $trackName on $ttsDlnaServer";
  1148. if(my $xmlTrack = BOSEST_searchTrack($hash, $ttsDlnaServer, $trackName)) {
  1149. BOSEST_setContentItem($hash,
  1150. $xmlTrack->{itemName},
  1151. $xmlTrack->{type},
  1152. $xmlTrack->{location},
  1153. $xmlTrack->{source},
  1154. $xmlTrack->{sourceAccount});
  1155. return undef;
  1156. }
  1157. }
  1158. foreach my $source (@{$hash->{helper}{sources}}) {
  1159. if($source->{source} eq "STORED_MUSIC" && $source->{status} eq "READY") {
  1160. Log3 $hash, 4, "BOSEST: Search for $trackName on $source->{sourceAccount}";
  1161. if(my $xmlTrack = BOSEST_searchTrack($hash, $source->{sourceAccount}, $trackName)) {
  1162. BOSEST_setContentItem($hash,
  1163. $xmlTrack->{itemName},
  1164. $xmlTrack->{type},
  1165. $xmlTrack->{location},
  1166. $xmlTrack->{source},
  1167. $xmlTrack->{sourceAccount});
  1168. $hash->{helper}{ttsdlnaserver} = $source->{sourceAccount};
  1169. last;
  1170. }
  1171. #sleep 100ms, otherwise internal server error from BOSE speaker
  1172. select(undef, undef, undef, 0.1);
  1173. }
  1174. }
  1175. return undef;
  1176. }
  1177. sub BOSEST_searchTrack($$$) {
  1178. my ($hash, $dlnaUid, $trackName) = @_;
  1179. my $postXml = '<search source="STORED_MUSIC" sourceAccount="'.
  1180. $dlnaUid.
  1181. '"><startItem>1</startItem><numItems>1</numItems><searchTerm filter="track">'.
  1182. $trackName.
  1183. '</searchTerm></search>';
  1184. if(my $xmlSearchResult = BOSEST_HTTPPOST($hash, '/search', $postXml)) {
  1185. #return first item from search results
  1186. if($xmlSearchResult->{searchResponse}->{items}) {
  1187. return $xmlSearchResult->{searchResponse}->{items}->{item}[0]->{ContentItem};
  1188. }
  1189. }
  1190. return undef;
  1191. }
  1192. ###### UPDATE VIA HTTP ######
  1193. sub BOSEST_updateClock($$) {
  1194. my ($hash, $deviceId) = @_;
  1195. my $clockDisplay = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/clockDisplay");
  1196. BOSEST_processXml($hash, $clockDisplay);
  1197. return undef;
  1198. }
  1199. sub BOSEST_updateInfo($$) {
  1200. my ($hash, $deviceId) = @_;
  1201. my $info = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/info");
  1202. BOSEST_processXml($hash, $info);
  1203. return undef;
  1204. }
  1205. sub BOSEST_updateSources($$) {
  1206. my ($hash, $deviceId) = @_;
  1207. my $sources = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/sources");
  1208. BOSEST_processXml($hash, $sources);
  1209. return undef;
  1210. }
  1211. sub BOSEST_updatePresets($$) {
  1212. my ($hash, $deviceId) = @_;
  1213. my $presets = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/presets");
  1214. BOSEST_processXml($hash, $presets);
  1215. return undef;
  1216. }
  1217. sub BOSEST_updateZone($$) {
  1218. my ($hash, $deviceId) = @_;
  1219. my $zone = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/getZone");
  1220. BOSEST_processXml($hash, $zone);
  1221. return undef;
  1222. }
  1223. sub BOSEST_updateVolume($$) {
  1224. my ($hash, $deviceId) = @_;
  1225. my $volume = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/volume");
  1226. BOSEST_processXml($hash, $volume);
  1227. return undef;
  1228. }
  1229. sub BOSEST_updateBass($$) {
  1230. my ($hash, $deviceId) = @_;
  1231. my $bass = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/bass");
  1232. BOSEST_processXml($hash, $bass);
  1233. return undef;
  1234. }
  1235. sub BOSEST_updateNowPlaying($$) {
  1236. my ($hash, $deviceId) = @_;
  1237. my $nowPlaying = BOSEST_HTTPGET($hash, $hash->{helper}{IP}, "/now_playing");
  1238. BOSEST_processXml($hash, $nowPlaying);
  1239. return undef;
  1240. }
  1241. sub BOSEST_updateAutoZone {
  1242. my ($hash, $location) = @_;
  1243. return undef if($location eq "");
  1244. return undef if(AttrVal($hash->{NAME}, "autoZone", "off") eq "off");
  1245. my @allPlayers = BOSEST_getAllBosePlayers($hash);
  1246. my $newZoneMaster;
  1247. my $createZone = 0;
  1248. foreach my $playerHash (@allPlayers) {
  1249. next if($playerHash->{DEVICEID} eq $hash->{DEVICEID});
  1250. my $playerLocation = ReadingsVal($playerHash->{NAME}, "contentItemLocation", "");
  1251. my $playerZoneMaster = ReadingsVal($playerHash->{NAME}, "zoneMaster", "");
  1252. Log3 $hash, 5, "BOSEST: auto-zone $hash->{NAME}: $location = $playerHash->{NAME}: $playerLocation?";
  1253. #make sure that $playerHash is master device
  1254. if($playerLocation eq $location && ($playerZoneMaster eq "" or $playerZoneMaster eq $playerHash->{DEVICEID})) {
  1255. #TODO: check if createZone is needed
  1256. $newZoneMaster = $playerHash;
  1257. $createZone = 1 if($playerZoneMaster eq "");
  1258. }
  1259. }
  1260. if($newZoneMaster) {
  1261. if($createZone) {
  1262. BOSEST_createZone($newZoneMaster, $hash->{DEVICEID});
  1263. } else {
  1264. BOSEST_addToZone($newZoneMaster, $hash->{DEVICEID});
  1265. }
  1266. }
  1267. }
  1268. sub BOSEST_checkDoubleTap($$) {
  1269. my ($hash, $channel) = @_;
  1270. return undef if($channel eq "" or $channel eq "0");
  1271. if(!defined($hash->{helper}{dt_nowSelectionUpdatedTS}) or $channel ne $hash->{helper}{dt_nowSelectionUpdatedCH}) {
  1272. $hash->{helper}{dt_nowSelectionUpdatedTS} = gettimeofday();
  1273. $hash->{helper}{dt_nowSelectionUpdatedCH} = $channel;
  1274. $hash->{helper}{dt_lastChange} = 0;
  1275. $hash->{helper}{dt_counter} = 1;
  1276. return undef;
  1277. }
  1278. my $timeDiff = gettimeofday() - $hash->{helper}{dt_nowSelectionUpdatedTS};
  1279. if($timeDiff < 1) {
  1280. $hash->{helper}{dt_counter}++;
  1281. if($hash->{helper}{dt_counter} == 2) {
  1282. if(ReadingsVal($hash->{NAME}, "zoneMaster", "") eq $hash->{DEVICEID}) {
  1283. BOSEST_stopPlayEverywhere($hash);
  1284. $hash->{helper}{dt_lastChange} = gettimeofday();
  1285. } elsif(ReadingsVal($hash->{NAME}, "zoneMaster", "") eq "") {
  1286. #make sure that play isn't started just after stop, that might confuse the player
  1287. my $timeDiffMasterChange = gettimeofday() - $hash->{helper}{dt_lastChange};
  1288. if($timeDiffMasterChange > 2) {
  1289. BOSEST_playEverywhere($hash);
  1290. $hash->{helper}{dt_lastChange} = gettimeofday();
  1291. }
  1292. }
  1293. } elsif($hash->{helper}{dt_counter} == 3) {
  1294. #handle three-tap function - ideas?
  1295. }
  1296. } else {
  1297. $hash->{helper}{dt_counter} = 1;
  1298. }
  1299. $hash->{helper}{dt_nowSelectionUpdatedTS} = gettimeofday();
  1300. return undef;
  1301. }
  1302. ###### XML PROCESSING ######
  1303. sub BOSEST_processXml($$) {
  1304. my ($hash, $wsxml) = @_;
  1305. Log3 $hash, 5, "BOSEST: processXml:\n".Dumper($wsxml);
  1306. if($wsxml->{updates}) {
  1307. if($wsxml->{updates}->{nowPlayingUpdated}) {
  1308. if($wsxml->{updates}->{nowPlayingUpdated}->{nowPlaying}) {
  1309. BOSEST_parseAndUpdateNowPlaying($hash, $wsxml->{updates}->{nowPlayingUpdated}->{nowPlaying});
  1310. if($hash->{helper}{switchSource} ne "") {
  1311. BOSEST_setSource($hash, $hash->{helper}{switchSource});
  1312. } else {
  1313. BOSEST_speakChannel($hash);
  1314. }
  1315. }
  1316. } elsif ($wsxml->{updates}->{volumeUpdated}) {
  1317. BOSEST_parseAndUpdateVolume($hash, $wsxml->{updates}->{volumeUpdated}->{volume});
  1318. } elsif ($wsxml->{updates}->{nowSelectionUpdated}) {
  1319. BOSEST_parseAndUpdateChannel($hash, $wsxml->{updates}->{nowSelectionUpdated}->{preset}[0]);
  1320. BOSEST_checkDoubleTap($hash, $wsxml->{updates}->{nowSelectionUpdated}->{preset}[0]->{id});
  1321. } elsif ($wsxml->{updates}->{recentsUpdated}) {
  1322. BOSEST_parseAndUpdateRecents($hash, $wsxml->{updates}->{recentsUpdated}->{recents});
  1323. } elsif ($wsxml->{updates}->{connectionStateUpdated}) {
  1324. #BOSE SoundTouch team says that it's not necessary to handle this one
  1325. } elsif ($wsxml->{updates}->{clockDisplayUpdated}) {
  1326. #TODO handle clockDisplayUpdated (feature currently unknown)
  1327. } elsif ($wsxml->{updates}->{presetsUpdated}) {
  1328. BOSEST_parseAndUpdatePresets($hash, $wsxml->{updates}->{presetsUpdated}->{presets});
  1329. } elsif ($wsxml->{updates}->{zoneUpdated}) {
  1330. #zoneUpdated is just a notification with no data
  1331. BOSEST_updateZone($hash, $hash->{DEVICEID});
  1332. } elsif ($wsxml->{updates}->{bassUpdated}) {
  1333. #bassUpdated is just a notification with no data
  1334. BOSEST_updateBass($hash, $hash->{DEVICEID});
  1335. } elsif ($wsxml->{updates}->{infoUpdated}) {
  1336. #infoUpdated is just a notification with no data
  1337. BOSEST_updateInfo($hash, $hash->{DEVICEID});
  1338. } elsif ($wsxml->{updates}->{sourcesUpdated}) {
  1339. #sourcesUpdated is just a notification with no data
  1340. BOSEST_updateSources($hash, $hash->{DEVICEID});
  1341. } elsif ($wsxml->{updates}->{clockTimeUpdated}) {
  1342. BOSEST_parseAndUpdateClock($hash, $wsxml->{updates}->{clockTimeUpdated});
  1343. } else {
  1344. Log3 $hash, 4, "BOSEST: Unknown event, please implement:\n".Dumper($wsxml);
  1345. }
  1346. } elsif($wsxml->{info}) {
  1347. BOSEST_parseAndUpdateInfo($hash, $wsxml->{info});
  1348. } elsif($wsxml->{nowPlaying}) {
  1349. BOSEST_parseAndUpdateNowPlaying($hash, $wsxml->{nowPlaying});
  1350. } elsif($wsxml->{volume}) {
  1351. BOSEST_parseAndUpdateVolume($hash, $wsxml->{volume});
  1352. } elsif($wsxml->{presets}) {
  1353. BOSEST_parseAndUpdatePresets($hash, $wsxml->{presets});
  1354. } elsif($wsxml->{bass}) {
  1355. BOSEST_parseAndUpdateBass($hash, $wsxml->{bass});
  1356. } elsif($wsxml->{zone}) {
  1357. BOSEST_parseAndUpdateZone($hash, $wsxml->{zone});
  1358. } elsif($wsxml->{sources}) {
  1359. BOSEST_parseAndUpdateSources($hash, $wsxml->{sources}->{sourceItem});
  1360. } elsif($wsxml->{msg}) {
  1361. if($wsxml->{msg}->{body} && $wsxml->{msg}->{body}->{pingRequest}) {
  1362. #pingpong
  1363. } else {
  1364. Log3 $hash, 4, "BOSEST: Unknown event, please implement:\n".Dumper($wsxml);
  1365. }
  1366. } else {
  1367. Log3 $hash, 4, "BOSEST: Unknown event, please implement:\n".Dumper($wsxml);
  1368. }
  1369. if($hash->{helper}{stateCheck}{enabled}) {
  1370. #check if state is action state
  1371. if(ReadingsVal($hash->{NAME}, "contentItemItemName", "") eq $hash->{helper}{stateCheck}{actionContentItemItemName}) {
  1372. $hash->{helper}{stateCheck}{actionActive} = 1;
  1373. } else {
  1374. if($hash->{helper}{stateCheck}{actionActive}) {
  1375. if(ReadingsVal($hash->{NAME}, "contentItemItemName", "") ne $hash->{helper}{stateCheck}{actionContentItemItemName}) {
  1376. #call function with $hash as argument
  1377. $hash->{helper}{stateCheck}{function}->($hash);
  1378. $hash->{helper}{stateCheck}{enabled} = 0;
  1379. $hash->{helper}{stateCheck}{actionActive} = 0;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. return undef;
  1385. }
  1386. sub BOSEST_parseAndUpdateClock($$) {
  1387. my ($hash, $clock) = @_;
  1388. if($clock->{clockTime}->{brightness} eq "0") {
  1389. readingsSingleUpdate($hash, "clockDisplay", "off", 1);
  1390. } else {
  1391. readingsSingleUpdate($hash, "clockDisplay", "on", 1);
  1392. }
  1393. return undef;
  1394. }
  1395. sub BOSEST_parseAndUpdateSources($$) {
  1396. my ($hash, $sourceItems) = @_;
  1397. $hash->{helper}->{sources} = ();
  1398. foreach my $sourceItem (@{$sourceItems}) {
  1399. Log3 $hash, 5, "BOSEST: Add $sourceItem->{source}";
  1400. #save source information
  1401. # - source (BLUETOOTH, STORED_MUSIC, ...)
  1402. # - sourceAccount
  1403. # - status
  1404. # - isLocal
  1405. # - name
  1406. $sourceItem->{isLocal} = "" if(!defined($sourceItem->{isLocal}));
  1407. $sourceItem->{sourceAccount} = "" if(!defined($sourceItem->{sourceAccount}));
  1408. $sourceItem->{sourceAccount} = "" if(!defined($sourceItem->{sourceAccount}));
  1409. my %source = (source => $sourceItem->{source},
  1410. sourceAccount => $sourceItem->{sourceAccount},
  1411. status => $sourceItem->{status},
  1412. isLocal => $sourceItem->{isLocal},
  1413. name => $sourceItem->{content});
  1414. push @{$hash->{helper}->{sources}}, \%source;
  1415. }
  1416. my $connectedDlnaServers = "";
  1417. foreach my $sourceItem (@{ $hash->{helper}->{sources} }) {
  1418. if($sourceItem->{source} eq "STORED_MUSIC") {
  1419. $connectedDlnaServers .= $sourceItem->{name}.",";
  1420. }
  1421. }
  1422. #remove last comma
  1423. $connectedDlnaServers = substr($connectedDlnaServers, 0, length($connectedDlnaServers) - 1);
  1424. #replace blank with hyphen
  1425. $connectedDlnaServers =~ s/\ /_/g;
  1426. readingsSingleUpdate($hash, "connectedDLNAServers", $connectedDlnaServers, 1);
  1427. return undef;
  1428. }
  1429. sub BOSEST_parseAndUpdateChannel($$) {
  1430. my ($hash, $preset) = @_;
  1431. readingsBeginUpdate($hash);
  1432. if($preset->{id} ne "0") {
  1433. BOSEST_XMLUpdate($hash, "channel", $preset->{id});
  1434. } else {
  1435. BOSEST_XMLUpdate($hash, "channel", "");
  1436. $preset->{ContentItem}->{itemName} = "" if(!defined($preset->{ContentItem}->{itemName}));
  1437. $preset->{ContentItem}->{location} = "" if(!defined($preset->{ContentItem}->{location}));
  1438. $preset->{ContentItem}->{source} = "" if(!defined($preset->{ContentItem}->{source}));
  1439. $preset->{ContentItem}->{sourceAccount} = "" if(!defined($preset->{ContentItem}->{sourceAccount}));
  1440. my $channelString = $preset->{ContentItem}->{itemName}."|".$preset->{ContentItem}->{location}."|".
  1441. $preset->{ContentItem}->{source}."|".$preset->{ContentItem}->{sourceAccount};
  1442. foreach my $channelNr (7..20) {
  1443. my $channelVal = AttrVal($hash->{NAME}, sprintf("channel_%02d", $channelNr), "0");
  1444. if($channelVal eq $channelString) {
  1445. BOSEST_XMLUpdate($hash, "channel", $channelNr);
  1446. }
  1447. }
  1448. }
  1449. readingsEndUpdate($hash, 1);
  1450. return undef;
  1451. }
  1452. sub BOSEST_parseAndUpdateZone($$) {
  1453. my ($hash, $zone) = @_;
  1454. #only update zone from master
  1455. if(defined($zone->{master})) {
  1456. my $masterHash = BOSEST_getBosePlayerByDeviceId($hash, $zone->{master});
  1457. if($masterHash->{DEVICEID} ne $hash->{DEVICEID}) {
  1458. return undef;
  1459. }
  1460. }
  1461. my $i = 1;
  1462. readingsBeginUpdate($hash);
  1463. BOSEST_XMLUpdate($hash, "zoneMaster", $zone->{master});
  1464. readingsEndUpdate($hash, 1);
  1465. if($zone->{member}) {
  1466. foreach my $member (@{$zone->{member}}) {
  1467. my $player = BOSEST_getBosePlayerByDeviceId($hash, $member->{content});
  1468. readingsBeginUpdate($hash);
  1469. BOSEST_XMLUpdate($hash, "zoneMember_$i", $player->{DEVICEID});
  1470. readingsEndUpdate($hash, 1);
  1471. readingsBeginUpdate($player);
  1472. BOSEST_XMLUpdate($player, "zoneMaster", $zone->{master});
  1473. readingsEndUpdate($player, 1);
  1474. $i++;
  1475. }
  1476. my $memberCnt = $i - 1;
  1477. foreach my $member (@{$zone->{member}}) {
  1478. my $player = BOSEST_getBosePlayerByDeviceId($hash, $member->{content});
  1479. readingsBeginUpdate($player);
  1480. foreach my $cnt ($memberCnt..1) {
  1481. BOSEST_XMLUpdate($player, "zoneMember_$cnt", ReadingsVal($hash->{NAME}, "zoneMember_$cnt", ""));
  1482. }
  1483. readingsEndUpdate($player, 1);
  1484. }
  1485. }
  1486. while ($i < 20) {
  1487. if(defined($hash->{READINGS}{"zoneMember_$i"})) {
  1488. my $zoneMemberUdn = ReadingsVal($hash->{NAME}, "zoneMember_$i", "");
  1489. if($zoneMemberUdn ne "") {
  1490. my $memberHash = BOSEST_getBosePlayerByDeviceId($hash, $zoneMemberUdn);
  1491. readingsBeginUpdate($memberHash);
  1492. BOSEST_XMLUpdate($memberHash, "zoneMaster", "");
  1493. my $j = 1;
  1494. while($j < 20) {
  1495. BOSEST_XMLUpdate($memberHash, "zoneMember_$j", "") if(defined($hash->{READINGS}{"zoneMember_$j"}));
  1496. $j++;
  1497. }
  1498. readingsEndUpdate($memberHash, 1);
  1499. }
  1500. readingsBeginUpdate($hash);
  1501. BOSEST_XMLUpdate($hash, "zoneMember_$i", "");
  1502. readingsEndUpdate($hash, 1);
  1503. }
  1504. $i++;
  1505. }
  1506. return undef;
  1507. }
  1508. sub BOSEST_parseAndUpdatePresets($$) {
  1509. my ($hash, $presets) = @_;
  1510. my $maxpresets = 6;
  1511. my %activePresets = ();
  1512. readingsBeginUpdate($hash);
  1513. foreach my $preset (1..6) {
  1514. $activePresets{$preset} = "-";
  1515. }
  1516. foreach my $preset (@{ $presets->{preset} }) {
  1517. $activePresets{$preset->{id}} = $preset->{ContentItem}->{itemName};
  1518. }
  1519. foreach my $preset (1..6) {
  1520. BOSEST_XMLUpdate($hash, sprintf("channel_%02d", $preset), $activePresets{$preset});
  1521. }
  1522. readingsEndUpdate($hash, 1);
  1523. return undef;
  1524. }
  1525. sub BOSEST_parseAndUpdateRecents($$) {
  1526. my ($hash, $recents) = @_;
  1527. my $i = 1;
  1528. readingsBeginUpdate($hash);
  1529. foreach my $recentEntry (@{$recents->{recent}}) {
  1530. BOSEST_XMLUpdate($hash, sprintf("recent_%02d", $i), $recentEntry->{contentItem}->{itemName});
  1531. $hash->{helper}{recents}{$i}{location} = $recentEntry->{contentItem}->{location};
  1532. $hash->{helper}{recents}{$i}{source} = $recentEntry->{contentItem}->{source};
  1533. $hash->{helper}{recents}{$i}{sourceAccount} = $recentEntry->{contentItem}->{sourceAccount};
  1534. $hash->{helper}{recents}{$i}{itemName} = $recentEntry->{contentItem}->{itemName};
  1535. $i++;
  1536. last if($i > 15);
  1537. }
  1538. foreach my $x ($i..15) {
  1539. BOSEST_XMLUpdate($hash, sprintf("recent_%02d", $x), "-");
  1540. delete $hash->{helper}{recents}{$x};
  1541. }
  1542. readingsEndUpdate($hash, 1);
  1543. return undef;
  1544. }
  1545. sub BOSEST_parseAndUpdateVolume($$) {
  1546. my ($hash, $volume) = @_;
  1547. readingsBeginUpdate($hash);
  1548. BOSEST_XMLUpdate($hash, "volume", $volume->{actualvolume});
  1549. BOSEST_XMLUpdate($hash, "mute", $volume->{muteenabled});
  1550. readingsEndUpdate($hash, 1);
  1551. return undef;
  1552. }
  1553. sub BOSEST_parseAndUpdateBass($$) {
  1554. my ($hash, $bass) = @_;
  1555. my $currBass = $bass->{actualbass} + 10;
  1556. readingsBeginUpdate($hash);
  1557. BOSEST_XMLUpdate($hash, "bass", $currBass);
  1558. readingsEndUpdate($hash, 1);
  1559. return undef;
  1560. }
  1561. sub BOSEST_parseAndUpdateInfo($$) {
  1562. my ($hash, $info) = @_;
  1563. $info->{name} = Encode::encode('UTF-8', $info->{name});
  1564. readingsBeginUpdate($hash);
  1565. BOSEST_XMLUpdate($hash, "deviceName", $info->{name});
  1566. BOSEST_XMLUpdate($hash, "model", $info->{type});
  1567. BOSEST_XMLUpdate($hash, "deviceID", $info->{deviceID});
  1568. BOSEST_XMLUpdate($hash, "softwareVersion", $info->{components}->{component}[0]->{softwareVersion});
  1569. readingsEndUpdate($hash, 1);
  1570. return undef;
  1571. }
  1572. sub BOSEST_parseAndUpdateNowPlaying($$) {
  1573. my ($hash, $nowPlaying) = @_;
  1574. Log3 $hash, 5, "BOSEST: parseAndUpdateNowPlaying";
  1575. readingsBeginUpdate($hash);
  1576. BOSEST_XMLUpdate($hash, "stationName", $nowPlaying->{stationName});
  1577. BOSEST_XMLUpdate($hash, "track", $nowPlaying->{track});
  1578. BOSEST_XMLUpdate($hash, "source", $nowPlaying->{source});
  1579. BOSEST_XMLUpdate($hash, "album", $nowPlaying->{album});
  1580. BOSEST_XMLUpdate($hash, "artist", $nowPlaying->{artist});
  1581. BOSEST_XMLUpdate($hash, "playStatus", $nowPlaying->{playStatus});
  1582. BOSEST_XMLUpdate($hash, "stationLocation", $nowPlaying->{stationLocation});
  1583. BOSEST_XMLUpdate($hash, "trackID", $nowPlaying->{trackID});
  1584. BOSEST_XMLUpdate($hash, "artistID", $nowPlaying->{artistID});
  1585. BOSEST_XMLUpdate($hash, "rating", $nowPlaying->{rating});
  1586. BOSEST_XMLUpdate($hash, "description", $nowPlaying->{description});
  1587. BOSEST_XMLUpdate($hash, "shuffle", $nowPlaying->{shuffleSetting});
  1588. BOSEST_XMLUpdate($hash, "repeat", $nowPlaying->{repeatSetting});
  1589. if($nowPlaying->{time}) {
  1590. BOSEST_XMLUpdate($hash, "time", $nowPlaying->{time}->{content});
  1591. BOSEST_XMLUpdate($hash, "timeTotal", $nowPlaying->{time}->{total});
  1592. } else {
  1593. BOSEST_XMLUpdate($hash, "time", "");
  1594. BOSEST_XMLUpdate($hash, "timeTotal", "");
  1595. }
  1596. if($nowPlaying->{art}) {
  1597. BOSEST_XMLUpdate($hash, "art", $nowPlaying->{art}->{content});
  1598. BOSEST_XMLUpdate($hash, "artStatus", $nowPlaying->{art}->{artImageStatus});
  1599. } else {
  1600. BOSEST_XMLUpdate($hash, "art", "");
  1601. BOSEST_XMLUpdate($hash, "artStatus", "");
  1602. }
  1603. if($nowPlaying->{ContentItem}) {
  1604. BOSEST_XMLUpdate($hash, "contentItemItemName", $nowPlaying->{ContentItem}->{itemName});
  1605. BOSEST_XMLUpdate($hash, "contentItemLocation", $nowPlaying->{ContentItem}->{location});
  1606. BOSEST_XMLUpdate($hash, "contentItemSourceAccount", $nowPlaying->{ContentItem}->{sourceAccount});
  1607. BOSEST_XMLUpdate($hash, "contentItemSource", $nowPlaying->{ContentItem}->{source});
  1608. BOSEST_XMLUpdate($hash, "contentItemIsPresetable", $nowPlaying->{ContentItem}->{isPresetable});
  1609. BOSEST_XMLUpdate($hash, "contentItemType", $nowPlaying->{ContentItem}->{type});
  1610. #TODO
  1611. #if location is the same as on other speaker, start auto-zone
  1612. BOSEST_updateAutoZone($hash, ReadingsVal($hash->{NAME}, "contentItemLocation", 1));
  1613. } else {
  1614. BOSEST_XMLUpdate($hash, "contentItemItemName", "");
  1615. BOSEST_XMLUpdate($hash, "contentItemLocation", "");
  1616. BOSEST_XMLUpdate($hash, "contentItemSourceAccount", "");
  1617. BOSEST_XMLUpdate($hash, "contentItemSource", "");
  1618. BOSEST_XMLUpdate($hash, "contentItemIsPresetable", "");
  1619. BOSEST_XMLUpdate($hash, "contentItemType", "");
  1620. }
  1621. if($nowPlaying->{connectionStatusInfo}) {
  1622. BOSEST_XMLUpdate($hash, "connectionStatusInfo", $nowPlaying->{connectionStatusInfo}->{status});
  1623. } else {
  1624. BOSEST_XMLUpdate($hash, "connectionStatusInfo", "");
  1625. }
  1626. #handle state based on play status and standby state
  1627. if($nowPlaying->{source} eq "STANDBY") {
  1628. BOSEST_XMLUpdate($hash, "state", "online");
  1629. } else {
  1630. if(defined($nowPlaying->{playStatus})) {
  1631. if($nowPlaying->{playStatus} eq "BUFFERING_STATE") {
  1632. BOSEST_XMLUpdate($hash, "state", "buffering");
  1633. } elsif($nowPlaying->{playStatus} eq "PLAY_STATE") {
  1634. BOSEST_XMLUpdate($hash, "state", "playing");
  1635. } elsif($nowPlaying->{playStatus} eq "STOP_STATE") {
  1636. BOSEST_XMLUpdate($hash, "state", "stopped");
  1637. } elsif($nowPlaying->{playStatus} eq "PAUSE_STATE") {
  1638. BOSEST_XMLUpdate($hash, "state", "paused");
  1639. } elsif($nowPlaying->{playStatus} eq "INVALID_PLAY_STATUS") {
  1640. BOSEST_XMLUpdate($hash, "state", "invalid");
  1641. }
  1642. }
  1643. }
  1644. #reset sent_off/on to enable the command again
  1645. #it's not allowed to send 2 times off/on due to toggle
  1646. #therefore I'm waiting for one signal to be
  1647. #received via websocket
  1648. $hash->{helper}{sent_off} = 0;
  1649. $hash->{helper}{sent_on} = 0;
  1650. readingsEndUpdate($hash, 1);
  1651. return undef;
  1652. }
  1653. ###### DISCOVERY #######
  1654. sub BOSEST_startDiscoveryProcess($) {
  1655. my ($hash) = @_;
  1656. if(!$init_done) {
  1657. #init not done yet, wait 3 more seconds
  1658. InternalTimer(gettimeofday()+3, "BOSEST_startDiscoveryProcess", $hash, 0);
  1659. }
  1660. if (!defined($hash->{helper}{DISCOVERY_PID})) {
  1661. $hash->{helper}{DISCOVERY_PID} = BlockingCall("BOSEST_Discovery", $hash->{NAME}."|".$hash, "BOSEST_finishedDiscovery");
  1662. }
  1663. }
  1664. sub BOSEST_handleDeviceByIp {
  1665. my ($hash, $ip) = @_;
  1666. my $return = "";
  1667. my $info = BOSEST_HTTPGET($hash, $ip, "/info");
  1668. #remove info tag to reduce line length
  1669. $info = $info->{info} if (defined($info->{info}));
  1670. #skip entry if no deviceid was found
  1671. return "" if (!defined($info->{deviceID}));
  1672. #TODO return if the device is already defined and IP is the same
  1673. # make sure that this can be done and no further code below is needed
  1674. #create new device if it doesn't exist
  1675. if(!defined(BOSEST_getBosePlayerByDeviceId($hash, $info->{deviceID}))) {
  1676. $info->{name} = Encode::encode('UTF-8',$info->{name});
  1677. Log3 $hash, 3, "BOSEST: Device $info->{name} ($info->{deviceID}) found.";
  1678. $return = $return."|commandDefineBOSE|$info->{deviceID},$info->{name}";
  1679. #set supported capabilities
  1680. my $capabilities = BOSEST_HTTPGET($hash, $ip, "/capabilities");
  1681. $return .= "|capabilities|$info->{deviceID}";
  1682. if($capabilities->{capabilities}->{clockDisplay}) {
  1683. $return .= ",".$capabilities->{capabilities}->{clockDisplay};
  1684. } else {
  1685. $return .= ",false";
  1686. }
  1687. #set supported bass capabilities
  1688. my $bassCapabilities = BOSEST_HTTPGET($hash, $ip, "/bassCapabilities");
  1689. $return .= "|bassCapabilities|$info->{deviceID}";
  1690. if($bassCapabilities->{bassCapabilities}) {
  1691. my $bassCap = $bassCapabilities->{bassCapabilities};
  1692. $return .= ",".$bassCap->{bassAvailable}.",".$bassCap->{bassMin}.",".
  1693. $bassCap->{bassMax}.",".$bassCap->{bassDefault};
  1694. }
  1695. }
  1696. #TODO create own function (add own DLNA server)
  1697. my $myIp = BOSEST_getMyIp($hash);
  1698. my $listMediaServers = BOSEST_HTTPGET($hash, $ip, "/listMediaServers");
  1699. #set supported sources
  1700. my $sources = BOSEST_HTTPGET($hash, $ip, "/sources");
  1701. $return .= "|supportedSources|$info->{deviceID}";
  1702. foreach my $source (@{ $sources->{sources}->{sourceItem} }) {
  1703. $return .= ",".$source->{source};
  1704. }
  1705. my $returnListMediaServers = "|listMediaServers|".$info->{deviceID};
  1706. foreach my $mediaServer (@{ $listMediaServers->{ListMediaServersResponse}->{media_server} }) {
  1707. $returnListMediaServers .= ",".$mediaServer->{friendly_name};
  1708. #check if it is already connected
  1709. my $isConnected = 0;
  1710. foreach my $source (@{ $sources->{sources}->{sourceItem} }) {
  1711. next if($source->{source} ne "STORED_MUSIC");
  1712. if(substr($source->{sourceAccount}, 0, length($mediaServer->{id})) eq $mediaServer->{id}) {
  1713. #already connected
  1714. $isConnected = 1;
  1715. next;
  1716. }
  1717. }
  1718. next if($isConnected);
  1719. if(($myIp eq $mediaServer->{ip}) ||
  1720. (AttrVal($hash->{NAME}, "autoAddDLNAServers", "0") eq "1" )) {
  1721. $return = $return."|setMusicServiceAccount|".$info->{deviceID}.",".$mediaServer->{friendly_name}.",".$mediaServer->{id};
  1722. Log3 $hash, 3, "BOSEST: DLNA Server ".$mediaServer->{friendly_name}." added.";
  1723. }
  1724. }
  1725. #append listMediaServers
  1726. $return .= $returnListMediaServers;
  1727. #update IP address of the device
  1728. $return = $return."|updateIP|".$info->{deviceID}.",".$ip;
  1729. return $return;
  1730. }
  1731. sub BOSEST_Discovery($) {
  1732. my ($string) = @_;
  1733. my ($name, $hash) = split("\\|", $string);
  1734. my $return = "$name";
  1735. $hash = $main::defs{$name};
  1736. eval {
  1737. my $res = Net::Bonjour->new('soundtouch');
  1738. $res->discover;
  1739. foreach my $device ($res->entries) {
  1740. $return .= BOSEST_handleDeviceByIp($hash, $device->address);
  1741. }
  1742. };
  1743. #update static players
  1744. my @staticIPs = split(",", AttrVal($hash->{NAME}, "staticIPs", ""));
  1745. foreach my $ip (@staticIPs) {
  1746. $return .= BOSEST_handleDeviceByIp($hash, $ip);
  1747. }
  1748. if($@) {
  1749. Log3 $hash, 3, "BOSEST: Discovery failed with: $@";
  1750. }
  1751. return $return;
  1752. }
  1753. sub BOSEST_finishedDiscovery($) {
  1754. my ($string) = @_;
  1755. my @commands = split("\\|", $string);
  1756. my $name = $commands[0];
  1757. my $hash = $defs{$name};
  1758. my $i = 0;
  1759. my $ignoreDeviceIDs = AttrVal($hash->{NAME}, "ignoreDeviceIDs", "");
  1760. delete($hash->{helper}{DISCOVERY_PID});
  1761. #start discovery again after 67s
  1762. InternalTimer(gettimeofday()+67, "BOSEST_startDiscoveryProcess", $hash, 1);
  1763. Log3 $hash, 5, "BOSEST: finished discovery";
  1764. for($i = 1; $i < @commands; $i = $i+2) {
  1765. my $command = $commands[$i];
  1766. my @params = split(",", $commands[$i+1]);
  1767. my $deviceId = shift(@params);
  1768. next if($ignoreDeviceIDs =~ /$deviceId/);
  1769. if($command eq "commandDefineBOSE") {
  1770. my $deviceName = $params[0];
  1771. BOSEST_commandDefine($hash, $deviceId, $deviceName);
  1772. } elsif($command eq "updateIP") {
  1773. my $ip = $params[0];
  1774. BOSEST_updateIP($hash, $deviceId, $ip);
  1775. } elsif($command eq "setMusicServiceAccount") {
  1776. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  1777. #0...friendly name
  1778. #1...UUID
  1779. BOSEST_setMusicServiceAccount($deviceHash, $params[0], $params[1]);
  1780. } elsif($command eq "listMediaServers") {
  1781. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  1782. $deviceHash->{helper}{dlnaServers} = join(",", @params);
  1783. $deviceHash->{helper}{dlnaServers} =~ s/\ /_/g;
  1784. } elsif($command eq "bassCapabilities") {
  1785. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  1786. #bassAvailable, bassMin, bassMax, bassDefault
  1787. $deviceHash->{helper}{bassAvailable} = 1 if($params[0] eq "true");
  1788. $deviceHash->{helper}{bassMin} = $params[1];
  1789. $deviceHash->{helper}{bassMax} = $params[2];
  1790. $deviceHash->{helper}{bassDefault} = $params[3];
  1791. if($params[0] eq "true") {
  1792. $deviceHash->{helper}{supportedBassCmds} = "bass:slider,1,1,10";
  1793. } else {
  1794. $deviceHash->{helper}{supportedBassCmds} = "";
  1795. }
  1796. } elsif($command eq "supportedSources") {
  1797. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  1798. #list of supported sources
  1799. $deviceHash->{helper}{bluetoothSupport} = 0;
  1800. $deviceHash->{helper}{auxSupport} = 0;
  1801. $deviceHash->{helper}{airplaySupport} = 0;
  1802. $deviceHash->{helper}{supportedSourcesCmds} = "";
  1803. foreach my $source (@params) {
  1804. if($source eq "BLUETOOTH") {
  1805. $deviceHash->{helper}{bluetoothSupport} = 1;
  1806. $deviceHash->{helper}{supportedSourcesCmds} .= "bluetooth,bt-discover,";
  1807. } elsif($source eq "AUX") {
  1808. $deviceHash->{helper}{auxSupport} = 1;
  1809. $deviceHash->{helper}{supportedSourcesCmds} .= "aux,";
  1810. } elsif($source eq "AIRPLAY") {
  1811. $deviceHash->{helper}{airplaySupport} = 1;
  1812. $deviceHash->{helper}{supportedSourcesCmds} .= "airplay,";
  1813. }
  1814. }
  1815. $deviceHash->{helper}{supportedSourcesCmds} = substr($deviceHash->{helper}{supportedSourcesCmds}, 0, length($deviceHash->{helper}{supportedSourcesCmds})-1);
  1816. } elsif($command eq "capabilities") {
  1817. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceId);
  1818. if(ReadingsVal($deviceHash->{NAME}, "supportClockDisplay", "") ne $params[0]) {
  1819. readingsSingleUpdate($deviceHash, "supportClockDisplay", $params[0], 1);
  1820. }
  1821. }
  1822. }
  1823. }
  1824. sub BOSEST_updateIP($$$) {
  1825. my ($hash, $deviceID, $ip) = @_;
  1826. my $deviceHash = BOSEST_getBosePlayerByDeviceId($hash, $deviceID);
  1827. #check current IP of the device
  1828. my $currentIP = $deviceHash->{helper}{IP};
  1829. $currentIP = "unknown" if(!defined($currentIP));
  1830. #if update is needed, get info/now_playing
  1831. if($currentIP ne $ip) {
  1832. $deviceHash->{helper}{IP} = $ip;
  1833. readingsSingleUpdate($deviceHash, "IP", $ip, 1);
  1834. readingsSingleUpdate($deviceHash, "presence", "online", 1);
  1835. Log3 $hash, 3, "BOSEST: $deviceHash->{NAME}, new IP ($ip)";
  1836. #get info
  1837. Log3 $hash, 5, "BOSEST: BOSEST_updateInfo";
  1838. BOSEST_updateInfo($deviceHash, $deviceID);
  1839. Log3 $hash, 5, "BOSEST: BOSEST_updateNowPlaying";
  1840. #get now_playing
  1841. BOSEST_updateNowPlaying($deviceHash, $deviceID);
  1842. Log3 $hash, 5, "BOSEST: BOSEST_setVolume";
  1843. #set previous volume if not playing anything
  1844. if(ReadingsVal($deviceHash->{NAME}, "state", "") eq "online") {
  1845. BOSEST_setVolume($deviceHash, ReadingsVal($deviceHash->{NAME}, "volume", 10));
  1846. }
  1847. #get current volume
  1848. Log3 $hash, 5, "BOSEST: BOSEST_updateVolume";
  1849. BOSEST_updateVolume($deviceHash, $deviceID);
  1850. #get current presets
  1851. Log3 $hash, 5, "BOSEST: BOSEST_updatePresets";
  1852. BOSEST_updatePresets($deviceHash, $deviceID);
  1853. #get current bass settings
  1854. Log3 $hash, 5, "BOSEST: BOSEST_updateBass";
  1855. BOSEST_updateBass($deviceHash, $deviceID);
  1856. #get current zone settings
  1857. Log3 $hash, 5, "BOSEST: BOSEST_updateZone";
  1858. BOSEST_updateZone($deviceHash, $deviceID);
  1859. #get current sources
  1860. Log3 $hash, 5, "BOSEST: BOSEST_updateSources";
  1861. BOSEST_updateSources($deviceHash, $deviceID);
  1862. #get current clock state
  1863. Log3 $hash, 5, "BOSEST: BOSEST_updateClock";
  1864. BOSEST_updateClock($deviceHash, $deviceID);
  1865. #connect websocket
  1866. Log3 $hash, 4, "BOSEST: $deviceHash->{NAME}, start new WebSocket.";
  1867. BOSEST_startWebSocketConnection($deviceHash);
  1868. BOSEST_checkWebSocketConnection($deviceHash);
  1869. }
  1870. return undef;
  1871. }
  1872. sub BOSEST_commandDefine($$$) {
  1873. my ($hash, $deviceID, $deviceName) = @_;
  1874. #check if device exists already
  1875. if(!defined(BOSEST_getBosePlayerByDeviceId($hash, $deviceID))) {
  1876. CommandDefine(undef, "BOSE_$deviceID BOSEST $deviceID");
  1877. CommandAttr(undef, "BOSE_$deviceID alias $deviceName");
  1878. }
  1879. return undef;
  1880. }
  1881. ###### WEBSOCKET #######
  1882. sub BOSEST_webSocketCallback($$$) {
  1883. my ($hash, $ua, $tx) = @_;
  1884. Log3 $hash, 5, "BOSEST: Callback called";
  1885. if(!$tx->is_websocket) {
  1886. Log3 $hash, 3, "BOSEST: $hash->{NAME}, WebSocket failed, retry.";
  1887. BOSEST_startWebSocketConnection($hash);
  1888. return undef;
  1889. } else {
  1890. #avoid multiple websocket connections to one speaker
  1891. $hash->{helper}{wsconnected} += 1;
  1892. if($hash->{helper}{wsconnected} > 1) {
  1893. $tx->finish;
  1894. return undef;
  1895. }
  1896. Log3 $hash, 3, "BOSEST: $hash->{NAME}, WebSocket connection succeed.";
  1897. }
  1898. #register on message method
  1899. $tx->on(message => sub { my ($tx2, $msg) = @_; BOSEST_webSocketReceivedMsg($hash, $tx2, $msg); });
  1900. #register on finish method
  1901. $tx->on(finish => sub { my $ws = shift; BOSEST_webSocketFinished($hash, $ws); });
  1902. #add recurring ping to mojo ioloop due to inactivity timeout
  1903. $hash->{helper}{mojoping} = Mojo::IOLoop->recurring(19 => sub { BOSEST_webSocketPing($hash, $tx); });
  1904. return undef;
  1905. }
  1906. sub BOSEST_webSocketFinished($$) {
  1907. my ($hash, $ws) = @_;
  1908. Log3 $hash, 3, "BOSEST: $hash->{NAME}, WebSocket connection dropped - try reconnect.";
  1909. #set IP to unknown due to connection drop
  1910. $hash->{helper}{IP} = "unknown";
  1911. #connection dropped
  1912. $hash->{helper}{wsconnected} -= 1;
  1913. #set presence & state to offline due to connection drop
  1914. readingsBeginUpdate($hash);
  1915. BOSEST_readingsSingleUpdateIfChanged($hash, "IP", "unknown", 1);
  1916. BOSEST_readingsSingleUpdateIfChanged($hash, "presence", "offline", 1);
  1917. BOSEST_readingsSingleUpdateIfChanged($hash, "state", "offline", 1);
  1918. readingsEndUpdate($hash, 1);
  1919. Mojo::IOLoop->remove($hash->{helper}{mojoping});
  1920. $ws->finish;
  1921. return undef;
  1922. }
  1923. sub BOSEST_webSocketPing($$) {
  1924. my ($hash, $tx) = @_;
  1925. #reset requestid for ping to avoid overflows
  1926. $hash->{helper}{requestId} = 1 if($hash->{helper}{requestId} > 9999);
  1927. $tx->send('<msg><header deviceID="'.
  1928. $hash->{DEVICEID}.
  1929. '" url="webserver/pingRequest" method="GET"><request requestID="'.
  1930. $hash->{helper}{requestId}.
  1931. '"><info type="new"/></request></header></msg>');
  1932. #$tx->send([1, 0, 0, 0, WS_PING, 'Hello World!']);
  1933. return undef;
  1934. }
  1935. sub BOSEST_webSocketReceivedMsg($$$) {
  1936. my ($hash, $tx, $msg) = @_;
  1937. Log3 $hash, 5, "BOSEST: $hash->{NAME}, received message.";
  1938. #parse XML
  1939. my $xml = "";
  1940. eval {
  1941. $xml = XMLin($msg, KeepRoot => 1, ForceArray => [qw(media_server item member recent preset)], KeyAttr => []);
  1942. };
  1943. if($@) {
  1944. Log3 $hash, 3, "BOSEST: Wrong XML format: $@";
  1945. }
  1946. #process message
  1947. BOSEST_processXml($hash, $xml);
  1948. $tx->resume;
  1949. }
  1950. sub BOSEST_startWebSocketConnection($) {
  1951. my ($hash) = @_;
  1952. Log3 $hash, 5, "BOSEST: $hash->{NAME}, start WebSocket connection.";
  1953. $hash->{helper}{requestId} = 1;
  1954. if($hash->{helper}{wsconnected} > 0) {
  1955. Log3 $hash, 3, "BOSEST: There are already $hash->{helper}{wsconnected} WebSockets connected.";
  1956. Log3 $hash, 3, "BOSEST: Prevent new connections.";
  1957. return undef;
  1958. }
  1959. eval {
  1960. $hash->{helper}{bosewebsocket} = $hash->{helper}{useragent}->websocket('ws://'.$hash->{helper}{IP}.':8080'
  1961. => ['gabbo'] => sub {
  1962. my ($ua, $tx) = @_;
  1963. BOSEST_webSocketCallback($hash, $ua, $tx);
  1964. return undef;
  1965. });
  1966. };
  1967. if($@) {
  1968. InternalTimer(gettimeofday()+5, "BOSEST_startWebSocketConnection", $hash, 1);
  1969. }
  1970. $hash->{helper}{useragent}->inactivity_timeout(25);
  1971. $hash->{helper}{useragent}->request_timeout(10);
  1972. Log3 $hash, 4, "BOSEST: $hash->{NAME}, WebSocket connected.";
  1973. return undef;
  1974. }
  1975. sub BOSEST_checkWebSocketConnection($) {
  1976. my ($hash) = @_;
  1977. if(defined($hash->{helper}{bosewebsocket})) {
  1978. #run mojo loop not longer than 0.5ms
  1979. Log3 $hash, 5, "BOSEST: run mojo loop";
  1980. my $id = Mojo::IOLoop->timer(0.0005 => sub {});
  1981. Mojo::IOLoop->one_tick;
  1982. Mojo::IOLoop->remove($id);
  1983. Log3 $hash, 5, "BOSEST: finished mojo loop";
  1984. }
  1985. InternalTimer(gettimeofday()+0.7, "BOSEST_checkWebSocketConnection", $hash, 1);
  1986. return undef;
  1987. }
  1988. ###### GENERIC ######
  1989. sub BOSEST_getMyIp($) {
  1990. #Attention: Blocking function
  1991. my ($hash) = @_;
  1992. my $socket = IO::Socket::INET->new(
  1993. Proto => 'udp',
  1994. PeerAddr => '198.41.0.4', #a.root-servers.net
  1995. PeerPort => '53' #DNS
  1996. );
  1997. my $local_ip_address = $socket->sockhost;
  1998. return $local_ip_address;
  1999. }
  2000. sub BOSEST_getSourceAccountByName($$) {
  2001. my ($hash, $sourceName) = @_;
  2002. foreach my $source (@{$hash->{helper}{sources}}) {
  2003. if($source->{name} eq $sourceName) {
  2004. return $source->{sourceAccount};
  2005. }
  2006. }
  2007. return undef;
  2008. }
  2009. sub BOSEST_getBosePlayerByDeviceId($$) {
  2010. my ($hash, $deviceId) = @_;
  2011. if (defined($deviceId)) {
  2012. foreach my $fhem_dev (sort keys %main::defs) {
  2013. return $main::defs{$fhem_dev} if($main::defs{$fhem_dev}{TYPE} eq 'BOSEST' && $main::defs{$fhem_dev}{DEVICEID} eq $deviceId);
  2014. }
  2015. } else {
  2016. return $hash;
  2017. }
  2018. return undef;
  2019. }
  2020. sub BOSEST_getAllBosePlayers($) {
  2021. my ($hash) = @_;
  2022. my @players = ();
  2023. foreach my $fhem_dev (sort keys %main::defs) {
  2024. push @players, $main::defs{$fhem_dev} if($main::defs{$fhem_dev}{TYPE} eq 'BOSEST' && $main::defs{$fhem_dev}{DEVICEID} ne "0");
  2025. }
  2026. return @players;
  2027. }
  2028. sub BOSEST_sendKey($$) {
  2029. my ($hash, $key) = @_;
  2030. my $postXml = '<key state="press" sender="Gabbo">'.$key.'</key>';
  2031. if(BOSEST_HTTPPOST($hash, '/key', $postXml)) {
  2032. $postXml = '<key state="release" sender="Gabbo">'.$key.'</key>';
  2033. if(BOSEST_HTTPPOST($hash, '/key', $postXml)) {
  2034. return undef;
  2035. }
  2036. }
  2037. #FIXME error handling
  2038. return undef;
  2039. }
  2040. sub BOSEST_HTTPGET($$$) {
  2041. my ($hash, $ip, $getURI) = @_;
  2042. if(!defined($ip) or $ip eq "unknown") {
  2043. Log3 $hash, 3, "BOSEST: $hash->{NAME}, Can't HTTP GET as long as IP is unknown.";
  2044. return undef;
  2045. }
  2046. my $ua = LWP::UserAgent->new();
  2047. my $req = HTTP::Request->new(GET => 'http://'.$ip.':8090'.$getURI);
  2048. my $response = $ua->request($req);
  2049. if($response->is_success) {
  2050. my $xmlres = "";
  2051. eval {
  2052. $xmlres = XMLin($response->decoded_content, KeepRoot => 1, ForceArray => [qw(media_server item member recent preset)], KeyAttr => []);
  2053. };
  2054. if($@) {
  2055. Log3 $hash, 3, "BOSEST: Wrong XML format: $@";
  2056. return undef;
  2057. }
  2058. return $xmlres;
  2059. }
  2060. return undef;
  2061. }
  2062. sub BOSEST_HTTPPOST($$$) {
  2063. my ($hash, $postURI, $postXml) = @_;
  2064. my $ua = LWP::UserAgent->new();
  2065. my $ip = $hash->{helper}{IP};
  2066. my $req = HTTP::Request->new(POST => 'http://'.$ip.':8090'.$postURI);
  2067. Log3 $hash, 4, "BOSEST: set ".$postURI." => ".$postXml;
  2068. $req->content($postXml);
  2069. my $response = $ua->request($req);
  2070. if($response->is_success) {
  2071. Log3 $hash, 4, "BOSEST: success: ".$response->decoded_content;
  2072. my $xmlres = "";
  2073. eval {
  2074. $xmlres = XMLin($response->decoded_content, KeepRoot => 1, ForceArray => [qw(media_server item member recent preset)], KeyAttr => []);
  2075. };
  2076. if($@) {
  2077. Log3 $hash, 3, "BOSEST: Wrong XML format: $@";
  2078. return undef;
  2079. }
  2080. return $xmlres;
  2081. } else {
  2082. #TODO return error
  2083. Log3 $hash, 3, "BOSEST: failed: ".$response->status_line;
  2084. return undef;
  2085. }
  2086. return undef;
  2087. }
  2088. sub BOSEST_XMLUpdate($$$) {
  2089. my ($hash, $readingName, $xmlItem) = @_;
  2090. my $curVal = ReadingsVal($hash->{NAME}, $readingName, "");
  2091. my $newVal = "";
  2092. #TODO update only on change
  2093. if(ref $xmlItem eq ref {}) {
  2094. if(keys %{$xmlItem}) {
  2095. $newVal = Encode::encode('UTF-8', $xmlItem);
  2096. }
  2097. } elsif($xmlItem) {
  2098. $newVal = Encode::encode('UTF-8', $xmlItem);
  2099. }
  2100. if($curVal ne $newVal) {
  2101. readingsBulkUpdate($hash, $readingName, $newVal);
  2102. }
  2103. return undef;
  2104. }
  2105. sub BOSEST_readingsSingleUpdateIfChanged {
  2106. my ($hash, $reading, $value, $trigger) = @_;
  2107. my $curVal = ReadingsVal($hash->{NAME}, $reading, "");
  2108. if($curVal ne $value) {
  2109. readingsSingleUpdate($hash, $reading, $value, $trigger);
  2110. }
  2111. }
  2112. 1;
  2113. =pod
  2114. =item device
  2115. =item summary Easily autodiscover and control your BOSE SoundTouch devices
  2116. =item summary_DE Autodiscover und einfache Steuerung deiner BOSE SoundTouch Geräte
  2117. =begin html
  2118. <a name="BOSEST"></a>
  2119. <h3>BOSEST</h3>
  2120. <ul>
  2121. BOSEST is used to control a BOSE SoundTouch system (one or more SoundTouch 10, 20 or 30 devices)<br><br>
  2122. <b>Note:</b> The followig libraries are required for this module:
  2123. <ul><li>libwww-perl</li> <li>libmojolicious-perl</li> <li>libxml-simple-perl</li> <li>libnet-bonjour-perl</li> <li>libev-perl</li><li>liburi-escape-xs-perl</li><li>sox</li><li>libsox-fmt-mp3</li><br>
  2124. Use <b>sudo apt-get install libwww-perl libmojolicious-perl libxml-simple-perl libnet-bonjour-perl libev-perl</b> to install this libraries.<br>Please note:
  2125. libmojolicious-perl must be >=5.54, but under wheezy is only 2.x avaible.<br>
  2126. Use <b>sudo apt-get install cpanminus</b> and <b>sudo cpanm Mojolicious</b> to update to the newest version<br>
  2127. TTS can be configured as described in the following thread: <a href=https://forum.fhem.de/index.php/topic,46838.0.html>Link</a><br>
  2128. <br>
  2129. Questions and/or feedback can be posted on the FHEM forum: <a https://forum.fhem.de/index.php/topic,46838.msg533050.html#new>Link</a><br>
  2130. </ul><br>
  2131. <a name="BOSESTdefine" id="BOSESTdefine"></a>
  2132. <b>Define</b>
  2133. <ul>
  2134. <code>define &lt;name&gt; BOSEST</code><br>
  2135. <br>
  2136. Example:
  2137. <ul>
  2138. <code>define bosesystem BOSEST</code><br>
  2139. Defines BOSE SoundTouch system. All devices/speakers will show up after 60s under "Unsorted" in FHEM.<br/>
  2140. </ul>
  2141. </ul>
  2142. <br>
  2143. <a name="BOSESTset" id="BOSESTset"></a>
  2144. <b>Set</b>
  2145. <ul>
  2146. <code>set &lt;name&gt; &lt;command&gt; [&lt;parameter&gt;]</code><br>
  2147. The following commands are defined for the devices/speakers (execpt <b>autoAddDLNAServers</b> is for the "main" BOSEST) :<br><br>
  2148. <ul><u>General commands</u>
  2149. <li><code><b>on</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; power on the device</li>
  2150. <li><code><b>off</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; turn the device off</li>
  2151. <li><code><b>power</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; toggle on/off</li>
  2152. <li><code><b>volume</b> [0...100] [+x|-x]</code> &nbsp;&nbsp;-&nbsp;&nbsp; set the volume level in percentage or change volume by ±x from current level</li>
  2153. <li><code><b>channel</b> 0...20</code> &nbsp;&nbsp;-&nbsp;&nbsp; select present to play</li>
  2154. <li><code><b>saveChannel</b> 07...20</code> &nbsp;&nbsp;-&nbsp;&nbsp; save current channel to channel 07 to 20</li>
  2155. <li><code><b>play</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; start/resume to play </li>
  2156. <li><code><b>pause</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; pause the playback</li>
  2157. <li><code><b>stop</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; stop playback</li>
  2158. <li><code><b>nextTrack</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; play next track</li>
  2159. <li><code><b>prevTrack</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; play previous track</li>
  2160. <li><code><b>mute</b> on|off|toggle</code> &nbsp;&nbsp;-&nbsp;&nbsp; control volume mute</li>
  2161. <li><code><b>shuffle</b> on|off</code> &nbsp;&nbsp;-&nbsp;&nbsp; control shuffle mode</li>
  2162. <li><code><b>repeat</b> all|one|off</code> &nbsp;&nbsp;-&nbsp;&nbsp; control repeat mode</li>
  2163. <li><code><b>bass</b> 0...10</code> &nbsp;&nbsp;-&nbsp;&nbsp; set the bass level</li>
  2164. <li><code><b>recent</b> 0...15</code> &nbsp;&nbsp;-&nbsp;&nbsp; set number of names in the recent list in readings</li>
  2165. <li><code><b>source</b> bluetooth,bt-discover,aux mode, airplay</code> &nbsp;&nbsp;-&nbsp;&nbsp; select a local source</li><br>
  2166. <li><code><b>addDLNAServer</b> Name1 [Name2] [Namex]</code> &nbsp;&nbsp;-&nbsp;&nbsp; add DLNA servers Name1 (and Name2 to Namex) to the BOSE library</li>
  2167. <li><code><b>removeDLNAServer</b> Name1 [Name2] [Namex]</code> &nbsp;&nbsp;-&nbsp;&nbsp; remove DLNA servers Name1 (and Name2 to Namex) to the BOSE library</li>
  2168. </ul><br>Example: <code>set BOSE_1234567890AB volume 25</code>&nbsp;&nbsp;Set volume on device with the name BOSE_1234567890AB <br><br><br>
  2169. <ul><u>Timer commands:</u>
  2170. <li><code><b>on-for-timer</b> 1...x</code> &nbsp;&nbsp;-&nbsp;&nbsp; power on the device for x seconds</li>
  2171. <li><code><b>off-for-timer</b> 1...x</code> &nbsp;&nbsp;-&nbsp;&nbsp; turn the device off and power on again after x seconds</li>
  2172. <li><code><b>on-till</b> hh:mm:ss</code> &nbsp;&nbsp;-&nbsp;&nbsp; power on the device until defined time</li>
  2173. <li><code><b>off-till</b> hh:mm:ss</code> &nbsp;&nbsp;-&nbsp;&nbsp; turn the device off and power on again at defined time</li>
  2174. <li><code><b>on-till-overneight</b> hh:mm:ss</code> &nbsp;&nbsp;-&nbsp;&nbsp; power on the device until defined time on the next day</li>
  2175. <li><code><b>off-till-overneight</b> hh:mm:ss</code> &nbsp;&nbsp;-&nbsp;&nbsp; turn the device off at defined time on the next day</li>
  2176. </ul><br>Example: <code>set BOSE_1234567890AB on-till 23:00:00</code>&nbsp;&nbsp;Switches device with the name BOSE_1234567890AB now on and at 23:00:00 off<br><br><br>
  2177. <ul><u>Multiroom commands:</u>
  2178. <li><code><b>createZone</b> deviceID</code> &nbsp;&nbsp;-&nbsp;&nbsp; create multiroom zone (defines <code>&lt;name&gt;</code> as zoneMaster) </li>
  2179. <li><code><b>addToZone</b> deviceID</code> &nbsp;&nbsp;-&nbsp;&nbsp; add device <code>&lt;name&gt;</code> to multiroom zone</li>
  2180. <li><code><b>removeFromZone</b> deviceID</code> &nbsp;&nbsp;-&nbsp;&nbsp; remove device <code>&lt;name&gt;</code> from multiroom zone</li>
  2181. <li><code><b>playEverywhere</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; play sound of device <code>&lt;name&gt;</code> on all others devices</li>
  2182. <li><code><b>stopPlayEverywhere</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; stop playing sound on all devices</li>
  2183. </ul><br>Example: <code>set BOSE_1234567890AB playEverywhere</code>&nbsp;&nbsp;Starts Multiroom with device with the name BOSE_1234567890AB as master <br><br><br>
  2184. <ul><u>TextToSpeach commands (needs Google Translate):</u>
  2185. <li><code><b>speak</b> "message" [0...100] [+x|-x] [en|de|xx]</code> &nbsp;&nbsp;-&nbsp;&nbsp; Text to speak, optional with volume adjustment and language to use. The message to speak may have up to 100 letters</li>
  2186. <li><code><b>speakOff</b> "message" [0...100] [+x|-x] [en|de|xx]</code> &nbsp;&nbsp;-&nbsp;&nbsp; Text to speak, optional with volume adjustment and language to use. The message to speak may have up to 100 letters. Device is switched off after speak</li>
  2187. <li><code><b>ttsVolume</b> [0...100] [+x|-x]</code> &nbsp;&nbsp;-&nbsp;&nbsp; set the TTS volume level in percentage or change volume by ±x from current level</li>
  2188. <li><code><b>ttsDirectory</b> "directory"</code> &nbsp;&nbsp;-&nbsp;&nbsp; set DLNA TTS directory. FHEM user needs permissions to write to that directory. </li>
  2189. <li><code><b>ttsLanguage </b> en|de|xx</code> &nbsp;&nbsp;-&nbsp;&nbsp; set default TTS language (default: en)</li>
  2190. <li><code><b>ttsSpeakOnError</b> 0|1</code> &nbsp;&nbsp;-&nbsp;&nbsp; 0=disable to speak "not available" text</li>
  2191. <li><code><b>autoAddDLNAServers</b> 0|1</code> &nbsp;&nbsp;-&nbsp;&nbsp; 1=automatically add all DLNA servers to BOSE library. This command is only for "main" BOSEST, not for devices/speakers!</li> <br>
  2192. </ul><br> Example: <code>set BOSE_1234567890AB speakOff "Music is going to switch off now. Good night." 30 en</code>&nbsp;&nbsp;Speaks message at volume 30 and then switches off device.<br><br> <br>
  2193. </ul><br>
  2194. <a name="BOSESTget" id="BOSESTget"></a>
  2195. <b>Get</b>
  2196. <ul>
  2197. <code>n/a</code>
  2198. </ul>
  2199. <br>
  2200. </ul>
  2201. =end html
  2202. =cut