Skip to content

Commit

Permalink
Web remote power menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Jun 14, 2024
1 parent 1a1bb6d commit af313ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
53 changes: 20 additions & 33 deletions packages/ui/components/powerDropdown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@
>
<DropdownMenu.Item
on:click={() => {
connect("sleep")
action("disconnect")
}}
class="flex cursor-pointer select-none items-center gap-3 rounded-xl p-2 duration-200 ease-in-out data-[highlighted]:bg-gray-600"
>
<div class="flex items-center">
<ScreenShareOff />
</div>
<div class="flex flex-col items-start">
<h4>Disconnect</h4>
</div>
</DropdownMenu.Item>
<DropdownMenu.Separator class="my-1 -ml-1 -mr-1 block h-px bg-gray-600" />
<DropdownMenu.Item
on:click={() => {
action("sleep")
}}
class="flex cursor-pointer select-none items-center gap-3 rounded-xl p-2 duration-200 ease-in-out data-[highlighted]:bg-gray-600"
>
Expand All @@ -26,7 +40,7 @@
<DropdownMenu.Separator class="my-1 -ml-1 -mr-1 block h-px bg-gray-600" />
<DropdownMenu.Item
on:click={() => {
connect("shutdown")
action("shutdown")
}}
class="flex cursor-pointer select-none items-center gap-3 rounded-xl p-2 duration-200 ease-in-out data-[highlighted]:bg-gray-600"
>
Expand All @@ -40,7 +54,7 @@
<DropdownMenu.Separator class="my-1 -ml-1 -mr-1 block h-px bg-gray-600" />
<DropdownMenu.Item
on:click={() => {
connect("restart")
action("restart")
}}
class="flex cursor-pointer select-none items-center gap-3 rounded-xl p-2 duration-200 ease-in-out data-[highlighted]:bg-gray-600"
>
Expand All @@ -55,36 +69,9 @@
</DropdownMenu.Root>

<script lang="ts">
import { Avatar, DropdownMenu } from "bits-ui"
import { DropdownMenu } from "bits-ui"
import { flyAndScale } from "../utils/transitions.ts"
import { Moon, Power, RotateCcw, UserCircle } from "lucide-svelte"
import { settings } from "ui/stores/settings.ts"
import { EzRTCClient } from "ezrtc"
let connect = (action: string) => {
let client: EzRTCClient
let sent = false
client = new EzRTCClient("wss://rtc-usw.levminer.com/one-to-many", $settings.connectionCode, [
{
urls: "stun:stun.relay.metered.ca:80",
},
{
urls: "turn:standard.relay.metered.ca:80",
username: "56feef2e09dcd8d33c5f67eb",
credential: "ynk5rIg6gGh4lEAk",
},
])
client.onMessage((message) => {
if (!sent) {
client.dataChannel.send(action)
sent = true
}
})
import { Moon, Power, RotateCcw, ScreenShareOff } from "lucide-svelte"
/* client.peerConnection.onconnectionstatechange = (event) => {
console.log("EEEEEEEEVVVVVEEEEENNNNttt")
} */
}
export let action = (type: string) => {}
</script>
4 changes: 2 additions & 2 deletions platforms/windows/lib/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public API Data {
}

public class Message {
public string Name {
public string Type {

Check warning on line 33 in platforms/windows/lib/Interfaces.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'Type' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
get; set;
}

public string Content {
public string Data {
get; set;
}
}
Expand Down
28 changes: 28 additions & 0 deletions platforms/windows/service/WSServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ static async Task ProcessWebSocketRequestAsync(WebSocket socket, HardwareInfo ha
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
connectedClients.TryRemove(socket, out _);
break;
} else if (result.MessageType == WebSocketMessageType.Text) {
string receivedText = Encoding.UTF8.GetString(receiveBuffer.Array, receiveBuffer.Offset, result.Count);
HandleMessage(receivedText);
}
}

Expand All @@ -75,4 +78,29 @@ static async Task ProcessWebSocketRequestAsync(WebSocket socket, HardwareInfo ha
public void Stop() {
listener.Stop();
}

static void HandleMessage(string message) {
try {
var netMessage = JsonSerializer.Deserialize<Message>(message, Program.SerializerOptions);

switch (netMessage?.Data) {
case "shutdown":
Commands.ExecuteCommand(@"shutdown /s /t 30");
break;

case "sleep":
Commands.ExecuteCommand("Start-Process rundll32.exe -ArgumentList 'powrprof.dll,SetSuspendState 0,1,0'");
break;

case "restart":
Commands.ExecuteCommand("shutdown /r /t 30");
break;
}

return;
}
catch (Exception) {
Console.WriteLine("Invalid message");
}
}
}

0 comments on commit af313ff

Please sign in to comment.