/// <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;
            }
        }
        /// <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;
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            try
            {
                //DuerOSClient.PutDeviceChangeQueue("MMSJ-1-1-1-102");
                //using (RedisListService service = new RedisListService())
                //{
                //    service.Publish("LotDeviceChangeQueue", "MMSJ-1-1-1-102");
                //}
                //using (RedisListService service = new RedisListService())
                //{
                //    service.RPush("BaiDuSoundControl1", "02ea45da-bf64-4ab2-a913-6ff5335f7c7d$" + true);
                //    service.Publish("YunZigStateChangeQueue1", "010000124b000f81f8c5_8");
                //}

                //using (RedisStringService service = new RedisStringService())
                //{
                //    service.StringSet("RedisStringService_key1", "RedisStringService_value1");
                //}
                //RedisHashService service = new RedisHashService();
                //    service.HashSet("Hsf", "Room1", "asdfjdkjas");
                //    string msgResult0 = service.HashGet("Room", "123");
                //    string msgResult1 = service.HashGet("RoomDevices", "123|0115170616344");
                //string msgResult2 = service.HashGet("AccountDevices", "123");

                //未找到具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序的实体框架提供程序
                //使用到数据库的都要加载ef,mysql.data/entity,6.9.12
                //mysql -connector-net-8.0.13可以向下兼容
                //using (HsfDBContext hsfDBContext = new HsfDBContext())
                //{
                //    var deviceList = hsfDBContext.host_device.Where(t => t.devposition == "123" && t.deletemark == 0).ToList();
                //}

                //SmartHomeHost.SendJoin("18660996839");
                //string dd=SmartHomeHost.ValidateSmsCode("18660996839", "767742");

                //using (DaHuaService.MobPhoneServiceClient client = new DaHuaService.MobPhoneServiceClient())
                //{
                //    //{"status":"1","resultMessage":"信息发送成功"}
                //    //{"status":"0","resultMessage":"设备不存在"}
                //    //var result2 = client.openDoor("{\"deviceCode\":\"1000001\"} ");
                //    //var result3 = client.getRoomNumByPhone("1000033");
                //    string sendmsg = "{\"deviceCode\":\"1000001\"}";
                //    string result1 = client.openDoor(sendmsg);
                //}
                //string a = "qazwsx123";
                //byte[] b = System.Text.Encoding.Default.GetBytes(a);
                ////转成 Base64 形式的 System.String
                //a = Convert.ToBase64String(b);

                //string mac16 = RYZigClient.ToHex("192.168.82.107;f1;06|cc dd f1 06 05 00 08 00 01 8c 7f", "utf-8", false);

                //string ss = RYZigClient.UnHex(mac16, "utf-8");

                //string mac16 = RYZigClient.ToHex("cc dd F1 05 05 00 08 01 01 8d dc|58.57.32.162;F1;05", "utf-8", false);
                //string strDataStr = RYZigClient.UnHex("63 63 20 64 64 20 ", "utf-8");

                //using (RedisHashService service = new RedisHashService())
                //{
                //    string st = service.GetValueFromHash("DeviceStatus", "58.57.32.162;F1;01_0;1");
                //    service.RemoveEntryFromHash("DeviceStatus", "58.57.32.162;F1;01_0;1");
                //    service.SetEntryInHash("DeviceStatus", "58.57.32.162;F1;04_0;1", st);
                //}
                //DaHuaDeviceService.EntranceGuardSendMsg("");
                //DaHuaDeviceService.EntranceGuardSendMsg("1000001$7$0$0");
                //DaHuaDeviceService.EntranceGuardSendMsg("1000000");
                //string dd= PingYinHelper.GetFirstSpell("名门世家");//MMSJ


                YunZigClient.ReConnect();//9004zige
                //RYZigClient.ReConnect();//50000

                SuperSocketMain.Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }