예제 #1
0
        /// <summary>
        /// 设备发现方法
        /// </summary>
        /// <param name="commonModel"></param>
        /// <param name="tokenModel"></param>
        /// <returns></returns>
        private object Discove(CommonReceiveModel commonModel, IntelligentHostCacheModel tokenModel)
        {
            //IntelligentHostCacheModel cacheModel = CacheData.GetAccessTokenCache(receiveObj.payload.accessToken);
            //CacheData.SetOpenUid(cacheModel.AccessToken, cacheModel, commonModel.payload.openUid);
            try
            {
                using (RedisHashService service = new RedisHashService())
                {
                    string hostId  = tokenModel.HostId;//账号
                    string openUid = commonModel.payload.openUid;
                    //设置设备与OpenUid的绑定关系
                    service.SetEntryInHash("DuerOSOpenUid_Host", hostId, openUid);
                    //拼装返回消息格式
                    DiscoveReturnModel resModel = new DiscoveReturnModel()
                    {
                        header = new Dueros.Models.CommonModel.Head()
                        {
                            messageId      = commonModel.header.messageId,
                            name           = commonModel.header.name.Replace("Request", "Response"),
                            @namespace     = commonModel.header.@namespace,
                            payloadVersion = commonModel.header.payloadVersion
                        },
                        payload = new DiscoverPayload()
                        {
                            discoveredGroups     = new List <DiscoveredGroup>(),
                            discoveredAppliances = new List <DiscoveredAppliance>()
                        }
                    };

                    //获取当前百度账号的设备列表,先找缓存
                    string discoverPayloadJson = service.GetValueFromHash("DuerOS_DiscoverPayload", hostId);

                    if (!string.IsNullOrEmpty(discoverPayloadJson))
                    {
                        resModel.payload = JsonConvert.DeserializeObject <DiscoverPayload>(discoverPayloadJson);
                    }
                    else
                    {
                        List <host_room>   roomList       = null; //房间区域列表
                        List <host_device> roomDeviceList = null; //区域设备列表

                        //1.找当前账号的房间列表区域
                        string roomListJson = service.GetValueFromHash("Room", hostId);
                        if (!string.IsNullOrEmpty(roomListJson))
                        {
                            roomList = JsonConvert.DeserializeObject <List <host_room> >(roomListJson);
                        }
                        else
                        {
                            using (HsfDBContext hsfDBContext = new HsfDBContext())
                            {
                                //默认房间为0,查询当前账号所有设备列表
                                roomList = hsfDBContext.host_room.Where(t => t.Account == hostId && t.DeleteMark == 0).OrderBy(t => t.CreateTime).ToList();

                                //缓存当前房间的设备列表,不包括状态,不管空与否都缓存,防止第二次还查数据库RoomDevices
                                service.SetEntryInHash("Room", hostId, JsonConvert.SerializeObject(roomList));
                            }
                        }
                        //2.组装区域列表
                        if (roomList.Count > 0)
                        {
                            //房间列表不为空,则拼装区域json
                            foreach (var item in roomList)
                            {
                                //获取当前区域的设备列表
                                roomDeviceList = GetDeviceListByRoom(service, hostId, item.posid);
                                if (roomDeviceList != null)
                                {
                                    List <string> applianceIds = new List <string>();
                                    foreach (var dev in roomDeviceList)
                                    {
                                        applianceIds.Add(dev.cachekey);
                                    }

                                    DiscoveredGroup discoveredGroup = new DiscoveredGroup()
                                    {
                                        groupName              = item.chinaname,
                                        applianceIds           = applianceIds,
                                        groupNotes             = item.chinaname + "分组控制",
                                        additionalGroupDetails = { }
                                    };
                                    resModel.payload.discoveredGroups.Add(discoveredGroup);
                                }
                            }
                        }

                        //调用逻辑获取缓存中0房间所有的设备
                        List <host_device> allDeviceList = GetDeviceListByRoom(service, hostId, "0");
                        foreach (var item in allDeviceList)
                        {
                            //类型对照表
                            List <string> applianceTypes = null;
                            List <string> actions        = null;
                            //根据对照,获取百度的设备类型type,操作action
                            GetDuerOSDveType(item.devtype, out applianceTypes, out actions);

                            //拼装发给百度的设备信息
                            if (applianceTypes != null && actions != null)
                            {
                                DiscoveredAppliance discoveredAppliance = new DiscoveredAppliance()
                                {
                                    applianceTypes             = applianceTypes,
                                    applianceId                = item.cachekey,//不使用id,使用mac+port
                                    modelName                  = item.devtype,
                                    version                    = "1.0",
                                    friendlyName               = item.chinaname,
                                    friendlyDescription        = "",
                                    isReachable                = true,
                                    actions                    = actions,
                                    additionalApplianceDetails = { },
                                    manufacturerName           = "DASKLIMA"
                                };
                                //以对象数组返回客户关联设备云帐户的设备、场景
                                resModel.payload.discoveredAppliances.Add(discoveredAppliance);
                                //设置设备与OpenUid的绑定关系
                                service.SetEntryInHash("DuerOSOpenUid_Device", item.cachekey, openUid);
                            }
                        }
                        //场景处理,后续

                        //缓存百度设备列表payload,不包含状态
                        service.SetEntryInHash("DuerOS_DiscoverPayload", hostId, JsonConvert.SerializeObject(resModel.payload));
                    }

                    //设备状态详情信息,都要执行,因为缓存的DuerOS_DiscoverPayload不存状态
                    foreach (var item in resModel.payload.discoveredAppliances)
                    {
                        List <AttributesItem> attributes = new List <AttributesItem>();
                        //获取当前缓存状态
                        AttributesItem attribute = GetStateByCachekey(service, item.applianceId);
                        attributes.Add(attribute);
                        item.attributes = attributes;
                    }

                    return(resModel);
                }
            }
            catch (Exception ex)
            {
                FunctionHelper.writeLog("GetDevice error:", ex.Message + "   " + ex.StackTrace, "DeviceDiscoverHandleError");
                return(null);
            }
        }
예제 #2
0
 public abstract object ProcessHandel(CommonReceiveModel receiveObj);