private async Task InitializeWebsocket() { try { _socket = new Client(App.Server); var webcams = new List <string>(); var audioout = new List <string>(); Putik.Text = (await TerminalIdentificationManager.GetIdentificationInfoAsync()).Putik; _socket.Error += (oo, ee) => { Dispatcher.Invoke(async() => { status.Text = "Check internet connection."; await RestartApplicationWithCounter(_socket, 4); }); }; _socket.On("connect", msg => Dispatcher.Invoke(async() => { _socket.Emit("register", new { mac = (await TerminalIdentificationManager.GetIdentificationInfoAsync()).Putik, data = new { mac_addresses = new[] { (await TerminalIdentificationManager.GetIdentificationInfoAsync()).Putik }, name = Environment.MachineName, devices = new { videoin = webcams, audioin = audioout } } }); status.Text = "Connected! Registration was sent..."; await RestartApplicationWithCounter(_socket, 120); })); _socket.On("disconnect", msg => Dispatcher.Invoke(async() => { status.Text = "Connection lost"; await RestartApplicationWithCounter(_socket); })); _socket.On("registerDeclined", msg => Dispatcher.Invoke(async() => { status.Text = "Registration declined :("; await RestartApplicationWithCounter(_socket); })); _socket.On("errMacLicensed", msg => Dispatcher.Invoke(async() => { status.Text = "Mac address already exists :/"; await RestartApplicationWithCounter(_socket); })); _socket.On("putikExists", msg => Dispatcher.Invoke(async() => { status.Text = "Terminal Id already exists :/"; await RestartApplicationWithCounter(_socket); })); _socket.On("registerPending", msg => Dispatcher.Invoke(() => { status.Text = "Registration is pending..."; })); _socket.On("registerSuccessful", msg => Dispatcher.Invoke(async() => { status.Text = "Registration successful..."; await RestartApplicationWithCounter(_socket, 10); })); _socket.Message += (oo, ee) => Console.WriteLine(ee.Message.MessageText); _socket.Connect(); Dispatcher.Invoke(() => { status.Text = "Connecting..."; }); } catch (Exception ex) { await Dispatcher.Invoke(async() => { status.Text = ex.Message; await RestartApplicationWithCounter(_socket, 10); }); } }
private async void Stop() { Emit("offline", new { mac = (await TerminalIdentificationManager.GetIdentificationInfoAsync()).Putik }); _webSocket.Close(); _listener.Stop(); }
internal override async Task <bool> OnPreUpdate(SafeDictionary <string, object> keys) { ReportProgress("Identifying..."); int tries = 0; while (await TerminalIdentificationManager.GetIdentificationInfoAsync() == null && tries < 10) { tries++; await TerminalIdentificationManager.IdentifyAsync(); if (await TerminalIdentificationManager.GetIdentificationInfoAsync() == null) { await Task.Delay(30000); } } if (await TerminalIdentificationManager.GetIdentificationInfoAsync() == null) { App.ShutdownSafe(); return(false); } keys["server"] = _server = (await PanaceaRegistry.GetServerInformation(false)).ManagementServer; if (string.IsNullOrEmpty(_server)) { ReportProgress("Please add a terminal server in registry..."); await Task.Delay(4000); App.ShutdownSafe(); return(false); } //we can allow a failure to access TS if HS server is stored locally BrowserSettingsHelper.UpdateSettings(new[] { "WebBrowser.SubProcess.exe", "Panacea.exe" }); ServerResponse <GetHospitalServersResponse> getHospitalServerResponse = null; try { getHospitalServerResponse = await ServerRequestHelper.GetObjectFromServerAsync <GetHospitalServersResponse>(_server, "get_hospital_servers/"); } catch { getHospitalServerResponse = (await PanaceaRegistry.GetServerInformation(false)).ManagementServerResponse; } if (getHospitalServerResponse == null) { throw new Exception("Terminal Server unreachable and no information stored in registry..."); } if (!getHospitalServerResponse.Success) { if (getHospitalServerResponse.Error != "self destruct") { ReportProgress(getHospitalServerResponse.Error); await Task.Delay(4000); ProcessHelper.StartRegistrator(_server); App.ShutdownSafe(); return(false); } var path = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\..\\"); foreach (var directory in Directory.GetDirectories(path)) { try { if (!directory.EndsWith("\\Updater")) { Directory.Delete(directory, true); } } catch { // ignored } } foreach (var directory in Directory.GetFiles(path)) { try { File.Delete(directory); } catch { // ignored } } App.ShutdownSafe(); return(false); } if (!await UpdateRegistry(getHospitalServerResponse)) { return(false); } keys["hospitalserver"] = _hospitalServer = getHospitalServerResponse.Result.HospitalServers[0]; if (string.IsNullOrEmpty(getHospitalServerResponse.Result.Crutch)) { return(true); } _crutch = getHospitalServerResponse.Result.Crutch; ReportProgress("Waiting for traffic controller... Please wait..."); var req = BuildHttpRequest( new Uri(new Uri(_crutch), "api/" + Common.GetMacAddress() + "/requestAccessLock/") .ToString()); try { var resp = await req.GetHttpResponseAsync(120000); if (resp.StatusCode != HttpStatusCode.Conflict && resp.StatusCode != HttpStatusCode.OK) { throw new WebException("Not conflict"); } } catch (WebException) { ReportProgress("Traffic controller unreachable... Please wait..."); await Task.Delay(new Random().Next(10000, 60000)); App.Restart(); return(false); } return(true); }
private void PrepareWebSocket() { _manager = new CmdManager(_webSocket, this); _webSocket.RetryConnectionAttempts = 0; _webSocket.Error += _webSocket_Error; _webSocket.SocketConnectionClosed += _webSocket_SocketConnectionClosed; _webSocket.On("connect", async fn => { queue.RemoveAll(m => m.Verb == "online"); Emit("online", new { mac = (await TerminalIdentificationManager.GetIdentificationInfoAsync()).Putik }); await Task.Delay(new Random().Next(350, 2600)); var queue2 = queue.ToList(); foreach (var msg in queue2) { ExecuteCommand(msg); queue.Remove(msg); await Task.Delay(new Random().Next(150, 300)); } foreach (var client in _writers.Values.ToList()) { try { client.WriteLine(JsonConvert.SerializeObject(new Message() { Target = Target.Hospital, Verb = "ConnectionStatus", Object = "true" })); client.WriteLine(JsonConvert.SerializeObject(new Message() { Target = Target.Hospital, Verb = "connect", Object = "{}" })); await Task.Delay(new Random().Next(150, 300)); } catch { } } }); _webSocket.Message += async(sender, args) => { if (args.Message.Event == null || args.Message.Event == "connect") { return; } try { MessageFromServer o = JsonConvert.DeserializeObject <MessageFromServer>(args.Message.Json.Args != null && args.Message.Json.Args[0] != null ? args.Message.Json.Args[0].ToString() : "{}"); if (o.Action == "reboot") { try { if (o.Data != null) { if (o.Data.Delay > 0) { await Task.Delay(o.Data.Delay * 1000); } } } catch { } Stop(); Process.Start("shutdown.exe", "-f -r -t 0"); Application.Current.Shutdown(); } } catch { //ignore } var msg = new Message() { Verb = args.Message.Event, Object = args.Message.Json.Args != null && args.Message.Json.Args[0] != null ? args.Message.Json.Args[0].ToString() : "{}" }; EmitToClients(msg); }; }