Ejemplo n.º 1
0
        /// <summary>
        /// 目录查询响应消息处理
        /// </summary>
        /// <param name="localEP">本地终结点</param>
        /// <param name="remoteEP">远程终结点</param>
        /// <param name="request">sip请求</param>
        /// <param name="catalog">目录结构体</param>
        private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog)
        {
            try
            {
                catalog.DeviceList.Items.FindAll(item => item != null).ForEach(catalogItem =>
                {
                    catalogItem.RemoteEP = remoteEP.ToHost();
                    var devCata          = DevType.GetCataType(catalogItem.DeviceID);
                    //logger.Debug("CatalogHandle: DevCataType=" + devCata);
                    if (devCata == DevCataType.Device)
                    {
                        if (!_nodeMonitorService.ContainsKey(catalogItem.DeviceID))
                        {
                            //remoteEP.Port = _LocalSipAccount.RemotePort;
                            _nodeMonitorService.TryAdd(catalogItem.DeviceID, new SIPMonitorCore(this, _transport, _sipAccountStorage)
                            {
                                RemoteEndPoint = remoteEP,
                                DeviceId       = catalogItem.DeviceID
                            });
                            logger.Debug("CatalogHandle: nodeMonitorService.Count=" + _nodeMonitorService.Count);
                            logger.Debug("CatalogHandle: nodeMonitorService.TryAdd DeviceId=" + catalogItem.DeviceID);
                        }
                    }
                    //   CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback;
                });

                OnCatalogReceived?.Invoke(catalog);
            }
            catch (Exception ex)
            {
                logger.Warn("CatalogHandle Exception: " + ex.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 目录查询响应消息处理
        /// </summary>
        /// <param name="localEP">本地终结点</param>
        /// <param name="remoteEP">远程终结点</param>
        /// <param name="request">sip请求</param>
        /// <param name="catalog">目录结构体</param>
        private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog)
        {
            foreach (var cata in catalog.DeviceList.Items)
            {
                if (cata == null)
                {
                    continue;
                }
                cata.RemoteEP = remoteEP.ToHost();
                DevCataType devCata = DevType.GetCataType(cata.DeviceID);
                if (devCata != DevCataType.Device)
                {
                    continue;
                }
                for (int i = 0; i < 2; i++)
                {
                    CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback;
                    MonitorKey  key     = new MonitorKey()
                    {
                        DeviceID = cata.DeviceID,
                        CmdType  = cmdType
                    };
                    lock (MonitorService)
                    {
                        if (MonitorService.ContainsKey(key))
                        {
                            continue;
                        }
                        remoteEP.Port = _account.KeepaliveInterval;
                        ISIPMonitorService monitor = new SIPMonitorCore(this, cata.DeviceID, remoteEP, _account);
                        MonitorService.Add(key, monitor);
                    }
                }
            }

            if (OnCatalogReceived != null)
            {
                OnCatalogReceived(catalog);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 目录查询响应消息处理
        /// </summary>
        /// <param name="localEP">本地终结点</param>
        /// <param name="remoteEP">远程终结点</param>
        /// <param name="request">sip请求</param>
        /// <param name="catalog">目录结构体</param>
        private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog)
        {
            catalog.DeviceList.Items.FindAll(item => item != null).ForEach(catalogItem =>
            {
                catalogItem.RemoteEP = remoteEP.ToHost();
                var devCata          = DevType.GetCataType(catalogItem.DeviceID);
                if (devCata != DevCataType.Device)
                {
                    if (!_nodeMonitorService.ContainsKey(catalogItem.DeviceID))
                    {
                        remoteEP.Port = _LocalSipAccount.KeepaliveInterval;
                        _nodeMonitorService.TryAdd(catalogItem.DeviceID, new SIPMonitorCoreService(this, _transport, _sipAccountStorage)
                        {
                            RemoteEndPoint = remoteEP,
                            DeviceId       = catalogItem.DeviceID
                        });
                    }
                }

                //   CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback;
            });

            OnCatalogReceived?.Invoke(catalog);
        }
Ejemplo n.º 4
0
        public void OnCatalogReceived(Catalog obj)
        {
            if (!Catalogs.ContainsKey(obj.DeviceID))
            {
                Catalogs.Add(obj.DeviceID, obj);
                logger.Debug("OnCatalogReceived: " + JsonConvert.SerializeObject(obj));
            }
            if (GBSIPTransactions.ContainsKey(obj.DeviceID))
            {
                SIPTransaction _SIPTransaction = GBSIPTransactions[obj.DeviceID];
                obj.DeviceList.Items.FindAll(item => item != null).ForEach(catalogItem =>
                {
                    var devCata = DevType.GetCataType(catalogItem.DeviceID);
                    if (devCata == DevCataType.Device)
                    {
                        _SIPTransaction.TransactionRequestFrom.URI.User = catalogItem.DeviceID;
                        string gbname = "GB_" + catalogItem.Name;
                        //string gbname = "gb" + _SIPTransaction.TransactionRequest.RemoteSIPEndPoint.Address.ToString();
                        if (!string.IsNullOrEmpty(catalogItem.ParentID) && !obj.DeviceID.Equals(catalogItem.DeviceID))
                        {
                            gbname = "GB_" + catalogItem.Name;
                        }
                        logger.Debug("OnCatalogReceived.DeviceDmsRegister: catalogItem=" + JsonConvert.SerializeObject(catalogItem));

                        //query device info from db
                        string edit = IsDeviceExisted(catalogItem.DeviceID) ? "updated" : "added";

                        //Device Dms Register
                        DeviceDmsRegister(_SIPTransaction, gbname);

                        //Device Edit Event
                        DeviceEditEvent(catalogItem.DeviceID, edit);
                    }
                });
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 设置设备目录
        /// </summary>
        /// <param name="cata">设备目录</param>
        private void SetDevText(Catalog cata)
        {
            foreach (Catalog.Item item in cata.DeviceList.Items)
            {
                DevCataType  devCata = DevType.GetCataType(item.DeviceID);
                ListViewItem lvItem;
                switch (devCata)
                {
                case DevCataType.UnKnown:
                    break;

                case DevCataType.ProviceCata:
                case DevCataType.CityCata:
                case DevCataType.AreaCata:
                case DevCataType.BasicUnit:
                case DevCataType.SystemCata:
                case DevCataType.BusinessGroupCata:
                case DevCataType.VirtualGroupCata:      //目录类型
                    lvItem          = new ListViewItem(new string[] { item.DeviceID, devCata.ToString(), item.Name });
                    lvItem.ImageKey = item.DeviceID;
                    this.Invoke(new Action(() =>
                    {
                        lvCata.Items.Add(lvItem);
                        //if (devCata == DevCataType.SystemCata)
                        //{
                        //    //_tn.Name = item.Name + "[" + item.DeviceID + "]";
                        //    //_tn.Text = item.Name + "[" + item.DeviceID + "]";
                        //    //_tn.Tag = devCata.ToString();
                        //}
                        //else if (devCata == DevCataType.CityCata)
                        //{
                        //    _tn.Name = item.Name + "[" + item.DeviceID + "]";
                        //    _tn.Text = item.Name + "[" + item.DeviceID + "]";
                        //    _tn.Tag = devCata.ToString();
                        //}
                        //else if (devCata == DevCataType.BusinessGroupCata)
                        //{
                        //    _bg.Name = item.Name + "[" + item.DeviceID + "]";
                        //    _bg.Text = item.Name + "[" + item.DeviceID + "]";
                        //    _bg.Tag = devCata.ToString();

                        //    //_tn.Nodes.Add(_bg);
                        //}
                        //else if (devCata == DevCataType.VirtualGroupCata)
                        //{
                        //    var vg = new TreeNode();
                        //    vg.Name = item.Name + "[" + item.DeviceID + "]";
                        //    vg.Text = item.Name + "[" + item.DeviceID + "]";
                        //    vg.Tag = devCata.ToString();

                        //    _bg.Nodes.Add(vg);
                        //}
                    }));
                    break;

                case DevCataType.Device:        //设备类型
                    lvItem          = new ListViewItem(new string[] { _devSN.ToString(), item.Name, item.Status.ToString() });
                    lvItem.ImageKey = item.DeviceID;
                    _devList.Add(lvItem);
                    this.Invoke(new Action(() =>
                    {
                        lvDev.Items.Add(lvItem);
                    }));
                    _devSN++;
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 设备状态上报
        /// </summary>
        internal void DeviceStatusReport()
        {
            //logger.Debug("DeviceStatusReport started.");
            // var pre = new TimeSpan(DateTime.Now.Ticks);
            while (true)
            {
                //report status every 8 seconds
                System.Threading.Thread.Sleep(8000);
                try
                {
                    foreach (string deviceid in _sipCoreMessageService.NodeMonitorService.Keys)
                    {
                        //if not device then skip
                        if (!DevType.GetCataType(deviceid).Equals(DevCataType.Device))
                        {
                            continue;
                        }

                        Event.Status stat = new Event.Status();
                        stat.Status_      = false;
                        stat.OccurredTime = (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
                        #region waiting DeviceStatuses add in for 500 Milliseconds
                        _sipCoreMessageService.DeviceStateQuery(deviceid);
                        TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);
                        while (true)
                        {
                            System.Threading.Thread.Sleep(100);
                            TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
                            if (DeviceStatuses.ContainsKey(deviceid))
                            {
                                //on line
                                stat.Status_ = DeviceStatuses[deviceid].Status.Equals("ON") ? true : false;
                                //logger.Debug("Device status of [" + deviceid + "]: " + DeviceStatuses[deviceid].Status);
                                DeviceStatuses.Remove(deviceid);
                                break;
                            }
                            else if (t2.Subtract(t1).Duration().Milliseconds > 500)
                            {
                                //off line
                                //logger.Debug("Device status of [" + deviceid + "]: OFF");
                                break;
                            }
                        }
                        #endregion
                        string GBServerChannelAddress = EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080";
                        var    channel = GrpcChannel.ForAddress(GBServerChannelAddress); // ChannelCredentials.Insecure);
                        var    client  = new Manage.ManageClient(channel);
                        QueryGBDeviceByGBIDsResponse rep = new QueryGBDeviceByGBIDsResponse();
                        QueryGBDeviceByGBIDsRequest  req = new QueryGBDeviceByGBIDsRequest();
                        req.GbIds.Add(deviceid);
                        rep = client.QueryGBDeviceByGBIDs(req);
                        if (rep.Devices != null && rep.Devices.Count > 0)
                        {
                            stat.DeviceID   = rep.Devices[0].Guid;
                            stat.DeviceName = rep.Devices[0].Name;

                            Message message = new Message();
                            Dictionary <string, string> dic = new Dictionary <string, string>();
                            dic.Add("Content-Type", "application/octet-stream");
                            message.Header = dic;
                            message.Body   = stat.ToByteArray();
                            byte[] payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
                            string subject = Event.StatusTopic.OriginalStatusTopic.ToString();
                            #region
                            Options opts = ConnectionFactory.GetDefaultOptions();
                            opts.Url            = EnvironmentVariables.GBNatsChannelAddress ?? Defaults.Url;
                            using IConnection c = new ConnectionFactory().CreateConnection(opts);
                            c.Publish(subject, payload);
                            c.Flush();
                            #endregion
                        }
                        else
                        {
                            logger.Debug("QueryGBDeviceByGBIDsRequest: Devices[" + deviceid + "] can't be found in database");
                            continue;
                        }
                        //logger.Debug("QueryGBDeviceByGBIDsRequest-Status .Devices: " + rep.Devices[0].ToString());
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("DeviceStatusReport Exception: " + ex.Message);
                }
            }
        }