public void runServiceCheck() { Utils util = new Utils(); ServiceController sc = null; try { sc = new ServiceController(serviceName); switch (sc.Status) { case ServiceControllerStatus.Running: break; case ServiceControllerStatus.Stopped: sc.Start(); util.writeToLogFile("service checker starting "+serviceName); break; case ServiceControllerStatus.Paused: sc.Stop(); sc.Start(); util.writeToLogFile("service checker starting " + serviceName); break; case ServiceControllerStatus.StopPending: sc.Stop(); sc.Start(); util.writeToLogFile("service checker starting " + serviceName); break; case ServiceControllerStatus.StartPending: sc.Stop(); sc.Start(); util.writeToLogFile("service checker starting " + serviceName); break; default: break; } } catch (Exception e) { util.writeEventLog(e.Message); util.writeEventLog(e.StackTrace); } finally { try { if (sc != null) sc.Close(); } catch (Exception) {} } }
/// <summary> /// Sends a heart-beat to the server, while also checking for updates /// </summary> public void runHeartBeat() { Boolean hasNewVersion = false; Constants.STATIC_RUN_COUNTER++; Utils util = new Utils(); try { ConfigObj cfg = util.readConfig(); if (cfg != null && cfg.remoteTarget != null && cfg.remoteServer != null && cfg.strId != null) { String data = util.getUrlStatusCode(cfg.remoteTarget); HeartbeatData h = new HeartbeatData(); h.downloadSpeed = WebUtil.DownloadSpeed(false); runUploadTrafficSensor(1024, out h.uploadSpeed); h.assVersion = util.getAssemblyVersion(); h.errorCounter = Constants.STATIC_ERROR_COUNTER; h.runCounter = Constants.STATIC_RUN_COUNTER; h.strId = cfg.strId; h.version = cfg.version; string json = JsonConvert.SerializeObject(h); String link = cfg.remoteServer + "?action=ACK&data=" + data + "&version=" + util.getAssemblyVersion() + "&strId=" + cfg.strId + "&errorcounter=" + Constants.STATIC_ERROR_COUNTER + "&runcounter=" + Constants.STATIC_RUN_COUNTER; link = link.Trim(); String code = util.getUrlStatusCode(link); util.writeToLogFile(link); if (code != null) { if (!code.Equals("OK")) { Constants.STATIC_ERROR_COUNTER++; } else { hasNewVersion = runDownloadTrafficSensor(); runUploadTrafficSensor(); if (hasNewVersion) { updateService(); } } } else { Constants.STATIC_ERROR_COUNTER++; } } } catch (Exception e) { Constants.STATIC_ERROR_COUNTER++; util.writeEventLog(e.Message); util.writeEventLog(e.StackTrace); } }