FilmPositionDetect_OBJProxy.cs 5.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 137 138 139 140 141 142 143 144 145
using AutoMapper;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class FilmPositionDetect_OBJProxy : FObj
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<IFilmPositionDetectService, FILMPOS_OBJ_INTERFACE.Pack_Params>();
            c.CreateMap<IFilmPositionDetectService, FILMPOS_OBJ_INTERFACE.Pack_State>();
        }));

        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_STATE = 2;
        #endregion

        IFilmPositionDetectService filmPositionDetectService;

        public FilmPositionDetect_OBJProxy(int objsys_idx, UInt32 id, IFilmPositionDetectService filmPositionDetectService) : base(objsys_idx)
        {
            ID = id;
            this.filmPositionDetectService = filmPositionDetectService;
            this.filmPositionDetectService.PropertyChanged += FilmPositionDetectService_PropertyChanged;
        }
        string[] propertyNames_params = new string[] {
            "FilmVThreshold","FilmVSrc","Encoder2_mmpp","MmOfR","VDistanceWithHeader","VSensorOffset"
        };
        string[] propertyNames_state = new string[] {
            "FilmVelocity",
            "FilmPosition","IsRunning","VDistanceWithHeaderInUsed","VSensorOffsetInUsed","HasReset" 
        };
        private void FilmPositionDetectService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (propertyNames_params.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, FILMPOS_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                                this, FILMPOS_OBJ_INTERFACE.PUSH_PARAMS,
                                buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
            else if (propertyNames_state.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, FILMPOS_OBJ_INTERFACE.GET_STATE, out buf);
                        CurrObjSys.PushObjInfoEx(
                                this, FILMPOS_OBJ_INTERFACE.PUSH_STATE,
                                buf);
                    }), this, MARKNO_PUSH_STATE);
            }
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case FILMPOS_OBJ_INTERFACE.GET_PARAMS:
                    {
                        var p = Mapper.Map<FILMPOS_OBJ_INTERFACE.Pack_Params>(filmPositionDetectService);
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case FILMPOS_OBJ_INTERFACE.GET_STATE:
                    {
                        var p = Mapper.Map<FILMPOS_OBJ_INTERFACE.Pack_State>(filmPositionDetectService);
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
            }
        }

        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid) 
            {
                case FILMPOS_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        Newtonsoft.Json.JsonConvert.PopulateObject(json, this.filmPositionDetectService);

                    }break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case FILMPOS_OBJ_INTERFACE.CALL_RESET:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<double>(json);

                        filmPositionDetectService.Reset(p);
                    }break;
                case FILMPOS_OBJ_INTERFACE.CALL_RESET_2:
                    {
                        filmPositionDetectService.Reset();
                    }
                    break;
                case FILMPOS_OBJ_INTERFACE.CALL_CLEAR_RESET:
                    {
                        filmPositionDetectService.ClearResetState();
                    }
                    break;
                case FILMPOS_OBJ_INTERFACE.CALL_SET_FILMPOS_AT01:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<double>(json);

                        filmPositionDetectService.SetFilmPosAt01(p);
                    }
                    break;
                case FILMPOS_OBJ_INTERFACE.CALL_STOP:
                    {
                        filmPositionDetectService.Stop();
                    }
                    break;
                case FILMPOS_OBJ_INTERFACE.CALL_START:
                    {
                        filmPositionDetectService.Start();
                    }
                    break;
            }
        }
    }
}