public void OnDeviceStatusReceived(SIPEndPoint arg1, DeviceStatus arg2) { DeviceStatuses.Remove(arg2.DeviceID); DeviceStatuses.Add(arg2.DeviceID, arg2); }
/// <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); } } }