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

namespace FLY.OBJComponents.Client
{
    public class PLCProxySystemServiceClient : FObj, IPLCProxySystemService, IPropertyOpt
    {
15
        public Dictionary<string, INotifyPropertyChanged> ObjNames { get; set; }
潘栩锋's avatar
潘栩锋 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

        public bool IsConnected { get; private set; } = false;

        SyncPropServiceClient syncPropServiceClient;
        /// <summary>
        /// 与PLC连接成功
        /// </summary>
        public bool IsConnectedWithPLC { get; set; } = false;

        IFConn mConn;
        UInt32 mServerID;

        public event PropertyChangedEventHandler PropertyChanged;

        public PLCProxySystemServiceClient(UInt32 serverID, Dictionary<string, INotifyPropertyChanged> objnames)
        {
            mServerID = serverID;
            ObjNames = objnames;

            syncPropServiceClient = new SyncPropServiceClient(
                serverID + 1,
                new Dictionary<string, INotifyPropertyChanged> {
                    {".", this }
                });
        }
        public UInt32[] GetIDs()
        {
            return new UInt32[] { ID,
                syncPropServiceClient.ID
            };
        }


        public override void ConnectNotify(IFConn from)
        {
            mConn = from;

            IsConnected = from.IsConnected;
            if (from.IsConnected)
            {
                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        #region IPLCProxySystemService
        public void FeedPlan(long planID)
        {
            PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest pack = new PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest()
            {
                planid = planID
            };
            string json = JsonConvert.SerializeObject(pack);

            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
                PLCOS_OBJ_INTERFACE.CALL_FEED_PLAN, Misc.Converter.StringToBytes(json));
        }
73 74 75 76 77 78 79 80 81 82 83
        public void RemovePlan(long planID)
        {
            PLCOS_OBJ_INTERFACE.Pack_RemovePlanRequest pack = new PLCOS_OBJ_INTERFACE.Pack_RemovePlanRequest()
            {
                planid = planID
            };
            string json = JsonConvert.SerializeObject(pack);

            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
                PLCOS_OBJ_INTERFACE.CALL_REMOVE_PLAN, Misc.Converter.StringToBytes(json));
        }
潘栩锋's avatar
潘栩锋 committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

        public void SetPlan(string objname, IEnumerable<string> propertynames, long planID)
        {
            PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest pack = new PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest()
            {
                objname = objname,
                propertyNames = propertynames,
                planid = planID
            };
            string json = JsonConvert.SerializeObject(pack);

            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
                PLCOS_OBJ_INTERFACE.CALL_SET_PLAN, Misc.Converter.StringToBytes(json));
        }

99 100 101 102 103 104
        /// <summary>
        /// 设置更新计划
        /// </summary>
        /// <param name="objname">对象名</param>
        /// <param name="propertynames">属性名</param>
        /// <param name="planID">计划的编号,应该全局唯一,建议使用时间ticks</param>
105
        public void SetPlan(string objname, IEnumerable<string> propertynames, AsyncCBHandler asyncDelegate, object asyncContext)
106 107 108 109 110 111 112 113
        {
            PLCOS_OBJ_INTERFACE.Pack_SetPlan2Request pack = new PLCOS_OBJ_INTERFACE.Pack_SetPlan2Request()
            {
                objname = objname,
                propertyNames = propertynames,
            };

            string json = JsonConvert.SerializeObject(pack);
潘栩锋's avatar
潘栩锋 committed
114

115
            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
116
                PLCOS_OBJ_INTERFACE.CALL_SET_PLAN2, Misc.Converter.StringToBytes(json), asyncDelegate, asyncContext);
117
        }
潘栩锋's avatar
潘栩锋 committed
118 119 120 121 122
        #endregion

        public string[] GetSyncPropNames()
        {
            return new string[] {
123
                nameof(IsConnectedWithPLC)
潘栩锋's avatar
潘栩锋 committed
124 125 126 127 128 129 130 131
            };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }

132
        public override void PushCallFunction(IFConn from, UInt32 srcid, UInt32 magic, UInt16 funcid, byte[] retdata, AsyncCBHandler asyncDelegate, object asyncContext)
133 134 135 136 137 138 139 140
        {
            switch (funcid)
            {
                case PLCOS_OBJ_INTERFACE.CALL_SET_PLAN2:
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        PLCOS_OBJ_INTERFACE.Pack_SetPlan2Reponse reponse = JsonConvert.DeserializeObject<PLCOS_OBJ_INTERFACE.Pack_SetPlan2Reponse>(json);

141
                        asyncDelegate(asyncContext, reponse.planid);
142 143 144 145 146 147

                    }
                    break;
            }
        }

潘栩锋's avatar
潘栩锋 committed
148 149
    }
}