Ejemplo n.º 1
0
        public string CreateCommonTxRaw(UserId toAddress, ulong fees, ulong amount, uint blockHeight)
        {
            var secret = new BitcoinSecret(Prikey, Network);
            var key    = Key.Parse(secret.ToWif(), Network);
            var tx     = new CommonTx()
            {
                Fees        = fees,
                Contract    = new VarString(),
                ValidHeight = blockHeight,
                SrcId       = this.UserId,
                DesId       = toAddress,
                Values      = amount
            };

            tx.SignByKey(key);

            var ms = new MemoryStream();
            var bs = new BitcoinStream(ms, true)
            {
                Type = SerializationType.Hash
            };

            bs.ReadWrite(tx);

            var str = HexHelper.ToHexString(ms.ToArray());

            return(str);
        }
Ejemplo n.º 2
0
        public string CreateContractTxRaw(string scriptRegId, string contract, ulong fees, uint blockHeight)
        {
            var hex    = NBitcoin.Wicc.HexHelper.ToByteArray(contract);
            var secret = new BitcoinSecret(Prikey, Network);
            var key    = Key.Parse(secret.ToWif(), Network);
            var tx     = new ContractTx()
            {
                Fees        = fees,
                Contract    = new VarString(hex),
                ValidHeight = (ulong)blockHeight,
                SrcId       = this.UserId,
                DesId       = new UserId(uint.Parse(scriptRegId.Split('-')[0]), uint.Parse(scriptRegId.Split('-')[1])),
                Values      = 0
            };

            tx.SignByKey(key);

            var ms = new MemoryStream();
            var bs = new BitcoinStream(ms, true)
            {
                Type = SerializationType.Hash
            };

            bs.ReadWrite(tx);

            var str = HexHelper.ToHexString(ms.ToArray());

            return(str);
        }
Ejemplo n.º 3
0
        public string GetRegisteAppRaw(string scriptName, string scriptCode, ulong fees, uint blockHeight)
        {
            var secret = new BitcoinSecret(Prikey, Network);
            var key    = Key.Parse(secret.ToWif(), Network);
            var tx     = new RegisterAppTx()
            {
                Fees        = fees,
                RegId       = UserId,
                ValidHeight = (ulong)blockHeight,
                Script      = new VarScript(scriptName, scriptCode)
            };

            tx.SignByKey(key);

            var ms = new MemoryStream();
            var bs = new BitcoinStream(ms, true)
            {
                Type = SerializationType.Hash
            };

            bs.ReadWrite(tx);

            var str = HexHelper.ToHexString(ms.ToArray());

            return(str);
        }
Ejemplo n.º 4
0
        public string GetRegisteAccountRaw(ulong fees, uint blockHeight)
        {
            var secret = new BitcoinSecret(Prikey, Network);
            var key    = Key.Parse(secret.ToWif(), Network);
            var tx     = new RegisterAccountTx
            {
                Fees        = fees,
                UserId      = new VarString(key.PubKey.ToBytes()),
                ValidHeight = (ulong)blockHeight
            };

            //tx.Contract = new VarString(Encoding.UTF8.GetBytes("hello wayki"));
            tx.SignByKey(key);

            var ms = new MemoryStream();
            var bs = new BitcoinStream(ms, true)
            {
                Type = SerializationType.Hash
            };

            bs.ReadWrite(tx);

            var str = HexHelper.ToHexString(ms.ToArray());

            return(str);
        }
Ejemplo n.º 5
0
        private static object ConvertType(object rawValue, PropertyInfo property, DbColumnAttribute attribute)
        {
            try
            {
                bool toNullable    = Nullable.GetUnderlyingType(property.PropertyType) == rawValue.GetType();
                bool numericToBool = property.PropertyType.ContainsType(typeof(bool), typeof(bool?)) &&
                                     rawValue.GetType().ContainsType(typeof(int), typeof(decimal), typeof(Int16));
                bool decimalToInt = property.PropertyType.ContainsType(typeof(int), typeof(int?)) &&
                                    rawValue.GetType().ContainsType(typeof(decimal));
                bool toGuid = property.PropertyType.ContainsType(typeof(Guid), typeof(Guid?)) &&
                              rawValue.GetType() == typeof(byte[]);

                if (toNullable)
                {
                    return(rawValue);
                }
                else if (numericToBool)
                {
                    rawValue = (Convert.ToInt16(rawValue) == 1) ? true : false;
                }
                else if (decimalToInt)
                {
                    rawValue = Convert.ToInt32(rawValue);
                }
                else if (toGuid)
                {
                    rawValue = new Guid(HexHelper.ToHexString((byte[])rawValue));
                }
                else if (attribute.ColumnConversionHint == DbColumnAttribute.ColumnConversionHints.ToListByPipe)
                {
                    rawValue = rawValue.ToString().Split('|').ToList();
                }
                else if (rawValue.GetType() != property.GetType())
                {
                    rawValue = Convert.ChangeType(rawValue, property.PropertyType);
                }

                return(rawValue);
            }
            catch (InvalidCastException ex)
            {
                log.Debug(string.Format("Type Conversion Exception:\n\tRaw Value:{0}(Type:{1})\n\tProperty:{2}(Type:{3})\nSTACKTRACE:\n{4}",
                                        rawValue.ToString(),
                                        rawValue.GetType(),
                                        property.Name,
                                        property.PropertyType.ToString(),
                                        ex.StackTrace));
                throw;
            }
            catch (Exception ex)
            {
                log.Error($"Message => {ex.Message}\nStacktrace => {ex.StackTrace}");
                throw;
            }
        }
Ejemplo n.º 6
0
        public void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (sp_buffer != null && (sp_buffer.Length >= sp_buffer_len || !sp_buffer.StartsWith("AA")))  //清理漏洞数据
            {
                sp_buffer = null;
            }
            else if (sp_buffer != null && !sp_buffer.StartsWith("AAAA") && sp_buffer.Length > 2)
            {
                sp_buffer = null;
            }
            byte[] readBuffer = new byte[sp_read_len];
            sp.Read(readBuffer, 0, readBuffer.Length);
            string readstr = HexHelper.ToHexString(readBuffer);

            readstr = TrimHelper.Trim(readstr, '0');
            showDispatchText("info.原始指令: " + readstr);
            if (!CheckCRC(readstr))
            {
                bool   cangoon = false;
                string newstr  = readstr.Substring(0, readstr.Length);

                for (int i = 0; i < newstr.Length; i += 2)
                {
                    char a = newstr[i];
                    char b = newstr[i + 1];
                    if (a != '0' || b != '0')
                    {
                        string wd = string.Concat(a, b);
                        if (sp_buffer != null && sp_buffer.StartsWith(sp_start_buf))
                        {
                            if (sp_buffer.StartsWith(sp_start_buf + sp_start_buf))
                            {
                                sp_buffer += wd;
                            }
                            else if (wd == sp_start_buf)
                            {
                                sp_buffer += wd;
                            }
                            else
                            {
                                sp_buffer = null;
                            }
                        }
                        else if (wd == sp_start_buf)
                        {
                            sp_buffer = wd;
                        }
                        if (!string.IsNullOrEmpty(sp_buffer))
                        {
                            if (CheckCRC(sp_buffer))
                            {
                                cangoon   = true;
                                readstr   = sp_buffer;
                                sp_buffer = null;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (!cangoon)
                {
                    showDispatchText("error:反馈指令错误..." + readstr);
                    return;
                }
            }

            TruckLogInfo log = new TruckLogInfo()
            {
                Createtime = DateTime.Now,
                FullCode   = readstr
            };
            List <string> list = InstructionHelper.getFromInstuct(readstr);

            log.Routecode = list[4];    //路径点
            log.RunStatus = list[3];    //运行状态
            log.Truckcode = list[2];    //叉车编号
            bool needlog = false;

            #region 重复处理
            string cacheKey = "listen_route_key" + log.FullCode;
            object obj      = CacheManager.Instance.Get(cacheKey);
            if (obj != null)
            {
                List <TruckLogInfo> tList = obj as List <TruckLogInfo>;
                var item = tList.Where(a => a.FullCode == readstr).LastOrDefault();
                if (item == null)
                {
                    needlog = true;
                }
            }
            else
            {
                List <TruckLogInfo> tList = new List <TruckLogInfo>();
                tList.Add(log);
                CacheManager.Instance.Set(cacheKey, tList, 0, 15);
                needlog = true;
            }
            #endregion
            if (routeShow != null && routeShow.IsVisible)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    routeShow.ShowTruckPosition(log.Routecode);
                }));
            }
            if (needlog)
            {
                x_tlService.Insert(log);
                if (log.RunStatus != "AA")  //叉车接收到启动指令后的返回消息
                {
                    //将叉车更新为运行状态
                    x_tkService.Update(() => new TruckInfo()
                    {
                        Truckstatus = 1, TruckrouteCode = log.Routecode
                    }, a => a.Truckcode == log.Truckcode);
                }
                else
                {
                    int status = 1;
                    if (log.RunStatus == "02") //停止指令
                    {
                        status = 0;            //叉车已停止运行,恢复为可用状态(暂定)
                    }
                    x_tkService.Update(() => new TruckInfo()
                    {
                        Truckstatus = status, TruckrouteCode = log.Routecode
                    }, a => a.Truckcode == log.Truckcode);
                }
            }
            if (log.RunStatus == "AA")  //叉车接收到启动指令后的返回消息
            {
                showDispatchText("start.叉车已启动:" + readstr);
            }
            else
            {
                if (log.RunStatus == "01")
                {
                    showDispatchText("runing.叉车运行到:" + readstr);
                }
                else
                {
                    showDispatchText("stop.叉车已停止运行:" + readstr);
                }
            }
        }
Ejemplo n.º 7
0
 public void ToHexStringTest()
 {
     Assert.AreEqual("FF", HexHelper.ToHexString(255));
     Assert.AreEqual("FFFF", HexHelper.ToHexString(65535));
     Assert.AreEqual("0A", HexHelper.ToHexString(10));
 }