Esempio n. 1
0
        private static List <IOTOI.Model.ZigBee.ZigBeeEndDevice> GetEndDevices()
        {
            List <IOTOI.Model.ZigBee.ZigBeeEndDevice> result = null;

            using (var db = new IOTOI.Model.Db.Context())
            {
                try
                {
                    result = db.ZigBeeEndDevice
                             .Include(b => b.EndPoints)
                             .ThenInclude(ba => ba.ZigBeeInClusters)
                             .ThenInclude(bc => bc.ZigBeeInClusterAttributes)
                             .Include(b => b.EndPoints)
                             .ThenInclude(ba => ba.ZigBeeOutClusters)
                             .ThenInclude(bc => bc.ZigBeeOutClusterAttributes)
                             .ToList();
                }
                catch (Exception e)
                {
                    loggingServices.Write(Log, e.Message, LogLevel.Error, e);
                }

                return(result);
            }
        }
Esempio n. 2
0
        public static bool GetEndPointStatus(string target)
        {
            bool rtn = false;

            byte[] tmp = null;
            using (var db = new IOTOI.Model.Db.Context())
            {
                try
                {
                    tmp = (
                        from ep in db.ZigBeeEndPoint
                        join zc in db.ZigBeeInCluster on ep.Id equals zc.ParentId
                        join zca in db.ZigBeeInClusterAttribute on zc.Id equals zca.ParentId
                        where (ep.CustomName != null && ep.CustomName.ToUpper().Replace(" ", "") == target.ToUpper().Replace(" ", "") && zca.ZigBeeType == 16)
                        select zca.AttrValue).FirstOrDefault();

                    rtn = BitConverter.ToBoolean(tmp, 0);
                }
                catch (Exception e)
                {
                    loggingServices.Write(Log, e.Message, LogLevel.Error, e);
                }
            }

            return(rtn);
        }
Esempio n. 3
0
        //추후 코타나에서 사용할때 이용할 예정
        public static Model.ZigBee.ZigBeeEndPoint GetEndPoint(string target)
        {
            //int id = 0;
            Model.ZigBee.ZigBeeEndPoint endPoint = null;
            using (var db = new IOTOI.Model.Db.Context())
            {
                try
                {
                    //id = (from ep in db.ZigBeeEndPoint
                    //      where (ep.CustomName).ToUpper().Replace(" ", "") == target.ToUpper().Replace(" ", "")
                    //      select ep.Id)
                    //     .FirstOrDefault();
                    endPoint = db.ZigBeeEndPoint
                               .Where(
                        b => b.CustomName != null && b.CustomName.ToUpper().Replace(" ", "").Equals(target.ToUpper().Replace(" ", ""))
                        ).AsNoTracking()
                               .FirstOrDefault();
                }
                catch (Exception e)
                {
                    loggingServices.Write(Log, e.Message, LogLevel.Error, e);
                }
            }

            //return id;

            return(endPoint);
        }
Esempio n. 4
0
        private static void InitTable()
        {
            using (var db = new IOTOI.Model.Db.Context())
            {
                try
                {
                    List <Model.ZigBee.ZigBeeEndDevice> ZigBeeEndDeviceList = db.ZigBeeEndDevice.ToList();

                    //result = db.ZigBeeEndDevice
                    //    .Include(b => b.EndPoints)
                    //        .ThenInclude(ba => ba.ZigBeeInClusters)

                    foreach (Model.ZigBee.ZigBeeEndDevice device in ZigBeeEndDeviceList)
                    {
                        ZigBeeEndDeviceList[ZigBeeEndDeviceList.IndexOf(device)].IsConnected = false;
                    }

                    List <Model.ZigBee.ZigBeeEndPoint> ZigBeeEndPointList = db.ZigBeeEndPoint.ToList();
                    foreach (Model.ZigBee.ZigBeeEndPoint endPoint in ZigBeeEndPointList)
                    {
                        ZigBeeEndPointList[ZigBeeEndPointList.IndexOf(endPoint)].IsActivated = false;
                    }

                    if (ZigBeeEndDeviceList.Count > 0)
                    {
                        db.ZigBeeEndDevice.UpdateRange(ZigBeeEndDeviceList);
                        db.ZigBeeEndPoint.UpdateRange(ZigBeeEndPointList);
                        db.SaveChanges();
                    }
                    //db.ZigBeeEndDevice.RemoveRange(db.ZigBeeEndDevice.ToList());
                    //db.SaveChanges();
                }
                catch (Exception e)
                {
                    loggingServices.Write(Log, e.Message, LogLevel.Error, e);
                }
            }
        }
Esempio n. 5
0
        private static List <string> SetEndDevices(List <ValueSet> list)
        {
            List <string> rtn = null;

            using (var db = new IOTOI.Model.Db.Context())
            {
                foreach (ValueSet vs in list)
                {
                    var result = db.ZigBeeEndPoint.SingleOrDefault(e => e.Id == (int)vs["Id"]);
                    if (result != null)
                    {
                        result.CustomName = (string)vs["Name"];
                    }
                }
                db.SaveChanges();

                rtn = (from ep in db.ZigBeeEndPoint
                       where ep.CustomName != null
                       select ep.CustomName)
                      .ToList();
            }
            return(rtn);
        }
Esempio n. 6
0
        /// <summary>
        /// Called by the <see cref="Watcher"/> when events specific to this instance occur
        /// </summary>
        /// <param name="notification"></param>
        internal void HandleNodeEvent(ZWNotification notification)
        {
            switch (notification.Type)
            {
            // NodeProtocolInfo: We now know what type of node it is
            case ZWNotificationType.NodeProtocolInfo:
            {
                // Basic node information has been received, such as whether the node is a listening
                // device, a routing device and its baud rate and basic, generic and specific types.
                // It is after this notification that you can call get the Label containing the device description.
                OnPropertyChanged(nameof(Label));

                // DB에 쓰기
                Debug.WriteLine("NodeId =[" + ID + "]");
                Debug.WriteLine("HomeId =[" + HomeID + "]");
                Debug.WriteLine("Label =[" + Label + "]");
                Debug.WriteLine("Name =[" + Name + "]");
                Debug.WriteLine("Manufacturer =[" + Manufacturer + "]");
                Debug.WriteLine("Product =[" + Product + "]");
                Debug.WriteLine("Location =[" + Location + "]");

                //// 1. 모델객체 세팅
                //CommaxIot.Model.ZWave.ZWaveNode zWaveNode = new
                //// 2. DB 쿼리
                using (var db = new IOTOI.Model.Db.Context())
                {
                    try
                    {
                        IOTOI.Model.ZWave.ZWaveNode nodeModel = null;

                        nodeModel = db.ZWaveNode.Find(Convert.ToInt32(ID));

                        if (nodeModel == null)
                        {
                            nodeModel              = new IOTOI.Model.ZWave.ZWaveNode();
                            nodeModel.Id           = Convert.ToInt32(ID);
                            nodeModel.HomeId       = HomeID;
                            nodeModel.Label        = Label;
                            nodeModel.Name         = Name;
                            nodeModel.Manufacturer = Manufacturer;
                            nodeModel.Product      = Product;
                            nodeModel.Location     = Location;

                            db.ZWaveNode.Add(nodeModel);
                        }
                        else
                        {
                            db.ZWaveNode.Update(nodeModel);
                        }
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                }
                //// 3. upsert gogo

                Debug.WriteLine($"ZWNotificationType.NodeProtocolInfo OnPropertyChanged :: {nameof(Label)}");
                break;
            }

            case ZWNotificationType.NodeNaming:
            {
                // One of the node names has changed (name, manufacturer, product)
                OnPropertyChanged(nameof(Name));
                OnPropertyChanged(nameof(Manufacturer));
                OnPropertyChanged(nameof(Product));
                OnPropertyChanged(nameof(ManufacturerAndProduct));
                OnPropertyChanged(nameof(Location));
                break;
            }

            case ZWNotificationType.EssentialNodeQueriesComplete:
            {
                // The queries on a node that are essential to its operation have been completed.
                // The node can now handle incoming messages.
                EssentialNodeQueriesComplete = true;
                OnPropertyChanged(nameof(EssentialNodeQueriesComplete));

                Debug.WriteLine($"ZWNotificationType.EssentialNodeQueriesComplete OnPropertyChanged :: {nameof(EssentialNodeQueriesComplete)}");
                break;
            }

            case ZWNotificationType.NodeQueriesComplete:
            {
                // All the initialization queries on a node have been completed.
                NodeQueriesComplete = true;
                OnPropertyChanged(nameof(NodeQueriesComplete));

                Debug.WriteLine($"ZWNotificationType.NodeQueriesComplete OnPropertyChanged :: {nameof(NodeQueriesComplete)}");
                break;
            }

            case ZWNotificationType.ValueAdded:
            case ZWNotificationType.ValueChanged:
            {
                // Added: A new node value has been added to OpenZWave's list. These notifications occur
                // after a node has been discovered, and details of its command classes have been
                // received. Each command class may generate one or more values depending on the
                // complexity of the item being represented.
                // Changed: A node value has been updated from the Z-Wave network and it is different from
                // the previous value.
                var value = notification.ValueId;
                //Debug.WriteLine($"ZWNotificationType.ValueChanged notification.ValueId :: AddValue {value}");
                AddValue(value);
                Debug.WriteLine($"{notification.Type}. Node {ID}: {ZWManager.Instance.GetValueLabel(value)} = {GetValue(value)} {ZWManager.Instance.GetValueUnits(value)}");
                break;
            }

            case ZWNotificationType.ValueRemoved:
            {
                // A node value has been removed from OpenZWave's list. This only occurs when a
                // node is removed.
                // Note to self: We probably don't need to handle this, since the node would have been
                // removed at this point
                RemoveValue(notification.ValueId);

                Debug.WriteLine($"ZWNotificationType.ValueRemoved RemoveValue :: {notification.ValueId}");
                break;
            }

            case ZWNotificationType.Group:
            {
                // The associations for the node have changed.The application should rebuild any
                // group information it holds about the node.
                break;
            }

            case ZWNotificationType.Notification:     //An error has occurred that we need to report.
            {
                Debug.WriteLine($"******Node error '{notification.Code}' @ ID: {ID}");
                var code = notification.Code;
                var v    = GetValue(notification.ValueId);
                Debug.WriteLine($"code '{notification.Code}' @ ID: {notification.ValueId}' value = {v}");
                break;
            }

            case ZWNotificationType.NodeEvent:     // A node has triggered an event. This is commonly caused when a node sends a Basic_Set command to the controller. The event value is stored in the notification.
            {
                var value = GetValue(notification.ValueId);
                Debug.WriteLine($"******Node Event @ ID: Value = {value}");


                break;
            }

            default:
            {
                Debug.WriteLine($"******Notification '{notification.Type}' not Handled @ ID: {ID}");
                break;
            }
            }
        }
Esempio n. 7
0
        private static void GetReturnData(ValueSet message, ref ValueSet returnData)
        {
            string command = message["Command"] as string;

            switch (command.ToUpper())
            {
            case "INIT":
            {
                Hashtable result = CommonInit();
                string    status = "OK";
                if (!(bool)result[TypeZigBee])
                {
                    status = "\nFail to init ZigBee";
                }
                returnData.Add("Status", status);
            }
            break;

            case "GETPHRASELIST":
            {
                List <string> result = null;
                try
                {
                    using (var db = new IOTOI.Model.Db.Context())
                    {
                        result = (from ep in db.ZigBeeEndPoint
                                  where ep.CustomName != null
                                  select ep.CustomName)
                                 .ToList();
                    }
                }
                catch (Exception e)
                {
                    loggingServices.Write(Log, e.Message, LogLevel.Error, e);
                }
                returnData.Add("Result", JsonConvert.SerializeObject(result));
                returnData.Add("Status", "OK");
            }
            break;

            case "GETDEVICE":
            {
                returnData.Add("Result", JsonConvert.SerializeObject(ZigBeeService.GetEndPoint(message["Target"] as string)));
                returnData.Add("Status", "OK");
            }
            break;

            case "GETSTATUS":
            {
                returnData.Add("Result", ZigBeeService.GetEndPointStatus(message["Target"] as string));
                returnData.Add("Status", "OK");
            }
            break;

            case "SETNESTTOKEN":
            {
                strNestToken = message["NESTTOKEN"].ToString();

                Nest.NestCommandHandler.ThermostatAPI.ApplyAccessToken(strNestToken);

                returnData.Add("Status", "OK");
            }
            break;

            case "GETNESTSTATUS":
            {
                //TODO : NEST GET STATUS
                //Result CASE 1(temperature : 60)
                //Result CASE 2(fan : true or false)

                #region
                if (message["Target"].ToString() == "temperature")
                {
                    string r = Nest.NestCommandHandler.CurrentTemperature();
                    if (r.Contains("Fail:"))
                    {
                        returnData.Add("Status", r);
                    }
                    else
                    {
                        returnData.Add("Result", r);
                        returnData.Add("Status", "OK");
                    }
                }
                else
                if (message["Target"].ToString() == "fan")
                {
                    string r = Nest.NestCommandHandler.CurrentFanStatus();
                    if (r.Contains("Fail:"))
                    {
                        returnData.Add("Status", r);
                    }
                    else
                    {
                        returnData.Add("Result", r);            // FanRunning , FanStopped
                        returnData.Add("Status", "OK");
                    }
                }
                #endregion
            }
            break;

            default:
            {
                returnData.Add("Status", "Fail: unknown command");
                break;
            }
            }
        }