Last active
May 1, 2020 18:52
-
-
Save donpdonp/830ded24ff7e71d71a622d1df7f507de to your computer and use it in GitHub Desktop.
gluon freeze watch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| alerting_cache() | |
| return {name:"freeze"} | |
| }) | |
| var alert_channel = "#pdxbots" | |
| var minor_channel = "#zrobo" | |
| var freezing = false | |
| var flip_time | |
| var inwarming = false | |
| var inwarm_time | |
| var db_freezing = "freezewatch:freezing" | |
| var db_fliptime = "freezewatch:fliptime" | |
| var history = [] | |
| var history_length = 60 | |
| var alert_min_hours = 2 | |
| function alerting_cache() { | |
| db.get(db_fliptime, function(timestamp){ | |
| if(timestamp && timestamp.length == 24) { | |
| flip_time = new Date(timestamp) | |
| } | |
| // step2 | |
| db.get(db_freezing, function(freeze){ | |
| freezing = freeze == "1" ? true : false | |
| bot.say(bot.admin_channel, "freeze cache set freezing to "+freezing+" and time "+flip_time+" ") | |
| }) | |
| }) | |
| } | |
| function go(msg) { | |
| if (msg.method == "clocktower") { | |
| var outdoor = get_sensor("h3936-out2") | |
| if(outdoor) { | |
| history_push(outdoor.temp) | |
| var hours = ((new Date()) - flip_time)/1000/60/60 | |
| var channel = hours < alert_min_hours ? bot.admin_channel : minor_channel | |
| if (outdoor.temp <= 0) { | |
| if (!freezing) { | |
| bot.say(channel, "freeze alert! "+format_outdoor(outdoor)+" ❄️❄️❄️"+" was warm for "+timewords(hours)) | |
| freezing = true | |
| flip_time = freeze(freezing) | |
| } | |
| } | |
| if (outdoor.temp > 0) { | |
| if (freezing) { | |
| bot.say(channel, "thaw alert "+format_outdoor(outdoor)+". was freezing for "+timewords(hours)) | |
| freezing = false | |
| flip_time = freeze(freezing) | |
| } | |
| } | |
| var indoor = get_sensor("h3936-up1") | |
| var inoutdiff = indoor.temp - outdoor.temp | |
| if(Math.abs(inoutdiff) > 1) { | |
| if (indoor.temp > outdoor.temp) { | |
| if (!inwarming) { | |
| bot.say(bot.admin_channel, "inside is warm. stay in! indoor="+indoor.temp+" outdoor="+outdoor.temp) | |
| inwarming = true | |
| } | |
| } else { | |
| if (inwarming) { | |
| bot.say(bot.admin_channel, "inside is cold! go out! indoor="+indoor.temp+" outdoor="+outdoor.temp) | |
| inwarming = false | |
| } | |
| } | |
| } | |
| } | |
| var changes_5min = history_changes(5) | |
| var changes_5min_limit = 4 // Celcius | |
| if(changes_5min.count == 5) { | |
| var templog = history.slice(history.length-5, history.length).join(', ') | |
| if(changes_5min.up > changes_5min_limit) { | |
| bot.say(minor_channel, "Warning: 5 min outdoor temp change UP of "+changes_5min.up.toFixed(1)+"C "+JSON.stringify(templog)) | |
| } | |
| if(changes_5min.down > changes_5min_limit) { | |
| bot.say(minor_channel, "Warning: 5 min outdoor temp change DOWN of "+changes_5min.down.toFixed(1)+"C "+JSON.stringify(templog)) | |
| } | |
| } | |
| } | |
| if (msg.method == "irc.privmsg") { | |
| var match = /^!?freeze(\s+(\w+))?$/.exec(msg.params.message) | |
| if(match) { | |
| var outdoor = get_sensor("h3936-out2") | |
| var indoor = get_sensor("h3936-up1") | |
| var delay_sec = (new Date() - outdoor.time) /1000 | |
| var changes_hr = history_changes(history_length) | |
| var changes_5min = history_changes(5) | |
| var yesno = outdoor.temp < 0 ? "Freezing" : "" | |
| var freezing_for = "" | |
| if(flip_time) { | |
| var hours = ((new Date()) - flip_time)/1000/60/60 | |
| if (outdoor.temp < 0) { | |
| freezing_for = "freezing" | |
| } else { | |
| freezing_for = "warm" | |
| } | |
| freezing_for += " for "+timewords(hours) | |
| } | |
| // " ("+delay_sec.toFixed(0)+" sec ago) "+ | |
| bot.say(msg.params.channel, format_outdoor(outdoor)+" "+ | |
| changes_hr.up.toFixed(1)+" chg up and "+changes_hr.down.toFixed(1)+" chg down over "+changes_hr.count+" minutes. "+ | |
| changes_5min.up.toFixed(1)+" chg up and "+changes_5min.down.toFixed(1)+" chg down over "+changes_5min.count+" minutes. "+ | |
| freezing_for | |
| ) | |
| } | |
| } | |
| } | |
| function timewords(hours){ | |
| if (hours < 24) { | |
| return hours.toFixed(1)+" hours" | |
| } else { | |
| return (hours/24).toFixed(1)+" days" | |
| } | |
| } | |
| function get_sensor(name) { | |
| var series = JSON.parse(http.get('https://donp.org/therm/last.php')).results[0].series | |
| var winner | |
| series.forEach(function(data){ | |
| if(data.tags.id == name) { | |
| winner = {time: new Date(data.values[0][0]), temp: data.values[0][1]} | |
| } | |
| }) | |
| if(!winner) { bot.say(bot.admin_channel, "warning: no sensor data found for "+name) } | |
| return winner | |
| } | |
| function format_outdoor(outdoor) { | |
| var C = outdoor.temp | |
| var F = (C*(9/5))+32 | |
| return C.toFixed(1)+"C/"+F.toFixed(1)+"F" | |
| } | |
| function freeze(yesno) { | |
| db.set(db_freezing, yesno ? "1" : "0") | |
| var now = new Date() | |
| db.set(db_fliptime, now.toISOString()) | |
| return now | |
| } | |
| function history_push(temp) { | |
| //bot.say(bot.admin_channel, "freeze: history_push "+temp+" history.length "+history.length+" history_length "+history_length+" first 3 "+JSON.stringify(history.slice(0,3)) + " last 3 "+JSON.stringify(history.slice(history.length-3,history.length))) | |
| if (history.length >= history_length) { | |
| history.shift() | |
| } | |
| history.push(temp) | |
| } | |
| function history_changes(min_count) { | |
| var changes = {up:0, down: 0, count: 0} | |
| var older | |
| var data = history.slice(history.length-min_count, history.length) | |
| data.forEach(function(t, idx){ | |
| if(idx > 0) { | |
| var diff = t - older | |
| if (diff < 0) { | |
| changes.down += (diff*-1) | |
| } else { | |
| changes.up += diff | |
| } | |
| changes.count += 1 | |
| } | |
| older = t | |
| }) | |
| return changes | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment