/// <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); var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, 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.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); } }
/// <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); var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, ChannelCredentials.Insecure); logger.Debug("Device Management Service Address: " + (EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080")); var client = new Manage.ManageClient(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 List <SIPSorcery.SIP.App.SIPAccount> SipAccountStorage_RPCGBServerConfigReceived() { try { string SystemConfigurationServiceAddress = EnvironmentVariables.SystemConfigurationServiceAddress ?? "systemconfigurationservice:8080"; logger.Debug("System Configuration Service Address: " + SystemConfigurationServiceAddress); //var channel = new Channel(SystemConfigurationServiceAddress, ChannelCredentials.Insecure); var channel = GrpcChannel.ForAddress(SystemConfigurationServiceAddress); var client = new Manage.ManageClient(channel); GetIntegratedPlatformConfigRequest req = new GetIntegratedPlatformConfigRequest(); GetIntegratedPlatformConfigResponse rep = client.GetIntegratedPlatformConfig(req); logger.Debug("GetIntegratedPlatformConfigResponse: " + rep.Config.ToString()); GBPlatformConfig item = rep.Config; List <GB28181.SIPSorcery.SIP.App.SIPAccount> _lstSIPAccount = new List <GB28181.SIPSorcery.SIP.App.SIPAccount>(); GB28181.SIPSorcery.SIP.App.SIPAccount obj = new GB28181.SIPSorcery.SIP.App.SIPAccount(); obj.Id = Guid.NewGuid(); //obj.Owner = item.Name; obj.GbVersion = string.IsNullOrEmpty(item.GbVersion) ? "GB-2016" : item.GbVersion; obj.LocalID = string.IsNullOrEmpty(item.LocalID) ? "34020000002000000001" : item.LocalID; obj.LocalIP = HostsEnv.GetRawIP(); obj.LocalPort = string.IsNullOrEmpty(item.LocalPort) ? Convert.ToUInt16(5060) : Convert.ToUInt16(item.LocalPort); obj.RemotePort = string.IsNullOrEmpty(item.RemotePort) ? Convert.ToUInt16(5060) : Convert.ToUInt16(item.RemotePort); obj.Authentication = string.IsNullOrEmpty(item.Authentication) ? false : bool.Parse(item.Authentication); obj.SIPUsername = string.IsNullOrEmpty(item.SIPUsername) ? "admin" : item.SIPUsername; obj.SIPPassword = string.IsNullOrEmpty(item.SIPPassword) ? "123456" : item.SIPPassword; obj.MsgProtocol = System.Net.Sockets.ProtocolType.Udp; obj.StreamProtocol = System.Net.Sockets.ProtocolType.Udp; obj.TcpMode = GB28181.SIPSorcery.Net.RTP.TcpConnectMode.passive; obj.MsgEncode = string.IsNullOrEmpty(item.MsgEncode) ? "GB2312" : item.MsgEncode; obj.PacketOutOrder = string.IsNullOrEmpty(item.PacketOutOrder) ? true : bool.Parse(item.PacketOutOrder); obj.KeepaliveInterval = string.IsNullOrEmpty(item.KeepaliveInterval) ? Convert.ToUInt16(5000) : Convert.ToUInt16(item.KeepaliveInterval); obj.KeepaliveNumber = string.IsNullOrEmpty(item.KeepaliveNumber) ? Convert.ToByte(3) : Convert.ToByte(item.KeepaliveNumber); _lstSIPAccount.Add(obj); logger.Debug("GetIntegratedPlatformConfigResponse SIPAccount: {LocalID:" + obj.LocalID + ", LocalIP:" + obj.LocalIP + ", LocalPort:" + obj.LocalPort + ", RemotePort:" + obj.RemotePort + ", SIPUsername:"******", SIPPassword:"******", KeepaliveInterval:" + obj.KeepaliveInterval + "}"); return(_lstSIPAccount); } catch (Exception ex) { logger.Warn("GetIntegratedPlatformConfigRequest: " + ex.Message); //logger.Debug("Can't get gb info from device-mgr, it will get gb info from config."); return(null); } }
private void _sipRegistrarCore_RPCDmsRegisterReceived(SIPTransaction sipTransaction, GB28181.SIPSorcery.SIP.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 Manage.ManageClient(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); } }
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) }; var channel = GrpcChannel.ForAddress(EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"); //, ChannelCredentials.Insecure); logger.Debug("Device Management Service Address: " + (EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080")); var client = new Manage.ManageClient(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 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); } } }
/// <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 string GBServerChannelAddress = EnvironmentVariables.DeviceManagementServiceAddress ?? "devicemanagementservice:8080"; logger.Debug("Device Management Service Address: " + GBServerChannelAddress); var channel = GrpcChannel.ForAddress(GBServerChannelAddress); var client = new Manage.ManageClient(channel); QueryGBDeviceByGBIDsResponse _rep = new QueryGBDeviceByGBIDsResponse(); QueryGBDeviceByGBIDsRequest 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); } }