using System;
using System.Net;

namespace WsSubDemoNetCore
{
    class Program
    {
        static string RedisServer = "";
        static void Main(string[] args)
        {
            string token = "xxx";//密钥,必须
			string server = "xxx.com";//ip或域名
			int port = 10004;//端口

			string fields = "";//可选，字段参考输出结构文档

			//subsymbols,subrouts,submarkets 必须填充一项
			string subsymbols = "EURUSD";//完整合约代码
			string subrouts = "";
            string submarkets = "";

			//写入Redis，Redis Server配置，可选项, 空或者 pwd@ip:port 例如 密码@127.0.0.1:6379 
			RedisServer = "";

			WSClient subclient = new WSClient();
            subclient.Init(2000, token, server, port, fields, subsymbols, subrouts, submarkets);

            subclient.Start(new EventMapCallback(Callback),true);

            Log4net.InfoFormat("{0} 启动,任意键关闭!", token);

			Console.WriteLine("{0} 启动，任意键关闭! 请检查日志 \\Log", token);

			Console.Read();
        }

        static object locker = new object();
        static RedisOutObj _redis = null;
        static RedisOutObj RedisObj
        {
            get
            {
                if (_redis == null)
                {
                    lock (locker)
                    {
                        if (_redis == null)
                        {
                            if(!string.IsNullOrEmpty(RedisServer))
							    _redis = new RedisOutObj(RedisServer);
                        }
                    }
                }

                return _redis;
            }
        }

        static void Callback(string msg)
        {
			//解析接收数据
			Data ent = ServiceStack.Text.JsonSerializer.DeserializeFromString<Data>(msg);
            if (ent.Cmd == "rm")
            {
                //写入Redis
                if (!string.IsNullOrEmpty(RedisServer))
                    RedisObj.SetItem(ent.FullSymbol(), msg);
                else
                    //或者打印输出
                    Console.WriteLine(msg);
            }
            else if (ent.Cmd.StartsWith("reqk") && ent.Code==0)
            {
				//解析接收的k线数据，k线数据作增量更新存储，从而获得连续的k线数据
				if (!string.IsNullOrEmpty(RedisServer))
                {
                    string[] cmds = ent.Cmd.Split('-');
                    if (cmds.Length == 3)
                    {
                        string symbol = cmds[1];
                        string period = cmds[2];
                        string kdatakey = symbol + period;

                        string[] datas = ent.Msg.Split(';');
                        if (datas.Length > 0)
                        {
                            foreach (string data in datas)
                            {
                                string[] kdatas = data.Split(',');
                                if (kdatas.Length > 0)
                                {
                                    long kticks = Convert.ToInt64(kdatas[0]);
                                    RedisObj.SetSortList(kdatakey, data, kticks);
                                }
                            }
                        }
                    }
                }
                else
					//或者打印输出
					Console.WriteLine(msg);
			}
            else Log4net.Warn(msg);
        }
    }

	public class Data
	{
        public int Code { get; set; }
		public string Cmd { get; set; }

		public string Msg { get; set; }


		public string FullSymbol()
		{
			return (M + S + C);
		}

		public string M { get; set; }

		public string S { get; set; }

		public string C { get; set; }

		//...
	}
}
