ramp-thermostat.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.ramp_limit = Math.abs(parseInt(config.ramplimit)) || 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. // Objects do no guarentee order, lets sort it
  145. var points_arr = Object.keys(profile.points)
  146. .map(key=>{return profile.points[key]})
  147. .sort( (a,b)=> { return a.m - b.m });
  148. //console.log("name " + profile.name + " profile.points " + JSON.stringify(profile.points));
  149. for( var k=0; k<points_arr.length; k++ ) {
  150. point_mins = points_arr[k].m;
  151. //console.log("mins " + point_mins + " temp " + profile.points[k]);
  152. target = point_target = points_arr[k].t;
  153. if( points_arr.length == 1 ) {
  154. // no need to go any further
  155. break;
  156. }
  157. if (current_mins < point_mins) {
  158. if( point_mins - current_mins > this.ramp_limit ) {
  159. // Not yet in window to start ramping thermostat, stay constant
  160. target = pre_target;
  161. break;
  162. }
  163. if( point_mins - pre_mins > this.ramp_limit && point_mins - current_mins <= this.ramp_limit ) {
  164. // bring the previous point forward to match ramp max
  165. pre_mins = point_mins - this.ramp_limit;
  166. }
  167. gradient = (point_target - pre_target) / (point_mins - pre_mins);
  168. target = pre_target + (gradient * (current_mins - pre_mins));
  169. //this.warn("k=" + k +" gradient " + gradient + " target " + target);
  170. break;
  171. }
  172. pre_mins = point_mins;
  173. pre_target = point_target;
  174. }
  175. if(isNaN(target)) {
  176. this.warn("target undefined");
  177. return {"state":null, "target":0, "status": {fill:"red",shape:"dot",text:"No points in profile"}};
  178. }
  179. var target_plus = parseFloat((target + this.h_plus).toFixed(1));
  180. var target_minus = parseFloat((target - this.h_minus).toFixed(1));
  181. //this.warn(target_minus+" - "+target+" - "+target_plus);
  182. if (current > target_plus) {
  183. state = false;
  184. status = {fill:"grey",shape:"ring",text:current+" > "+target_plus+" ("+profile.name+")"};
  185. } else if (current < target_minus) {
  186. state = true;
  187. status = {fill:"yellow",shape:"dot",text:current+" < "+target_minus+" ("+profile.name+")"};
  188. } else if (current == target_plus) {
  189. state = null;
  190. status = {fill:"grey",shape:"ring",text:current+" = "+target_plus+" ("+profile.name+")"};
  191. } else if (current == target_minus) {
  192. state = null;
  193. status = {fill:"grey",shape:"ring",text:current+" = "+target_minus+" ("+profile.name+")"};
  194. } else {
  195. state = null;
  196. status = {fill:"grey",shape:"ring",text:target_minus+" < "+current+" < "+target_plus+" ("+profile.name+")"};
  197. }
  198. return {"state":state, "target":parseFloat(target.toFixed(1)), "status":status};
  199. }
  200. function setTarget(target) {
  201. var valid;
  202. var status = {};
  203. var profile = {};
  204. if (typeof target === "string") {
  205. target = parseFloat(target);
  206. }
  207. if (typeof target === "number") {
  208. profile.name = "manual";
  209. profile.points = {"1":{"m":0,"t":target},"2":{"m":1440,"t":target}};
  210. valid = true;
  211. status = {fill:"green",shape:"dot",text:"set target to "+target+" ("+profile.name+")"};
  212. } else {
  213. valid = false;
  214. status = {fill:"red",shape:"dot",text:"invalid type of target"};
  215. }
  216. return {"profile":profile, "status":status, "isValid": valid};
  217. }
  218. function setProfile(input) {
  219. var found = false;
  220. var status = {};
  221. var profile = {};
  222. var result = {};
  223. //var count = 0;
  224. var type = typeof input;
  225. switch (type) {
  226. case "string":
  227. if (input === "default") {
  228. profile.name = "default";
  229. found = true;
  230. } else {
  231. RED.nodes.eachNode(function(n) {
  232. if (n.type === "profile" && n.name === input) {
  233. profile.n = n;
  234. profile.name = n.name;
  235. result = getPoints(profile.n);
  236. if (result.isValid) {
  237. profile.points = result.points;
  238. } else {
  239. this.warn("Profile temperature not numeric");
  240. }
  241. found = true;
  242. }
  243. //count++;
  244. });
  245. //console.log("count " + count);
  246. if (found) {
  247. status = {fill:"green",shape:"dot",text:"profile set to "+profile.name};
  248. } else {
  249. status = {fill:"red",shape:"dot",text:input+" not found"};
  250. }
  251. }
  252. break;
  253. case "object":
  254. profile.name = input.name || "input profile";
  255. var points = {};
  256. var arr, minutes;
  257. var i = 1;
  258. for (var k in input.points) {
  259. arr = k.split(":");
  260. minutes = parseInt(arr[0])*60 + parseInt(arr[1]);
  261. points[i] = JSON.parse('{"m":' + minutes + ',"t":' + input.points[k] + '}');
  262. //points[minutes] = input.points[k];
  263. i++;
  264. }
  265. profile.points = points;
  266. found = true;
  267. status = {fill:"green",shape:"dot",text:"profile set to "+profile.name};
  268. //console.log(points);
  269. break;
  270. default:
  271. status = {fill:"red",shape:"dot",text:"invalid type "+type};
  272. }
  273. return {"profile":profile, "status":status, "found":found};
  274. }
  275. function getPoints(n) {
  276. var timei, tempi, arr, minutes;
  277. var points = {};
  278. var valid = true;
  279. var points_str = '{';
  280. for (var i=1; i<=10; i++) {
  281. timei = "time"+i;
  282. tempi = "temp"+i;
  283. if (isNaN(n[tempi])) {
  284. valid = false;
  285. } else {
  286. if (typeof(n[timei]) !== "undefined" && n[timei] !== "") {
  287. arr = n[timei].split(":");
  288. minutes = parseInt(arr[0])*60 + parseInt(arr[1]);
  289. points_str += '"' + i + '":{"m":' + minutes + ',"t":' + n[tempi] + '},';
  290. }
  291. }
  292. }
  293. if (valid) {
  294. points_str = points_str.slice(0,points_str.length-1);
  295. points_str += '}';
  296. //console.log(points_str);
  297. points = JSON.parse(points_str);
  298. //console.log(JSON.stringify(points));
  299. }
  300. return {"points":points, "isValid":valid};
  301. }
  302. function readNodeVersion () {
  303. var package_json = "../package.json";
  304. //console.log(__dirname+" - "+package_json);
  305. var packageJSONPath = path.join(__dirname, package_json);
  306. var packageJSON = JSON.parse(fs.readFileSync(packageJSONPath));
  307. return packageJSON.version;
  308. }
  309. function read_npmVersion(pck, callback) {
  310. var exec = require('child_process').exec;
  311. var cmd = 'npm view '+pck+' version';
  312. var npm_version;
  313. exec(cmd, function(error, stdout, stderr) {
  314. npm_version = stdout.trim();
  315. callback(npm_version);
  316. //console.log("npm_version "+npm_version);
  317. });
  318. }
  319. }