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.

compliments.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Magic Mirror
  2. * Module: Compliments
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. Module.register("compliments", {
  8. // Module config defaults.
  9. defaults: {
  10. compliments: {
  11. anytime: ["Hey there sexy!"],
  12. morning: ["Good morning, handsome!", "Enjoy your day!", "How was your sleep?"],
  13. afternoon: ["Hello, beauty!", "You look sexy!", "Looking good today!"],
  14. evening: ["Wow, you look hot!", "You look nice!", "Hi, sexy!"],
  15. "....-01-01": ["Happy new year!"]
  16. },
  17. updateInterval: 30000,
  18. remoteFile: null,
  19. fadeSpeed: 4000,
  20. morningStartTime: 3,
  21. morningEndTime: 12,
  22. afternoonStartTime: 12,
  23. afternoonEndTime: 17,
  24. random: true,
  25. mockDate: null
  26. },
  27. lastIndexUsed: -1,
  28. // Set currentweather from module
  29. currentWeatherType: "",
  30. // Define required scripts.
  31. getScripts: function () {
  32. return ["moment.js"];
  33. },
  34. // Define start sequence.
  35. start: function () {
  36. Log.info("Starting module: " + this.name);
  37. this.lastComplimentIndex = -1;
  38. if (this.config.remoteFile !== null) {
  39. this.complimentFile((response) => {
  40. this.config.compliments = JSON.parse(response);
  41. this.updateDom();
  42. });
  43. }
  44. // Schedule update timer.
  45. setInterval(() => {
  46. this.updateDom(this.config.fadeSpeed);
  47. }, this.config.updateInterval);
  48. },
  49. /**
  50. * Generate a random index for a list of compliments.
  51. *
  52. * @param {string[]} compliments Array with compliments.
  53. * @returns {number} a random index of given array
  54. */
  55. randomIndex: function (compliments) {
  56. if (compliments.length === 1) {
  57. return 0;
  58. }
  59. const generate = function () {
  60. return Math.floor(Math.random() * compliments.length);
  61. };
  62. let complimentIndex = generate();
  63. while (complimentIndex === this.lastComplimentIndex) {
  64. complimentIndex = generate();
  65. }
  66. this.lastComplimentIndex = complimentIndex;
  67. return complimentIndex;
  68. },
  69. /**
  70. * Retrieve an array of compliments for the time of the day.
  71. *
  72. * @returns {string[]} array with compliments for the time of the day.
  73. */
  74. complimentArray: function () {
  75. const hour = moment().hour();
  76. const date = this.config.mockDate ? this.config.mockDate : moment().format("YYYY-MM-DD");
  77. let compliments;
  78. if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) {
  79. compliments = this.config.compliments.morning.slice(0);
  80. } else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime && this.config.compliments.hasOwnProperty("afternoon")) {
  81. compliments = this.config.compliments.afternoon.slice(0);
  82. } else if (this.config.compliments.hasOwnProperty("evening")) {
  83. compliments = this.config.compliments.evening.slice(0);
  84. }
  85. if (typeof compliments === "undefined") {
  86. compliments = [];
  87. }
  88. if (this.currentWeatherType in this.config.compliments) {
  89. compliments.push.apply(compliments, this.config.compliments[this.currentWeatherType]);
  90. }
  91. compliments.push.apply(compliments, this.config.compliments.anytime);
  92. for (let entry in this.config.compliments) {
  93. if (new RegExp(entry).test(date)) {
  94. compliments.push.apply(compliments, this.config.compliments[entry]);
  95. }
  96. }
  97. return compliments;
  98. },
  99. /**
  100. * Retrieve a file from the local filesystem
  101. *
  102. * @param {Function} callback Called when the file is retrieved.
  103. */
  104. complimentFile: function (callback) {
  105. const xobj = new XMLHttpRequest(),
  106. isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
  107. path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
  108. xobj.overrideMimeType("application/json");
  109. xobj.open("GET", path, true);
  110. xobj.onreadystatechange = function () {
  111. if (xobj.readyState === 4 && xobj.status === 200) {
  112. callback(xobj.responseText);
  113. }
  114. };
  115. xobj.send(null);
  116. },
  117. /**
  118. * Retrieve a random compliment.
  119. *
  120. * @returns {string} a compliment
  121. */
  122. randomCompliment: function () {
  123. // get the current time of day compliments list
  124. const compliments = this.complimentArray();
  125. // variable for index to next message to display
  126. let index;
  127. // are we randomizing
  128. if (this.config.random) {
  129. // yes
  130. index = this.randomIndex(compliments);
  131. } else {
  132. // no, sequential
  133. // if doing sequential, don't fall off the end
  134. index = this.lastIndexUsed >= compliments.length - 1 ? 0 : ++this.lastIndexUsed;
  135. }
  136. return compliments[index] || "";
  137. },
  138. // Override dom generator.
  139. getDom: function () {
  140. const wrapper = document.createElement("div");
  141. wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line";
  142. // get the compliment text
  143. const complimentText = this.randomCompliment();
  144. // split it into parts on newline text
  145. const parts = complimentText.split("\n");
  146. // create a span to hold it all
  147. const compliment = document.createElement("span");
  148. // process all the parts of the compliment text
  149. for (const part of parts) {
  150. // create a text element for each part
  151. compliment.appendChild(document.createTextNode(part));
  152. // add a break `
  153. compliment.appendChild(document.createElement("BR"));
  154. }
  155. // remove the last break
  156. compliment.lastElementChild.remove();
  157. wrapper.appendChild(compliment);
  158. return wrapper;
  159. },
  160. // From data currentweather set weather type
  161. setCurrentWeatherType: function (type) {
  162. this.currentWeatherType = type;
  163. },
  164. // Override notification handler.
  165. notificationReceived: function (notification, payload, sender) {
  166. if (notification === "CURRENTWEATHER_TYPE") {
  167. this.setCurrentWeatherType(payload.type);
  168. }
  169. }
  170. });