/// <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); } }
/// <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); }
/// <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); }
/// <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); } }