internal Error send() { // Assume success Error ret = Error.Success; if (Initialized) { // Build up the data object List <object> allArgs = new List <object>(); double curTimeStamp = Util.Timestamp(); // The interface calls require two time stamps (to support batching), so we'll send the same one for both allArgs.Add(curTimeStamp); allArgs.Add(curTimeStamp); allArgs.Add(UserId); allArgs.Add(DeviceId); allArgs.AddRange(_args); // Build the event and store it in the depot IDictionary <string, object> eventData = new Dictionary <string, object>(2); eventData.Add("method", _call); eventData.Add("args", allArgs); ret = EventDepot.store(eventData); } else { ret = Error.NotInitialized; } return(ret); }
void HttpRequest.Listener.onComplete(HttpRequest.Result result) { Error retError = Error.Generic; bool userNew = false; bool deviceNew = false; if (Error.Success == result.ErrorCode) { try { var dict = Json.Deserialize(result.Response) as Dictionary <string, object>; if (dict.ContainsKey("error") && (Error.Success == (Error)Enum.ToObject(typeof(Error), dict["error"]))) { if (dict.ContainsKey("data")) { var ret = dict["data"] as Dictionary <string, object>; // NOTE: deviceId should only be set during the callback from init() if ((null != ret) && ret.ContainsKey("deviceid") && isValidId(ret["deviceid"] as string)) { deviceNew = ret.ContainsKey("devicenew") ? (bool)ret["devicenew"] : false; DeviceId = ret["deviceid"] as string; if (null != DeviceId) { // Save it off Prefs.Add(DEVICEID_KEY_NAME, DeviceId); if (ret.ContainsKey("devicetuning")) { var deviceTuning = ret["devicetuning"] as IDictionary <string, object>; if (null != deviceTuning) { mTuningUpdater.onUpdate(Constants.ENTITY_TYPE_DEVICE, DeviceId, deviceTuning); } } } } // now handle the user id if there is one if ((null != ret) && ret.ContainsKey("userid") && isValidId(ret["userid"] as string)) { userNew = ret.ContainsKey("usernew") ? (bool)ret["usernew"] : false; UserId = ret["userid"] as string; if (null != UserId) { if (ret.ContainsKey("usertuning")) { var userTuning = ret["usertuning"] as IDictionary <string, object>; if (null != userTuning) { mTuningUpdater.onUpdate(Constants.ENTITY_TYPE_USER, UserId, userTuning); } } if (!sRegisteredUsers.Contains(UserId)) { sRegisteredUsers.Add(UserId); } } } } } else { string desc = null; if (dict.ContainsKey("description")) { desc = dict["description"] as string; } Util.logError(String.Format("Problem on initialization [{0}]", (null != desc) ? desc : "Unknown")); retError = Error.Generic; } } catch (Exception) { Util.logError("Exception during intialization: " + result.Response); retError = Error.Generic; } mTuningUpdater.commit(); } else { // Request failure (likely a timeout), pass it through Util.logError("Initialization call failed: code " + result.ErrorCode); retError = result.ErrorCode; } // even if the init call failed, all is well as long as we AT LEAST have a device id if (isValidId(DeviceId)) { // If initialization is successful, we can initialize the EventDepot EventDepot.init(Host, getQueryParms(), ReqTimeout); Initialized = true; // queue up some telemetry for the initial state... if (null != mDeviceProperties) { new DataPointBuilder("datacollector_updateDeviceState").setArg(mDeviceProperties).send(); } if (null != mUserProperties) { new DataPointBuilder("datacollector_updateUserState").setArg(mUserProperties).send(); } if (deviceNew) { new DataPointBuilder("datacollector_newDevice").send(); } if (userNew) { new DataPointBuilder("datacollector_newUser").send(); } // TODO - decide if we want to send a TuningFailed error at this point, if there was some kind of error? retError = Error.Success; } // Call the callback if (null != mCallback) { mCallback(retError); } }