Esempio n. 1
0
 /// <summary>
 /// Static constructor which adds all the conversion methods for base types.
 /// </summary>
 public void AddConversionFunction(string type, ConversionFunction function)
 {
     if (!conversionFunctions.ContainsKey(type))
     {
         conversionFunctions.Add(type, function);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Sets the conversion function from two units of the same quantity type.
        /// </summary>
        /// <typeparam name="TQuantity">The type of quantity, must implement <see cref="IQuantity"/>.</typeparam>
        /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
        /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
        /// <param name="conversionFunction">The quantity conversion function.</param>
        public void SetConversionFunction <TQuantity>(Enum from, Enum to, ConversionFunction <TQuantity> conversionFunction)
            where TQuantity : IQuantity
        {
            var quantityType     = typeof(TQuantity);
            var conversionLookup = new ConversionFunctionLookupKey(quantityType, from, quantityType, to);

            SetConversionFunction(conversionLookup, conversionFunction);
        }
Esempio n. 3
0
        public dynamic GetValue(byte[] p)
        {
            var raw = GetRawValue(p);

            if (raw == null)
            {
                return(null);
            }
            var cnv = ConversionFunction.Convert(raw);

            return(cnv);
        }
Esempio n. 4
0
    public static T GetAppParam <T>(string configParam, ConversionFunction <T> conversionFunction, T defaultValue)
    {
        T      returnValue = defaultValue;
        string fromConfig  = ConfigurationSettings.AppSettings[configParam];;

        try
        {
            returnValue = conversionFunction(fromConfig);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return(returnValue);
    }
Esempio n. 5
0
        public dynamic UploadEmployee(/*HttpPostedFileBase file*/)
        {
            try
            {
                HttpPostedFileBase file     = Request.Files["Filedata"];
                string             tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
                string             savepath = Server.MapPath(tempPath);
                // Process the file here.
                //
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }
                    var fullpathname = Path.Combine(savepath, fileName);
                    file.SaveAs(fullpathname);

                    // Process start
                    var convert = new ConversionFunction();
                    convert.ImportEmployeeFromExcel(fullpathname);
                    //ConversionFunction.ImporttoSQL(fullpathname);

                    // Process End

                    string ret = tempPath + "/" + fileName;
                    //Response.Write(ret);
                    Response.StatusCode = 200;
                    return(ret);
                }
                //return "Upload processed. filename=" + file.FileName;
            }
            catch (Exception ex)
            {
                string ret = "Error: " + ex.Message;
                //Response.Write(ret);
                return(ret);
            }
            return(Response);
        }
Esempio n. 6
0
 /// <summary>
 /// Sets the conversion function from two units of different quantity types.
 /// </summary>
 /// <typeparam name="TQuantityFrom">From quantity type, must implement <see cref="IQuantity"/>.</typeparam>
 /// <typeparam name="TQuantityTo">To quantity type, must implement <see cref="IQuantity"/>.</typeparam>
 /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
 /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
 /// <param name="conversionFunction">The quantity conversion function.</param>
 public void SetConversionFunction <TQuantityFrom, TQuantityTo>(Enum from, Enum to, ConversionFunction conversionFunction)
     where TQuantityFrom : IQuantity
     where TQuantityTo : IQuantity
 {
     SetConversionFunction(typeof(TQuantityFrom), from, typeof(TQuantityTo), to, conversionFunction);
 }
Esempio n. 7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lookupKey"></param>
 /// <param name="conversionFunction"></param>
 /// <returns>true if set; otherwise, false.</returns>
 public bool TryGetConversionFunction(ConversionFunctionLookupKey lookupKey, out ConversionFunction conversionFunction)
 {
     return(_conversionFunctions.TryGetValue(lookupKey, out conversionFunction));
 }
Esempio n. 8
0
        /// <summary>
        /// Try to get the conversion function for two units of the same quantity type.
        /// </summary>
        /// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param>
        /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
        /// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param>
        /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
        /// <param name="conversionFunction">The quantity conversion function.</param>
        /// <returns>true if set; otherwise, false.</returns>
        public bool TryGetConversionFunction(Type fromType, Enum from, Type toType, Enum to, out ConversionFunction conversionFunction)
        {
            var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to);

            return(TryGetConversionFunction(conversionLookup, out conversionFunction));
        }
Esempio n. 9
0
 /// <summary>
 /// Gets the conversion function for two units of different quantity types.
 /// </summary>
 /// <typeparam name="TQuantityFrom">From quantity type, must implement <see cref="IQuantity"/>.</typeparam>
 /// <typeparam name="TQuantityTo">To quantity type, must implement <see cref="IQuantity"/>.</typeparam>
 /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
 /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
 /// <param name="conversionFunction">The quantity conversion function.</param>
 /// <returns>true if set; otherwise, false.</returns>
 public bool TryGetConversionFunction <TQuantityFrom, TQuantityTo>(Enum from, Enum to, out ConversionFunction conversionFunction)
     where TQuantityFrom : IQuantity
     where TQuantityTo : IQuantity
 {
     return(TryGetConversionFunction(typeof(TQuantityFrom), from, typeof(TQuantityTo), to, out conversionFunction));
 }
Esempio n. 10
0
        /// <summary>
        /// Sets the conversion function for a particular conversion function lookup.
        /// </summary>
        /// <typeparam name="TQuantity">The quantity type, must implement <see cref="IQuantity"/>.</typeparam>
        /// <param name="conversionLookup">The quantity conversion function lookup key.</param>
        /// <param name="conversionFunction">The quantity conversion function.</param>
        internal void SetConversionFunction <TQuantity>(ConversionFunctionLookupKey conversionLookup, ConversionFunction <TQuantity> conversionFunction)
            where TQuantity : IQuantity
        {
            IQuantity TypelessConversionFunction(IQuantity quantity) => conversionFunction((TQuantity)quantity);

            _conversionFunctions[conversionLookup] = TypelessConversionFunction;
        }
Esempio n. 11
0
        public unsafe dynamic GetRawValue(byte[] p)
        {
            switch (FieldType)
            {
            case PointType.F1234:
            {
                var v = (p[byte_offset] << 24) | (p[1 + byte_offset] << 16) | (p[2 + byte_offset] << 8) |
                        p[3 + byte_offset];
                var fp = (float *)&v;
                var f  = *fp;
                return(f);
            }

            case PointType.F12345678:
            {
                var v = ((ulong)p[0 + byte_offset] << 56) | ((ulong)p[1 + byte_offset] << 48) |
                        ((ulong)p[2 + byte_offset] << 40) | ((ulong)p[3 + byte_offset] << 32) |
                        ((ulong)p[4 + byte_offset] << 24) | ((ulong)p[5 + byte_offset] << 16) |
                        ((ulong)p[6 + byte_offset] << 8) | (ulong)p[7 + byte_offset];
                var dp = (double *)&v;
                var d  = *dp;
                return(d);
            }

            case PointType.I1:
                return((sbyte)p[byte_offset]);

            case PointType.I12:
                return((Int16)((p[byte_offset] << 8) | (p[byte_offset + 1])));

            case PointType.I12b:
            {
                long temp = ((p[byte_offset] << 8) | (p[byte_offset + 1]));
                temp = temp >> (15 - bit_stop);
                var   len    = bit_stop - bit_start + 1;
                var   result = (_mask32[len] & temp);
                var   isNeg  = (_bit32[len - 1] & result) != 0;
                Int16 r      = 0;
                if (isNeg)
                {
                    result = result - _bit32[len];
                    r      = (Int16)result;
                }
                return(r);
            }

            case PointType.I1234:
                return
                    ((Int32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.I1234b:
            {
                long temp = ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                             (p[byte_offset + 3]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (_mask32[len] & temp);
                var isNeg  = (_bit32[len - 1] & result) != 0;
                var r      = 0;
                if (isNeg)
                {
                    result = result - _bit32[len];
                    r      = (Int32)result;
                }
                return(r);
            }

            case PointType.S1:
            {
                int count = byte_size;
                for (var i = 0; i < byte_size; i++)
                {
                    if (p[i + byte_offset] == 0)
                    {
                        count = i;
                        break;
                    }
                }

                var s = Encoding.ASCII.GetString(p, byte_offset, count);
                return(s);
            }

            case PointType.TIME40:
                return
                    ((UInt32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.TIME42:
                return(((ulong)p[0 + byte_offset] << 40) | ((ulong)p[1 + byte_offset] << 32) |
                       ((ulong)p[2 + byte_offset] << 24) | ((ulong)p[3 + byte_offset] << 16) |
                       ((ulong)p[4 + byte_offset] << 8) | (ulong)p[5 + byte_offset]);

            case PointType.TIME44:
                return(((ulong)p[0 + byte_offset] << 56) | ((ulong)p[1 + byte_offset] << 48) |
                       ((ulong)p[2 + byte_offset] << 40) | ((ulong)p[3 + byte_offset] << 32) |
                       ((ulong)p[4 + byte_offset] << 24) | ((ulong)p[5 + byte_offset] << 16) |
                       ((ulong)p[6 + byte_offset] << 8) | (ulong)p[7 + byte_offset]);

            case PointType.U1:
                return(p[byte_offset]);

            case PointType.U1b:
            {
                int temp = p[byte_offset];
                temp = temp >> (7 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (byte)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U12:
                return((UInt16)((p[byte_offset] << 8) | (p[byte_offset + 1])));

            case PointType.U12b:
            {
                var temp = ((p[byte_offset] << 8) | (p[byte_offset + 1]));
                temp = temp >> (15 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt16)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U1234:
                return
                    ((UInt32)
                     ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                      (p[byte_offset + 3])));

            case PointType.U12345678:
                return
                    ((UInt64)
                     (((ulong)p[byte_offset] << 56) | ((ulong)p[byte_offset + 1] << 48) | ((ulong)p[byte_offset + 2] << 40) | ((ulong)p[byte_offset + 3] << 32) |
                      ((ulong)p[byte_offset + 4] << 24) | ((ulong)p[byte_offset + 5] << 16) | ((ulong)p[byte_offset + 6] << 8) |
                      ((ulong)p[byte_offset + 7])));

            // This isn't right because the value is big endian
            //return (dynamic) BitConverter.ToUInt64(p, byte_offset);
            case PointType.U1234b:
            {
                var temp = ((p[byte_offset] << 24) | (p[byte_offset + 1] << 16) | (p[byte_offset + 2] << 8) |
                            (p[byte_offset + 3]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt32)(_mask32[len] & temp);
                return(result);
            }

            case PointType.U21:
                return((UInt16)((p[byte_offset + 1] << 8) | (p[byte_offset])));

            case PointType.U4321:
                return
                    ((UInt32)
                     ((p[byte_offset + 3] << 24) | (p[byte_offset + 2] << 16) | (p[byte_offset + 1] << 8) |
                      (p[byte_offset])));

            case PointType.U4321b:
            {
                var temp = ((p[byte_offset + 3] << 24) | (p[byte_offset + 2] << 16) | (p[byte_offset + 1] << 8) |
                            (p[byte_offset]));
                temp = temp >> (31 - bit_stop);
                var len    = bit_stop - bit_start + 1;
                var result = (UInt32)(_mask32[len] & temp);
                return(result);
            }

            case PointType.Pseudo:
                return(ConversionFunction.Convert(0));

            case PointType.FullPacketConversion:
                return(ConversionFunction.Convert(p));
            }
            return(null);
        }
Esempio n. 12
0
 internal UnitConversionValueSlot(Unit from, Unit to, ConversionFunction conversionFunction)
 {
     this.from = from;
     this.to   = to;
     this.conversionFunction = conversionFunction;
 }
Esempio n. 13
0
		/// <summary>
		/// Register a conversion function.
		/// </summary>
		/// <param name="fromUnit">The unit from which this conversion function allows conversion.</param>
		/// <param name="toUnit">The unit to which this conversion function allows conversion to.</param>
		/// <param name="conversionFunction">The unit conversion function.</param>
		/// <remarks>
		/// A unit conversion function is registered to convert from one unit to another. It will
		/// however be applied to convert from any unit of the same family of the fromUnit, to any
		/// unit family of the toUnit. For reverse conversion, a separate function must be registered.
		/// </remarks>
		public static void RegisterConversion(Unit fromUnit, Unit toUnit, ConversionFunction conversionFunction)
		{
			Instance.conversions[new UnitConversionKeySlot(fromUnit, toUnit)] = new UnitConversionValueSlot(fromUnit, toUnit, conversionFunction);
		}
Esempio n. 14
0
			public UnitConversionValueSlot(Unit from, Unit to, ConversionFunction conversionFunction)
			{
				this.from = from;
				this.to = to;
				this.conversionFunction = conversionFunction;
			}
Esempio n. 15
0
        /// <summary>
        /// Sets the conversion function from two units of different quantity types.
        /// </summary>
        /// <param name="fromType">From quantity type, must implement <see cref="IQuantity"/>.</param>
        /// <param name="from">From unit enum value, such as <see cref="LengthUnit.Kilometer" />.</param>
        /// <param name="toType">To quantity type, must implement <see cref="IQuantity"/>.</param>
        /// <param name="to">To unit enum value, such as <see cref="LengthUnit.Centimeter"/>.</param>
        /// <param name="conversionFunction">The quantity conversion function.</param>
        public void SetConversionFunction(Type fromType, Enum from, Type toType, Enum to, ConversionFunction conversionFunction)
        {
            var conversionLookup = new ConversionFunctionLookupKey(fromType, from, toType, to);

            SetConversionFunction(conversionLookup, conversionFunction);
        }
Esempio n. 16
0
 /// <summary>
 /// Sets the conversion function for a particular conversion function lookup.
 /// </summary>
 /// <param name="lookupKey">The lookup key.</param>
 /// <param name="conversionFunction">The quantity conversion function.</param>
 internal void SetConversionFunction(ConversionFunctionLookupKey lookupKey, ConversionFunction conversionFunction)
 {
     _conversionFunctions[lookupKey] = conversionFunction;
 }
Esempio n. 17
0
 /// <summary>
 /// Register a conversion function.
 /// </summary>
 /// <param name="fromUnit">The unit from which this conversion function allows conversion.</param>
 /// <param name="toUnit">The unit to which this conversion function allows conversion to.</param>
 /// <param name="conversionFunction">The unit conversion function.</param>
 /// <remarks>
 /// A unit conversion function is registered to convert from one unit to another. It will
 /// however be applied to convert from any unit of the same family of the fromUnit, to any
 /// unit family of the toUnit. For reverse conversion, a separate function must be registered.
 /// </remarks>
 public static void RegisterConversion(Unit fromUnit, Unit toUnit, ConversionFunction conversionFunction)
 {
     Instance._conversions[new UnitConversionKeySlot(fromUnit, toUnit)] = new UnitConversionValueSlot(fromUnit, toUnit, conversionFunction);
 }