/// <summary>
        /// 8145关闭设备 8;8135;设备id
        /// user:123_DCD9165057AD type:other msg:123_DCD9165057AD;8;8145;01120925117040;3,0$/r$
        /// 8135打开设备 8;8135;设备id
        /// user:123_DCD9165057AD type:other msg:123_DCD9165057AD;8;8135;01120925117040;2;8$/r$
        /// {"code":1002,"id":"010000124b0014c6aaee","ep":1,"serial":1,"control":{"on":true},"result":0,"zigbee":"00ff2c2c2c6a6f005979"}
        /// user:DAJCHSF_% type:other msg:DAJCHSF_Server;devrefresh;1041656180510,true,DAJCHSF_2047DABEF936$/r$
        /// </summary>
        /// <param name="msg"></param>
        public static bool DeviceStateChange(string msg)//, string code, bool state, string success, out string relayUser
        {
            try
            {
                //string appUser = msg.Split(';')[0];
                string code   = msg.Split(';')[2];
                string device = msg.Split(';')[3];
                bool   state;
                if (code == "8135")
                {
                    state = true;
                }
                else if (code == "8145")
                {
                    state = false;
                }
                else
                {
                    return(false);//指令不对返回失败
                }

                using (HsfDBContext hsfDBContext = new HsfDBContext())
                {
                    var deviceEntity = hsfDBContext.host_device.Where(t => t.deviceid == device && t.deletemark == 0).FirstOrDefault();//注意device的唯一性
                    if (deviceEntity != null)
                    {
                        string cachekey = deviceEntity.cachekey;//缓存处理--

                        Zigbee1002 zigbee1002 = new Zigbee1002()
                        {
                            code    = 1002,
                            id      = deviceEntity.devmac,//010000124b0014c6aaee
                            ep      = Convert.ToInt32(deviceEntity.devport),
                            serial  = 1,
                            control = new Control {
                                on = state
                            },
                            result = 0,
                            zigbee = deviceEntity.devchannel                           //00ff2c2c2c6a6f005979
                        };
                        YunZigClient.SendMsg(JsonConvert.SerializeObject(zigbee1002)); //发送
                        if (StateResult(cachekey, state))
                        {
                            //返回广播消息,直接开始广播
                            //user:hiddenpath_% type:other msg:hiddenpath_Server;devrefresh;924150429051,false,hiddenpath_ASDFDSSE123$/r$
                            //relayUser = appUser.Split('_')[0] + "_%";
                            //relayUser = appUser;
                            //return $"{appUser};{code};{device};Zip;{success}$/r$";//ok//closeok@808 181248576
                            //return $"{relayUser};devrefresh;{device},{state},{appUser}$/r$";//后面的发给前面的,与请求的对调一下
                            return(true);
                        }
                        else
                        {
                            log.Debug($"设备状态改变超时失败! cachekey:{cachekey}!");
                            //relayUser = appUser;
                            //return $"{appUser};{code};{device};Zip;H4sIAAAAAAAEAEstKsovAgBxvN1dBQAAAA==$/r$";//error
                            return(false);
                        }
                    }
                    else
                    {
                        //relayUser = appUser;
                        return(false);//error
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 添加设备 8;8211;All;+Base64(zip(设备对象jhson串))
        /// </summary>
        /// <param name="msg">user:123_Server type:other msg:
        /// 123_DCD9165057AD;8;8211;ALL;H4sIAAAAAAAAAH2QPU7EMBCF7zJ1CjuRs2wuQEdBuQitBmectZTEke2wQqutEAeg5Bx0SHscfo6Bf6JINMiNv/eeZ8Zzd4IbOt6SNLaFxtuZCtjrcGUFyIMeccSBoIGvt4+f58v35fXz5Z1B8Hp0TsUgXAVs6RF7tEPATPKA40j9ylpSCvOyFFyIWvCKbdniTcFgTKlSxlNjrRgTG6WyPaD815/QIjSncwZjfWxTic12sY3TXpsx1siKpU6altI0VZacR0/rsIn4XyxX9E9TjO5090C0v44PC9ADdvmHIcGZCFLfuTRM2ORkjmE/89JhdmRTFM73v4aKUN2AAQAA$/r$
        /// user:123_DCD9165057AD type:other msg:
        /// 123_DCD9165057AD;8;8212;ALL;H4sIAAAAAAAAAG2RsU7DMBCG38VzVOUcN6TZGBlggBGh6ORcUkuJHdlJI1RVYmFn4ykQAyuv04G3wIlLRBHy4u/+/3y/zvd7dkPjLUljS5b3dqCIFcpf44jJrdKosSWWM5RZUiUgCKsL5qUGnasmH8s8lrTDBm3rMZDcotbULKwkzWbgsAbBIRYiSeOT1s1CEqhFOeGGryDNVlm2gpgHpUOLLN/72cZNkY7PH1+vbxEHiPz522A60pPp8+n48v6/6RCeNbZfcnbGqV4ZHaICCJHGsPlJaqmWpqTfcV2PPS3tM8E58gX7x26yXl4V16Tr0gy+rFqsw2q8AeK1LzW1myP5H+jM6Bc7nAYMjuxsHbFRxR3ZHVl2ePgGQ/XuYsMBAAA=$/r$
        ///
        /// </param>
        public static string AddDeviceToRoom(string msg, string code)
        {
            try
            {
                if (msg.Split(';').Length >= 4)
                {
                    string appUser     = msg.Split(';')[0];
                    string account     = appUser.Split('_')[0];
                    string mac         = appUser.Split('_')[1];
                    string zipStr      = msg.Split(';')[4].Replace("$/r$", "");
                    string base64j     = EncryptionHelp.Decrypt(zipStr, true);
                    var    deviceLists = JsonConvert.DeserializeObject <List <host_device> >(base64j);//list多件开关,ALL数组
                    using (HsfDBContext hsfDBContext = new HsfDBContext())
                    {
                        string posid      = "";
                        string cachekey   = "";
                        string devchannel = "";
                        string devtype    = "";
                        string devmac     = "";
                        foreach (var item in deviceLists)
                        {
                            devtype    = item.devtype;
                            devchannel = item.devchannel;
                            posid      = item.devposition;
                            devmac     = item.devmac;
                            cachekey   = item.devmac + "_" + item.devport;//存在mac相同,端口不相同的多键设备

                            var deviceEntity = hsfDBContext.host_device.Where(t => t.cachekey == cachekey && t.deletemark == 0).FirstOrDefault();
                            if (deviceEntity != null)
                            {
                                deviceEntity.deletemark  = 1;
                                deviceEntity.modifiyuser = appUser;
                                deviceEntity.modifiytime = DateTime.Now;
                                AddDeviceEntity(hsfDBContext, item, appUser, account, mac);
                                log.Debug($"{code} OK,重新添加设备成功!");
                            }
                            else
                            {
                                //当前房间id需要保存,网关房间id为0,不可以
                                AddDeviceEntity(hsfDBContext, item, appUser, account, mac);
                            }
                        }
                        //如果设备网关字段不为空,则查询网关最初状态,可能不全部关闭?
                        if (!string.IsNullOrEmpty(devchannel) && devtype.Contains("Zigbee"))
                        {
                            YunZigClient.SendMsg($"{{\"code\" :5001,\"serial\": 11111,\"device\":[{{\"id\": \"{devmac}\"}}],\"zigbee\":\"{devchannel}\"}}");//发送
                        }

                        //清除房间设备列表缓存
                        service.HashDelete("RoomDevices", account + "|" + posid);
                        log.Debug($"清除房间设备列表缓存RoomDevices {account}|{posid}");
                        //清除当前设备类型的设备列表缓存
                        service.HashDelete("TypeDevices", account + "|" + devtype);
                        log.Debug($"清除当前设备类型的设备列表缓存TypeDevices {account}|{devtype}");

                        //2.主机返回app添加成功
                        string msgResult = $"{appUser};{code};ALL;Zip;H4sIAAAAAAAAAHNMScnPBgD0Si5gBQAAAA==$/r$";//拼接
                        log.Debug($"{code} OK,添加设备成功!返回信息:{msgResult}");
                        return(msgResult);
                    }
                }
                else
                {
                    log.Debug($"{code} Fail,添加设备失败,命令不符合规范!");
                    return(null);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }