/** * Takes a picture **/ private void AskTakePicture() { mHTTP.ChangeCommand(HttpRequest.Commands.POST_C_EXECUTE); mHTTP.SetJSONData(ConstructTakePictureJSONString()); mCurrentState = OSCStates.TAKE_PHOTO; mHTTP.Execute(); }
/** * Starts live preview mode **/ private void AskStartLivePreview() { mCurrentState = OSCStates.LIVE_PREVIEW; mHTTP.ChangeCommand(HttpRequest.Commands.POST_C_EXECUTE); mHTTP.SetJSONData(ConstructStartLivePreviewJSONString()); mHTTP.NextRequestIsStream(); mHTTP.Execute(); }
/** * After delete call the callback to inform that we are done * Go to IDLE state **/ void ManageDelete(JsonData jdata) { if (mCallBack != null) { mCallBack(); mCallBack = null; } mCurrentState = OSCStates.IDLE; }
private void Init() { mInternalData.fileURL = ""; mInternalData.currentOperationId = ""; mInternalData.sessionId = ""; mInternalData.isBusy = false; mInternalData.remainingConnectionTry = 3; mCurrentState = OSCStates.INIT; EnqueueAction(OSCActions.CAMERA_STATE); }
/** * Passes the camera in API version 2 if it is in version 1 * then sets options **/ void ManageInit(JsonData jdata) { Debug.Log(jdata.ToString()); if (jdata["state"]["_apiVersion"].ToString() == "1") { EnqueueAction(OSCActions.START_SESSION); EnqueueAction(OSCActions.UPGRADE_API); EnqueueAction(OSCActions.SET_OPTIONS); mCurrentState = OSCStates.DISCONNECTED; } else { mCurrentState = OSCStates.IDLE; } }
/** * Handle error from the HTTP part **/ private void HandleError(string err) { if (mInternalData.remainingConnectionTry > 0) { int tmp = mInternalData.remainingConnectionTry; Debug.Log("HTTP error: " + err); //Log error message RebootController(); mInternalData.remainingConnectionTry = --tmp; } else //3 try, 3 fails, application goes to error state { ClearQueue(); mCurrentState = OSCStates.ERROR; } }
/** * When taking a photo we ask for operation progress until state is done * Then go to DOWNLOAD_PHOTO **/ void ManageTakePhoto(JsonData jdata) { string state = jdata["state"].ToString(); if (state == "inProgress") { mInternalData.currentOperationId = jdata["id"].ToString(); EnqueueAction(OSCActions.PROGRESS_STATUS); } else if (state == "done") { mInternalData.fileURL = jdata["results"]["fileUrl"].ToString(); EnqueueAction(OSCActions.DOWNLOAD); mCurrentState = OSCStates.DOWNLOAD_PHOTO; } }
/** * Issues enqueued commands and gets response **/ private void Update() { //Dequeue and invoke a new method if we are done with the previous request if (!mInternalData.isBusy && mExecutionQueue.Count > 0) { mExecutionQueue.Dequeue().Invoke(this, null); mInternalData.isBusy = true; } else if (mInternalData.isBusy && mHTTP.IsTerminated()) //else if the request is terminated and successful handle it in the FSM { string s = mHTTP.GetHTTPResponse(); Debug.Log("Request answer: " + s); if (mHTTP.IsSuccessful()) { mInternalData.remainingConnectionTry = 3; ResponseHandler(s); } else { HandleError(s); } mInternalData.isBusy = false; } else if (mCurrentState == OSCStates.LIVE_PREVIEW) //else if we are streaming check for a new image or try restarting live preview { if (mHTTP.mStreamRequest.IsStreamOnError()) { if (mInternalData.remainingConnectionTry > 0) { int tmp = mInternalData.remainingConnectionTry; StopLivePreview(); RebootController(); EnqueueAction(OSCActions.LIVE_PREVIEW); mInternalData.remainingConnectionTry = --tmp; } else { ClearQueue(); mCurrentState = OSCStates.ERROR; } } else { ResponseHandler(null); } } }
/** * Stop a live preview acquisition going back to IDLE state and closing streaming connection **/ public void StopLivePreview() { mInternalData.isBusy = false; mCurrentState = OSCStates.IDLE; mHTTP.CloseStreaming(); }
/** * Save photo as byte array and go to DELETE_PHOTO **/ void ManageDownload(JsonData jdata) { mBuffer = mHTTP.GetRawResponse(); EnqueueAction(OSCActions.DELETE); mCurrentState = OSCStates.DELETE_PHOTO; }
/** * When disconnected we can only received the result of a startSession command * Go to IDLE state **/ void ManageDisconnected(JsonData jdata) { mInternalData.sessionId = jdata["results"]["sessionId"].ToString(); mCurrentState = OSCStates.IDLE; }