index.js 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653
  1. // index.js
  2. // 获取应用实例
  3. import * as echarts from '../../ec-canvas/echarts';
  4. // const { connect } = require("../../utils/mqtt4/mqtt");
  5. /******************* 必须要改!!不然得不到天气!! ******************/
  6. const hefengKey = " aa373d4025b0487196c15d8e6526cfcf"; // 和风天气Web api的key
  7. /******************* 可能需要你修改的部分 ******************/
  8. //const mqttHost = "broker.emqx.io"; //mqtt 服务器域名/IP
  9. // const mqttHost = "www.nekolee.top";
  10. const mqttHost = "haold.aidsleep.cn";
  11. // const mqttHost = "192.168.3.65";
  12. // const mqttPort = 8084; //mqtt 服务器域名/IP
  13. const mqttPort = 8083; //mqtt 服务器域名/IP
  14. const hefengVIP = false; // 和风天气是免费的api(false)还是付费api(true)
  15. const deviceSubTopic_LED = "/NekoHome/sub_LED"; // 设备订阅topic(小程序发布命令topic)
  16. const deviceSubTopic_BEEP = "/NekoHome/sub_BEEP";
  17. const deviceSubTopic_CU = "/NekoHome/sub_CU";
  18. const deviceSubTopic_DO = "/NekoHome/sub_DO";
  19. const deviceSubTopic_SAN = "/NekoHome/sub_SAN";
  20. const deviceSubTopic_Mode = "/NekoHome/sub_mode";
  21. // const devicePubTopic_env = "/NekoHome/pub/Environment"; // 设备发布topic(订阅环境参数)
  22. // const devicePubTopic_con = "/NekoHome/pub/Controll"; // 设备发布topic(订阅控制参数)
  23. const devicePubTopic_env = "testtopic/1"; // 设备发布topic(订阅环境参数)
  24. const devicePubTopic_con = "testtopic/1"; // 设备发布topic(订阅控制参数)
  25. /********************* 一般不用动这些 ********************/
  26. //小程序接收的主题
  27. const mpSubTopic_env = devicePubTopic_env;
  28. const mpSubTopic_con = devicePubTopic_con;
  29. //小程序发送的主题
  30. const mpPubTopic_LED = deviceSubTopic_LED;
  31. const mpPubTopic_BEEP = deviceSubTopic_BEEP;
  32. const mpPubTopic_CU = deviceSubTopic_CU;
  33. const mpPubTopic_DO = deviceSubTopic_DO;
  34. const mpPubTopic_SAN = deviceSubTopic_SAN;
  35. const mpPubTopic_Mode = deviceSubTopic_Mode;
  36. const mqttUrl = `wx://${mqttHost}:${mqttPort}/mqtt`; // mqtt连接路径
  37. const hefengApi = "https://api.qweather.com/v7"; // 和风天气付费API前缀
  38. const hefengFreeApi = "https://devapi.qweather.com/v7"; // 和风天气免费API前缀
  39. const hefengWeather = `${hefengVIP ? hefengApi : hefengFreeApi}/weather/now?`; // 和风天气实时天气api
  40. const hefengAir = `${hefengVIP ? hefengApi : hefengFreeApi}/air/now?`; // 和风天气空气质量api
  41. const geoApi = "https://geoapi.qweather.com/v2/city/lookup?" // 地理位置api(用来获取经纬度对应的城市/城区名字)
  42. var homeApi = "http://haold.aidsleep.cn:8123/api";
  43. var homeApiLater = "http://haold.aidsleep.cn:8123";
  44. var homeApi_https = "https://haold.aidsleep.cn/api";
  45. var homeApiLater_https = "https://haold.aidsleep.cn";
  46. var entity = "";
  47. // const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiN2JlOGE0MDExZjY0NjA2OGUxYjJiZTE4M2Y2NzU0NSIsImlhdCI6MTcxMTYxNDY2NCwiZXhwIjoyMDI2OTc0NjY0fQ.XJ73cXAoEsouTgvgpNVobq9oKdwlJr88TiAFLYlaahE";
  48. const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJlZGVjOWI3YWQ0MTQ0ZTIxYWQ1NTE1Nzc5NTkzNjBjNiIsImlhdCI6MTcxMjEzNTM2MywiZXhwIjoyMDI3NDk1MzYzfQ.IfrvdiaVuhCKsdy69JvUYhJ64bnhMT8MI1hN8R38whc";
  49. const aipushApi = "https://aipush.aidsleep.cn";
  50. const token_push = "b74fd5754c5ef24cf600c39194abdaeb";
  51. const options = {
  52. connectTimeout:4000,//超时时间
  53. clientId:'emqx_ODAyNT',//随机生成ID
  54. username:'test1',
  55. password:'123456'
  56. }
  57. // let chart4;
  58. // function flushChart4(data) {
  59. // console.lg('111111112222222222222333333333');
  60. // var option = {
  61. // // title: {
  62. // // text: '睡眠与起床时间'
  63. // // },
  64. // title: {
  65. // text: '睡眠与清醒时间',
  66. // left: 'center',
  67. // textStyle: {
  68. // color: '#ffffff', // 字体颜色
  69. // fontStyle: 'normal', // 字体风格
  70. // // fontWeight: 'bold', // 字体粗细
  71. // fontFamily: 'sans-serif', // 字体族
  72. // fontSize: 18 // 字体大小
  73. // },
  74. // backgroundColor: '#ff5246'
  75. // },
  76. // tooltip: {},
  77. // series: [{
  78. // type: 'gauge',
  79. // detail: {formatter: '{value} 小时'},
  80. // data: [{"name":"睡眠时间","value":7},{"name":"清醒时间","value":1}]
  81. // }]
  82. // };
  83. // chart4.setOption(option, true);
  84. // }
  85. function initChartHumidifier(canvas, width, height ,data) {//这里多加一个参数
  86. const chart = echarts.init(canvas, null, {
  87. width: width,
  88. height: height
  89. })
  90. canvas.setChart(chart);
  91. var option = {
  92. //提示框组件。开发实际中去掉了指针,提示框可以不用设置。
  93. tooltip: {
  94. formatter: "{a} <br/>{b} : {c}%"
  95. },
  96. //工具栏。刷新,下载
  97. // toolbox: {
  98. // feature: {
  99. // restore: {},
  100. // saveAsImage: {}
  101. // }
  102. // },
  103. //下面属性才是仪表盘的核心!!反正我是这么认为的!!!
  104. series: [{
  105. //类型
  106. type: 'gauge',
  107. //半径
  108. radius: 150,
  109. //起始角度。圆心 正右手侧为0度,正上方为90度,正左手侧为180度。
  110. startAngle: 200,
  111. //结束角度。
  112. endAngle: -20,
  113. center: ['50%', '65%'],
  114. min:data.min_humidity,
  115. max:data.max_humidity,
  116. //仪表盘轴线相关配置。
  117. axisLine: {
  118. show: true,
  119. color: '#333',
  120. // 属性lineStyle控制线条样式
  121. lineStyle: {
  122. width: 20,
  123. color: [[data.humidity?(data.humidity / data.max_humidity):'', '#b8b8b8'], [1, '#ebebeb']]
  124. }
  125. },
  126. //分隔线样式。
  127. splitLine: {
  128. show: false,
  129. },
  130. //刻度样式。
  131. axisTick: {
  132. show: false,
  133. },
  134. //刻度标签。
  135. axisLabel: {
  136. show: false,
  137. },
  138. // 结尾的圆圈图标
  139. markPoint: {
  140. silent: true,
  141. data: [
  142. {
  143. name: '结束点',
  144. coord: ['100%', '0%'],
  145. itemStyle: {
  146. normal: {
  147. label: {
  148. show: false
  149. },
  150. borderRadius: 10,
  151. borderColor: '#aa00ff',
  152. borderWidth: 2,
  153. color: 'rgba(255, 255, 255, 0.3)'
  154. }
  155. }
  156. }
  157. ]
  158. },
  159. //仪表盘指针。
  160. pointer: {
  161. show: false,
  162. //指针长度
  163. length: '90%',
  164. width: 0,
  165. },
  166. //仪表盘标题。
  167. title: {
  168. show: true,
  169. offsetCenter: [0, '-40%'], // x, y,单位px
  170. textStyle: {
  171. color: '#000',
  172. fontSize: 30
  173. }
  174. },
  175. //仪表盘详情,用于显示数据。
  176. detail: {
  177. show: true,
  178. offsetCenter: [0, '-10%'],
  179. formatter: data.humidity,
  180. textStyle: {
  181. fontSize: 30,
  182. color: '#31e4f4'
  183. }
  184. },
  185. data: [{
  186. value: data.humidity?data.humidity:null,
  187. name: '湿度'
  188. }]
  189. }]
  190. };
  191. chart.setOption(option);
  192. return chart;
  193. }
  194. function initChartClimate(canvas, width, height ,data) {//这里多加一个参数
  195. const chart = echarts.init(canvas, null, {
  196. width: width,
  197. height: height
  198. })
  199. canvas.setChart(chart);
  200. var option = {
  201. //提示框组件。开发实际中去掉了指针,提示框可以不用设置。
  202. tooltip: {
  203. formatter: "{a} <br/>{b} : {c}%"
  204. },
  205. //工具栏。刷新,下载
  206. // toolbox: {
  207. // feature: {
  208. // restore: {},
  209. // saveAsImage: {}
  210. // }
  211. // },
  212. series: [{
  213. //类型
  214. type: 'gauge',
  215. //半径
  216. radius: 150,
  217. //起始角度。圆心 正右手侧为0度,正上方为90度,正左手侧为180度。
  218. startAngle: 200,
  219. //结束角度。
  220. endAngle: -20,
  221. center: ['50%', '65%'],
  222. min:data.min_temp,
  223. max:data.max_temp,
  224. //仪表盘轴线相关配置。
  225. axisLine: {
  226. show: true,
  227. color: '#333',
  228. // 属性lineStyle控制线条样式
  229. lineStyle: {
  230. width: 20,
  231. color: [[data.max_temp?(data.temperature / data.max_temp):0, '#b8b8b8'], [1, '#ebebeb']]
  232. }
  233. },
  234. //分隔线样式。
  235. splitLine: {
  236. show: false,
  237. },
  238. //刻度样式。
  239. axisTick: {
  240. show: false,
  241. },
  242. //刻度标签。
  243. axisLabel: {
  244. show: false,
  245. },
  246. // 结尾的圆圈图标
  247. markPoint: {
  248. silent: true,
  249. data: [
  250. {
  251. name: '结束点',
  252. coord: ['100%', '0%'],
  253. itemStyle: {
  254. normal: {
  255. label: {
  256. show: false
  257. },
  258. borderRadius: 10,
  259. borderColor: '#aa00ff',
  260. borderWidth: 2,
  261. color: 'rgba(255, 255, 255, 0.3)'
  262. }
  263. }
  264. }
  265. ]
  266. },
  267. //仪表盘指针。
  268. pointer: {
  269. show: false,
  270. //指针长度
  271. length: '90%',
  272. width: 0,
  273. },
  274. //仪表盘标题。
  275. title: {
  276. show: true,
  277. offsetCenter: [0, '-40%'], // x, y,单位px
  278. textStyle: {
  279. color: '#000',
  280. fontSize: 30
  281. }
  282. },
  283. //仪表盘详情,用于显示数据。
  284. detail: {
  285. show: true,
  286. offsetCenter: [0, '-10%'],
  287. formatter: data.temperature,
  288. textStyle: {
  289. fontSize: 30,
  290. color: '#31e4f4'
  291. }
  292. },
  293. data: [{
  294. value: data.temperature,
  295. name: '温度'
  296. }]
  297. }]
  298. };
  299. chart.setOption(option);
  300. return chart;
  301. }
  302. import Notify from '../../@vant/weapp/notify/notify';
  303. import Dialog from '../../@vant/weapp/dialog/dialog';
  304. import Toast from '../../@vant/weapp/toast/toast';
  305. Page({
  306. data: {
  307. //单选框
  308. radio_select: '10',
  309. radio_switch: '10',
  310. //倒计时时间
  311. Time: 0,
  312. //火灾时的呼吸效果变量
  313. breathNum:0,
  314. //火灾头顶警示
  315. topTips: true,
  316. hide: false,
  317. //火灾弹窗显示
  318. fire_show:false,
  319. //设备控制弹窗参数 开始
  320. show_led: false,
  321. show_beep: false,
  322. show_curtain:false,
  323. show_san:false,
  324. show_door:false,
  325. show_time: false,
  326. actions_led: [
  327. {
  328. name: '打开',
  329. subname: '打开客厅灯',
  330. id:1,
  331. url:'',
  332. entity_id:'',
  333. },
  334. {
  335. name: '关闭',
  336. subname: '关闭客厅灯',
  337. id:0,
  338. url:'',
  339. entity_id:'',
  340. },
  341. ],
  342. actions_beep: [
  343. {
  344. name: '打开',
  345. subname: '打开警报器',
  346. id:1,
  347. url:'/services/switch/turn_on',
  348. entity_id:'switch.deerma_jsq2g_392f_alarm',
  349. },
  350. {
  351. name: '关闭',
  352. subname: '关闭警报器',
  353. id:0,
  354. url:'/services/switch/turn_off',
  355. entity_id:'switch.deerma_jsq2g_392f_alarm',
  356. },
  357. ],
  358. actions_curtain: [
  359. {
  360. name: '打开',
  361. subname: '打开香薰机',
  362. id:90,
  363. url:'',
  364. entity_id:'',
  365. },
  366. {
  367. name: '关闭',
  368. subname: '关闭香薰机',
  369. id:0,
  370. url:'',
  371. entity_id:'',
  372. },
  373. ],
  374. actions_san: [
  375. {
  376. name: '打开',
  377. subname: '打开音响',
  378. id:1,
  379. url:'',
  380. entity_id:'',
  381. },
  382. {
  383. name: '关闭',
  384. subname: '关闭音响',
  385. id:0,
  386. url:'',
  387. entity_id:'',
  388. },
  389. ],
  390. actions_door: [
  391. {
  392. name: '打开',
  393. subname: '打开智能开关',
  394. id:90,
  395. url:'',
  396. entity_id:'',
  397. },
  398. {
  399. name: '关闭',
  400. subname: '关闭智能开关',
  401. id:0,
  402. url:'',
  403. entity_id:'',
  404. },
  405. ],
  406. //设备控制弹窗参数 结束
  407. client: {},
  408. //设备状态参数 开始
  409. status_beep: 0,
  410. status_led: 0,
  411. status_san: 0,
  412. status_curtain: 0,
  413. status_door: 0,
  414. status_fire: 0,
  415. status_light: 0,
  416. status_mode: 0,
  417. //设备状态参数 结束
  418. //设备环境参数 开始
  419. Temp: 0,
  420. Humi: 0,
  421. Voltage1: 0,
  422. Voltage2: 0,
  423. //设备环境参数 结束
  424. //香薰机参数 开始
  425. Light: 0,
  426. LiquidLevel: 0,
  427. //香薰机参数 开始
  428. area: "请求中", //城区
  429. city: "请求中", //城市
  430. airText: "请求中", //空气优良
  431. airValue: 0, //空气指数
  432. weather: "请求中", //天气
  433. //weatherAdvice: "今天天气不错", //天气建议
  434. time: 30 * 60 * 60 * 1000,
  435. gradientColor: {
  436. '0%': '#ffd01e',
  437. '100%': '#ee0a24',
  438. },
  439. value: 25,
  440. pickerValue: [0], // picker-view 的当前值,数组形式,表示选择器的当前位置
  441. array: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'], // 滑动块可选的值
  442. items: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'], // 滑动块可选的值
  443. scaleTextStyle:{
  444. show:true,
  445. size:12,
  446. color:''
  447. },
  448. indicatorTextStyle:{
  449. show:true,
  450. size:16,
  451. text:'湿度'
  452. },
  453. indicatorTextStyleClimate:{
  454. show:true,
  455. size:16,
  456. text:'温度'
  457. },
  458. indicatorValueStyle: {
  459. show: true,
  460. size: 55,
  461. color:''
  462. },
  463. indicatorCircleStyle:{
  464. show:true,
  465. boderColor:[
  466. {
  467. progress:0,
  468. value:"#4575e8"
  469. },
  470. {
  471. progress: 1,
  472. value: "#fff"
  473. }
  474. ]
  475. },
  476. listCover: [],//窗帘开关
  477. listSwitch: [],//开关
  478. listScroll: [],//滚动条
  479. listUnit: [],//rgb、%、minutes、w等
  480. listConversation: [],//智能音响
  481. listConversation1: [],//智能音响
  482. listHumidity: [],//加湿器
  483. listButton: [],//按钮 '按下'
  484. listSelect: [],//下拉框的
  485. listClimate: [],//空调控制 图像
  486. listMiotMediaPlayer: [],//智能音箱 图像
  487. listMitvMediaPlayer: [],//电视 图像
  488. listHumidifier: [],//加湿器 图像
  489. listSensor: [],//文字描述的
  490. listText: [],//文本框填写的
  491. isPlaying: false,
  492. currentTime: 0,
  493. currentIndex: 0,
  494. duration: 0,
  495. currentSongTitle: '--',
  496. currentSongArtist: '--',
  497. canPrev: true,
  498. canNext: true,
  499. songs: [], // 歌曲列表
  500. scrollValue: [],
  501. selectedIndex: 0, // 默认选中第一个选项
  502. home_room: [
  503. 'company_laboratory',
  504. '2802',
  505. ],
  506. climateMode: '',//空调模式:加热还是除湿。在空调增加按钮和减少按钮 时使用
  507. canvasWidth: 300, // 画布宽度
  508. canvasHeight: 150, // 画布高度
  509. value:60 , // 当前得分
  510. total:100, // 总分值
  511. tvEntityId:'',// 电视实体Id
  512. entityName:'',//
  513. sleep_duration:'50',
  514. ecHumidifier: {
  515. onInit: initChartHumidifier
  516. },
  517. ecClimate: {
  518. onInit: initChartClimate
  519. },
  520. itemList:[
  521. { name: '1', data: [{ value: 10 }, { value: 1 }] },
  522. { name: '2', data: [{ value: 10 }, { value: 4 }] },
  523. { name: '3', data: [{ value: 10 }, { value: 7 }] },
  524. ],
  525. blackList:[
  526. // '指示灯','电视模式','电视音量','空调遥控','智能音箱','电视控制码','传感器模式','开关模式','耗电量','智能音箱会话'
  527. '加湿器','空调','音响'
  528. ],
  529. temperatureHumidifyName:'温度',
  530. isButtonPressedHumidityUp:false,
  531. isButtonPressedHumidityDown:false,
  532. isButtonPressedClimateUp:false,
  533. isButtonPressedClimateDown:false,
  534. tcp: '',
  535. w_temp: '',//温度
  536. w_humidity: '',//湿度
  537. imgSrc: '',//图片地址
  538. isLoading: false,
  539. observer: '',
  540. hotelcode: '',//酒店编码
  541. roomcode: '',//房间编码
  542. isClickedPrevSong: false,
  543. isClickedTogglePlayPause: false,
  544. isClickedNextSong: false,
  545. isClickedOnHumidifierUp: false,
  546. isClickedOnHumidifierDown: false,
  547. isClickedOnClimateAuto: false,
  548. isClickedOnClimateUp: false,
  549. intervalId1: null,
  550. intervalId2: null,
  551. },
  552. replaceAll: function(str, find, replace) {
  553. return str.replace(new RegExp(find, 'g'), replace);
  554. },
  555. checkFuzzyMatch: function(data) {
  556. var searchString = data.attributes.friendly_name
  557. var domain = data.entity_id.split(".")[0]
  558. var isMatched = true;
  559. var list = this.data.blackList;
  560. if(!searchString || !list) return false;
  561. for (var i = 0; i < list.length; i++) {
  562. if (searchString.indexOf(list[i]) !== -1) {
  563. isMatched = false;
  564. break;
  565. }
  566. }
  567. return isMatched;
  568. },
  569. onChange: function (e) {
  570. this.setData({
  571. pickerValue: e.detail.value, // 更新picker-view的值
  572. });
  573. // 在这里添加点击事件的处理逻辑
  574. // 根据pickerValue的变化执行相应的操作
  575. },
  576. swiperTap(e) {
  577. // 处理整个swiper的点击事件
  578. },
  579. itemTap(e) {
  580. // 处理swiper-item的点击事件
  581. const index = e.currentTarget.dataset.index;
  582. // 根据index执行其他操作
  583. },
  584. //定时组件
  585. start() {
  586. const countDown = this.selectComponent('.control-count-down');
  587. countDown.start();
  588. },
  589. pause() {
  590. const countDown = this.selectComponent('.control-count-down');
  591. countDown.pause();
  592. },
  593. reset() {
  594. const countDown = this.selectComponent('.control-count-down');
  595. countDown.reset();
  596. },
  597. set(){
  598. this.setData({
  599. show_time:true,
  600. })
  601. },
  602. onClose_TimeSet() {
  603. this.setData({ show_time: false });
  604. },
  605. onChange_Silder(event) {
  606. wx.showToast({
  607. icon: 'none',
  608. title: `定时:${event.detail}s`,
  609. });
  610. this.setData({
  611. Time:event.detail,
  612. })
  613. },
  614. onChange_Select(event) {
  615. this.setData({
  616. radio_select: event.detail,
  617. });
  618. },
  619. onChange_Switch(event) {
  620. this.setData({
  621. radio_switch: event.detail,
  622. });
  623. },
  624. finished() {
  625. var that = this;
  626. //定时开关哪个设备?
  627. if(that.data.radio_select == 2)
  628. {
  629. if(that.data.radio_switch == 1){
  630. console.log("选择了开");
  631. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":1}', function (err) {
  632. if (!err) {
  633. console.log("成功下发命令——打开客厅灯");
  634. }
  635. });
  636. }else{
  637. console.log("选择了关");
  638. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":0}', function (err) {
  639. if (!err) {
  640. console.log("成功下发命令——关闭客厅灯");
  641. }
  642. });
  643. }
  644. }else if(that.data.radio_select == 3){
  645. console.log("选择了BEEP");
  646. if(that.data.radio_switch == 1){
  647. console.log("选择了开");
  648. that.data.client.publish(mpPubTopic_BEEP, '{"target":"BEEP","value":1}', function (err) {
  649. if (!err) {
  650. console.log("成功下发命令——打开报警器");
  651. }
  652. });
  653. }else{
  654. console.log("选择了关");
  655. that.data.client.publish(mpPubTopic_BEEP, '{"target":"BEEP","value":0}', function (err) {
  656. if (!err) {
  657. console.log("成功下发命令——关闭报警器");
  658. }
  659. });
  660. }
  661. }else if(that.data.radio_select == 4){
  662. console.log("选择了CU");
  663. if(that.data.radio_switch == 1){
  664. console.log("选择了开");
  665. that.data.client.publish(mpPubTopic_CU, '{"target":"CU","value":1}', function (err) {
  666. if (!err) {
  667. console.log("成功下发命令——开窗帘");
  668. }
  669. });
  670. }else{
  671. console.log("选择了关");
  672. that.data.client.publish(mpPubTopic_CU, '{"target":"CU","value":0}', function (err) {
  673. if (!err) {
  674. console.log("成功下发命令——关窗帘");
  675. }
  676. });
  677. }
  678. }else if(that.data.radio_select == 5){
  679. console.log("选择了SAN");
  680. if(that.data.radio_switch == 1){
  681. console.log("选择了开");
  682. that.data.client.publish(mpPubTopic_SAN, '{"target":"SAN","value":1}', function (err) {
  683. if (!err) {
  684. console.log("成功下发命令——开风扇");
  685. }
  686. });
  687. }else{
  688. console.log("选择了关");
  689. that.data.client.publish(mpPubTopic_SAN, '{"target":"SAN","value":0}', function (err) {
  690. if (!err) {
  691. console.log("成功下发命令——关风扇");
  692. }
  693. });
  694. }
  695. }
  696. Toast('倒计时结束');
  697. },
  698. setTextBreathing: function(cls,e) {
  699. //使用记录呼吸效果
  700. var transparency = 10;
  701. var reduce = true; //记录当前做透明度增加或降低操作
  702. setInterval(function(){
  703. if (reduce === true){
  704. transparency -= 1;
  705. if (transparency === 0){
  706. reduce = false;
  707. }
  708. } else if (reduce === false){
  709. transparency += 1;
  710. if (transparency === 10){
  711. reduce = true;
  712. }
  713. }
  714. cls._defaultComponent.setData({
  715. breathNum:transparency/10
  716. })
  717. //通过setData的方式,设置breathNum的值
  718. },80)
  719. },
  720. // 各个设备弹窗逻辑
  721. onOpen_led() {
  722. this.setData({ show_led: true });
  723. },
  724. onClose_led(){
  725. this.setData({
  726. show_led:false
  727. })
  728. },
  729. onSelect_led(event) {
  730. console.log(event.detail.id);
  731. this.onLedChange(event.detail.id);
  732. },
  733. onOpen_beep() {
  734. this.setData({ show_beep: true });
  735. },
  736. onClose_beep(){
  737. this.setData({
  738. show_beep:false
  739. })
  740. },
  741. onSelect_beep(event) {
  742. console.log(event.detail.id);
  743. this.onBeepChange(event.detail.id,event.detail.url,event.detail.entity_id);
  744. },
  745. getRealApi(){
  746. // if(wx.getStorageSync('res').result && wx.getStorageSync('res').result.includes('辰悦颐养斋')){
  747. // homeApi = "http://zhh.aidsleep.cn:9123/api";
  748. // homeApiLater = "http://zhh.aidsleep.cn:9123";
  749. // homeApi_https = "https://zhh.aidsleep.cn/api";
  750. // homeApiLater_https = "https://zhh.aidsleep.cn";
  751. // entity = wx.getStorageSync('res').result=='company_laboratory'?'公司实验室':wx.getStorageSync('res').result
  752. // if(entity && entity.includes('-')){
  753. // entity = entity.slice(entity.indexOf('-')+1)
  754. // }
  755. // if(entity && entity.includes('|')){
  756. // entity = entity.slice(entity.indexOf('|')+1)
  757. // }
  758. // }else if(wx.getStorageSync('res').result && (!wx.getStorageSync('res').result.includes('辰悦颐养斋'))){
  759. // homeApi = "http://haold.aidsleep.cn:8123/api";
  760. // homeApiLater = "http://haold.aidsleep.cn:8123";
  761. // homeApi_https = "https://haold.aidsleep.cn/api";
  762. // homeApiLater_https = "https://haold.aidsleep.cn";
  763. // entity = wx.getStorageSync('res').result=='company_laboratory'?'公司实验室':wx.getStorageSync('res').result
  764. // if(entity && entity.includes('-')){
  765. // entity = str.slice(str.indexOf('-')+1)
  766. // }
  767. // if(entity && entity.includes('|')){
  768. // entity = str.slice(str.indexOf('|')+1)
  769. // }
  770. // }
  771. },
  772. onSelect_vangrid(url,entity_id) {
  773. // this.setData({
  774. // status_beep: Number(param4)
  775. // })
  776. wx.request({
  777. url: `${homeApi_https}`+url,
  778. method: 'POST',
  779. data: {
  780. "entity_id":entity_id
  781. },
  782. header: {
  783. 'content-type': 'application/json', // 默认值
  784. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  785. },
  786. success(res) {
  787. },
  788. });
  789. },
  790. onSelect_All(e) {
  791. var that = this;
  792. // 获取绑定的参数
  793. var param0 = e.currentTarget.dataset.param0;
  794. var param1 = e.currentTarget.dataset.param1;
  795. var param2 = e.currentTarget.dataset.param2;
  796. // var param3 = e.currentTarget.dataset.param3;
  797. // var param4 = e.currentTarget.dataset.param4;
  798. var param5 = e.currentTarget.dataset.param5;
  799. console.log('param1='+param1);
  800. console.log('param2='+param2);
  801. // console.log('param3='+param3);
  802. // console.log('param4='+param4);
  803. console.log('param5='+param5);
  804. // this.setData({
  805. // status_beep: Number(param4)
  806. // })
  807. // if(param3 == "/turn_on"){
  808. // that.data.listSwitch[param0].state = "off";
  809. // that.data.listSwitch[param0].service = "turn_off";
  810. // }else if(param3 == "/turn_off"){
  811. // that.data.listSwitch[param0].state = "on";
  812. // that.data.listSwitch[param0].service = "turn_on";
  813. // }
  814. console.log('switch 发生 change 事件,携带值为', e.detail.value)
  815. wx.request({
  816. url: `${homeApi_https}`+param1+param2+(e.detail.value?'/turn_on':'/turn_off'),
  817. method: 'POST',
  818. data: {
  819. "entity_id":param5
  820. },
  821. header: {
  822. 'content-type': 'application/json', // 默认值
  823. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  824. },
  825. success(res) {
  826. // if(res.data){
  827. // that.getSwitch(res.data,that)
  828. // }
  829. },
  830. });
  831. },
  832. onSelect_Cover(e) {
  833. var that = this;
  834. // 获取绑定的参数
  835. var param0 = e.currentTarget.dataset.param0;
  836. var param1 = e.currentTarget.dataset.param1;
  837. var param2 = e.currentTarget.dataset.param2;
  838. // var param3 = e.currentTarget.dataset.param3;
  839. // var param4 = e.currentTarget.dataset.param4;
  840. var param5 = e.currentTarget.dataset.param5;
  841. console.log('param1='+param1);
  842. console.log('param2='+param2);
  843. // console.log('param3='+param3);
  844. // console.log('param4='+param4);
  845. console.log('param5='+param5);
  846. // this.setData({
  847. // status_beep: Number(param4)
  848. // })
  849. // if(param3 == "/turn_on"){
  850. // that.data.listSwitch[param0].state = "off";
  851. // that.data.listSwitch[param0].service = "turn_off";
  852. // }else if(param3 == "/turn_off"){
  853. // that.data.listSwitch[param0].state = "on";
  854. // that.data.listSwitch[param0].service = "turn_on";
  855. // }
  856. console.log('switch 发生 change 事件,携带值为', e.detail.value)
  857. wx.request({
  858. url: `${homeApi_https}`+param1+param2+(e.detail.value?'/open_cover':'/close_cover'),
  859. method: 'POST',
  860. data: {
  861. "entity_id":param5
  862. },
  863. header: {
  864. 'content-type': 'application/json', // 默认值
  865. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  866. },
  867. success(res) {
  868. // if(res.data){
  869. // that.getSwitch(res.data,that)
  870. // }
  871. },
  872. });
  873. },
  874. onStop_Cover(e) {
  875. var that = this;
  876. // 获取绑定的参数
  877. var param0 = e.currentTarget.dataset.param0;
  878. var param1 = e.currentTarget.dataset.param1;
  879. var param2 = e.currentTarget.dataset.param2;
  880. // var param3 = e.currentTarget.dataset.param3;
  881. // var param4 = e.currentTarget.dataset.param4;
  882. var param5 = e.currentTarget.dataset.param5;
  883. console.log('param1='+param1);
  884. console.log('param2='+param2);
  885. // console.log('param3='+param3);
  886. // console.log('param4='+param4);
  887. console.log('param5='+param5);
  888. // this.setData({
  889. // status_beep: Number(param4)
  890. // })
  891. // if(param3 == "/turn_on"){
  892. // that.data.listSwitch[param0].state = "off";
  893. // that.data.listSwitch[param0].service = "turn_off";
  894. // }else if(param3 == "/turn_off"){
  895. // that.data.listSwitch[param0].state = "on";
  896. // that.data.listSwitch[param0].service = "turn_on";
  897. // }
  898. console.log('switch 发生 change 事件,携带值为', e.detail.value)
  899. wx.request({
  900. url: `${homeApi_https}`+param1+param2+'/stop_cover',
  901. method: 'POST',
  902. data: {
  903. "entity_id":param5
  904. },
  905. header: {
  906. 'content-type': 'application/json', // 默认值
  907. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  908. },
  909. success(res) {
  910. // if(res.data){
  911. // that.getSwitch(res.data,that)
  912. // }
  913. },
  914. });
  915. },
  916. onPressButton(e) {
  917. var that = this;
  918. // 获取绑定的参数
  919. var param = e.currentTarget.dataset.param;
  920. console.log("sliderChange-param="+param);
  921. // wx.request({
  922. // url: `${homeApi_https}`+param1+param2+param3,
  923. // method: 'POST',
  924. // data: {
  925. // "entity_id":param5
  926. // },
  927. // header: {
  928. // 'content-type': 'application/json', // 默认值
  929. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  930. // },
  931. // success(res) {
  932. // // if(res.data){
  933. // // that.getSwitch(res.data,that)
  934. // // }
  935. // console.log('res.data====='+JSON.stringify(res.data));
  936. // },
  937. // });
  938. let domain = e.currentTarget.dataset.domain;
  939. let entity_id = e.currentTarget.dataset.id;
  940. let data = {
  941. "entity_id":entity_id,
  942. // "value":newValue
  943. }
  944. this.getService("/services","/"+domain,"/press","POST",data);
  945. },
  946. onSelect_beep1(e) {
  947. // 获取绑定的参数
  948. var param1 = e.currentTarget.dataset.param1;
  949. var param2 = e.currentTarget.dataset.param2;
  950. var param3 = e.currentTarget.dataset.param3;
  951. var param4 = e.currentTarget.dataset.param4;
  952. var param5 = e.currentTarget.dataset.param5;
  953. console.log('param1='+param1);
  954. console.log('param2='+param2);
  955. console.log('param3='+param3);
  956. console.log('param4='+param4);
  957. console.log('param5='+param5);
  958. this.setData({
  959. status_beep: Number(param4)
  960. })
  961. wx.request({
  962. url: `${homeApi_https}`+param1+param2+param3,
  963. method: 'POST',
  964. data: {
  965. "entity_id":param5
  966. },
  967. header: {
  968. 'content-type': 'application/json', // 默认值
  969. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  970. },
  971. success(res) {
  972. console.log('res.data====='+JSON.stringify(res.data));
  973. },
  974. });
  975. },
  976. status_led1(e) {
  977. // 获取绑定的参数
  978. var param1 = e.currentTarget.dataset.param1;
  979. var param2 = e.currentTarget.dataset.param2;
  980. var param3 = e.currentTarget.dataset.param3;
  981. var param4 = e.currentTarget.dataset.param4;
  982. var param5 = e.currentTarget.dataset.param5;
  983. this.setData({
  984. status_led: Number(param4)
  985. })
  986. wx.request({
  987. url: `${homeApi_https}`+param1+param2+param3,
  988. method: 'POST',
  989. data: {
  990. "entity_id":param5
  991. },
  992. header: {
  993. 'content-type': 'application/json', // 默认值
  994. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  995. },
  996. success(res) {
  997. console.log('res.data====='+JSON.stringify(res.data));
  998. },
  999. });
  1000. },
  1001. status_san1(e) {
  1002. // 获取绑定的参数
  1003. var param1 = e.currentTarget.dataset.param1;
  1004. var param2 = e.currentTarget.dataset.param2;
  1005. var param3 = e.currentTarget.dataset.param3;
  1006. var param4 = e.currentTarget.dataset.param4;
  1007. var param5 = e.currentTarget.dataset.param5;
  1008. this.setData({
  1009. status_san: Number(param4)
  1010. })
  1011. wx.request({
  1012. url: `${homeApi_https}`+param1+param2+param3,
  1013. method: 'POST',
  1014. data: {
  1015. "entity_id":param5
  1016. },
  1017. header: {
  1018. 'content-type': 'application/json', // 默认值
  1019. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  1020. },
  1021. success(res) {
  1022. console.log('res.data====='+JSON.stringify(res.data));
  1023. },
  1024. });
  1025. },
  1026. status_curtain1(e) {
  1027. // 获取绑定的参数
  1028. var param1 = e.currentTarget.dataset.param1;
  1029. var param2 = e.currentTarget.dataset.param2;
  1030. var param3 = e.currentTarget.dataset.param3;
  1031. var param4 = e.currentTarget.dataset.param4;
  1032. var param5 = e.currentTarget.dataset.param5;
  1033. this.setData({
  1034. status_curtain: Number(param4)
  1035. })
  1036. wx.request({
  1037. url: `${homeApi_https}`+param1+param2+param3,
  1038. method: 'POST',
  1039. data: {
  1040. "entity_id":param5
  1041. },
  1042. header: {
  1043. 'content-type': 'application/json', // 默认值
  1044. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  1045. },
  1046. success(res) {
  1047. console.log('res.data====='+JSON.stringify(res.data));
  1048. },
  1049. });
  1050. },
  1051. status_door1(e) {
  1052. // 获取绑定的参数
  1053. var param1 = e.currentTarget.dataset.param1;
  1054. var param2 = e.currentTarget.dataset.param2;
  1055. var param3 = e.currentTarget.dataset.param3;
  1056. var param4 = e.currentTarget.dataset.param4;
  1057. var param5 = e.currentTarget.dataset.param5;
  1058. this.setData({
  1059. status_door: Number(param4)
  1060. })
  1061. wx.request({
  1062. url: `${homeApi_https}`+param1+param2+param3,
  1063. method: 'POST',
  1064. data: {
  1065. "entity_id":param5
  1066. },
  1067. header: {
  1068. 'content-type': 'application/json', // 默认值
  1069. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  1070. },
  1071. success(res) {
  1072. console.log('res.data====='+JSON.stringify(res.data));
  1073. },
  1074. });
  1075. },
  1076. onOpen_curtain() {
  1077. this.setData({ show_curtain: true });
  1078. },
  1079. onClose_curtain(){
  1080. this.setData({
  1081. show_curtain:false
  1082. })
  1083. },
  1084. onSelect_curtain(event) {
  1085. console.log(event.detail.id);
  1086. this.onCurtainChange(event.detail.id);
  1087. },
  1088. onOpen_san() {
  1089. this.setData({ show_san: true });
  1090. },
  1091. onClose_san(){
  1092. this.setData({
  1093. show_san:false
  1094. })
  1095. },
  1096. onSelect_san(event) {
  1097. console.log(event.detail.id);
  1098. this.onSanChange(event.detail.id);
  1099. },
  1100. onOpen_door() {
  1101. this.setData({ show_door: true });
  1102. },
  1103. onClose_door(){
  1104. this.setData({
  1105. show_door:false
  1106. })
  1107. },
  1108. onSelect_door(event) {
  1109. console.log(event.detail.id);
  1110. this.onDoorChange(event.detail.id);
  1111. },
  1112. // 各个设备弹窗逻辑 结束
  1113. //设备控制函数 开始
  1114. onFireChange(event){
  1115. var that = this;
  1116. console.log(event);
  1117. let sw = event;
  1118. that.setData({
  1119. status_led: sw
  1120. })
  1121. if (sw) {
  1122. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":1}', function (err) {
  1123. if (!err) {
  1124. console.log("成功下发命令——打开客厅灯");
  1125. }
  1126. });
  1127. } else {
  1128. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":0}', function (err) {
  1129. if (!err) {
  1130. console.log("成功下发命令——关闭客厅灯");
  1131. }
  1132. });
  1133. }
  1134. },
  1135. onLedChange(event) {
  1136. var that = this;
  1137. console.log(event);
  1138. let sw = event;
  1139. that.setData({
  1140. status_led: sw
  1141. })
  1142. if (sw) {
  1143. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":1}', function (err) {
  1144. if (!err) {
  1145. console.log("成功下发命令——打开客厅灯");
  1146. }
  1147. });
  1148. } else {
  1149. that.data.client.publish(mpPubTopic_LED, '{"target":"LED","value":0}', function (err) {
  1150. if (!err) {
  1151. console.log("成功下发命令——关闭客厅灯");
  1152. }
  1153. });
  1154. }
  1155. },
  1156. onBeepChange(event,url,entity_id) {
  1157. var that = this;
  1158. console.log(event);
  1159. let sw = event;
  1160. that.setData({
  1161. status_beep: sw
  1162. })
  1163. console.log("event="+event);
  1164. console.log("url="+url);
  1165. console.log("entity_id="+entity_id);
  1166. if (sw) {
  1167. // that.data.client.publish(mpPubTopic_BEEP, '{"target":"BEEP","value":1}', function (err) {
  1168. // if (!err) {
  1169. // console.log("成功下发命令——打开报警器");
  1170. // }
  1171. // });
  1172. this.onSelect_vangrid(url,entity_id);
  1173. } else {
  1174. // that.data.client.publish(mpPubTopic_BEEP, '{"target":"BEEP","value":0}', function (err) {
  1175. // if (!err) {
  1176. // console.log("成功下发命令——关闭报警器");
  1177. // }
  1178. // });
  1179. this.onSelect_vangrid(url,entity_id);
  1180. }
  1181. },
  1182. onCurtainChange(event) {
  1183. var that = this;
  1184. console.log(event);
  1185. let sw = event;
  1186. that.setData({
  1187. status_curtain: sw
  1188. })
  1189. if (sw) {
  1190. that.data.client.publish(mpPubTopic_CU, '{"target":"CU","value":1}', function (err) {
  1191. if (!err) {
  1192. console.log("成功下发命令——开窗帘");
  1193. }
  1194. });
  1195. } else{
  1196. that.data.client.publish(mpPubTopic_CU, '{"target":"CU","value":0}', function (err) {
  1197. if (!err) {
  1198. console.log("成功下发命令——关窗帘");
  1199. }
  1200. });
  1201. }
  1202. },
  1203. onSanChange(event) {
  1204. var that = this;
  1205. console.log(event);
  1206. let sw = event;
  1207. that.setData({
  1208. status_san: sw
  1209. })
  1210. if (sw) {
  1211. that.data.client.publish(mpPubTopic_SAN, '{"target":"SAN","value":1}', function (err) {
  1212. if (!err) {
  1213. console.log("成功下发命令——开风扇");
  1214. }
  1215. });
  1216. } else{
  1217. that.data.client.publish(mpPubTopic_SAN, '{"target":"SAN","value":0}', function (err) {
  1218. if (!err) {
  1219. console.log("成功下发命令——关风扇");
  1220. }
  1221. });
  1222. }
  1223. },
  1224. onDoorChange(event) {
  1225. var that = this;
  1226. console.log(event);
  1227. let sw = event;
  1228. that.setData({
  1229. status_door: sw
  1230. })
  1231. if (sw) {
  1232. that.data.client.publish(mpPubTopic_DO, '{"target":"DO","value":1}', function (err) {
  1233. if (!err) {
  1234. console.log("成功下发命令——开门");
  1235. }
  1236. });
  1237. } else{
  1238. that.data.client.publish(mpPubTopic_DO, '{"target":"DO","value":0}', function (err) {
  1239. if (!err) {
  1240. console.log("成功下发命令——关门");
  1241. }
  1242. });
  1243. }
  1244. },
  1245. onModeChange(event) {
  1246. var that = this;
  1247. console.log(event);
  1248. let sw = event.detail.value;
  1249. console.log(sw);
  1250. that.setData({
  1251. status_mode: sw
  1252. })
  1253. if (sw) {
  1254. that.data.client.publish(mpPubTopic_Mode, '{"target":"OUT","value":1}', function (err) {
  1255. if (!err) {
  1256. console.log("成功下发命令——离家模式");
  1257. }
  1258. });
  1259. } else{
  1260. that.data.client.publish(mpPubTopic_Mode, '{"target":"OUT","value":0}', function (err) {
  1261. if (!err) {
  1262. console.log("成功下发命令——居家模式");
  1263. }
  1264. });
  1265. }
  1266. },
  1267. getEntityState(list,entity_id) {
  1268. if(list==null) return null;
  1269. var obj = list.find(function (obj) {
  1270. return obj.entity_id === entity_id
  1271. })
  1272. console.log('getEntityState=='+obj?(obj.state=='on'?1:0):null);
  1273. return obj?(obj.state=='on'?1:0):null;
  1274. },
  1275. getEntityStateValue(list,entity_id) {
  1276. if(list==null) return null;
  1277. var obj = list.find(function (obj) {
  1278. return obj.entity_id === entity_id
  1279. })
  1280. console.log('getEntityStateValue=='+obj?obj.state:null);
  1281. return obj?obj.state:null;
  1282. },
  1283. //扫码
  1284. tapScan(){
  1285. wx.scanCode({
  1286. success: function(res) {
  1287. console.log('扫码获取的参数',res)
  1288. }
  1289. })
  1290. },
  1291. getSwitch(resData,that) {
  1292. try {
  1293. var listSwitchTemp = [];
  1294. var entityIdList = [];
  1295. const res = wx.getStorageSync('res');
  1296. const home_room = wx.getStorageSync('res').home_room;
  1297. for(let i=0;i<resData.length;i++){
  1298. if(resData[i].attributes && resData[i].attributes.home_room && resData[i].attributes.home_room.includes((entity))){
  1299. entityIdList.push(resData[i].entity_id)
  1300. }
  1301. }
  1302. console.log('entityIdList====='+JSON.stringify(entityIdList));
  1303. for(let i=0;i<resData.length;i++){
  1304. if((resData[i].attributes && resData[i].attributes.home_room && resData[i].attributes.home_room.includes((entity)))
  1305. || (resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id))
  1306. ){
  1307. var obj={};
  1308. obj.entity_id = resData[i].entity_id;//实体ID
  1309. obj.parent_entity_id = resData[i].parent_entity_id;//父实体ID
  1310. //api/services/<domain>/<service>
  1311. if(resData[i].entity_id){
  1312. obj.domain = resData[i].entity_id.split(".")[0];
  1313. }
  1314. obj.state = (resData[i].state=='unknown'?"未知":resData[i].state);
  1315. if(resData[i].attributes && resData[i].attributes.friendly_name){
  1316. obj.friendly_name = resData[i].attributes.friendly_name; //设备名称
  1317. }
  1318. if(resData[i].attributes && resData[i].attributes.home_room){
  1319. obj.home_room = resData[i].attributes.home_room.split(' ')[1]; //房间号
  1320. }
  1321. if((resData[i].state && (resData[i].state=='on' || resData[i].state=='off'))||
  1322. (resData[i].state && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList
  1323. && entityIdList.includes(resData[i].attributes.parent_entity_id) && (resData[i].state=='on' || resData[i].state=='off'))
  1324. ){
  1325. if(resData[i].state=='on'){
  1326. obj.service = 'turn_on';
  1327. }else if(resData[i].state=='off'){
  1328. obj.service = 'turn_off';
  1329. }
  1330. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  1331. listSwitchTemp.push(obj)
  1332. }
  1333. }
  1334. }
  1335. }
  1336. console.log('listSwitchTemp='+JSON.stringify(listSwitchTemp));
  1337. that.setData({
  1338. listSwitch:listSwitchTemp,
  1339. })
  1340. } catch (error) {
  1341. console.error(error);
  1342. }
  1343. },
  1344. //设备控制函数 结束
  1345. sliderChange(e){
  1346. let list = this.data.listScroll;//这里是将需要循环的数组赋值给list
  1347. let indexS = Number(e.currentTarget.dataset.index);
  1348. let domain = e.currentTarget.dataset.domain;
  1349. let entity_id = e.currentTarget.dataset.id;
  1350. // 将本地缓存数据渲染到对应修改的案例
  1351. list.forEach(function (value,index, array){
  1352. if (index == indexS) {
  1353. array[index].state = e.detail.value;
  1354. }
  1355. })
  1356. this.setData({
  1357. listScroll: list
  1358. })
  1359. let data = {
  1360. "entity_id":entity_id,
  1361. "value":e.detail.value,
  1362. }
  1363. this.getService("/services","/"+domain,"/set_value","POST",data);
  1364. // 设置两秒(2000毫秒)后的延迟
  1365. // setTimeout(() => {
  1366. // this.getMiotMediaPlayer(this);
  1367. // }, 2000);
  1368. },
  1369. onArrowUp(e) {
  1370. // 在这里处理向上箭头的点击事件
  1371. // 例如,你可以增加文本框的值
  1372. let currentValue = e.currentTarget.dataset.state;
  1373. let newValue = parseInt(currentValue) + 1; // 假设文本框中的是数字
  1374. // this.setData({
  1375. // inputValue: newValue.toString()
  1376. // });
  1377. let list = this.data.listUnit;//这里是将需要循环的数组赋值给list
  1378. let indexS = Number(e.currentTarget.dataset.index)
  1379. let domain = e.currentTarget.dataset.domain;
  1380. let entity_id = e.currentTarget.dataset.id;
  1381. // 将本地缓存数据渲染到对应修改的案例
  1382. list.forEach(function (value,index, array){
  1383. if (index == indexS) {
  1384. array[index].state = newValue;
  1385. }
  1386. })
  1387. this.setData({
  1388. listUnit: list
  1389. })
  1390. let data = {
  1391. "entity_id":entity_id,
  1392. "value":newValue
  1393. }
  1394. this.getService("/services","/"+domain,"/set_value","POST",data);
  1395. },
  1396. onHumidifierDown(e) {
  1397. var that = this;
  1398. this.setData({
  1399. isClickedOnHumidifierDown: true
  1400. });
  1401. // 在这里处理向下箭头的点击事件
  1402. // 例如,你可以减少文本框的值
  1403. let currentValue = e.currentTarget.dataset.humidity;
  1404. let min = e.currentTarget.dataset.min;
  1405. let max = e.currentTarget.dataset.max;
  1406. let newValue = parseInt(currentValue) - 1; // 假设文本框中的是数字
  1407. if(newValue<min){
  1408. newValue = min;
  1409. }
  1410. // this.setData({
  1411. // inputValue: newValue.toString()
  1412. // });
  1413. let list = this.data.listHumidifier;//这里是将需要循环的数组赋值给list
  1414. let indexS = Number(e.currentTarget.dataset.index)
  1415. let domain = e.currentTarget.dataset.domain;
  1416. let entity_id = e.currentTarget.dataset.id;
  1417. // 将本地缓存数据渲染到对应修改的案例
  1418. list.forEach(function (value,index, array){
  1419. if (index == indexS) {
  1420. array[index].humidity = newValue;
  1421. }
  1422. })
  1423. this.setData({
  1424. listHumidifier: list,
  1425. // isButtonPressedHumidityDown:!that.data.isButtonPressedHumidityDown
  1426. })
  1427. let data = {
  1428. "entity_id":entity_id,
  1429. "humidity":newValue
  1430. }
  1431. this.getService("/services","/"+domain,"/set_humidity","POST",data);
  1432. },
  1433. onHumidifierUp(e) {
  1434. var that = this;
  1435. this.setData({
  1436. isClickedOnHumidifierUp: true
  1437. });
  1438. // 在这里处理向上箭头的点击事件
  1439. // 例如,你可以增加文本框的值
  1440. let currentValue = e.currentTarget.dataset.humidity;
  1441. let min = e.currentTarget.dataset.min;
  1442. let max = e.currentTarget.dataset.max;
  1443. let newValue = parseInt(currentValue) + 1; // 假设文本框中的是数字
  1444. if(newValue>max){
  1445. newValue = max;
  1446. }
  1447. // this.setData({
  1448. // inputValue: newValue.toString()
  1449. // });
  1450. let list = this.data.listHumidifier;//这里是将需要循环的数组赋值给list
  1451. let indexS = Number(e.currentTarget.dataset.index)
  1452. let domain = e.currentTarget.dataset.domain;
  1453. let entity_id = e.currentTarget.dataset.id;
  1454. // 将本地缓存数据渲染到对应修改的案例
  1455. list.forEach(function (value,index, array){
  1456. if (index == indexS) {
  1457. array[index].humidity = newValue;
  1458. }
  1459. })
  1460. this.setData({
  1461. listHumidifier: list,
  1462. // isButtonPressedHumidityUp:!that.data.isButtonPressedHumidityUp
  1463. })
  1464. let data = {
  1465. "entity_id":entity_id,
  1466. "humidity":newValue
  1467. }
  1468. this.getService("/services","/"+domain,"/set_humidity","POST",data);
  1469. },
  1470. onHumidifierOn(e) {
  1471. let domain = e.currentTarget.dataset.domain;
  1472. let entity_id = e.currentTarget.dataset.id;
  1473. let data = {
  1474. "entity_id":entity_id,
  1475. }
  1476. this.getService("/services","/"+domain,"/turn_on","POST",data);
  1477. },
  1478. onHumidifierOff(e) {
  1479. let domain = e.currentTarget.dataset.domain;
  1480. let entity_id = e.currentTarget.dataset.id;
  1481. let min = e.currentTarget.dataset.min;
  1482. let max = e.currentTarget.dataset.max;
  1483. let data = {
  1484. "entity_id":entity_id,
  1485. }
  1486. this.getService("/services","/"+domain,"/turn_off","POST",data);
  1487. },
  1488. onClimateDown(e) {
  1489. var that = this;
  1490. if(!that.data.climateMode){
  1491. wx.showToast({
  1492. title: '请先选择模式',
  1493. icon: 'none'
  1494. });
  1495. return;
  1496. }
  1497. console.log('that.data.climateMode=='+that.data.climateMode);
  1498. let currentValue = e.currentTarget.dataset.temperature;
  1499. let fanMode = e.currentTarget.dataset.fanMode;
  1500. // let list = this.data.listClimate;
  1501. // this.setData({
  1502. // inputValue: newValue.toString()
  1503. // });
  1504. let domain = e.currentTarget.dataset.domain;
  1505. let entity_id = e.currentTarget.dataset.id;
  1506. let min = e.currentTarget.dataset.min;
  1507. let max = e.currentTarget.dataset.max;
  1508. // this.setData({
  1509. // isButtonPressedClimateDown: !that.data.isButtonPressedClimateDown
  1510. // })
  1511. // 将本地缓存数据渲染到对应修改的案例
  1512. // list.forEach(function (value,index, array){
  1513. // if (index == indexS) {
  1514. // array[index].temperature = newValue;
  1515. // }
  1516. // })
  1517. // this.setData({
  1518. // listClimate: list
  1519. // })
  1520. let data = {};
  1521. if(that.data.climateMode=='heat' ||that.data.climateMode=='cool'){
  1522. let newValue = parseInt(currentValue) - 1; // 假设文本框中的是数字
  1523. if(newValue<min) newValue = min;
  1524. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1525. let indexS = Number(e.currentTarget.dataset.index);
  1526. // 将本地缓存数据渲染到对应修改的案例
  1527. list.forEach(function (value,index, array){
  1528. if (index == indexS) {
  1529. array[index].temperature = newValue;
  1530. }
  1531. })
  1532. this.setData({
  1533. listClimate: list
  1534. })
  1535. console.log('onClimateDown-entity_id='+entity_id);
  1536. data = {
  1537. "entity_id":entity_id,
  1538. "temperature":newValue,
  1539. "hvac_mode":that.data.climateMode
  1540. }
  1541. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1542. }else if(that.data.climateMode=='dry'){
  1543. let newValue = parseInt(currentValue) - 1; // 假设文本框中的是数字
  1544. if(newValue<min) newValue = min;
  1545. data = {
  1546. "entity_id":entity_id,
  1547. "temperature":newValue,
  1548. "hvac_mode":that.data.climateMode
  1549. }
  1550. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1551. }else if(that.data.climateMode=='fan_only'){
  1552. let newValue = parseInt(currentValue) - 1; // 假设文本框中的是数字
  1553. if(newValue<min) newValue = min;
  1554. data = {
  1555. "entity_id":entity_id,
  1556. // "fan_mode":(fanMode=='Fan Speed Up')?'Fan Speed Down':'Fan Speed Up',
  1557. "temperature":newValue,
  1558. "hvac_mode":that.data.climateMode
  1559. }
  1560. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1561. }
  1562. },
  1563. onClimateUp(e) {
  1564. var that = this;
  1565. this.setData({
  1566. isClickedOnClimateUp: true
  1567. });
  1568. console.log('that.data.climateMode=='+that.data.climateMode);
  1569. if(!that.data.climateMode){
  1570. wx.showToast({
  1571. title: '请先选择模式',
  1572. icon: 'none'
  1573. });
  1574. return;
  1575. }
  1576. let currentValue = e.currentTarget.dataset.temperature;
  1577. let fanMode = e.currentTarget.dataset.fanMode;
  1578. let min = e.currentTarget.dataset.min;
  1579. let max = e.currentTarget.dataset.max;
  1580. // this.setData({
  1581. // inputValue: newValue.toString()
  1582. // });
  1583. // let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1584. // let indexS = Number(e.currentTarget.dataset.index)
  1585. let domain = e.currentTarget.dataset.domain;
  1586. let entity_id = e.currentTarget.dataset.id;
  1587. // this.setData({
  1588. // isButtonPressedClimateUp: !that.data.isButtonPressedClimateUp
  1589. // })
  1590. // 将本地缓存数据渲染到对应修改的案例
  1591. // list.forEach(function (value,index, array){
  1592. // if (index == indexS) {
  1593. // array[index].temperature = newValue;
  1594. // }
  1595. // })
  1596. // this.setData({
  1597. // listClimate: list
  1598. // })
  1599. let data = {};
  1600. if(that.data.climateMode=='heat' ||that.data.climateMode=='cool'){
  1601. let newValue = parseInt(currentValue) + 1; // 假设文本框中的是数字
  1602. if(newValue>max) newValue = max;
  1603. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1604. let indexS = Number(e.currentTarget.dataset.index);
  1605. // 将本地缓存数据渲染到对应修改的案例
  1606. list.forEach(function (value,index, array){
  1607. if (index == indexS) {
  1608. array[index].temperature = newValue;
  1609. }
  1610. })
  1611. this.setData({
  1612. listClimate: list
  1613. })
  1614. data = {
  1615. "entity_id":entity_id,
  1616. "temperature":newValue,
  1617. "hvac_mode":that.data.climateMode
  1618. }
  1619. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1620. }else if(that.data.climateMode=='dry'){
  1621. let newValue = parseInt(currentValue) + 1; // 假设文本框中的是数字
  1622. if(newValue>max) newValue = max;
  1623. data = {
  1624. "entity_id":entity_id,
  1625. "temperature":newValue,
  1626. "hvac_mode":that.data.climateMode
  1627. }
  1628. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1629. }else if(that.data.climateMode=='fan_only'){
  1630. let newValue = parseInt(currentValue) + 1; // 假设文本框中的是数字
  1631. if(newValue>max) newValue = max;
  1632. data = {
  1633. "entity_id":entity_id,
  1634. // "fan_mode":(fanMode=='Fan Speed Up')?'Fan Speed Down':'Fan Speed Up',
  1635. "temperature":newValue,
  1636. "hvac_mode":that.data.climateMode
  1637. }
  1638. this.getService("/services","/"+domain,"/set_temperature","POST",data);
  1639. }
  1640. },
  1641. onClimateAuto(e) {
  1642. this.setData({
  1643. isClickedOnClimateAuto: true
  1644. });
  1645. let domain = e.currentTarget.dataset.domain;
  1646. let entity_id = e.currentTarget.dataset.id;
  1647. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1648. let indexS = Number(e.currentTarget.dataset.index);
  1649. // 将本地缓存数据渲染到对应修改的案例
  1650. list.forEach(function (value,index, array){
  1651. if (index == indexS) {
  1652. array[index].state = newValue;
  1653. }
  1654. })
  1655. this.setData({
  1656. temperatureHumidifyName:'自动',
  1657. climateMode:'auto',
  1658. listClimate: list
  1659. })
  1660. let data = {
  1661. "entity_id":entity_id,
  1662. "hvac_mode":'auto',
  1663. }
  1664. this.getService("/services","/"+domain,"/set_hvac_mode","POST",data);
  1665. },
  1666. onClimateHeat(e) {
  1667. let domain = e.currentTarget.dataset.domain;
  1668. let entity_id = e.currentTarget.dataset.id;
  1669. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1670. let indexS = Number(e.currentTarget.dataset.index);
  1671. // 将本地缓存数据渲染到对应修改的案例
  1672. list.forEach(function (value,index, array){
  1673. if (index == indexS) {
  1674. array[index].state = newValue;
  1675. }
  1676. })
  1677. this.setData({
  1678. temperatureHumidifyName:'制热',
  1679. climateMode:'heat',
  1680. listClimate: list
  1681. })
  1682. let data = {
  1683. "entity_id":entity_id,
  1684. "hvac_mode":'heat',
  1685. }
  1686. this.getService("/services","/"+domain,"/set_hvac_mode","POST",data);
  1687. },
  1688. onClimateCool(e) {
  1689. let domain = e.currentTarget.dataset.domain;
  1690. let entity_id = e.currentTarget.dataset.id;
  1691. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1692. let indexS = Number(e.currentTarget.dataset.index);
  1693. // 将本地缓存数据渲染到对应修改的案例
  1694. list.forEach(function (value,index, array){
  1695. if (index == indexS) {
  1696. array[index].state = newValue;
  1697. }
  1698. })
  1699. this.setData({
  1700. temperatureHumidifyName:'制冷',
  1701. climateMode:'cool',
  1702. listClimate: list
  1703. })
  1704. let data = {
  1705. "entity_id":entity_id,
  1706. "hvac_mode":'cool',
  1707. }
  1708. this.getService("/services","/"+domain,"/set_hvac_mode","POST",data);
  1709. },
  1710. onClimateDehumidify(e) {
  1711. let domain = e.currentTarget.dataset.domain;
  1712. let entity_id = e.currentTarget.dataset.id;
  1713. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1714. let indexS = Number(e.currentTarget.dataset.index);
  1715. // 将本地缓存数据渲染到对应修改的案例
  1716. list.forEach(function (value,index, array){
  1717. if (index == indexS) {
  1718. array[index].state = newValue;
  1719. }
  1720. })
  1721. this.setData({
  1722. temperatureHumidifyName:'除湿',
  1723. climateMode:'dry',
  1724. listClimate: list
  1725. })
  1726. let data = {
  1727. "entity_id":entity_id,
  1728. "hvac_mode":'dry',
  1729. }
  1730. this.getService("/services","/"+domain,"/set_hvac_mode","POST",data);
  1731. },
  1732. onClimateAirSupply(e) {
  1733. let domain = e.currentTarget.dataset.domain;
  1734. let entity_id = e.currentTarget.dataset.id;
  1735. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1736. let indexS = Number(e.currentTarget.dataset.index);
  1737. // 将本地缓存数据渲染到对应修改的案例
  1738. list.forEach(function (value,index, array){
  1739. if (index == indexS) {
  1740. array[index].state = newValue;
  1741. }
  1742. })
  1743. this.setData({
  1744. temperatureHumidifyName:'送风',
  1745. climateMode:'fan_only',
  1746. listClimate: list
  1747. })
  1748. let data = {
  1749. "entity_id":entity_id,
  1750. "hvac_mode":'fan_only',
  1751. }
  1752. this.getService("/services","/"+domain,"/set_hvac_mode","POST",data);
  1753. },
  1754. onClimateOff(e) {
  1755. let domain = e.currentTarget.dataset.domain;
  1756. let entity_id = e.currentTarget.dataset.id;
  1757. let list = this.data.listClimate;//这里是将需要循环的数组赋值给list
  1758. let indexS = Number(e.currentTarget.dataset.index);
  1759. // 将本地缓存数据渲染到对应修改的案例
  1760. list.forEach(function (value,index, array){
  1761. if (index == indexS) {
  1762. array[index].state = newValue;
  1763. }
  1764. })
  1765. this.setData({
  1766. temperatureHumidifyName:'关闭',
  1767. climateMode:'off',
  1768. listClimate: list
  1769. })
  1770. let data = {
  1771. "entity_id":entity_id,
  1772. }
  1773. this.getService("/services","/"+domain,"/turn_off","POST",data);
  1774. },
  1775. handleInput: function(e) {
  1776. // 这里你需要自己实现歌曲列表和切换逻辑
  1777. // 例如,如果你有一个歌曲列表,你可以通过修改当前播放歌曲的索引来切换歌曲
  1778. // 假设你有一个歌曲列表`songs`和一个当前歌曲索引`currentIndex`
  1779. // if (this.data.currentIndex > 0) {
  1780. // this.data.currentIndex--;
  1781. // // 加载新歌曲并播放
  1782. // this.loadAndPlaySong(this.data.currentIndex);
  1783. // }
  1784. let min = e.currentTarget.dataset.min;
  1785. let max = e.currentTarget.dataset.max;
  1786. if(e.detail.value && (e.detail.value.length>max || e.detail.value.length<min)){
  1787. wx.showModal({
  1788. title: '提示',
  1789. content: '文本长度介于'+min+'~'+max+'长度之间',
  1790. showCancel: false
  1791. });
  1792. return;
  1793. }
  1794. let domain = e.currentTarget.dataset.domain;
  1795. let entity_id = e.currentTarget.dataset.id;
  1796. let data = {
  1797. "entity_id":entity_id,
  1798. "value":e.detail.value,
  1799. }
  1800. this.getService("/services","/"+domain,"/set_value","POST",data);
  1801. },
  1802. onArrowDown(e) {
  1803. // 在这里处理向下箭头的点击事件
  1804. // 例如,你可以减少文本框的值
  1805. let currentValue = e.currentTarget.dataset.state;
  1806. let newValue = parseInt(currentValue) - 1; // 假设文本框中的是数字
  1807. // this.setData({
  1808. // inputValue: newValue.toString()
  1809. // });
  1810. let list = this.data.listUnit;//这里是将需要循环的数组赋值给list
  1811. let indexS = Number(e.currentTarget.dataset.index)
  1812. let domain = e.currentTarget.dataset.domain;
  1813. let entity_id = e.currentTarget.dataset.id;
  1814. // 将本地缓存数据渲染到对应修改的案例
  1815. list.forEach(function (value,index, array){
  1816. if (index == indexS) {
  1817. array[index].state = newValue;
  1818. }
  1819. })
  1820. this.setData({
  1821. listUnit: list
  1822. })
  1823. let data = {
  1824. "entity_id":entity_id,
  1825. "value":newValue
  1826. }
  1827. this.getService("/services","/"+domain,"/set_value","POST",data);
  1828. },
  1829. prevSong: function(e) {
  1830. // 这里你需要自己实现歌曲列表和切换逻辑
  1831. // 例如,如果你有一个歌曲列表,你可以通过修改当前播放歌曲的索引来切换歌曲
  1832. // 假设你有一个歌曲列表`songs`和一个当前歌曲索引`currentIndex`
  1833. // if (this.data.currentIndex > 0) {
  1834. // this.data.currentIndex--;
  1835. // // 加载新歌曲并播放
  1836. // this.loadAndPlaySong(this.data.currentIndex);
  1837. // }
  1838. this.setData({
  1839. isClickedPrevSong: true
  1840. });
  1841. let domain = e.currentTarget.dataset.domain;
  1842. let entity_id = e.currentTarget.dataset.id;
  1843. let data = {
  1844. "entity_id":entity_id,
  1845. }
  1846. this.getService("/services","/"+domain,"/media_previous_track","POST",data);
  1847. // setTimeout(() => {
  1848. // this.getMiotMediaPlayer(this);
  1849. // }, 8000);
  1850. },
  1851. tvTurnOn: function(e) {
  1852. let domain = e.currentTarget.dataset.domain;
  1853. let entity_id = e.currentTarget.dataset.id;
  1854. let data = {
  1855. "entity_id":entity_id,
  1856. }
  1857. this.getService("/services","/"+domain,"/turn_on","POST",data);
  1858. },
  1859. tvTurnOff: function(e) {
  1860. let domain = e.currentTarget.dataset.domain;
  1861. let entity_id = e.currentTarget.dataset.id;
  1862. let data = {
  1863. "entity_id":entity_id,
  1864. }
  1865. this.getService("/services","/"+domain,"/turn_off","POST",data);
  1866. },
  1867. // 切换播放/暂停
  1868. togglePlayPause: function(e) {
  1869. // console.log("this.data.isPlaying="+this.data.isPlaying);
  1870. // if (this.data.isPlaying) {
  1871. // this.innerAudioContext.pause();
  1872. // this.setData({ isPlaying: false });
  1873. // this.getService("/services","/"+domain,"/media_pause","POST",null);
  1874. // } else {
  1875. // this.innerAudioContext.play();
  1876. // this.setData({ isPlaying: true });
  1877. // this.getService("/services","/"+domain,"/media_play","POST",null);
  1878. // }
  1879. // let list = this.data.listMiotMediaPlayer;//这里是将需要循环的数组赋值给list
  1880. this.setData({
  1881. isClickedTogglePlayPause: true
  1882. });
  1883. let indexS = Number(e.currentTarget.dataset.index)
  1884. let domain = e.currentTarget.dataset.domain;
  1885. let entity_id = e.currentTarget.dataset.id;
  1886. let state = e.currentTarget.dataset.state;
  1887. // 将本地缓存数据渲染到对应修改的案例
  1888. // list.forEach(function (value,index, array){
  1889. // if (index == indexS) {
  1890. // array[index].state = (state=='playing'?'idle':'playing');
  1891. // }
  1892. // })
  1893. // this.setData({
  1894. // listMiotMediaPlayer: list
  1895. // })
  1896. let data = {
  1897. "entity_id":entity_id,
  1898. }
  1899. this.getService("/services","/"+domain,"/media_play_pause","POST",data);
  1900. // 设置两秒(2000毫秒)后的延迟
  1901. // setTimeout(() => {
  1902. // this.getMiotMediaPlayer(this);
  1903. // }, 8000);
  1904. },
  1905. // 切换下一首歌曲
  1906. nextSong: function(e) {
  1907. // 和prevSong类似,你需要实现歌曲列表和切换逻辑
  1908. // 假设歌曲列表为`songs`,当前歌曲索引为`currentIndex`
  1909. // if (this.data.currentIndex < songs.length - 1) {
  1910. // this.data.currentIndex++;
  1911. // // 加载新歌曲并播放
  1912. // this.loadAndPlaySong(this.data.currentIndex);
  1913. // }
  1914. this.setData({
  1915. isClickedNextSong: true
  1916. });
  1917. let domain = e.currentTarget.dataset.domain;
  1918. let entity_id = e.currentTarget.dataset.id;
  1919. let data = {
  1920. "entity_id":entity_id,
  1921. }
  1922. this.getService("/services","/"+domain,"/media_next_track","POST",data);
  1923. // setTimeout(() => {
  1924. // this.getMiotMediaPlayer(this);
  1925. // }, 8000);
  1926. },
  1927. // 加载并播放指定索引的歌曲
  1928. loadAndPlaySong: function(index) {
  1929. // 假设你有一个歌曲列表`songs`,每个歌曲对象有`url`属性表示音频链接
  1930. const song = songs[index];
  1931. this.innerAudioContext.src = song.url;
  1932. this.innerAudioContext.play();
  1933. // 更新当前播放的歌曲信息(例如标题和艺术家)
  1934. this.setData({
  1935. currentSongTitle: song.title,
  1936. currentSongArtist: song.artist
  1937. });
  1938. },
  1939. base64Decode:function(input) {
  1940. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  1941. let str = '';
  1942. let output = '';
  1943. let chr1, chr2, chr3;
  1944. let enc1, enc2, enc3, enc4;
  1945. let i = 0;
  1946. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  1947. while (i < input.length) {
  1948. enc1 = chars.indexOf(input.charAt(i++));
  1949. enc2 = chars.indexOf(input.charAt(i++));
  1950. enc3 = chars.indexOf(input.charAt(i++));
  1951. enc4 = chars.indexOf(input.charAt(i++));
  1952. chr1 = (enc1 << 2) | (enc2 >> 4);
  1953. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  1954. chr3 = ((enc3 & 3) << 6) | enc4;
  1955. str += String.fromCharCode(chr1);
  1956. if (enc3 != 64) {
  1957. str += String.fromCharCode(chr2);
  1958. }
  1959. if (enc4 != 64) {
  1960. str += String.fromCharCode(chr3);
  1961. }
  1962. }
  1963. return str;
  1964. },
  1965. onPickerChange: function(e) {
  1966. // this.setData({
  1967. // selectedIndex: e.detail.value
  1968. // });
  1969. // // 可以在这里处理选中值的变化,比如发送请求等
  1970. // console.log('选中的值是:', this.data.options[e.detail.value].value);
  1971. let list = this.data.listSelect;//这里是将需要循环的数组赋值给list
  1972. let indexS = Number(e.currentTarget.dataset.index)
  1973. let domain = e.currentTarget.dataset.domain;
  1974. let entity_id = e.currentTarget.dataset.id;
  1975. // 将本地缓存数据渲染到对应修改的案例
  1976. list.forEach(function (value,index, array){
  1977. if (index == indexS) {
  1978. array[index].selectedIndex = e.detail.value;
  1979. }
  1980. })
  1981. this.setData({
  1982. listSelect: list
  1983. })
  1984. let data = {
  1985. "entity_id":entity_id,
  1986. "option":e.detail.value,
  1987. }
  1988. this.getService("/services","/"+domain,"/select_option","POST",data);
  1989. },
  1990. getService(services,domain,service,method,data) {
  1991. wx.request({
  1992. url: `${homeApi_https}`+services+domain+service,
  1993. method: method,
  1994. data: data,
  1995. header: {
  1996. 'content-type': 'application/json', // 默认值
  1997. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  1998. },
  1999. success(res) {
  2000. console.log('res.data====='+JSON.stringify(res.data));
  2001. },
  2002. });
  2003. },
  2004. // 初始化时调用即可
  2005. showCanvasRing() {
  2006. try {
  2007. //作画
  2008. var ctx = wx.createCanvasContext("circleBar", this); //canvas组建封装,需要后加个this
  2009. ctx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight); // 清除画布
  2010. var circle_r = this.data.canvasWidth / 2 - 10; //画布的一半,用来找中心点和半径
  2011. var scoreText = this.data.value; // 当前得分
  2012. var total = this.data.total;// 总分值
  2013. var score = (100 * scoreText) / total; // 满分为100时对应的值
  2014. let that = this;
  2015. //定义起始点
  2016. ctx.translate(this.data.canvasWidth / 2, this.data.canvasWidth / 2 + 5);
  2017. // 白边圆弧
  2018. ctx.beginPath();
  2019. ctx.setStrokeStyle("#FFFFFF");
  2020. ctx.setLineWidth(16);
  2021. ctx.setLineCap("round");
  2022. ctx.arc(0, 0, circle_r - 15, 1 * Math.PI, 0, false);
  2023. ctx.stroke();
  2024. ctx.closePath();
  2025. //灰色圆弧
  2026. ctx.beginPath();
  2027. ctx.setStrokeStyle("#D6E4FF");
  2028. ctx.setLineWidth(12);
  2029. ctx.setLineCap("round"); //线条结束端点样式 butt 平直 round 圆形 square 正方形
  2030. ctx.arc(0, 0, circle_r - 15, 1 * Math.PI, 0, false);
  2031. ctx.stroke();
  2032. ctx.closePath();
  2033. // 蓝色圆弧
  2034. ctx.beginPath();
  2035. ctx.setStrokeStyle("#3366FF");
  2036. ctx.setLineWidth(16);
  2037. ctx.arc(
  2038. 0,
  2039. 0,
  2040. circle_r - 15,
  2041. 1 * Math.PI,
  2042. (score / 100 + 1) * Math.PI,
  2043. false
  2044. );
  2045. ctx.stroke();
  2046. ctx.closePath();
  2047. // 指示器-外层
  2048. let deg = 270 - score * 1.8;
  2049. const xAxis = Math.sin(((2 * Math.PI) / 360) * deg) * (circle_r - 15);
  2050. const yAxis = Math.cos(((2 * Math.PI) / 360) * deg) * (circle_r - 15);
  2051. ctx.beginPath();
  2052. ctx.arc(xAxis, yAxis, 11, 0, 2 * Math.PI);
  2053. ctx.setFillStyle("#FFFFFF");
  2054. ctx.fill();
  2055. // 指示器-内层
  2056. const xAxis2 = Math.sin(((2 * Math.PI) / 360) * deg) * (circle_r - 15);
  2057. const yAxis2 = Math.cos(((2 * Math.PI) / 360) * deg) * (circle_r - 15); // circle_r - 10
  2058. ctx.beginPath();
  2059. ctx.arc(xAxis2, yAxis2, 6, 0, 2 * Math.PI);
  2060. ctx.setFillStyle("#3366FF");
  2061. ctx.fill();
  2062. // 文字-总得分
  2063. ctx.setTextAlign("center"); // 字体位置
  2064. ctx.setFillStyle("#333333");
  2065. ctx.font = "normal normal 16px Arial,sans-serif";
  2066. ctx.fillText("总得分", 0, -30);
  2067. // 文字-具体分数
  2068. ctx.setTextAlign("center"); // 字体位置
  2069. ctx.setFillStyle("#000000");
  2070. ctx.font = "normal bold 36px Arial,sans-serif";
  2071. ctx.fillText(scoreText, 0, 13);
  2072. // 最低分
  2073. ctx.setFillStyle("#666666");
  2074. ctx.font = "normal normal 13px Arial,sans-serif";
  2075. ctx.fillText(0, -circle_r + 10, 25);
  2076. // 最高分
  2077. ctx.setFillStyle("#666666");
  2078. ctx.font = "normal normal 13px Arial,sans-serif";
  2079. ctx.fillText(total, circle_r - 15, 25);
  2080. // 绘图
  2081. ctx.draw(false, function () {
  2082. //将生成好的图片保存到本地
  2083. wx.canvasToTempFilePath(
  2084. {
  2085. canvasId: "circleBar",
  2086. success: function (res) {
  2087. var tempFilePath = res.tempFilePath;
  2088. that.setData({
  2089. loadImagePath: tempFilePath,
  2090. });
  2091. },
  2092. fail: function (res) {},
  2093. },
  2094. );
  2095. });
  2096. } catch (error) {
  2097. }
  2098. },
  2099. getHAData(that,hotelcode,roomcode){
  2100. let start = Date.now(); // 获取当前时间(毫秒)
  2101. // console.log('start=='+start);
  2102. wx.request({
  2103. url: `${aipushApi}/getha`, //
  2104. method: 'POST',
  2105. // header: {
  2106. // 'content-type': 'application/json', // 默认值
  2107. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2108. // },
  2109. data:{
  2110.     "locations":hotelcode,"token":token_push
  2111. },
  2112. success(ress) {
  2113. if(!ress || !ress.data){
  2114. console.error("getha(),没有获取到数据");
  2115. return;
  2116. }
  2117. var resData = ress.data
  2118. console.log("resData-=="+JSON.stringify(resData));
  2119. console.log("resData.loc-=="+JSON.stringify(resData.loc));
  2120. homeApi_https = resData.loc+"/api";
  2121. homeApiLater_https = resData.loc;
  2122. // console.log('res.data='+JSON.stringify(resData));
  2123. if (ress.data.code == "400") {
  2124. console.error("400错误!");
  2125. return;
  2126. }
  2127. if (ress.data.code == "401") {
  2128. console.error("401错误!");
  2129. return;
  2130. }
  2131. try {
  2132. //获取HA 设备状态数据
  2133. // wx.request({
  2134. // url: `${homeApi_https}/states`, //获取空气数据
  2135. // method: 'GET',
  2136. // header: {
  2137. // 'content-type': 'application/json', // 默认值
  2138. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2139. // },
  2140. // success(res) {
  2141. // if(!res || !res.data){
  2142. // return;
  2143. // }
  2144. // var resData = res.data
  2145. // // console.log('res.data='+JSON.stringify(resData));
  2146. // if (res.data.code == "400") {
  2147. // console.error("400错误!");
  2148. // return;
  2149. // }
  2150. // if (res.data.code == "401") {
  2151. // console.error("401错误 --- 请检查你的API或Key是否正确!");
  2152. // return;
  2153. // }
  2154. // try {
  2155. // const {
  2156. // now
  2157. // } = res.data;
  2158. // console.log('res.data1='+JSON.stringify(resData));
  2159. // res.data.find(function (obj) {
  2160. // console.log('obj='+obj);
  2161. // })
  2162. // res.data.forEach((item, index) => {
  2163. // console.log(index, item);
  2164. // });
  2165. var listCoverTemp = [];
  2166. var listSwitchTemp = [];
  2167. var listScrollTemp = [];
  2168. var listUnitTemp = [];
  2169. var listConversationTemp = [];
  2170. var listConversation1Temp = [];
  2171. var listHumidityTemp = [];
  2172. var listButtonTemp = [];
  2173. var listSelectTemp = [];
  2174. var listClimateTemp = [];
  2175. var listMiotMediaPlayerTemp = [];
  2176. var listMitvMediaPlayerTemp = [];
  2177. var listHumidifierTemp = [];
  2178. var listSensorTemp = [];
  2179. var listTextTemp = [];
  2180. var tvEntityIdTemp = '';
  2181. var climateModeTemp = '';
  2182. var temperatureHumidifyNameTemp = '温度';
  2183. var entityIdList = [];
  2184. const res = wx.getStorageSync('res');
  2185. const home_room = wx.getStorageSync('res').home_room;
  2186. // console.log('entity=====@==='+entity);
  2187. wx.request({
  2188. url: `${homeApi_https}/aidsleep/entity`, //获取空气数据
  2189. method: 'POST',
  2190. header: {
  2191. 'content-type': 'application/json', // 默认值
  2192. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2193. },
  2194. //
  2195. data:{
  2196. "template": "{{ label_entities('"+(entity)+"') }}"
  2197. },
  2198. success(resTemplate) {
  2199. let mid = Date.now(); // 再次获取当前时间(毫秒)
  2200. // console.log('mid=='+mid);
  2201. if(!resTemplate || !resTemplate.data){
  2202. console.log('no data:'+mid);
  2203. return;
  2204. }
  2205. let resData = resTemplate.data;
  2206. // resData = resData.filter(item1 => {
  2207. // return JSON.parse(that.replaceAll(resTemplate.data,'\'','\"')).find(item2 => item2 === item1.entity_id) !== undefined;
  2208. // });
  2209. // console.log('entityIdList-resTemplate====='+JSON.stringify(resTemplate.data));
  2210. for(let i=0;i<resData.length;i++){
  2211. // if(resTemplate.data.includes(resData[i].entity_id) && that.checkFuzzyMatch(resData[i])){
  2212. var obj={};
  2213. obj.entity_id = resData[i].entity_id;//实体ID
  2214. obj.parent_entity_id = resData[i].parent_entity_id;//父实体ID
  2215. //api/services/<domain>/<service>
  2216. if(resData[i].entity_id){
  2217. obj.domain = resData[i].entity_id.split(".")[0];
  2218. }
  2219. obj.state = (resData[i].state=='unknown'?"未知":resData[i].state);
  2220. if(resData[i].attributes && resData[i].attributes.friendly_name){
  2221. obj.friendly_name = resData[i].attributes.friendly_name; //设备名称
  2222. if(obj.friendly_name.includes("灯")){
  2223. obj.imgSrc = '/static/images/led.png';
  2224. }else if(obj.friendly_name.includes("加湿器")){
  2225. obj.imgSrc = '/static/images/hum.png';
  2226. }else if(obj.friendly_name.includes("香薰机")){
  2227. obj.imgSrc = '/static/images/light.png';
  2228. }else if(obj.friendly_name.includes("排气扇")){
  2229. obj.imgSrc = '/static/images/temp.png';
  2230. }else if(obj.friendly_name.includes("空调")){
  2231. obj.imgSrc = '/static/images/alarm.png';
  2232. }else{
  2233. obj.imgSrc = '/static/images/temp.png';
  2234. }
  2235. }
  2236. if(resData[i].attributes && resData[i].attributes.home_room){
  2237. obj.home_room = resData[i].attributes.home_room.split(' ')[1]; //房间号
  2238. }
  2239. if((resData[i].state && (resData[i].state=='on' || resData[i].state=='off')) && that.checkFuzzyMatch(resData[i])
  2240. ){
  2241. if(resData[i].state=='on'){
  2242. obj.service = 'turn_on';
  2243. }else if(resData[i].state=='off'){
  2244. obj.service = 'turn_off';
  2245. }
  2246. //截取字符串,防止开关设备名称过长
  2247. obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2248. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2249. listSwitchTemp.push(obj)
  2250. }
  2251. }
  2252. if((resData[i].state && obj.domain && obj.domain=='cover')
  2253. ){
  2254. obj.current_position = resData[i].attributes.current_position;
  2255. obj.target_position = resData[i].attributes.target_position;
  2256. obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2257. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2258. listCoverTemp.push(obj)
  2259. }
  2260. }
  2261. // if((resData[i].state && obj.domain && obj.domain=='number')||
  2262. // (resData[i].state && obj.domain && obj.domain=='number' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2263. // )
  2264. // ){
  2265. // obj.min = resData[i].attributes.min;
  2266. // obj.max = resData[i].attributes.max;
  2267. // obj.step = resData[i].attributes.step;
  2268. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2269. // if(resData[i].state!='unknown'){
  2270. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2271. // }
  2272. // if((resData[i].attributes.min || resData[i].attributes.max) && resData[i].attributes.unit_of_measurement!='rgb' && resData[i].attributes.unit_of_measurement!='minutes'){
  2273. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2274. // listScrollTemp.push(obj);
  2275. // }
  2276. // }else if((resData[i].attributes.min || resData[i].attributes.max) && (resData[i].attributes.unit_of_measurement=='rgb' || resData[i].attributes.unit_of_measurement=='minutes')){
  2277. // if(resData[i].state!='unknown'){
  2278. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2279. // }
  2280. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2281. // listUnitTemp.push(obj);
  2282. // }
  2283. // }
  2284. // }
  2285. // if((resData[i].state && obj.domain && obj.domain=='button')||
  2286. // (resData[i].state && obj.domain && obj.domain=='button' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2287. // )
  2288. // ){
  2289. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2290. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2291. // listButtonTemp.push(obj);
  2292. // }
  2293. // }
  2294. // if((resData[i].state && obj.domain && obj.domain=='select')||
  2295. // (resData[i].state && obj.domain && obj.domain=='select' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2296. // )
  2297. // ){
  2298. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2299. // obj.options = resData[i].attributes.options;
  2300. // obj.selectedIndex = 0;
  2301. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2302. // listSelectTemp.push(obj);
  2303. // }
  2304. // }
  2305. if((resData[i].state && obj.domain && obj.domain=='climate' && resData[i].attributes.entity_class=='MiirClimateEntity')
  2306. ){
  2307. climateModeTemp = obj.state;
  2308. obj.temperature = resData[i].attributes.temperature;
  2309. obj.min_temp = resData[i].attributes.min_temp;
  2310. obj.max_temp = resData[i].attributes.max_temp;
  2311. obj.hvac_modes = resData[i].attributes.hvac_modes;
  2312. obj.fan_modes = resData[i].attributes.fan_modes;
  2313. obj.fan_mode = resData[i].attributes.fan_mode;
  2314. obj.target_temp_step = resData[i].attributes.target_temp_step;
  2315. if(obj.state=='auto'){
  2316. temperatureHumidifyNameTemp = '自动';
  2317. }
  2318. if(obj.state=='heat'){
  2319. temperatureHumidifyNameTemp = '制热';
  2320. }
  2321. if(obj.state=='cool'){
  2322. temperatureHumidifyNameTemp = '制冷';
  2323. }
  2324. if(obj.state=='dry'){
  2325. temperatureHumidifyNameTemp = '除湿';
  2326. }
  2327. if(obj.state=='fan_only'){
  2328. temperatureHumidifyNameTemp = '送风';
  2329. }
  2330. if(obj.state=='off'){
  2331. temperatureHumidifyNameTemp = '关闭';
  2332. }
  2333. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2334. listClimateTemp.push(obj);
  2335. }
  2336. }
  2337. if((resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MiotMediaPlayerEntity')
  2338. ){
  2339. // obj["speaker.volume"] = resData[i].attribute["speaker.volume"];
  2340. obj.repeat = resData[i].attributes.repeat;
  2341. obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2342. obj.media_title = resData[i].attributes.media_title;
  2343. obj.media_artist = resData[i].attributes.media_artist;
  2344. obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2345. obj.media_duration = resData[i].attributes.media_duration;
  2346. obj.media_position = resData[i].attributes.media_position;
  2347. obj.media_late = resData[i].attributes.media_position + '/' + resData[i].attributes.media_duration;
  2348. // obj.microphone.mute = resData[i].attributes.microphone.mute;
  2349. // obj.speaker.mute = resData[i].attributes.speaker.mute;
  2350. // console.log('media-player-miot-obj=='+obj);
  2351. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2352. listMiotMediaPlayerTemp.push(obj);
  2353. }
  2354. }
  2355. // if((resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MitvMediaPlayerEntity')||
  2356. // (resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MitvMediaPlayerEntity' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2357. // )
  2358. // ){
  2359. // obj.volume_level = resData[i].attributes.volume_level;
  2360. // obj.is_volume_muted = resData[i].attributes.is_volume_muted;
  2361. // obj.source = resData[i].attributes.source;
  2362. // obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2363. // obj.media_title = resData[i].attributes.media_title;
  2364. // obj.media_artist = resData[i].attributes.media_artist;
  2365. // obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2366. // obj.media_duration = resData[i].attributes.media_duration;
  2367. // obj.media_position = resData[i].attributes.media_position;
  2368. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2369. // listMitvMediaPlayerTemp.push(obj);
  2370. // }
  2371. // }
  2372. if((resData[i].state && obj.domain && obj.domain=='media_player')
  2373. ){
  2374. obj.friendly_name = resData[i].attributes.friendly_name;
  2375. if(obj.entity_id.includes('teevee') || (obj.entity_id.includes('media_player') && obj.entity_id.includes('tv_dlna_') && obj.friendly_name && obj.friendly_name.includes('电视'))){
  2376. obj.volume_level = resData[i].attributes.volume_level;
  2377. obj.is_volume_muted = resData[i].attributes.is_volume_muted;
  2378. obj.source = resData[i].attributes.source;
  2379. obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2380. obj.media_title = resData[i].attributes.media_title;
  2381. obj.media_artist = resData[i].attributes.media_artist;
  2382. obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2383. obj.media_duration = resData[i].attributes.media_duration;
  2384. obj.media_position = resData[i].attributes.media_position;
  2385. obj.media_late = resData[i].attributes.media_position + '/' + resData[i].attributes.media_duration;
  2386. // obj["speaker.volume"] = resData[i].attributes["speaker.volume"];
  2387. // console.log('media-player-mitv-obj=='+obj);
  2388. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain && !listMitvMediaPlayerTemp){
  2389. listMitvMediaPlayerTemp.push(obj);
  2390. }
  2391. tvEntityIdTemp = obj.entity_id;
  2392. // console.log('obj===='+obj);
  2393. // console.log('tvEntityIdTemp='+tvEntityIdTemp);
  2394. wx.setStorageSync('tvEntityId',tvEntityIdTemp);
  2395. }
  2396. }
  2397. if((resData[i].state && obj.domain && obj.domain=='humidifier')
  2398. ){
  2399. obj.min_humidity = resData[i].attributes.min_humidity;
  2400. obj.max_humidity = resData[i].attributes.max_humidity;
  2401. obj.humidity = resData[i].attributes.humidity;
  2402. obj.min = resData[i].attributes.min;
  2403. obj.max = resData[i].attributes.max;
  2404. // console.log('humidifier-obj=='+JSON.stringify(obj));
  2405. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2406. listHumidifierTemp.push(obj);
  2407. }
  2408. }
  2409. // if((resData[i].state && obj.domain && obj.domain=='sensor')||
  2410. // (resData[i].state && obj.domain && obj.domain=='sensor' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2411. // )
  2412. // ){
  2413. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2414. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2415. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2416. // listSensorTemp.push(obj);
  2417. // }
  2418. // }
  2419. // if((resData[i].state && obj.domain && obj.domain=='text')||
  2420. // (resData[i].state && obj.domain && obj.domain=='text' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2421. // )
  2422. // ){
  2423. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2424. // obj.min = resData[i].attributes.min;
  2425. // obj.max = resData[i].attributes.max;
  2426. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2427. // listTextTemp.push(obj);
  2428. // }
  2429. // }
  2430. // if((resData[i].state && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2431. // || (resData[i].state && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2432. // && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2433. // ){
  2434. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2435. // listConversationTemp.push(obj)
  2436. // }
  2437. // }
  2438. // if((resData[i].state && resData[i].attributes && resData[i].attributes.speaker && resData[i].attributes.speaker.volume && obj.friendly_name && obj.friendly_name.includes("智能音箱"))||
  2439. // (resData[i].state && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2440. // && resData[i].attributes.speaker && resData[i].attributes.speaker.volume && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2441. // ){
  2442. // obj.volume = resData[i].attributes.speaker.volume;//音量
  2443. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2444. // listConversation1Temp.push(obj)
  2445. // }
  2446. // }
  2447. //加湿器单独拿出来
  2448. // if((obj.friendly_name && obj.friendly_name.includes("加湿器"))||
  2449. // (resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2450. // && obj.friendly_name && obj.friendly_name.includes("加湿器"))
  2451. // ){
  2452. // listHumidityTemp.push(obj)
  2453. // }
  2454. // }
  2455. }
  2456. that.setData({
  2457. // status_beep: that.getEntityState(res.data,'switch.xiaomi_pro3_02bd_left_switch_service'), //实验室主灯
  2458. // status_led: that.getEntityState(res.data,'light.deerma_jsq2g_392f_indicator_light'), //加湿器 Indicator Light
  2459. // status_san: that.getEntityState(res.data,'switch.xwhzp_xwxfj_a441_diffuser'), //香薰机 Diffuser
  2460. // status_curtain: that.getEntityState(res.data,'switch.xiaomi_pro3_02bd_middle_switch_service'), //实验室侧灯
  2461. // status_door: that.getEntityState(res.data,'remote.xiaomi_lx06_f593_wifispeaker_4'), //智能音箱
  2462. // Temp: that.getEntityStateValue(res.data,'sensor.deerma_jsq2g_392f_temperature'), //加湿器温度
  2463. // Humi: that.getEntityStateValue(res.data,'sensor.deerma_jsq2g_392f_relative_humidity'), //加湿器湿度
  2464. // Light: that.getEntityStateValue(res.data,'number.xwhzp_xwxfj_a441_brightness'), //香薰机亮度
  2465. // LiquidLevel: that.getEntityStateValue(res.data,'sensor.xwhzp_xwxfj_a441_fragrance_liquid_left_level'), //香薰液浓度
  2466. listSwitch:listSwitchTemp,
  2467. listUnit:listUnitTemp,
  2468. listScroll:listScrollTemp,
  2469. listConversation:listConversationTemp,
  2470. listConversation1:listConversation1Temp,
  2471. listHumidity:listHumidityTemp,
  2472. listButton:listButtonTemp,
  2473. listSelect:listSelectTemp,
  2474. listClimate:listClimateTemp,
  2475. listMiotMediaPlayer:listMiotMediaPlayerTemp,
  2476. listMitvMediaPlayer:listMitvMediaPlayerTemp,
  2477. listHumidifier:listHumidifierTemp,
  2478. listSensor:listSensorTemp,
  2479. listText:listTextTemp,
  2480. tvEntityId:tvEntityIdTemp,
  2481. climateMode:climateModeTemp,
  2482. temperatureHumidifyName:temperatureHumidifyNameTemp,
  2483. listCover:listCoverTemp,
  2484. })
  2485. // console.log('listMiotMediaPlayerTemp='+JSON.stringify(listMiotMediaPlayerTemp));
  2486. },
  2487. });
  2488. // } catch (error) {
  2489. // console.error(error);
  2490. // }
  2491. // },
  2492. // });
  2493. } catch (error) {
  2494. console.error(error);
  2495. }
  2496. },
  2497. fail: function (error) {
  2498. console.error('error', error);
  2499. that.setData({
  2500. isLoading: false,
  2501. });
  2502. }
  2503. });
  2504. let end = Date.now(); // 再次获取当前时间(毫秒)
  2505. // console.log('end=='+end);
  2506. let executionTime = end - start;
  2507. // console.log(`myFunctionToTest 执行时间为: ${executionTime} 毫秒`);
  2508. return true;
  2509. },
  2510. getHADataStart(that,hotelcode,roomcode){
  2511. let start = Date.now(); // 获取当前时间(毫秒)
  2512. console.log('start=='+start);
  2513. var that = this;
  2514. wx.request({
  2515. url: `${aipushApi}/getha`, //
  2516. method: 'POST',
  2517. // header: {
  2518. // 'content-type': 'application/json', // 默认值
  2519. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2520. // },
  2521. data:{
  2522.     "locations":hotelcode,"token":token_push
  2523. },
  2524. success(ress) {
  2525. if(!ress || !ress.data){
  2526. console.error("getha(),没有获取到数据");
  2527. return;
  2528. }
  2529. var resData = ress.data
  2530. console.log("resData-=="+JSON.stringify(resData));
  2531. console.log("resData.loc-=="+JSON.stringify(resData.loc));
  2532. homeApi_https = resData.loc+"/api";
  2533. homeApiLater_https = resData.loc;
  2534. // console.log('res.data='+JSON.stringify(resData));
  2535. if (ress.data.code == "400") {
  2536. console.error("400错误!");
  2537. return;
  2538. }
  2539. if (ress.data.code == "401") {
  2540. console.error("401错误!");
  2541. return;
  2542. }
  2543. try {
  2544. //获取HA 设备状态数据
  2545. // wx.request({
  2546. // url: `${homeApi_https}/states`,
  2547. // method: 'GET',
  2548. // header: {
  2549. // 'content-type': 'application/json', // 默认值
  2550. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2551. // },
  2552. // success(res) {
  2553. // if(!res || !res.data){
  2554. // that.setData({
  2555. // isLoading: false,
  2556. // });
  2557. // return;
  2558. // }
  2559. // var resData = res.data
  2560. // // console.log('res.data='+JSON.stringify(resData));
  2561. // if (res.data.code == "400") {
  2562. // console.error("400错误!");
  2563. // return;
  2564. // }
  2565. // if (res.data.code == "401") {
  2566. // console.error("401错误 --- 请检查你的API或Key是否正确!");
  2567. // return;
  2568. // }
  2569. // try {
  2570. // const {
  2571. // now
  2572. // } = res.data;
  2573. // console.log('res.data1='+JSON.stringify(resData));
  2574. // res.data.find(function (obj) {
  2575. // console.log('obj='+obj);
  2576. // })
  2577. // res.data.forEach((item, index) => {
  2578. // console.log(index, item);
  2579. // });
  2580. var listCoverTemp = [];
  2581. var listSwitchTemp = [];
  2582. var listScrollTemp = [];
  2583. var listUnitTemp = [];
  2584. var listConversationTemp = [];
  2585. var listConversation1Temp = [];
  2586. var listHumidityTemp = [];
  2587. var listButtonTemp = [];
  2588. var listSelectTemp = [];
  2589. var listClimateTemp = [];
  2590. var listMiotMediaPlayerTemp = [];
  2591. var listMitvMediaPlayerTemp = [];
  2592. var listHumidifierTemp = [];
  2593. var listSensorTemp = [];
  2594. var listTextTemp = [];
  2595. var tvEntityIdTemp = '';
  2596. var climateModeTemp = '';
  2597. var temperatureHumidifyNameTemp = '温度';
  2598. var entityIdList = [];
  2599. const res = wx.getStorageSync('res');
  2600. const home_room = wx.getStorageSync('res').home_room;
  2601. // console.log('entity=====@==='+entity);
  2602. wx.request({
  2603. url: `${homeApi_https}/aidsleep/entity`, //获取空气数据
  2604. method: 'POST',
  2605. header: {
  2606. 'content-type': 'application/json', // 默认值
  2607. 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  2608. },
  2609. //
  2610. data:{
  2611. "template": "{{ label_entities('"+(entity)+"') }}"
  2612. },
  2613. success(resTemplate) {
  2614. let mid = Date.now(); // 再次获取当前时间(毫秒)
  2615. // console.log('mid=='+mid);
  2616. if(!resTemplate || !resTemplate.data){
  2617. console.log('no data:'+mid);
  2618. return;
  2619. }
  2620. let resData = resTemplate.data;
  2621. // resData = resData.filter(item1 => {
  2622. // return JSON.parse(that.replaceAll(resTemplate.data,'\'','\"')).find(item2 => item2 === item1.entity_id) !== undefined;
  2623. // });
  2624. console.log("resData-union="+JSON.stringify(resData));
  2625. // console.log('entityIdList-resTemplate====='+JSON.stringify(resTemplate.data));
  2626. for(let i=0;i<resData.length;i++){
  2627. // if(resTemplate.data.includes(resData[i].entity_id) && that.checkFuzzyMatch(resData[i])){
  2628. var obj={};
  2629. obj.entity_id = resData[i].entity_id;//实体ID
  2630. obj.parent_entity_id = resData[i].parent_entity_id;//父实体ID
  2631. //api/services/<domain>/<service>
  2632. if(resData[i].entity_id){
  2633. obj.domain = resData[i].entity_id.split(".")[0];
  2634. }
  2635. obj.state = (resData[i].state=='unknown'?"未知":resData[i].state);
  2636. if(resData[i].attributes && resData[i].attributes.friendly_name){
  2637. obj.friendly_name = resData[i].attributes.friendly_name; //设备名称
  2638. if(obj.friendly_name.includes("灯")){
  2639. obj.imgSrc = '/static/images/led.png';
  2640. }else if(obj.friendly_name.includes("加湿器")){
  2641. obj.imgSrc = '/static/images/hum.png';
  2642. }else if(obj.friendly_name.includes("香薰机")){
  2643. obj.imgSrc = '/static/images/light.png';
  2644. }else if(obj.friendly_name.includes("排气扇")){
  2645. obj.imgSrc = '/static/images/temp.png';
  2646. }else if(obj.friendly_name.includes("空调")){
  2647. obj.imgSrc = '/static/images/alarm.png';
  2648. }else{
  2649. obj.imgSrc = '/static/images/temp.png';
  2650. }
  2651. }
  2652. if(resData[i].attributes && resData[i].attributes.home_room){
  2653. obj.home_room = resData[i].attributes.home_room.split(' ')[1]; //房间号
  2654. }
  2655. if((resData[i].state && (resData[i].state=='on' || resData[i].state=='off')) && that.checkFuzzyMatch(resData[i])
  2656. ){
  2657. if(resData[i].state=='on'){
  2658. obj.service = 'turn_on';
  2659. }else if(resData[i].state=='off'){
  2660. obj.service = 'turn_off';
  2661. }
  2662. //截取字符串,防止开关设备名称过长
  2663. obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2664. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2665. listSwitchTemp.push(obj)
  2666. }
  2667. }
  2668. if((resData[i].state && obj.domain && obj.domain=='cover')
  2669. ){
  2670. obj.current_position = resData[i].attributes.current_position;
  2671. obj.target_position = resData[i].attributes.target_position;
  2672. obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2673. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2674. listCoverTemp.push(obj)
  2675. }
  2676. }
  2677. // if((resData[i].state && obj.domain && obj.domain=='number')||
  2678. // (resData[i].state && obj.domain && obj.domain=='number' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2679. // )
  2680. // ){
  2681. // obj.min = resData[i].attributes.min;
  2682. // obj.max = resData[i].attributes.max;
  2683. // obj.step = resData[i].attributes.step;
  2684. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2685. // if(resData[i].state!='unknown'){
  2686. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2687. // }
  2688. // if((resData[i].attributes.min || resData[i].attributes.max) && resData[i].attributes.unit_of_measurement!='rgb' && resData[i].attributes.unit_of_measurement!='minutes'){
  2689. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2690. // listScrollTemp.push(obj);
  2691. // }
  2692. // }else if((resData[i].attributes.min || resData[i].attributes.max) && (resData[i].attributes.unit_of_measurement=='rgb' || resData[i].attributes.unit_of_measurement=='minutes')){
  2693. // if(resData[i].state!='unknown'){
  2694. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2695. // }
  2696. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2697. // listUnitTemp.push(obj);
  2698. // }
  2699. // }
  2700. // }
  2701. // if((resData[i].state && obj.domain && obj.domain=='button')||
  2702. // (resData[i].state && obj.domain && obj.domain=='button' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2703. // )
  2704. // ){
  2705. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2706. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2707. // listButtonTemp.push(obj);
  2708. // }
  2709. // }
  2710. // if((resData[i].state && obj.domain && obj.domain=='select')||
  2711. // (resData[i].state && obj.domain && obj.domain=='select' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2712. // )
  2713. // ){
  2714. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2715. // obj.options = resData[i].attributes.options;
  2716. // obj.selectedIndex = 0;
  2717. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2718. // listSelectTemp.push(obj);
  2719. // }
  2720. // }
  2721. if((resData[i].state && obj.domain && obj.domain=='climate' && resData[i].attributes.entity_class=='MiirClimateEntity')
  2722. ){
  2723. climateModeTemp = obj.state;
  2724. obj.temperature = resData[i].attributes.temperature;
  2725. obj.min_temp = resData[i].attributes.min_temp;
  2726. obj.max_temp = resData[i].attributes.max_temp;
  2727. obj.hvac_modes = resData[i].attributes.hvac_modes;
  2728. obj.fan_modes = resData[i].attributes.fan_modes;
  2729. obj.fan_mode = resData[i].attributes.fan_mode;
  2730. obj.target_temp_step = resData[i].attributes.target_temp_step;
  2731. if(obj.state=='auto'){
  2732. temperatureHumidifyNameTemp = '自动';
  2733. }
  2734. if(obj.state=='heat'){
  2735. temperatureHumidifyNameTemp = '制热';
  2736. }
  2737. if(obj.state=='cool'){
  2738. temperatureHumidifyNameTemp = '制冷';
  2739. }
  2740. if(obj.state=='dry'){
  2741. temperatureHumidifyNameTemp = '除湿';
  2742. }
  2743. if(obj.state=='fan_only'){
  2744. temperatureHumidifyNameTemp = '送风';
  2745. }
  2746. if(obj.state=='off'){
  2747. temperatureHumidifyNameTemp = '关闭';
  2748. }
  2749. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2750. listClimateTemp.push(obj);
  2751. }
  2752. }
  2753. if((resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MiotMediaPlayerEntity')
  2754. ){
  2755. // obj["speaker.volume"] = resData[i].attribute["speaker.volume"];
  2756. obj.repeat = resData[i].attributes.repeat;
  2757. obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2758. obj.media_title = resData[i].attributes.media_title;
  2759. obj.media_artist = resData[i].attributes.media_artist;
  2760. obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2761. obj.media_duration = resData[i].attributes.media_duration;
  2762. obj.media_position = resData[i].attributes.media_position;
  2763. obj.media_late = resData[i].attributes.media_position + '/' + resData[i].attributes.media_duration;
  2764. // obj.microphone.mute = resData[i].attributes.microphone.mute;
  2765. // obj.speaker.mute = resData[i].attributes.speaker.mute;
  2766. // console.log('media-player-miot-obj=='+obj);
  2767. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2768. listMiotMediaPlayerTemp.push(obj);
  2769. }
  2770. }
  2771. // if((resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MitvMediaPlayerEntity')||
  2772. // (resData[i].state && obj.domain && obj.domain=='media_player' && resData[i].attributes.entity_class=='MitvMediaPlayerEntity' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2773. // )
  2774. // ){
  2775. // obj.volume_level = resData[i].attributes.volume_level;
  2776. // obj.is_volume_muted = resData[i].attributes.is_volume_muted;
  2777. // obj.source = resData[i].attributes.source;
  2778. // obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2779. // obj.media_title = resData[i].attributes.media_title;
  2780. // obj.media_artist = resData[i].attributes.media_artist;
  2781. // obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2782. // obj.media_duration = resData[i].attributes.media_duration;
  2783. // obj.media_position = resData[i].attributes.media_position;
  2784. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2785. // listMitvMediaPlayerTemp.push(obj);
  2786. // }
  2787. // }
  2788. if((resData[i].state && obj.domain && obj.domain=='media_player')
  2789. ){
  2790. obj.friendly_name = resData[i].attributes.friendly_name;
  2791. if(obj.entity_id.includes('teevee') || (obj.friendly_name && obj.friendly_name.includes('电视'))){
  2792. obj.volume_level = resData[i].attributes.volume_level;
  2793. obj.is_volume_muted = resData[i].attributes.is_volume_muted;
  2794. obj.source = resData[i].attributes.source;
  2795. obj.entity_picture = resData[i].attributes.entity_picture?"background-image: url('"+`${homeApiLater_https}`+resData[i].attributes.entity_picture+"');width: 100%; height: 100%;background-size: cover;background-repeat: no-repeat;background-position: center;":"background: radial-gradient(circle at center, #ff0000, #0000ff);border-radius: 30rpx;";
  2796. obj.media_title = resData[i].attributes.media_title;
  2797. obj.media_artist = resData[i].attributes.media_artist;
  2798. obj.volume_level = resData[i].attributes.volume_level?(resData[i].attributes.volume_level * 100):0;
  2799. obj.media_duration = resData[i].attributes.media_duration;
  2800. obj.media_position = resData[i].attributes.media_position;
  2801. obj.media_late = resData[i].attributes.media_position + '/' + resData[i].attributes.media_duration;
  2802. // obj["speaker.volume"] = resData[i].attributes["speaker.volume"];
  2803. // console.log('media-player-mitv-obj=='+obj);
  2804. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain && !listMitvMediaPlayerTemp){
  2805. listMitvMediaPlayerTemp.push(obj);
  2806. }
  2807. tvEntityIdTemp = obj.entity_id;
  2808. // console.log('obj===='+obj);
  2809. // console.log('tvEntityIdTemp='+tvEntityIdTemp);
  2810. wx.setStorageSync('tvEntityId',tvEntityIdTemp);
  2811. }
  2812. }
  2813. if((resData[i].state && obj.domain && obj.domain=='humidifier')
  2814. ){
  2815. obj.min_humidity = resData[i].attributes.min_humidity;
  2816. obj.max_humidity = resData[i].attributes.max_humidity;
  2817. obj.humidity = resData[i].attributes.humidity;
  2818. obj.min = resData[i].attributes.min;
  2819. obj.max = resData[i].attributes.max;
  2820. // console.log('humidifier-obj=='+JSON.stringify(obj));
  2821. if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2822. listHumidifierTemp.push(obj);
  2823. }
  2824. }
  2825. // if((resData[i].state && obj.domain && obj.domain=='sensor')||
  2826. // (resData[i].state && obj.domain && obj.domain=='sensor' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2827. // )
  2828. // ){
  2829. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2830. // obj.unit_of_measurement = resData[i].attributes.unit_of_measurement;
  2831. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2832. // listSensorTemp.push(obj);
  2833. // }
  2834. // }
  2835. // if((resData[i].state && obj.domain && obj.domain=='text')||
  2836. // (resData[i].state && obj.domain && obj.domain=='text' && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2837. // )
  2838. // ){
  2839. // obj.friendly_name = that.truncateString(resData[i].attributes.friendly_name);
  2840. // obj.min = resData[i].attributes.min;
  2841. // obj.max = resData[i].attributes.max;
  2842. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2843. // listTextTemp.push(obj);
  2844. // }
  2845. // }
  2846. // if((resData[i].state && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2847. // || (resData[i].state && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2848. // && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2849. // ){
  2850. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2851. // listConversationTemp.push(obj)
  2852. // }
  2853. // }
  2854. // if((resData[i].state && resData[i].attributes && resData[i].attributes.speaker && resData[i].attributes.speaker.volume && obj.friendly_name && obj.friendly_name.includes("智能音箱"))||
  2855. // (resData[i].state && resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2856. // && resData[i].attributes.speaker && resData[i].attributes.speaker.volume && obj.friendly_name && obj.friendly_name.includes("智能音箱"))
  2857. // ){
  2858. // obj.volume = resData[i].attributes.speaker.volume;//音量
  2859. // if(obj.entity_id && obj.state && obj.friendly_name && obj.domain){
  2860. // listConversation1Temp.push(obj)
  2861. // }
  2862. // }
  2863. //加湿器单独拿出来
  2864. // if((obj.friendly_name && obj.friendly_name.includes("加湿器"))||
  2865. // (resData[i].attributes && resData[i].attributes.parent_entity_id && entityIdList && entityIdList.includes(resData[i].attributes.parent_entity_id)
  2866. // && obj.friendly_name && obj.friendly_name.includes("加湿器"))
  2867. // ){
  2868. // listHumidityTemp.push(obj)
  2869. // }
  2870. // }
  2871. }
  2872. that.setData({
  2873. // status_beep: that.getEntityState(res.data,'switch.xiaomi_pro3_02bd_left_switch_service'), //实验室主灯
  2874. // status_led: that.getEntityState(res.data,'light.deerma_jsq2g_392f_indicator_light'), //加湿器 Indicator Light
  2875. // status_san: that.getEntityState(res.data,'switch.xwhzp_xwxfj_a441_diffuser'), //香薰机 Diffuser
  2876. // status_curtain: that.getEntityState(res.data,'switch.xiaomi_pro3_02bd_middle_switch_service'), //实验室侧灯
  2877. // status_door: that.getEntityState(res.data,'remote.xiaomi_lx06_f593_wifispeaker_4'), //智能音箱
  2878. // Temp: that.getEntityStateValue(res.data,'sensor.deerma_jsq2g_392f_temperature'), //加湿器温度
  2879. // Humi: that.getEntityStateValue(res.data,'sensor.deerma_jsq2g_392f_relative_humidity'), //加湿器湿度
  2880. // Light: that.getEntityStateValue(res.data,'number.xwhzp_xwxfj_a441_brightness'), //香薰机亮度
  2881. // LiquidLevel: that.getEntityStateValue(res.data,'sensor.xwhzp_xwxfj_a441_fragrance_liquid_left_level'), //香薰液浓度
  2882. listSwitch:listSwitchTemp,
  2883. listUnit:listUnitTemp,
  2884. listScroll:listScrollTemp,
  2885. listConversation:listConversationTemp,
  2886. listConversation1:listConversation1Temp,
  2887. listHumidity:listHumidityTemp,
  2888. listButton:listButtonTemp,
  2889. listSelect:listSelectTemp,
  2890. listClimate:listClimateTemp,
  2891. listMiotMediaPlayer:listMiotMediaPlayerTemp,
  2892. listMitvMediaPlayer:listMitvMediaPlayerTemp,
  2893. listHumidifier:listHumidifierTemp,
  2894. listSensor:listSensorTemp,
  2895. listText:listTextTemp,
  2896. tvEntityId:tvEntityIdTemp,
  2897. climateMode:climateModeTemp,
  2898. temperatureHumidifyName:temperatureHumidifyNameTemp,
  2899. listCover:listCoverTemp,
  2900. isLoading: false,
  2901. })
  2902. },
  2903. });
  2904. let mid = Date.now(); // 获取当前时间(毫秒)
  2905. console.log('mid=='+mid);
  2906. console.log('start=='+start);
  2907. console.log('mid-start=='+(mid-start));
  2908. // that.setData({
  2909. // isLoading: true,
  2910. // });
  2911. // } catch (error) {
  2912. // console.error(error);
  2913. // }
  2914. // },
  2915. // fail: function (error) {
  2916. // console.error('error', error);
  2917. // that.setData({
  2918. // isLoading: false,
  2919. // });
  2920. // }
  2921. // });
  2922. } catch (error) {
  2923. console.error(error);
  2924. that.setData({
  2925. isLoading: false,
  2926. });
  2927. }
  2928. },
  2929. fail: function (error) {
  2930. console.error('error', error);
  2931. that.setData({
  2932. isLoading: false,
  2933. });
  2934. }
  2935. });
  2936. let end = Date.now(); // 再次获取当前时间(毫秒)
  2937. console.log('end=='+end);
  2938. let executionTime = end - start;
  2939. console.log(`myFunctionToTest 执行时间为: ${executionTime} 毫秒`);
  2940. return true;
  2941. },
  2942. imageLoad: function () {
  2943. this.setData({
  2944. isLoading: false,
  2945. });
  2946. console.log('图片渲染完成');
  2947. },
  2948. startInterval: function () {
  2949. // 使用 setInterval 创建定时任务,每8秒执行一次 this.myMethod 方法
  2950. var that = this;
  2951. // this.intervalId = setInterval(() => {
  2952. // that.getHAData(that,that.data.hotelcode,that.data.roomcode);
  2953. // }, 6000);
  2954. this.data.intervalId1 = setInterval(() => {
  2955. that.getHAData(that,that.data.hotelcode,that.data.roomcode);
  2956. }, 6000);
  2957. // 第二个定时器,每2秒执行一次
  2958. this.data.intervalId2 = setInterval(() => {
  2959. // 这里执行你的代码
  2960. that.setData({
  2961. isClickedPrevSong: false,
  2962. isClickedTogglePlayPause: false,
  2963. isClickedNextSong: false,
  2964. isClickedOnHumidifierUp: false,
  2965. isClickedOnHumidifierDown: false,
  2966. isClickedOnClimateAuto: false,
  2967. isClickedOnClimateUp: false,
  2968. });
  2969. }, 300);
  2970. },
  2971. clearInterval: function () {
  2972. // 清除定时任务
  2973. // if (this.intervalId) {
  2974. // clearInterval(this.intervalId);
  2975. // this.intervalId = null;
  2976. // }
  2977. if (this.data.intervalId1) {
  2978. clearInterval(this.data.intervalId1);
  2979. this.data.intervalId1 = null; // 可选,但建议清除以避免潜在的错误
  2980. }
  2981. // 停止第二个定时器
  2982. if (this.data.intervalId2) {
  2983. clearInterval(this.data.intervalId2);
  2984. this.data.intervalId2 = null; // 可选,但建议清除以避免潜在的错误
  2985. }
  2986. },
  2987. stopTouchMove: function(e) {
  2988. return false;
  2989. },
  2990. truncateString: function(str) {
  2991. if(!str) return ';'
  2992. if (str.length <= 6) {
  2993. // 如果字符串长度小于等于5,直接返回原字符串
  2994. return str;
  2995. } else{
  2996. // 如果字符串长度在6到9之间,返回后五个字符(此时没有前部分可拼接)
  2997. return '..'+str.slice(-6);
  2998. }
  2999. },
  3000. onLoad(option){
  3001. console.log("index页面");
  3002. // wx.clearStorage();
  3003. // this.setTextBreathing(wx.createSelectorQuery(".header-title-fire"),this);
  3004. //console.log('option.res='+JSON.stringify(wx.getStorageSync('res')));
  3005. if(!wx.getStorageSync('res') || !wx.getStorageSync('res').result){
  3006. wx.showModal({
  3007. title: '提示',
  3008. content: '请先扫描房间二维码',
  3009. showCancel: false
  3010. });
  3011. setTimeout(function() {
  3012. wx.reLaunch({
  3013. url: '/pages/scan/scan',
  3014. })
  3015. }, 2000) // 设置延时时间,单位为毫秒
  3016. }else{
  3017. var that = this;
  3018. // wx.request({
  3019. // url: `${aipushApi}`+'/getbigreport',
  3020. // method: 'POST',
  3021. // header: {
  3022. // 'content-type': 'application/json', // 默认值
  3023. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  3024. // },
  3025. // data:{
  3026. //     "sn":'',"tokens":token_push
  3027. // },
  3028. // success(ress) {
  3029. // if(ress && ress.data){
  3030. // wx.request({
  3031. // url: `${homeApi_https}/states`, //获取空气数据
  3032. // method: 'GET',
  3033. // header: {
  3034. // 'content-type': 'application/json', // 默认值
  3035. // 'Authorization': 'Bearer ' + token // 在头部设置认证信息,例如使用Bearer Token
  3036. // },
  3037. // success(res) {
  3038. // if(!res || !res.data){
  3039. // wx.showModal({
  3040. // title: '提示',
  3041. // content: '请先连接房间wi-fi,再次扫描',
  3042. // showCancel: false
  3043. // });
  3044. // setTimeout(function() {
  3045. // wx.reLaunch({
  3046. // url: '/pages/scan/scan',
  3047. // })
  3048. // }, 2000) // 设置延时时间,单位为毫秒
  3049. // }else{
  3050. // // console.log("res.data====="+JSON.stringify(res.data));
  3051. // }
  3052. // },
  3053. // });
  3054. // }else{
  3055. // wx.showModal({
  3056. // title: '提示',
  3057. // content: '请先连接房间wi-fi,再次扫描',
  3058. // showCancel: false
  3059. // });
  3060. // setTimeout(function() {
  3061. // wx.reLaunch({
  3062. // url: '/pages/scan/scan',
  3063. // })
  3064. // }, 2000) // 设置延时时间,单位为毫秒
  3065. // }
  3066. // },
  3067. // });
  3068. // const socketUrl = 'http://192.168.3.65:8123';
  3069. // wx.connectSocket({
  3070. // url: socketUrl,
  3071. // success: function (res) {
  3072. // console.log('WebSocket连接成功', res);
  3073. // },
  3074. // fail: function (error) {
  3075. // console.error('WebSocket连接打开失败', error);
  3076. // }
  3077. // });
  3078. //wi-fi判断
  3079. // wx.startWifi({
  3080. // fail(error) {
  3081. // console.log('初始化wifi-失败', error)
  3082. // },
  3083. // success(res) {
  3084. // console.log('初始化wifi-成功', res)
  3085. // },
  3086. // });
  3087. let hotelcodeTemp = '';
  3088. let roomcodeTemp = '';
  3089. if(wx.getStorageSync('res').result.split('|').length != 2){
  3090. wx.showModal({
  3091. title: '提示',
  3092. content: '房间二维码不正确,请重新扫码',
  3093. showCancel: false
  3094. });
  3095. setTimeout(function() {
  3096. wx.reLaunch({
  3097. url: '/pages/scan/scan',
  3098. })
  3099. }, 2000) // 设置延时时间,单位为毫秒
  3100. }
  3101. hotelcodeTemp = wx.getStorageSync('res').result.split('|')[0];
  3102. roomcodeTemp = wx.getStorageSync('res').result.split('|')[1];
  3103. entity = roomcodeTemp;
  3104. if(entity == 'labs'){
  3105. entity = '公司实验室';
  3106. wx.setStorageSync('roomname','604');
  3107. }else{
  3108. wx.setStorageSync('roomname',entity);
  3109. }
  3110. this.setData({
  3111. isLoading: true,
  3112. hotelcode: hotelcodeTemp,
  3113. roomcode: roomcodeTemp,
  3114. });
  3115. if(this.getHADataStart(this,hotelcodeTemp,roomcodeTemp)){
  3116. // 页面加载时开始定时任务
  3117. // this.startInterval();
  3118. }
  3119. }
  3120. // else if(!this.data.home_room.includes(wx.getStorageSync('res').result)){
  3121. // wx.showModal({
  3122. // title: '提示',
  3123. // content: '房间二维码不正确,请重新扫描',
  3124. // showCancel: false
  3125. // });
  3126. // setTimeout(function() {
  3127. // wx.reLaunch({
  3128. // url: '/pages/scan/scan',
  3129. // })
  3130. // }, 2000) // 设置延时时间,单位为毫秒
  3131. // }
  3132. // this.showCanvasRing();
  3133. // 创建音频上下文
  3134. // this.innerAudioContext = wx.createInnerAudioContext();
  3135. // // 设置音频源
  3136. // this.innerAudioContext.src = '你的音频文件链接'; // 替换为你的音频文件链接
  3137. // // 监听播放状态
  3138. // this.innerAudioContext.onPlay(() => {
  3139. // this.setData({ isPlaying: true });
  3140. // });
  3141. // this.innerAudioContext.onPause(() => {
  3142. // this.setData({ isPlaying: false });
  3143. // });
  3144. // this.innerAudioContext.onStop(() => {
  3145. // this.setData({ isPlaying: false });
  3146. // });
  3147. // this.innerAudioContext.onError((res) => {
  3148. // console.log('res.errorCode='+res.errorCode);
  3149. // console.log('res.errorMsg='+res.errorMsg);
  3150. // });
  3151. // // 监听播放进度
  3152. // this.innerAudioContext.onTimeUpdate(() => {
  3153. // this.setData({
  3154. // currentTime: this.innerAudioContext.currentTime,
  3155. // duration: this.innerAudioContext.duration
  3156. // });
  3157. // });
  3158. // // 初始化歌曲列表
  3159. // this.setData({
  3160. // songs: [
  3161. // { id: 1, title: '歌曲1', artist: '歌手1', url: 'song1.mp3' },
  3162. // { id: 2, title: '歌曲2', artist: '歌手2', url: 'song2.mp3' },
  3163. // { id: 3, title: '歌曲3', artist: '歌手3', url: 'song3.mp3' }
  3164. // ]
  3165. // });
  3166. },
  3167. mounted() {
  3168. },
  3169. onHide(){
  3170. // wx.clearStorage();
  3171. // 页面卸载时清除定时任务
  3172. this.clearInterval();
  3173. // if(this.tcp){
  3174. // this.tcp.onClose(() => {
  3175. // // this.infoList.push('关闭')
  3176. // console.log('tcp关闭');
  3177. // this.tcp.close()
  3178. // })
  3179. // }
  3180. },
  3181. onUnload(){
  3182. //wx.clearStorage();
  3183. // 页面卸载时清除定时任务
  3184. this.clearInterval();
  3185. // if(this.tcp){
  3186. // this.tcp.onClose(() => {
  3187. // // this.infoList.push('关闭')
  3188. // console.log('tcp关闭');
  3189. // this.tcp.close()
  3190. // })
  3191. // }
  3192. // if (this.data.observer) {
  3193. // this.data.observer.disconnect();
  3194. // }
  3195. },
  3196. onReady(){
  3197. console.log('页面渲染完成')
  3198. },
  3199. showLoading:function(){
  3200. wx.showToast({
  3201. title:'加载中',
  3202. icon:'loading',
  3203. })
  3204. },
  3205. cancelLoading:function(){
  3206. wx.hideToast()
  3207. },
  3208. onShow() {
  3209. var that = this;
  3210. that.startInterval();
  3211. //mqtt程序,保留
  3212. /* wx.showToast({
  3213. title: "连接服务器....",
  3214. icon: "loading",
  3215. duration: 10000,
  3216. mask: true,
  3217. });
  3218. let second = 10;
  3219. var toastTimer = setInterval(() => {
  3220. second--;
  3221. if (second) {
  3222. wx.showToast({
  3223. title: `连接服务器...${second}`,
  3224. icon: "loading",
  3225. duration: 1000,
  3226. mask: true,
  3227. });
  3228. } else {
  3229. clearInterval(toastTimer);
  3230. wx.showToast({
  3231. title: "连接失败",
  3232. icon: "error",
  3233. mask: true,
  3234. });
  3235. }
  3236. }, 1000);
  3237. that.setData({
  3238. client: connect(mqttUrl,options)
  3239. })
  3240. that.data.client.on("connect", function () {
  3241. console.log("成功连接mqtt服务器!");
  3242. clearInterval(toastTimer);
  3243. wx.showToast({
  3244. title: "连接成功",
  3245. icon: "success",
  3246. mask: true,
  3247. });
  3248. // 一秒后订阅主题
  3249. setTimeout(() => {
  3250. that.data.client.subscribe(mpSubTopic_env, function (err) {
  3251. if (!err) {
  3252. console.log("成功订阅设备上行数据环境Topic!");
  3253. wx.showToast({
  3254. title: "订阅成功",
  3255. icon: "success",
  3256. mask: true,
  3257. });
  3258. }
  3259. });
  3260. }, 500);
  3261. });
  3262. setTimeout(() => {
  3263. that.data.client.subscribe(mpSubTopic_con, function (err) {
  3264. if (!err) {
  3265. console.log("成功订阅设备上行数据控制Topic!");
  3266. wx.showToast({
  3267. title: "订阅成功",
  3268. icon: "success",
  3269. mask: true,
  3270. });
  3271. }
  3272. });
  3273. }, 200);
  3274. that.data.client.on("message", function (topic, message) {
  3275. console.log(topic);
  3276. // message是16进制的Buffer字节流
  3277. let dataFromDev = {};
  3278. console.log('message='+message);
  3279. // 尝试进行JSON解析
  3280. try {
  3281. dataFromDev = JSON.parse(message);
  3282. console.log(dataFromDev);
  3283. that.setData({
  3284. //环境状态
  3285. Temp: dataFromDev.Temp,
  3286. Humi: dataFromDev.Humi,
  3287. Voltage1: dataFromDev.Voltage1,
  3288. Voltage2: dataFromDev.Voltage2,
  3289. //设备状态
  3290. status_beep: dataFromDev.Beep_Flag,
  3291. status_led: dataFromDev.Led_Flag,
  3292. status_curtain: dataFromDev.Angle1,
  3293. status_san: dataFromDev.San_Flag,
  3294. status_door: dataFromDev.Angle2,
  3295. status_fire: dataFromDev.Fire_Flag,
  3296. status_light: dataFromDev.Light_Flag,
  3297. status_mode: dataFromDev.Mode_Flag
  3298. })
  3299. } catch (error) {
  3300. // 解析失败错误捕获并打印(错误捕获之后不会影响程序继续运行)
  3301. console.log(error);
  3302. }
  3303. }) */
  3304. // if(this.tcp){
  3305. // this.tcp.onClose(() => {
  3306. // // this.infoList.push('关闭')
  3307. // console.log('tcp关闭');
  3308. // this.tcp.close()
  3309. // this.tcp = '';
  3310. // })
  3311. // }
  3312. // // 初始化TCP实例
  3313. // this.tcp = wx.createTCPSocket()
  3314. // // tcp实例向服务端发起连接
  3315. // this.tcp.connect({ address: '192.168.3.65', port: '81231' })
  3316. // // this.infoList.push(`向${this.ip}:${~~this.port}建立链接`)
  3317. // this.tcp.offClose(() => {
  3318. // })
  3319. // this.tcp.onConnect(() => {
  3320. // // this.infoList.push('连接成功')
  3321. // console.log('tcp连接成功');
  3322. // })
  3323. // this.tcp.offConnect(() => {
  3324. // // this.infoList.push('offConnect')
  3325. // console.log('tcp-offConnect');
  3326. // })
  3327. // this.tcp.onError(error => {
  3328. // // this.infoList.push('异常:' + error)
  3329. // console.log('tcp异常:' + error);
  3330. // wx.showModal({
  3331. // title: '提示',
  3332. // content: '请先连接房间wi-fi,再次扫描',
  3333. // showCancel: false
  3334. // });
  3335. // setTimeout(function() {
  3336. // wx.reLaunch({
  3337. // url: '/pages/scan/scan',
  3338. // })
  3339. // }, 2000) // 设置延时时间,单位为毫秒
  3340. // })
  3341. // this.tcp.onMessage(data => {
  3342. // // this.infoList.push('tcp接收到数据: ' + transitionArrayBufferToString(data.message))
  3343. // console.log('tcp接收到数据: ' + JSON.stringify(data.message));
  3344. // })
  3345. wx.getLocation({
  3346. type: "wgs84",
  3347. success(res) {
  3348. const latitude = res.latitude;
  3349. const longitude = res.longitude;
  3350. const key = hefengKey;
  3351. wx.request({
  3352. url: `${geoApi}location=${longitude},${latitude}&key=${key}`, //获取地理位置
  3353. success(res) {
  3354. console.log('getLocation='+JSON.stringify(res.data));
  3355. if (res.data.code == "401") {
  3356. console.error("HUAQING --- 请检查你的和风天气API或Key是否正确!");
  3357. return;
  3358. }
  3359. try {
  3360. const {
  3361. location
  3362. } = res.data;
  3363. that.setData({
  3364. area: location[0].name, //城区
  3365. city: location[0].adm2 //城市
  3366. })
  3367. } catch (error) {
  3368. console.error(error);
  3369. }
  3370. },
  3371. });
  3372. wx.request({
  3373. url: `${hefengWeather}location=${longitude},${latitude}&key=${key}`, //获取实时天气数据
  3374. success(res) {
  3375. console.log("实时天气=="+JSON.stringify(res.data));
  3376. if (res.data.code == "401") {
  3377. console.error("HUAQING --- 请检查你的和风天气API或Key是否正确!");
  3378. return;
  3379. }
  3380. try {
  3381. const {
  3382. now
  3383. } = res.data;
  3384. that.setData({
  3385. weather: now.text, // 天气
  3386. w_temp: now.temp, // 温度
  3387. w_humidity: now.humidity, // 湿度
  3388. })
  3389. } catch (error) {
  3390. console.error(error);
  3391. }
  3392. },
  3393. });
  3394. wx.request({
  3395. url: `${hefengAir}location=${longitude},${latitude}&key=${key}`, //获取空气数据
  3396. success(res) {
  3397. console.log("空气数据=="+JSON.stringify(res.data));
  3398. if (res.data.code == "401") {
  3399. console.error("HUAQING --- 请检查你的和风天气API或Key是否正确!");
  3400. return;
  3401. }
  3402. try {
  3403. const {
  3404. now
  3405. } = res.data;
  3406. that.setData({
  3407. airText: now.category, //空气质量
  3408. airValue: now.aqi //空气指数
  3409. })
  3410. } catch (error) {
  3411. console.error(error);
  3412. }
  3413. },
  3414. });
  3415. },
  3416. });
  3417. // 获取天气相关数据
  3418. // wx.scanCode({
  3419. // success: (res) => {
  3420. // console.log('扫描结果:'+res.result) // 扫描结果
  3421. // }
  3422. // });
  3423. }
  3424. })