public static LightSceneEntity GetConfLightingScene(int?deviceID)
        {
            LightSceneEntity lightSceneEntity = new LightSceneEntity();
            var bacnetDeviceFromDB            = lutronEntities.BACnetDevices
                                                .Where(x => x.device_id == deviceID &&
                                                       x.object_instance == (int?)LutronObjectType.Lighting_Scene)
                                                .Select(x => x).FirstOrDefault();


            IList <BacnetValue> bacnetValueList;
            BacnetAddress       bacnetAddress;

            bacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id);

            bacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source,
                                                           (ushort)bacnetDeviceFromDB.routed_net);

            if (bacNetClient == null)
            {
                bacNetClient = BackNetClientInit.NewBackNetClient();
            }
            bacNetClient.ReadPropertyRequest(bacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_MULTI_STATE_VALUE,
                                                                               (uint)LutronObjectType.Lighting_Scene), BacnetPropertyIds.PROP_PRESENT_VALUE, out bacnetValueList);

            if (bacnetValueList != null && bacnetValueList.Count > 0)
            {
                lightSceneEntity.Value = Convert.ToString(bacnetValueList.FirstOrDefault().Value);
            }

            lightSceneEntity.DeviceID = (Int32)deviceID;

            return(lightSceneEntity);
        }
        public static LightStateEntity SetConfLightState(LightStateEntity lightStateEntity)
        {
            var bacnetDeviceFromDB = lutronEntities.BACnetDevices
                                     .Where(x => x.device_id == lightStateEntity.DeviceID &&
                                            x.object_instance == (int?)LutronObjectType.Lighting_State)
                                     .Select(x => x).FirstOrDefault();

            if (bacnetDeviceFromDB != null && bacnetDeviceFromDB.bacnet_device_id > 0)
            {
                BacnetAddress bacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id);
                bacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source,
                                                               (ushort)bacnetDeviceFromDB.routed_net);


                BacnetValue   newLightState      = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, lightStateEntity.LightState == true ? 1 : 0);
                BacnetValue[] writeNewLightState = { newLightState };
                if (bacNetClient == null)
                {
                    bacNetClient = BackNetClientInit.NewBackNetClient();
                }
                bacNetClient.WritePropertyRequest(bacnetAddress,
                                                  new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE, (uint)LutronObjectType.Lighting_State),
                                                  BacnetPropertyIds.PROP_PRESENT_VALUE, writeNewLightState);
            }

            Thread.Sleep(1000);

            return(BackNetReadAction.GetConfLightState(lightStateEntity.DeviceID));
        }
        public static LightStateEntity GetConfLightState(int?deviceID)
        {
            var bacnetDeviceFromDB = lutronEntities.BACnetDevices
                                     .Where(x => x.device_id == deviceID &&
                                            x.object_instance == (int?)LutronObjectType.Lighting_State)
                                     .Select(x => x).FirstOrDefault();


            IList <BacnetValue> loBacnetValueList;
            BacnetAddress       loBacnetAddress;

            loBacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id);

            loBacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source,
                                                             (ushort)bacnetDeviceFromDB.routed_net);
            if (bacNetClient == null)
            {
                bacNetClient = BackNetClientInit.NewBackNetClient();
            }
            bacNetClient.ReadPropertyRequest(loBacnetAddress, new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_VALUE,
                                                                                 (uint)LutronObjectType.Lighting_State), BacnetPropertyIds.PROP_PRESENT_VALUE, out loBacnetValueList);

            if (loBacnetValueList != null && loBacnetValueList.Count > 0)
            {
                return(new LightStateEntity {
                    DeviceID = (Int32)deviceID, LightState = Convert.ToBoolean(loBacnetValueList.FirstOrDefault().Value)
                });
            }
            else
            {
                return(new LightStateEntity {
                    DeviceID = (Int32)deviceID, LightState = null
                });
            }
        }
        public static LightLevelEntity SetConfLightLevel(LightLevelEntity lightLevelEntity)
        {
            var bacnetDeviceFromDB = lutronEntities.BACnetDevices
                                     .Where(x => x.device_id == lightLevelEntity.DeviceID &&
                                            x.object_instance == (int?)LutronObjectType.Lighting_Level)
                                     .Select(x => x).FirstOrDefault();

            if (bacnetDeviceFromDB != null && bacnetDeviceFromDB.bacnet_device_id > 0)
            {
                BacnetAddress bacnetAddress = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.network_id);
                bacnetAddress.RoutedSource = new BacnetAddress(BacnetAddressTypes.IP, bacnetDeviceFromDB.routed_source,
                                                               (ushort)bacnetDeviceFromDB.routed_net);


                BacnetValue   newLightLevel      = new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_REAL, Convert.ToSingle(lightLevelEntity.LightLevel));
                BacnetValue[] writeNewLightLevel = { newLightLevel };
                if (bacNetClient == null)
                {
                    bacNetClient = BackNetClientInit.NewBackNetClient();
                }
                bacNetClient.WritePropertyRequest(bacnetAddress,
                                                  new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_VALUE, (uint)LutronObjectType.Lighting_Level),
                                                  BacnetPropertyIds.PROP_PRESENT_VALUE, writeNewLightLevel);
            }



            return(BackNetReadAction.GetConfLightLevel(lightLevelEntity.DeviceID));
        }
        /// <summary>
        /// Adds bacnet device details.
        /// </summary>
        private static void AddBackNetDeviceDetail()
        {
            if (bacNetDeviceModel != null && bacNetDeviceModel.BACnetDeviceList != null)
            {
                List <BACnetDevice>        bACnetDeviceLst        = new List <BACnetDevice>();
                List <BACnetDeviceMapping> bACnetDeviceMappingLst = new List <BACnetDeviceMapping>();
                foreach (var deviceDetail in bacNetDeviceModel.BACnetDeviceList)
                {
                    IList <BacnetValue> objValueLst;
                    if (bacNetClient == null)
                    {
                        bacNetClient = BackNetClientInit.NewBackNetClient();
                    }
                    bacNetClient.ReadPropertyRequest(deviceDetail.BacNetAddress,
                                                     new BacnetObjectId(BacnetObjectTypes.OBJECT_DEVICE, deviceDetail.DeviceId),
                                                     BacnetPropertyIds.PROP_OBJECT_LIST, out objValueLst);
                    if (objValueLst == null)
                    {
                        continue;
                    }
                    foreach (var objValue in objValueLst)
                    {
                        var isExistNetworkId = CheckIfExistNetworkAddress(deviceDetail.BacNetAddress.ToString(),
                                                                          (int)((BacnetObjectId)objValue.Value).Instance, (int)deviceDetail.DeviceId
                                                                          , ((BacnetObjectId)objValue.Value).Type.ToString());
                        if (isExistNetworkId == true)
                        {
                            continue;
                        }
                        IList <BacnetValue> objNameList;
                        bacNetClient.ReadPropertyRequest(deviceDetail.BacNetAddress,
                                                         new BacnetObjectId((BacnetObjectTypes)((BacnetObjectId)objValue.Value).Type,
                                                                            ((BacnetObjectId)objValue.Value).Instance),
                                                         BacnetPropertyIds.PROP_OBJECT_NAME, out objNameList);
                        if (deviceDetail.BacNetAddress.RoutedSource != null && deviceDetail.BacNetAddress.RoutedSource.net != null)
                        {
                            var bacNetdevice = new BACnetDevice
                            {
                                device_id       = Convert.ToInt32(deviceDetail.DeviceId),
                                network_id      = deviceDetail.BacNetAddress.ToString(),
                                object_type     = ((BacnetObjectId)objValue.Value).type.ToString(),
                                object_instance = Convert.ToInt32(((BacnetObjectId)objValue.Value).Instance.ToString()),
                                object_name     = objNameList != null && objNameList.Count > 0 ? objNameList[0].Value.ToString() : null,
                                routed_source   = deviceDetail.BacNetAddress.RoutedSource.ToString(),
                                routed_net      = deviceDetail.BacNetAddress.RoutedSource.net
                            };
                            bACnetDeviceLst.Add(bacNetdevice);
                        }
                        int?suiteID = null, roomID = null;
                        var objName = Enum.GetName(typeof(LutronFloorObjectType), LutronFloorObjectType.OBJECT_ANALOG_VALUE).ToString();
                        if (((BacnetObjectId)objValue.Value).type.ToString().ToUpper() == objName)
                        {
                            if (Convert.ToInt32(((BacnetObjectId)objValue.Value).Instance.ToString()) < 4)
                            {
                                suiteID = 1;
                            }
                            else
                            {
                                suiteID = 2;
                            }

                            roomID = Convert.ToInt32(((BacnetObjectId)objValue.Value).Instance.ToString());
                        }
                        var bACnetDeviceMapping = new BACnetDeviceMapping
                        {
                            device_id       = Convert.ToInt32(deviceDetail.DeviceId),
                            floor_id        = Convert.ToInt32(deviceDetail.DeviceId),
                            suite_id        = suiteID,
                            room_id         = roomID,
                            object_instance = Convert.ToInt32(((BacnetObjectId)objValue.Value).Instance.ToString())
                        };
                        bACnetDeviceMappingLst.Add(bACnetDeviceMapping);
                    }
                }

                if (bACnetDeviceLst.Count() > 0)
                {
                    AddBacNetDeviceDetail(bACnetDeviceLst);
                }
                if (bACnetDeviceMappingLst.Count() > 0)
                {
                    AddBacNetMappingDetail(bACnetDeviceMappingLst);
                }
            }
        }