/// <summary> /// 设备编辑事件 /// </summary> internal void DeviceEditEvent(string DeviceID, string edittype) { try { Event.Event evt = new Event.Event(); evt.EventType = Event.Event.Types.EventType.MediaConfigurationChanged; evt.Detail = "DeviceEditEvent: " + edittype + " " + DeviceID; evt.OccurredTime = (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; string GBServerChannelAddress = EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"; // Channel channel = new Channel(GBServerChannelAddress, ChannelCredentials.Insecure); //devicemanagementservice 是预留的服务标识(暂命名为设备管理服务).目前没有这个服务. //需要你的微服务架构中实现一个设备资产以及一个配置管理服务(或者二合一的资源管服务) //以达到两个目的:1、用来为当前GB服务提供启动配置,2、为GB收到注册的设备/平台信息,提供全平台的统一的存储服务. var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, ChannelCredentials.Insecure); var client = new DevicesManager.DevicesManagerClient(channel); QueryGBDeviceByGBIDsResponse rep = new QueryGBDeviceByGBIDsResponse(); QueryGBDeviceByGBIDsRequest req = new QueryGBDeviceByGBIDsRequest(); req.GbIds.Add(DeviceID); rep = client.QueryGBDeviceByGBIDs(req); if (rep.Devices.Count > 0) { evt.DeviceID = rep.Devices[0].Guid; evt.DeviceName = rep.Devices[0].Name; logger.Debug("DeviceEditEvent: " + edittype + " " + rep.Devices[0].ToString()); } else { logger.Warn("DeviceEditEvent: Devices[" + DeviceID + "] can't be found in database"); return; } Message message = new Message(); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("Content-Type", "application/octet-stream"); message.Header = dic; message.Body = evt.ToByteArray(); byte[] payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); string subject = Event.EventTopic.OriginalEventTopic.ToString();//"OriginalEventTopic" #region Options opts = ConnectionFactory.GetDefaultOptions(); opts.Url = EnvironmentVariables.GBNatsChannelAddress ?? Defaults.Url; using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { c.Publish(subject, payload); c.Flush(); logger.Debug("Device add/update event published."); } #endregion } catch (Exception ex) { logger.Error("DeviceEditEvent Exception: " + ex.Message); } }
private void _sipRegistrarCore_RPCDmsRegisterReceived(SIPTransaction sipTransaction, GB28181.App.SIPAccount sIPAccount) { try { Device _device = new Device(); SIPRequest sipRequest = sipTransaction.TransactionRequest; _device.Guid = Guid.NewGuid().ToString(); _device.IP = sipTransaction.TransactionRequest.RemoteSIPEndPoint.Address.ToString();//IPC _device.Name = "gb" + _device.IP; _device.LoginUser.Add(new LoginUser() { LoginName = sIPAccount.SIPUsername ?? "admin", LoginPwd = sIPAccount.SIPPassword ?? "123456" }); _device.Port = Convert.ToUInt32(sipTransaction.TransactionRequest.RemoteSIPEndPoint.Port); //5060 _device.GBID = sipTransaction.TransactionRequestFrom.URI.User; //42010000001180000184 _device.PtzType = 0; _device.ProtocolType = 0; _device.ShapeType = ShapeType.Dome; //var options = new List<ChannelOption> { new ChannelOption(ChannelOptions.MaxMessageLength, int.MaxValue) }; // var channel = new Channel(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080", ChannelCredentials.Insecure); var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); logger.Debug("Device Management Service Address: " + (EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080")); var client = new DevicesManager.DevicesManagerClient(channel); AddDeviceRequest _AddDeviceRequest = new AddDeviceRequest(); _AddDeviceRequest.Device.Add(_device); _AddDeviceRequest.LoginRoleId = "XXXX"; var reply = client.AddDevice(_AddDeviceRequest); if (reply.Status == OP_RESULT_STATUS.OpSuccess) { logger.Debug("Device[" + sipTransaction.TransactionRequest.RemoteSIPEndPoint + "] have completed registering DMS service."); } else { logger.Error("_sipRegistrarCore_RPCDmsRegisterReceived: " + reply.Status.ToString()); } } catch (Exception ex) { logger.Error("Device[" + sipTransaction.TransactionRequest.RemoteSIPEndPoint + "] register DMS failed: " + ex.Message); } }
/// <summary> /// query device info from db /// </summary> /// <param name="deviceid"></param> /// <returns></returns> private bool IsDeviceExisted(string deviceid) { bool tf = false; //var options = new List<ChannelOption> { new ChannelOption(ChannelOptions.MaxMessageLength, int.MaxValue) }; // Channel channel = new Channel(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080", ChannelCredentials.Insecure); //devicemanagementservice 是预留的服务标识(暂命名为设备管理服务).目前没有这个服务. //需要你的微服务架构中实现一个设备资产以及一个配置管理服务(或者二合一的资源管服务) //以达到两个目的:1、用来为当前GB服务提供启动配置,2、为GB收到注册的设备/平台信息,提供全平台的统一的存储服务. var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, ChannelCredentials.Insecure); logger.Debug("Device Management Service Address: " + (EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080")); var client = new DevicesManager.DevicesManagerClient(channel); QueryGBDeviceByGBIDsResponse rep = new QueryGBDeviceByGBIDsResponse(); QueryGBDeviceByGBIDsRequest req = new QueryGBDeviceByGBIDsRequest(); req.GbIds.Add(deviceid); rep = client.QueryGBDeviceByGBIDs(req); tf = rep.Devices.Count > 0; return(tf); }
private void DeviceDmsRegister(SIPTransaction sipTransaction, string gbname) { try { //Device insert into database Device _device = new Device(); SIPRequest sipRequest = sipTransaction.TransactionRequest; _device.Guid = Guid.NewGuid().ToString(); _device.IP = sipTransaction.TransactionRequest.RemoteSIPEndPoint.Address.ToString();//IPC _device.Name = gbname; _device.LoginUser.Add(new LoginUser() { LoginName = _SIPAccount.SIPUsername ?? "admin", LoginPwd = _SIPAccount.SIPPassword ?? "123456" }); //same to GB config service _device.Port = Convert.ToUInt32(sipTransaction.TransactionRequest.RemoteSIPEndPoint.Port); //5060 _device.GBID = sipTransaction.TransactionRequestFrom.URI.User; //42010000001180000184 _device.PtzType = 0; _device.ProtocolType = 0; _device.ShapeType = ShapeType.Dome; //var options = new List<ChannelOption> { new ChannelOption(ChannelOptions.MaxMessageLength, int.MaxValue) }; //devicemanagementservice 是预留的服务标识(暂命名为设备管理服务).目前没有这个服务. //需要你的微服务架构中实现一个设备资产以及一个配置管理服务(或者二合一的资源管服务) //以达到两个目的:1、用来为当前GB服务提供启动配置,2、为GB收到注册的设备/平台信息,提供全平台的统一的存储服务. var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, ChannelCredentials.Insecure); logger.Debug("Device Management Service Address: " + (EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080")); var client = new DevicesManager.DevicesManagerClient(channel); //if (!_sipCoreMessageService.NodeMonitorService.ContainsKey(_device.GBID)) //{ // AddDeviceRequest _AddDeviceRequest = new AddDeviceRequest(); // _AddDeviceRequest.Device.Add(_device); // _AddDeviceRequest.LoginRoleId = "XXXX"; // var reply = client.AddDevice(_AddDeviceRequest); // if (reply.Status == OP_RESULT_STATUS.OpSuccess) // { // logger.Debug("Device[" + sipTransaction.TransactionRequest.RemoteSIPEndPoint + "] have added registering DMS service."); // DeviceEditEvent(_device.GBID, "add"); // } // else // { // logger.Error("_sipRegistrarCore_RPCDmsRegisterReceived.AddDevice: " + reply.Status.ToString()); // } //} //else //{ // UpdateDeviceRequest _UpdateDeviceRequest = new UpdateDeviceRequest(); // _UpdateDeviceRequest.DeviceItem.Add(_device); // _UpdateDeviceRequest.LoginRoleId = "XXXX"; // var reply = client.UpdateDevice(_UpdateDeviceRequest); // if (reply.Status == OP_RESULT_STATUS.OpSuccess) // { // logger.Debug("Device[" + sipTransaction.TransactionRequest.RemoteSIPEndPoint + "] have updated registering DMS service."); // } // else // { // logger.Error("_sipRegistrarCore_RPCDmsRegisterReceived.UpdateDevice: " + reply.Status.ToString()); // } //} //add & update device AddDeviceRequest _AddDeviceRequest = new AddDeviceRequest(); _AddDeviceRequest.Device.Add(_device); _AddDeviceRequest.LoginRoleId = "XXXX"; var reply = client.AddDevice(_AddDeviceRequest); if (reply.Status == OP_RESULT_STATUS.OpSuccess) { logger.Debug("Device added into DMS service: " + JsonConvert.SerializeObject(_device)); } else { logger.Warn("DeviceDmsRegister.AddDevice: " + reply.Status.ToString()); } } catch (Exception ex) { logger.Error("DeviceDmsRegister Exception: " + ex.Message); } }
/// <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 //devicemanagementservice 是预留的服务标识(暂命名为设备管理服务).目前没有这个服务. //需要你的微服务架构中实现一个设备资产以及一个配置管理服务(或者二合一的资源管服务) //以达到两个目的:1、用来为当前GB服务提供启动配置,2、为GB收到注册的设备/平台信息,提供全平台的统一的存储服务. string GBServerChannelAddress = EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"; var channel = GrpcChannel.ForAddress(GBServerChannelAddress); // ChannelCredentials.Insecure); var client = new DevicesManager.DevicesManagerClient(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); } } }
/// <summary> /// 设备报警 /// </summary> /// <param name="alarm"></param> internal void OnAlarmReceived(Alarm alarm) { try { //logger.Debug("OnAlarmReceived started."); Event.Alarm alm = new Event.Alarm(); alm.AlarmType = alm.AlarmType = Event.Alarm.Types.AlarmType.CrossingLine; //switch (alarm.AlarmMethod) //{ // case "1": // break; // case "2": // alm.AlarmType = Event.Alarm.Types.AlarmType.AlarmOutput; // break; //} alm.Detail = alarm.AlarmDescription ?? string.Empty; //alm.DeviceID = alarm.DeviceID;//dms deviceid //alm.DeviceName = alarm.DeviceID;//dms name //devicemanagementservice 是预留的服务标识(暂命名为设备管理服务).目前没有这个服务. //需要你的微服务架构中实现一个设备资产以及一个配置管理服务(或者二合一的资源管服务) //以达到两个目的:1、用来为当前GB服务提供启动配置,2、为GB收到注册的设备/平台信息,提供全平台的统一的存储服务. string GBServerChannelAddress = EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"; logger.Debug("Device Management Service Address: " + GBServerChannelAddress); var channel = GrpcChannel.ForAddress(GBServerChannelAddress); var client = new DevicesManager.DevicesManagerClient(channel); var _rep = new QueryGBDeviceByGBIDsResponse(); var req = new QueryGBDeviceByGBIDsRequest(); logger.Debug("OnAlarmReceived Alarm: " + JsonConvert.SerializeObject(alarm)); req.GbIds.Add(alarm.DeviceID); _rep = client.QueryGBDeviceByGBIDs(req); if (_rep.Devices != null && _rep.Devices.Count > 0) { alm.DeviceID = _rep.Devices[0].GBID; alm.DeviceName = _rep.Devices[0].Name; } else { logger.Debug("QueryGBDeviceByGBIDsResponse Devices.Count: " + _rep.Devices.Count); } logger.Debug("QueryGBDeviceByGBIDsRequest-Alarm .Devices: " + _rep.Devices[0].ToString()); UInt64 timeStamp = (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; alm.EndTime = timeStamp; alm.StartTime = timeStamp; Message message = new Message(); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("Content-Type", "application/octet-stream"); message.Header = dic; message.Body = alm.ToByteArray(); byte[] payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); string subject = Event.AlarmTopic.OriginalAlarmTopic.ToString();//"OriginalAlarmTopic" #region Options opts = ConnectionFactory.GetDefaultOptions(); opts.Url = EnvironmentVariables.GBNatsChannelAddress ?? Defaults.Url; logger.Debug("GB Nats Channel Address: " + opts.Url); using (IConnection c = new ConnectionFactory().CreateConnection(opts)) { c.Publish(subject, payload); c.Flush(); logger.Debug("Device alarm created connection and published."); } #endregion new Action(() => { logger.Debug("OnAlarmReceived AlarmResponse: " + alm.ToString()); _sipCoreMessageService.NodeMonitorService[alarm.DeviceID].AlarmResponse(alarm); }).Invoke(); } catch (Exception ex) { logger.Error("OnAlarmReceived Exception: " + ex.Message); } }