Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. // Use separate scope to prevent global scope pollution
  3. (function () {
  4. const config = {};
  5. /**
  6. * Helper function to get server address/hostname from either the commandline or env
  7. */
  8. function getServerAddress() {
  9. /**
  10. * Get command line parameters
  11. * Assumes that a cmdline parameter is defined with `--key [value]`
  12. *
  13. * @param {string} key key to look for at the command line
  14. * @param {string} defaultValue value if no key is given at the command line
  15. * @returns {string} the value of the parameter
  16. */
  17. function getCommandLineParameter(key, defaultValue = undefined) {
  18. const index = process.argv.indexOf(`--${key}`);
  19. const value = index > -1 ? process.argv[index + 1] : undefined;
  20. return value !== undefined ? String(value) : defaultValue;
  21. }
  22. // Prefer command line arguments over environment variables
  23. ["address", "port"].forEach((key) => {
  24. config[key] = getCommandLineParameter(key, process.env[key.toUpperCase()]);
  25. });
  26. // determine if "--use-tls"-flag was provided
  27. config["tls"] = process.argv.indexOf("--use-tls") > 0;
  28. }
  29. /**
  30. * Gets the config from the specified server url
  31. *
  32. * @param {string} url location where the server is running.
  33. * @returns {Promise} the config
  34. */
  35. function getServerConfig(url) {
  36. // Return new pending promise
  37. return new Promise((resolve, reject) => {
  38. // Select http or https module, depending on requested url
  39. const lib = url.startsWith("https") ? require("https") : require("http");
  40. const request = lib.get(url, (response) => {
  41. let configData = "";
  42. // Gather incoming data
  43. response.on("data", function (chunk) {
  44. configData += chunk;
  45. });
  46. // Resolve promise at the end of the HTTP/HTTPS stream
  47. response.on("end", function () {
  48. resolve(JSON.parse(configData));
  49. });
  50. });
  51. request.on("error", function (error) {
  52. reject(new Error(`Unable to read config from server (${url} (${error.message}`));
  53. });
  54. });
  55. }
  56. /**
  57. * Print a message to the console in case of errors
  58. *
  59. * @param {string} message error message to print
  60. * @param {number} code error code for the exit call
  61. */
  62. function fail(message, code = 1) {
  63. if (message !== undefined && typeof message === "string") {
  64. console.log(message);
  65. } else {
  66. console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080 [--use-tls]'");
  67. }
  68. process.exit(code);
  69. }
  70. getServerAddress();
  71. (config.address && config.port) || fail();
  72. const prefix = config.tls ? "https://" : "http://";
  73. // Only start the client if a non-local server was provided
  74. if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
  75. getServerConfig(`${prefix}${config.address}:${config.port}/config/`)
  76. .then(function (configReturn) {
  77. // Pass along the server config via an environment variable
  78. const env = Object.create(process.env);
  79. const options = { env: env };
  80. configReturn.address = config.address;
  81. configReturn.port = config.port;
  82. configReturn.tls = config.tls;
  83. env.config = JSON.stringify(configReturn);
  84. // Spawn electron application
  85. const electron = require("electron");
  86. const child = require("child_process").spawn(electron, ["js/electron.js"], options);
  87. // Pipe all child process output to current stdout
  88. child.stdout.on("data", function (buf) {
  89. process.stdout.write(`Client: ${buf}`);
  90. });
  91. // Pipe all child process errors to current stderr
  92. child.stderr.on("data", function (buf) {
  93. process.stderr.write(`Client: ${buf}`);
  94. });
  95. child.on("error", function (err) {
  96. process.stdout.write(`Client: ${err}`);
  97. });
  98. child.on("close", (code) => {
  99. if (code !== 0) {
  100. console.log(`There something wrong. The clientonly is not running code ${code}`);
  101. }
  102. });
  103. })
  104. .catch(function (reason) {
  105. fail(`Unable to connect to server: (${reason})`);
  106. });
  107. } else {
  108. fail();
  109. }
  110. })();