public virtual bool send(IDictionary <string, object> egCollectMessage) { lock (batchMsgList) { if (sendTimer == -1) { sendTimer = DateTimeHelperClass.CurrentUnixTimeMillis(); } batchMsgList.Add(egCollectMessage); string xWhat = "xwhat"; if (batchMsgList.Count >= batchNum || EventName.ALIAS.Value.Equals(egCollectMessage[xWhat])) { upload(); } } return(true); }
private void run() { while (isListen) { try { Thread.Sleep(1000); } catch (Exception e1) { Console.WriteLine(e1); } if (sendTimer != -1 && (DateTimeHelperClass.CurrentUnixTimeMillis() - sendTimer >= batchSec)) { try { upload(); } catch (Exception) { } } } }
/// <summary> /// 上传数据,首先校验相关KEY和VALUE,符合规则才可以上传 </summary> /// <param name="distinctId"> 用户标识 </param> /// <param name="isLogin"> 是否登陆 </param> /// <param name="eventName"> 事件名称 </param> /// <param name="properties"> 属性 </param> /// <param name="platform"> 平台类型 </param> /// <param name="xwhen"> 时间戳 </param> /// <exception cref="AnalysysException"> 自定义异常 </exception> private void upload(string distinctId, bool isLogin, string eventName, IDictionary <string, object> properties, string platform, string xwhen) { Dictionary <string, object> targetProperties = new Dictionary <string, object>(); if (properties != null) { foreach (var item in properties) { targetProperties.Add(item.Key, item.Value); } } //ValidHandle.checkProperty(distinctId, eventName, targetProperties, this.xcontextSuperProperties.Count); IDictionary <string, object> eventMap = new Dictionary <string, object>(8); eventMap["xwho"] = distinctId; if (!string.ReferenceEquals(xwhen, null) && xwhen.Trim().Length > 0) { if (xwhen.Trim().Length != 13) { throw new AnalysysException(string.Format("The param xwhen {0} not a millisecond timestamp.", xwhen.Trim())); } try { long when = Convert.ToInt64(xwhen.Trim()); //(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")).format(new DateTime(when)); //eventMap["xwhen"] = when; DateTime dtstart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long ltime = long.Parse(dtstart + "0000"); TimeSpan tonow = new TimeSpan(ltime); DateTime dtresult = dtstart.Add(tonow); when = long.Parse(dtresult.ToString("yyyyMMddHHmmssSSS")); eventMap["xwhen"] = when; } catch (Exception error) { throw new AnalysysException(string.Format("The param xwhen {0} not a timestamp.", xwhen.Trim())); } } else { if (EventName.ALIAS.Value.Equals(eventName)) { eventMap["xwhen"] = DateTimeHelperClass.CurrentUnixTimeMillis() - 3; } else { eventMap["xwhen"] = DateTimeHelperClass.CurrentUnixTimeMillis(); //测试时间 //DateTime time = System.DateTime.MinValue; //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); //time = startTime.AddMilliseconds((double)((long)eventMap["xwhen"])); } } eventMap["xwhat"] = eventName; eventMap["appid"] = appId; IDictionary <string, object> newProperties = new Dictionary <string, object>(16); string profile = "$profile"; if (!eventName.StartsWith(profile, StringComparison.Ordinal) && !eventName.StartsWith(EventName.ALIAS.Value, StringComparison.Ordinal)) { //JAVA TO C# CONVERTER CRACKED BY X-CRACKER TODO TASK: There is no .NET Dictionary equivalent to the Java 'putAll' method: foreach (var item in xcontextSuperProperties) { newProperties.Add(item.Key, item.Value); } } newProperties["$debug"] = debugMode; if (targetProperties != null) { foreach (var item in targetProperties) { newProperties.Add(item.Key, item.Value); } } foreach (var item in egBaseProperties) { newProperties.Add(item.Key, item.Value); } newProperties["$is_login"] = isLogin; string newPlatForm = getPlatForm(platform); if (!string.ReferenceEquals(newPlatForm, null) && newPlatForm.Trim().Length > 0) { newProperties["$platform"] = newPlatForm; } eventMap["xcontext"] = newProperties; this.collecter.debug(Debug); bool ret = this.collecter.send(eventMap); if (eventName.StartsWith(profile, StringComparison.Ordinal) && Debug && ret) { Console.WriteLine(string.Format("{0} success.", eventName.Substring(1))); } }