BufferServiceClient.cs 6.13 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.OBJComponents.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FLY.OBJComponents.Client
{
17
    public class BufferServiceClient<T> : FObjServiceClient, IBuffer<T>
潘栩锋's avatar
潘栩锋 committed
18 19
    {
        #region property
20
        public int NewestID { get; private set; } = -1;
潘栩锋's avatar
潘栩锋 committed
21 22 23 24 25 26 27 28 29
        public int Count { get; private set; } = 0;
        /// <summary>
        /// 总容量,当总数量 大于等于 总容量的 100%, 前面的10%数据会被删除
        /// </summary>
        public int Capacity { get; set; } = 1000;
        #endregion
        public event NotifyBufferChangedEventHandler<T> BufferChanged;
        public event PropertyChangedEventHandler PropertyChanged;

30
        public BufferServiceClient(UInt32 serviceId) : base(serviceId) { }
潘栩锋's avatar
潘栩锋 committed
31

32 33 34 35 36 37
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceId"></param>
        /// <param name="connName"></param>
        public BufferServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
潘栩锋's avatar
潘栩锋 committed
38 39 40 41


        public override void ConnectNotify(IFConn from)
        {
42
            base.ConnectNotify(from);
潘栩锋's avatar
潘栩锋 committed
43 44 45 46 47 48
            if (from.IsConnected)
            {
                //获取所有数据,设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BUFFER_OBJ_INTERFACE.GET_PARAMS);
潘栩锋's avatar
潘栩锋 committed
49 50 51
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BUFFER_OBJ_INTERFACE.GET_STATUS);
潘栩锋's avatar
潘栩锋 committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff,
                    SENSE_CONFIG.ADD);

                BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.IsConnected
                });
            }
        }

        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {

                case BUFFER_OBJ_INTERFACE.GET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
潘栩锋's avatar
潘栩锋 committed
73 74 75 76 77 78 79 80 81 82
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_Params>(json);
                        Capacity = p.capcity;
                    }
                    break;
                case BUFFER_OBJ_INTERFACE.GET_STATUS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BUFFER_OBJ_INTERFACE.Pack_Status>(json);
                        Count = p.count;
                        NewestID = p.newestId;
潘栩锋's avatar
潘栩锋 committed
83 84 85 86 87 88 89 90 91
                    }
                    break;

            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
潘栩锋's avatar
潘栩锋 committed
92
                case BUFFER_OBJ_INTERFACE.PUSH_STATUS:
潘栩锋's avatar
潘栩锋 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                case BUFFER_OBJ_INTERFACE.PUSH_PARAMS:
                    {
                        PushGetValue(from, srcid, infoid, infodata);
                    }
                    break;
                case BUFFER_OBJ_INTERFACE.PUSH_BUFFERCHANGED:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        NotifyBufferChangedEventArgs<T> p = JsonConvert.DeserializeObject<NotifyBufferChangedEventArgs<T>>(json);
                        BufferChanged?.Invoke(this, p);
                    }
                    break;
            }

        }
108 109

        public override void PushCallFunction(IFConn from, UInt32 srcid, UInt32 magic, UInt16 funcid, byte[] retdata, AsyncCBHandler asyncDelegate, object asyncContext)
潘栩锋's avatar
潘栩锋 committed
110 111 112 113 114 115 116 117
        {
            switch (funcid)
            {
                case BUFFER_OBJ_INTERFACE.CALL_GETRECORD:
                case BUFFER_OBJ_INTERFACE.CALL_GETRECORD_NEWEST:
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        GetRecordReponse<T> p = JsonConvert.DeserializeObject<GetRecordReponse<T>>(json);
118
                        asyncDelegate(asyncContext, p);
潘栩锋's avatar
潘栩锋 committed
119 120 121 122 123 124 125 126 127 128 129 130
                    }
                    break;
            }
        }

        public void Reset()
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_RESET, null);
        }

        public void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
        {
131 132 133 134 135 136
            BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest request = new BUFFER_OBJ_INTERFACE.Pack_GetRecordRequest()
            {
                last_id = last_id,
                count = count
            };

潘栩锋's avatar
潘栩锋 committed
137 138 139 140 141 142 143 144 145
            string json = JsonConvert.SerializeObject(request);

            CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_GETRECORD,
                Misc.Converter.StringToBytes(json),
                asyncCB, asyncContext
                );
        }
        public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
        {
146 147 148 149
            BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest request = new BUFFER_OBJ_INTERFACE.Pack_GetRecordNewestRequest()
            {
                count = count
            };
潘栩锋's avatar
潘栩锋 committed
150 151 152 153 154 155 156
            string json = JsonConvert.SerializeObject(request);

            CurrObjSys.CallFunctionEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.CALL_GETRECORD_NEWEST,
                Misc.Converter.StringToBytes(json),
                asyncCB, asyncContext
                );
        }
潘栩锋's avatar
潘栩锋 committed
157 158 159 160 161 162 163 164 165 166 167
    
        public void Apply()
        {
            var p = new BUFFER_OBJ_INTERFACE.Pack_Params()
            {
                capcity = Capacity
            };
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
            CurrObjSys.SetValueEx(mConn, mServerID, ID, BUFFER_OBJ_INTERFACE.SET_PARAMS,
                Misc.Converter.StringToBytes(json));
        }
潘栩锋's avatar
潘栩锋 committed
168 169
    }
}