[{"id":"d89906f9.5f71b8","type":"tab","label":"格力空调","disabled":false,"info":""},{"id":"61065ac0.d38f84","type":"debug","z":"d89906f9.5f71b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":827,"y":277,"wires":[]},{"id":"82bbfd1.140bc","type":"function","z":"d89906f9.5f71b8","name":"数据处理","func":"/*\n * 具体格式分析见:[url]https://blog.csdn.net/dddxxxx/article/details/53033748[/url]\n */\n\nvar ac_ir = msg.payload.split('\\n');\nvar w = {};\nvar CMD_LEN = 67;\nvar cmd = new ArrayBuffer(CMD_LEN); //命令长度,67bit,第一部分35bit,第二部分32bit\nvar dv = new DataView(cmd);\n\nfunction makeCode(idx, cmd) {\n for(var i = 0; i < cmd.length; i++) {\n dv.setUint8(idx + i, cmd[i].charCodeAt(0));\n }\n}\n\nfor(var i = 0; i < CMD_LEN; i++) {\n dv.setUint8(i, 48); //初始化为"0"\n }\n\nvar set_mode = ac_ir[0]; //工作模式\nvar set_temp = ac_ir[1]; //设置温度\nvar set_fanSpeed = ac_ir[2]; //风量\nvar set_power = ac_ir[3]; //电源开关\n\nvar LR_flow = ac_ir[4]; //左右扫风\nvar UD_flow = ac_ir[5]; //上下扫风\n\n// 空调工作模式\nvar ac_mod = 0;\nif(set_mode == "自动")\n{\n makeCode(0,"000");\n}else if(set_mode == "制冷")\n{\n ac_mod = 1;\n makeCode(0,"100");\n}else if(ac_ir[0] == "制热")\n{\n ac_mod = 4;\n makeCode(0,"001");\n}else if(set_mode == "除湿")\n{\n ac_mod = 2;\n makeCode(0,"010");\n}\nelse if(set_mode == "送风")\n{\n ac_mod = 3;\n makeCode(0,"110");\n}\n \n//风速\nif(set_fanSpeed == "自动")\n{\n makeCode(4,"00");\n}else if(set_fanSpeed == "一级")\n{\n makeCode(4,"10");\n}else if(set_fanSpeed == "二级")\n{\n makeCode(4,"01");\n}\nelse if(set_fanSpeed == "三级")\n{\n makeCode(4,"11");\n}\n\n// 温度\nvar temp_code = "";\ntemp_code = (set_temp - 16).toString("2"); //转为二进制\nfor(var i = 0; i < 4; i++) {\n dv.setUint8(8 + i, temp_code[3-i].charCodeAt(0)); //反序\n }\nw['temp'] = temp_code;\n\n//电源\nvar power_mod = 0;\nif(set_power == "off")\n{\n makeCode(3,"0"); \n}else\n{\n power_mod = 1;\n makeCode(3,"1");\n}\n\n//扫风\nvar lr_mod = 0;\nif(LR_flow == "on")\n{\n dv.setUint8(6,49);\n dv.setUint8(39,49);\n lr_mod = 1;\n}\n\nif(UD_flow == "on")\n{\n dv.setUint8(6,49);\n dv.setUint8(35,49);\n}\n\n// 默认值\ndv.setUint8(21,49); //灯光开\nmakeCode(43,"10"); //显示温度\nmakeCode(27,"01010010"); //第一部分结束码\ndv.setUint8(48,49); \n//计算校验值\n//校验码 = (模式 – 1) + (温度 – 16) + 5 + 左右扫风 + 换气 + 节能\n//取二进制后四位,再逆序\n\nvar dat = (ac_mod - 1) + (set_temp - 16) + 5 + lr_mod;\nvar crc_code = dat.toString("2");\nw['crc'] = crc_code;\nvar crc4 = crc_code.substr(-4);\nfor(var i = 0; i < 4; i++) {\n dv.setUint8(63 + i, crc4[3-i].charCodeAt(0)); //反序\n}\n// 如果是关机,最后一位校验位取反\nif(power_mod === 0)\n{\n if(dv.getUint8(66) == 48)\n {\n dv.setUint8(66, 49);\n }else{\n dv.setUint8(66, 48);\n }\n}\n\n//生成pronto hex码\nvar pre_code = "pronto:0000 006C 0046 0000 "; //包含连接码共68bit\nvar leader_code = "015A 00AD ";\nvar end_code = "0019 0300 "; //连接码与此相同\nvar high_level = "0019 003E ";\nvar low_level = "0019 0015 ";\nvar raw_str = pre_code + leader_code;\n\n\n\n\nvar str_code="";\nfor(var i = 0; i < CMD_LEN; i++) {\n \n str_code = str_code + String.fromCharCode(dv.getUint8(i));\n if(dv.getUint8(i) == 48)\n raw_str += low_level;\n else\n raw_str += high_level;\n if(i == 34)\n raw_str += end_code; //35bit之后添加连接码\n }\nraw_str += end_code; \n\nvar call_str = {"entity_id":"remote.ir_remote","command":raw_str};\nw['code'] = str_code;\nw['data'] = call_str;\n\nmsg.payload = w;\nreturn msg;\n","outputs":1,"noerr":0,"x":543,"y":277,"wires":[["61065ac0.d38f84","55679b99.e3b6e4"]]},{"id":"5797567e.1d7898","type":"api-render-template","z":"d89906f9.5f71b8","name":"数据模板","server":"3e738894.04e198","template":"{{ states('input_select.ac_mode_select') }}\n{{ states('input_number.ac_temperature') }}\n{{ states('input_select.ac_fanspeed_select') }}\n{{ states('input_boolean.ac_power') }}\n{{ states('input_boolean.ac_lr_flow') }}\n{{ states('input_boolean.ac_ud_flow') }}","x":288,"y":277,"wires":[["82bbfd1.140bc"]]},{"id":"c871692f.d2ed28","type":"server-state-changed","z":"d89906f9.5f71b8","name":"发送事件","server":"3e738894.04e198","entityidfilter":"input_boolean.ac_ir_send","entityidfiltertype":"substring","haltifstate":"off","x":89,"y":304,"wires":[["5797567e.1d7898"]]},{"id":"55679b99.e3b6e4","type":"api-call-service","z":"d89906f9.5f71b8","name":"小米红外遥控","server":"3e738894.04e198","service_domain":"remote","service":"send_command","data":"","mergecontext":"","x":816,"y":429,"wires":[[]]},{"id":"3e738894.04e198","type":"server","z":"","name":"HASS","url":"http://192.168.1.19:8123","pass":""}]