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

namespace FLY.Thick.Blowing.Client
{
    public class BlowingServiceClient : FObj, IBlowing
    {
        protected IFConn mConn;
        protected UInt32 mServerID;
17 18
        
        public bool IsConnected { get; set; }
潘栩锋's avatar
潘栩锋 committed
19 20 21 22 23 24

        #region IBlowing 成员变量
        #region 分区设定
        /// <summary>
        /// 加热通道数
        /// </summary>
25 26
        public int ChannelCnt { get; set; } = 44;

潘栩锋's avatar
潘栩锋 committed
27 28 29 30

        /// <summary>
        /// 分区数/加热通道数
        /// </summary>
31
        public int BPC { get; set; } = 2;
潘栩锋's avatar
潘栩锋 committed
32 33 34 35 36


        /// <summary>
        /// 风环螺丝数
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
37 38
        public int NBolts => ChannelCnt * BPC;

潘栩锋's avatar
潘栩锋 committed
39 40


41
        public int OrgBoltNo { get; set; } = 1;
潘栩锋's avatar
潘栩锋 committed
42 43 44 45

        /// <summary>
        /// 使用分区表
        /// </summary>
46 47
        public bool IsUsedMap { get; set; }

潘栩锋's avatar
潘栩锋 committed
48 49 50 51

        /// <summary>
        /// 分区表
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
52
        [PropertyChanged.DoNotCheckEquality]
53 54
        public List<BoltMapCell> Map { get; set; } = new List<BoltMapCell>();

潘栩锋's avatar
潘栩锋 committed
55 56 57 58 59 60 61 62 63 64
        #endregion


        #endregion

        /// <summary>
        /// 每次有新数据时候推送。 修改参数,不会推送
        /// </summary>
        public event RenZiJiaDataEventHandler DataEvent;

65
        public BlowingServiceClient(UInt32 serverid)
潘栩锋's avatar
潘栩锋 committed
66
        {
67
            mServerID = serverid;
潘栩锋's avatar
潘栩锋 committed
68 69 70 71 72
        }

        #region 功能
        public virtual void Apply()
        {
潘栩锋's avatar
潘栩锋 committed
73 74 75 76 77 78 79
            string json = JsonConvert.SerializeObject(new BLOWING_OBJ_INTERFACE.Pack_Params()
            {
                channelcnt = ChannelCnt,
                bpc = BPC,
                orgBolt = OrgBoltNo
            });

潘栩锋's avatar
潘栩锋 committed
80 81 82
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID,
                BLOWING_OBJ_INTERFACE.SET_PARAMS,
潘栩锋's avatar
潘栩锋 committed
83 84 85 86 87 88 89
                Misc.Converter.StringToBytes(json));

            json = JsonConvert.SerializeObject(new BLOWING_OBJ_INTERFACE.Pack_Map()
            {
                IsUsedMap = IsUsedMap,
                Map = Map
            });
潘栩锋's avatar
潘栩锋 committed
90 91 92

            CurrObjSys.SetValueEx(
                mConn, mServerID, ID,
潘栩锋's avatar
潘栩锋 committed
93 94
                BLOWING_OBJ_INTERFACE.SET_MAP, 
                Misc.Converter.StringToBytes(json));
潘栩锋's avatar
潘栩锋 committed
95 96 97 98 99 100
        }

        #endregion


        #region INotifyPropertyChanged 成员
101

潘栩锋's avatar
潘栩锋 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            IsConnected = from.IsConnected;
            if (from.IsConnected)
            {
                //获取所有数据,设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BLOWING_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BLOWING_OBJ_INTERFACE.GET_MAP);

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

            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BLOWING_OBJ_INTERFACE.GET_PARAMS:
                    {
潘栩锋's avatar
潘栩锋 committed
137 138 139
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<BLOWING_OBJ_INTERFACE.Pack_Params>(json);
                        
潘栩锋's avatar
潘栩锋 committed
140 141 142 143 144 145 146
                        ChannelCnt = p.channelcnt;//总分区数,从1开始
                        BPC = p.bpc;
                        OrgBoltNo = p.orgBolt;
                    }
                    break;
                case BLOWING_OBJ_INTERFACE.GET_MAP:
                    {
潘栩锋's avatar
潘栩锋 committed
147 148 149
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BLOWING_OBJ_INTERFACE.Pack_Map>(json);
                        
潘栩锋's avatar
潘栩锋 committed
150 151 152 153 154 155 156 157 158 159 160 161
                        IsUsedMap = p.IsUsedMap;
                        Map = p.Map;
                    }
                    break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case BLOWING_OBJ_INTERFACE.PUSH_DATA:
                    {
潘栩锋's avatar
潘栩锋 committed
162 163 164
                        string json = Misc.Converter.BytesToString(infodata);
                        RenZiJiaDataEventArgs e = Newtonsoft.Json.JsonConvert.DeserializeObject<RenZiJiaDataEventArgs>(json);

潘栩锋's avatar
潘栩锋 committed
165 166 167 168 169 170 171 172 173 174 175 176
                        DataEvent?.Invoke(this, e);
                    }
                    break;
                default:
                    PushGetValue(from, srcid, infoid, infodata);
                    break;
            }
        }

       
    }
}