ramp-thermostat.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. "use strict";
  2. var fs = require('fs');
  3. var path = require('path');
  4. module.exports = function(RED) {
  5. function Profile(n) {
  6. RED.nodes.createNode(this,n);
  7. this.n = n;
  8. this.name = n.name;
  9. }
  10. RED.nodes.registerType("profile",Profile);
  11. function RampThermostat(config) {
  12. RED.nodes.createNode(this, config);
  13. //this.warn(node_name+" - "+JSON.stringify(this));
  14. var node_name;
  15. if (typeof this.name !== "undefined" ) {
  16. node_name = this.name.replace(/\s/g, "_");
  17. } else {
  18. node_name = this.id;
  19. }
  20. var globalContext = this.context().global;
  21. this.current_status = {};
  22. this.points_result = {};
  23. this.h_plus = Math.abs(parseFloat(config.hysteresisplus)) || 0;
  24. this.h_minus = Math.abs(parseFloat(config.hysteresisminus)) || 0;
  25. this.h_left = Math.abs(parseInt(config.hysteresisleft)) || 1440;
  26. // experimental
  27. //this.profile = globalContext.get(node_name);
  28. //if (typeof this.profile === "undefined") {
  29. this.profile = RED.nodes.getNode(config.profile);
  30. this.points_result = getPoints(this.profile.n);
  31. if (this.points_result.isValid) {
  32. this.profile.points = this.points_result.points;
  33. } else {
  34. this.warn("Profile temperature not numeric");
  35. }
  36. globalContext.set(node_name, this.profile);
  37. //}
  38. this.current_status = {fill:"green",shape:"dot",text:"profile set to "+this.profile.name};
  39. this.status(this.current_status);
  40. //this.warn(node_name+" - "+JSON.stringify(this.profile));
  41. this.on('input', function(msg) {
  42. var msg1 = {"topic":"state"};
  43. var msg2 = {"topic":"current"};
  44. var msg3 = {"topic":"target"};
  45. var result = {};
  46. //this.warn(JSON.stringify(msg));
  47. if (typeof msg.payload === "undefined") {
  48. this.warn("msg.payload undefined");
  49. } else {
  50. switch (msg.topic) {
  51. case "setCurrent":
  52. case "setcurrent":
  53. case "":
  54. case undefined:
  55. if (typeof msg.payload === "string") {
  56. msg.payload = parseFloat(msg.payload);
  57. }
  58. if (isNaN(msg.payload)) {
  59. this.warn("Non numeric input");
  60. } else {
  61. result = this.getState(msg.payload, this.profile);
  62. if (result.state !== null) {
  63. msg1.payload = result.state;
  64. } else {
  65. msg1 = null;
  66. }
  67. msg2.payload = msg.payload;
  68. msg3.payload = result.target;
  69. this.send([msg1, msg2, msg3]);
  70. this.current_status = result.status;
  71. this.status(this.current_status);
  72. }
  73. break;
  74. case "setTarget":
  75. case "settarget":
  76. result = setTarget(msg.payload);
  77. if (result.isValid) {
  78. this.profile = result.profile;
  79. globalContext.set(node_name, this.profile);
  80. this.current_status = result.status;
  81. this.status(this.current_status);
  82. }
  83. break;
  84. case "setProfile":
  85. case "setprofile":
  86. //this.warn(JSON.stringify(msg.payload));
  87. result = setProfile(msg.payload);
  88. if (result.found) {
  89. this.profile = result.profile;
  90. if (this.profile.name === "default") {
  91. this.profile = RED.nodes.getNode(config.profile);
  92. this.points_result = getPoints(this.profile.n);
  93. if (this.points_result.isValid) {
  94. this.profile.points = this.points_result.points;
  95. } else {
  96. this.warn("Profile temperature not numeric.");
  97. }
  98. //this.warn("default "+this.profile.name);
  99. this.current_status = {fill:"green",shape:"dot",text:"profile set to default ("+this.profile.name+")"};
  100. } else {
  101. this.current_status = result.status;
  102. }
  103. globalContext.set(node_name, this.profile);
  104. } else {
  105. this.current_status = result.status;
  106. this.warn(msg.payload+" not found");
  107. }
  108. this.status(this.current_status);
  109. break;
  110. case "checkUpdate":
  111. case "checkupdate":
  112. var version = readNodeVersion();
  113. var pck_name = "node-red-contrib-ramp-thermostat";
  114. read_npmVersion(pck_name, function(npm_version) {
  115. if (npm_version > version) {
  116. this.warn("A new "+pck_name+" version "+npm_version+" is avaiable.");
  117. } else {
  118. this.warn(pck_name+" "+version+" is up to date.");
  119. }
  120. }.bind(this));
  121. this.warn("ramp-thermostat version: "+version);
  122. this.status({fill:"green",shape:"dot",text:"version: "+version});
  123. var set_timeout = setTimeout(function() {
  124. this.status(this.current_status);
  125. }.bind(this), 4000);
  126. break;
  127. default:
  128. this.warn("invalid topic >"+msg.topic+"<");
  129. }
  130. }
  131. });
  132. }
  133. RED.nodes.registerType("ramp-thermostat",RampThermostat);
  134. /**
  135. * ramp-thermostat specific functions
  136. **/
  137. RampThermostat.prototype.getState = function(current, profile) {
  138. var point_mins, pre_mins, pre_target, point_target, target, gradient;
  139. var state;
  140. var status = {};
  141. current = parseFloat((current).toFixed(1));
  142. var date = new Date();
  143. var current_mins = date.getHours()*60 + date.getMinutes();
  144. //console.log("name " + profile.name + " profile.points " + JSON.stringify(profile.points));
  145. for (var k in profile.points) {
  146. point_mins = parseInt(profile.points[k].m);
  147. //console.log("mins " + point_mins + " temp " + profile.points[k]);
  148. point_target = profile.points[k].t;
  149. if (current_mins < point_mins) {
  150. if( point_mins - current_mins > this.h_left ) {
  151. // Not yet in window to start ramping thermostat, stay constant
  152. target = pre_target;
  153. break;
  154. }
  155. if( point_mins - pre_mins > this.h_left && point_mins - current_mins <= this.h_left ) {
  156. pre_mins = point_mins - this.h_left;
  157. }
  158. gradient = (point_target - pre_target) / (point_mins - pre_mins);
  159. target = pre_target + (gradient * (current_mins - pre_mins));
  160. //this.warn("k=" + k +" gradient " + gradient + " target " + target);
  161. break;
  162. }
  163. pre_mins = point_mins;
  164. pre_target = point_target;
  165. }
  166. if(isNaN(target)) {
  167. this.warn("target undefined");
  168. }
  169. var target_plus = parseFloat((target + this.h_plus).toFixed(1));
  170. var target_minus = parseFloat((target - this.h_minus).toFixed(1));
  171. //this.warn(target_minus+" - "+target+" - "+target_plus);
  172. if (current > target_plus) {
  173. state = false;
  174. status = {fill:"grey",shape:"ring",text:current+" > "+target_plus+" ("+profile.name+")"};
  175. } else if (current < target_minus) {
  176. state = true;
  177. status = {fill:"yellow",shape:"dot",text:current+" < "+target_minus+" ("+profile.name+")"};
  178. } else if (current == target_plus) {
  179. state = null;
  180. status = {fill:"grey",shape:"ring",text:current+" = "+target_plus+" ("+profile.name+")"};
  181. } else if (current == target_minus) {
  182. state = null;
  183. status = {fill:"grey",shape:"ring",text:current+" = "+target_minus+" ("+profile.name+")"};
  184. } else {
  185. state = null;
  186. status = {fill:"grey",shape:"ring",text:target_minus+" < "+current+" < "+target_plus+" ("+profile.name+")"};
  187. }
  188. return {"state":state, "target":parseFloat(target.toFixed(1)), "status":status};
  189. }
  190. function setTarget(target) {
  191. var valid;
  192. var status = {};
  193. var profile = {};
  194. if (typeof target === "string") {
  195. target = parseFloat(target);
  196. }
  197. if (typeof target === "number") {
  198. profile.name = "manual";
  199. profile.points = {"1":{"m":0,"t":target},"2":{"m":1440,"t":target}};
  200. valid = true;
  201. status = {fill:"green",shape:"dot",text:"set target to "+target+" ("+profile.name+")"};
  202. } else {
  203. valid = false;
  204. status = {fill:"red",shape:"dot",text:"invalid type of target"};
  205. }
  206. return {"profile":profile, "status":status, "isValid": valid};
  207. }
  208. function setProfile(input) {
  209. var found = false;
  210. var status = {};
  211. var profile = {};
  212. var result = {};
  213. //var count = 0;
  214. var type = typeof input;
  215. switch (type) {
  216. case "string":
  217. if (input === "default") {
  218. profile.name = "default";
  219. found = true;
  220. } else {
  221. RED.nodes.eachNode(function(n) {
  222. if (n.type === "profile" && n.name === input) {
  223. profile.n = n;
  224. profile.name = n.name;
  225. result = getPoints(profile.n);
  226. if (result.isValid) {
  227. profile.points = result.points;
  228. } else {
  229. this.warn("Profile temperature not numeric");
  230. }
  231. found = true;
  232. }
  233. //count++;
  234. });
  235. //console.log("count " + count);
  236. if (found) {
  237. status = {fill:"green",shape:"dot",text:"profile set to "+profile.name};
  238. } else {
  239. status = {fill:"red",shape:"dot",text:input+" not found"};
  240. }
  241. }
  242. break;
  243. case "object":
  244. profile.name = input.name || "input profile";
  245. var points = {};
  246. var arr, minutes;
  247. var i = 1;
  248. for (var k in input.points) {
  249. arr = k.split(":");
  250. minutes = parseInt(arr[0])*60 + parseInt(arr[1]);
  251. points[i] = JSON.parse('{"m":' + minutes + ',"t":' + input.points[k] + '}');
  252. //points[minutes] = input.points[k];
  253. i++;
  254. }
  255. profile.points = points;
  256. found = true;
  257. status = {fill:"green",shape:"dot",text:"profile set to "+profile.name};
  258. //console.log(points);
  259. break;
  260. default:
  261. status = {fill:"red",shape:"dot",text:"invalid type "+type};
  262. }
  263. return {"profile":profile, "status":status, "found":found};
  264. }
  265. function getPoints(n) {
  266. var timei, tempi, arr, minutes;
  267. var points = {};
  268. var valid = true;
  269. var points_str = '{';
  270. for (var i=1; i<=10; i++) {
  271. timei = "time"+i;
  272. tempi = "temp"+i;
  273. if (isNaN(n[tempi])) {
  274. valid = false;
  275. } else {
  276. if (typeof(n[timei]) !== "undefined" && n[timei] !== "") {
  277. arr = n[timei].split(":");
  278. minutes = parseInt(arr[0])*60 + parseInt(arr[1]);
  279. points_str += '"' + i + '":{"m":' + minutes + ',"t":' + n[tempi] + '},';
  280. }
  281. }
  282. }
  283. if (valid) {
  284. points_str = points_str.slice(0,points_str.length-1);
  285. points_str += '}';
  286. //console.log(points_str);
  287. points = JSON.parse(points_str);
  288. //console.log(JSON.stringify(points));
  289. }
  290. return {"points":points, "isValid":valid};
  291. }
  292. function readNodeVersion () {
  293. var package_json = "../package.json";
  294. //console.log(__dirname+" - "+package_json);
  295. var packageJSONPath = path.join(__dirname, package_json);
  296. var packageJSON = JSON.parse(fs.readFileSync(packageJSONPath));
  297. return packageJSON.version;
  298. }
  299. function read_npmVersion(pck, callback) {
  300. var exec = require('child_process').exec;
  301. var cmd = 'npm view '+pck+' version';
  302. var npm_version;
  303. exec(cmd, function(error, stdout, stderr) {
  304. npm_version = stdout.trim();
  305. callback(npm_version);
  306. //console.log("npm_version "+npm_version);
  307. });
  308. }
  309. }