.net core dbus 通讯

由于.netcore 无法在linux 下面写界面,包括新出的3.0 也是没有linux 下做界面,本来准备用qt 写界面 通过http api 通讯请求,初步测试虽然通过感觉本地交互使用此种方式还是效率不高,于是想起之前的dbus通讯,

研究.net core 使用dbus 最终发现 tmds.dbus

地址  https://github.com/tmds/Tmds.DBus

不过文档比较匮乏只能通过别人的提问获取答案研究了两天写出 使用.net core 创建服务 与广播信号

 public static class TestPosService
    {
        public static async Task TestAsync()
        {
            string dbusAddress = "tcp:host=localhost,port=54321,family=ipv4";
            if (!string.IsNullOrEmpty(Address.System))
            {
                dbusAddress = Address.System;
            }
            using (var connection = new Connection(dbusAddress))
            {
                await connection.ConnectAsync();
                var PosDbus = new PosDbusOperations();
                await connection.RegisterObjectAsync(PosDbus);

                await connection.RegisterServiceAsync("scpos.pos.system");
                // Console.ReadLine();

                while (true)
                {
                    // await Task.Delay(int.MaxValue);
                    var datetime = DateTime.Now;
                    await PosDbus.PingAsync(datetime.ToString());
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
            }
        }

    }

    class PosDbusOperations : IStringOperations
    {
        
        public static readonly ObjectPath Path = new ObjectPath("/");
        private ObjectPath _path;
        public PosDbusOperations()
        {
            _path = Path;
        }
        public PosDbusOperations(ObjectPath path)
        {
            _path = path;
        }
        public Task<string> ConcatAsync(string s1, string s2)
        {
            Console.WriteLine("In ConcatAsync!{0},{1}", s1, s2);

            return Task.FromResult($"{s1}{s2}");
        }
        

        /// <summary>
        /// 用于触发测试信号
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public Task PingAsync(string message)
        {
            Message?.Invoke(message);
            MessageNoArg?.Invoke();
            return Task.CompletedTask;
        }


        public event Action<string> Message;
        public event Action MessageNoArg;
        /// <summary>
        /// 一个参数信号
        /// </summary>
        /// <param name="reply"></param>
        /// <returns></returns>
        public Task<IDisposable> WatchMytestsignalAsync(Action<string> reply)
        {
            return SignalWatcher.AddAsync(this, nameof(Message), reply);
        }
        /// <summary>
        /// 没有参数的信号
        /// </summary>
        /// <param name="reply"></param>
        /// <returns></returns>
        public Task<IDisposable> WatchMytestsignalNoArgAsync(Action reply)
        {
            return SignalWatcher.AddAsync(this, nameof(MessageNoArg), reply);
        }

        #region 内部声明
        public ObjectPath ObjectPath { get { return _path; } }


        #endregion
    }
    /// <summary>
    /// method
    /// </summary>
    [DBusInterface("pos.server")]
    public interface IStringOperations : IDBusObject
    {
        
        Task<string> ConcatAsync(string s1, string s2);
        /// <summary>
        /// 信号测试
        /// </summary>
        /// <param name="reply"></param>
        /// <returns></returns>
        Task<IDisposable> WatchMytestsignalAsync(Action<string> reply);
        Task<IDisposable> WatchMytestsignalNoArgAsync(Action reply);

    }

Qt 接收信号

 QDBusConnection::sessionBus().connect(QString(), QString(), "pos.server", "Mytestsignal", this, SLOT(testslot(QString)));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值