public override void OnMessage(string content) { Console.WriteLine("end got " + content); //Read the JSON. WebPebbleRequest request = JsonConvert.DeserializeObject <WebPebbleRequest>(content); //If this isn't an auth request, ignore. if (!authenticated && request.type != WebPebbleRequestType.Auth) { return; } //Do what is needed based on the request. switch (request.type) { case WebPebbleRequestType.Auth: DoAuth(request); break; case WebPebbleRequestType.Screenshot: DoGetScreenshot(request); break; case WebPebbleRequestType.InstallApp: InstallApp(request); break; case WebPebbleRequestType.YcmdComplete: OnYcmdRequest(request); break; } }
public void QuickReply(int id, WebPebbleRequestType type, Dictionary <string, object> dict) { WebPebbleRequest req = new WebPebbleRequest { requestid = id, type = type, data = dict }; SendData(JsonConvert.SerializeObject(req)); }
public bool CheckIfConnected(WebPebbleRequest req) { if (!pair.connected) { //Tell client we are disconnected. QuickReply(req.requestid, WebPebbleRequestType.Auth, new Dictionary <string, object>() { { "error", "Phone not connected." } }); return(false); } return(true); }
public void InstallApp(WebPebbleRequest req) { //If we're not connected, tell them so. if (!CheckIfConnected(req)) { return; } //Download the PBW file prompted. using (var client = new WebClient()) { byte[] pbw = client.DownloadData((string)req.data["url"]); pair.phone.InstallApp(pbw); } }
private void OnYcmdRequest(WebPebbleRequest data) { //Get the data passed in. string assetId = (string)data.data["asset_id"]; string projectId = (string)data.data["project_id"]; long lineNo = (long)data.data["line_no"]; long colNo = (long)data.data["col_no"]; string unsavedBuffer = (string)data.data["buffer"]; //Get the project requested. WebPebbleProject proj = null; var collect = Program.database.GetCollection <WebPebbleProject>("projects"); var projects = collect.Find(x => x.projectId == projectId && x.authorId == user_uuid); if (projects.Count() == 1) { proj = projects.ToArray()[0]; } if (proj == null) { return; } //Now, find the asset inside the project. var asset = proj.media[assetId]; if (asset == null) { return; } //Now that we have the pathname, prompt the proxy. YcmdCodeCompleteReplyWs reply = new YcmdCodeCompleteReplyWs(); /*if(new WebPebble.Entities.PebbleProject.PebbleProject(proj.projectId).package.pebble.sdkVersion == "2") * { * //Prompt only SDK 2. * reply.sdks.Add("sdk2_aplite", ycmd.YcmdCodeComplete.GetCodeComplete(asset.GetAbsolutePath(projectId), (int)colNo, (int)lineNo, unsavedBuffer, ycmd.YcmdProcesses.Any)); * } else * { * //Prompt all platforms on SDK 3. * reply.sdks.Add("sdk3_aplite", ycmd.YcmdCodeComplete.GetCodeComplete(asset.GetAbsolutePath(projectId), (int)colNo, (int)lineNo, unsavedBuffer, ycmd.YcmdProcesses.Any)); * reply.sdks.Add("sdk3_diorite", ycmd.YcmdCodeComplete.GetCodeComplete(asset.GetAbsolutePath(projectId), (int)colNo, (int)lineNo, unsavedBuffer, ycmd.YcmdProcesses.Any)); * reply.sdks.Add("sdk3_chalk", ycmd.YcmdCodeComplete.GetCodeComplete(asset.GetAbsolutePath(projectId), (int)colNo, (int)lineNo, unsavedBuffer, ycmd.YcmdProcesses.Any)); * }*/ reply.sdks.Add("sdk", ycmd.YcmdCodeComplete.GetCodeComplete(asset.GetAbsolutePath(projectId), (int)colNo, (int)lineNo, unsavedBuffer, ycmd.YcmdProcesses.Any, out string commands)); //Reply with this data. QuickReply(data.requestid, data.type, new Dictionary <string, object>() { { "ycmd", reply }, { "ycmd_commands", commands } }); }
private void DoGetScreenshot(WebPebbleRequest req) { pendingScreenshotRequestId = req.requestid; //If we're not connected, tell them so. if (!CheckIfConnected(req)) { return; } //Ask the client for a screenshot. pair.phone.GetScreenshot((byte[] data) => { //Create the reply and encode the image data as base64. string output = Convert.ToBase64String(data); QuickReply(pendingScreenshotRequestId, WebPebbleRequestType.Screenshot, new Dictionary <string, object>() { { "data", output }, { "img_header", "data:image/png;base64," }, { "download_header", "data:application/octet-stream;base64," } }); }); }
/* Api */ private void DoAuth(WebPebbleRequest req) { //Get the token that was offered. We get this from the HTTP request. string token = http.Request.Cookies["access-token"]; //Check this against the RPWS server. E_RPWS_User user = Oauth.RpwsAuth.AuthenticateUser(token); if (user == null) { //Respond with failure. QuickReply(req.requestid, WebPebbleRequestType.Auth, new Dictionary <string, object>() { { "ok", "false" } }); } else { user_uuid = user.uuid; authenticated = true; //Respond with ok. QuickReply(req.requestid, WebPebbleRequestType.Auth, new Dictionary <string, object>() { { "ok", "true" } }); //Add myself to the list of clients. if (WebPebble.WebSockets.WebSocketServer.connectedClients.ContainsKey(user_uuid)) { pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid]; //If the phone is connected, tell it we have connected. try { pair.phone.SetStatus(true); pair.connected = true; } catch { //Remain in "disconnected" state. pair.connected = false; } //If another WebPebble session is connected, boot them. try { pair.web.QuickReply(-1, WebPebbleRequestType.CloseOldClient, new Dictionary <string, object>() { }); } catch { } //Replace old WebPebble user, if any. WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid].web = this; //Set our connection status. SetStatus(pair.connected); } else { //Add ourself. WebSocketPair new_pair = new WebSocketPair { web = this }; WebPebble.WebSockets.WebSocketServer.connectedClients.Add(user_uuid, new_pair); this.pair = WebPebble.WebSockets.WebSocketServer.connectedClients[user_uuid]; } } }