Skip to content

Commit

Permalink
Merge pull request #10 from istnv/upd/cleanup
Browse files Browse the repository at this point in the history
Better es6 module compatability
  • Loading branch information
istnv authored Jul 18, 2023
2 parents c084bad + b2850d8 commit 72da505
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 61 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ Stuck backstage in some cold, dark ballroom, running A/V support for meetings?<b
Do you wonder what the weather is like outside?<br>
Display the current weather information on a Companion button!<br>
This module requires an active internet connection and you will need your own API key from <a href="https://openweathermap.org/home/sign_up" title="OpenWeather">OpenWeather.com</a>
--------
Contributions for maintenance and development of this open source module are always welcome
https://github.com/sponsors/istnv

--------
15 changes: 12 additions & 3 deletions companion/HELP.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
## <a href="https://openweathermap.org/"> <img src="https://openweathermap.org/themes/openweathermap/assets/img/logo_white_cropped.png" width=150></a>

Retrieve and display weather information. <br>
This module requires an active internet connection and you will need your own API key from here: <a href="https://openweathermap.org/home/sign_up" title="OpenWeather">OpenWeather.com</a>
.<br>

Stuck backstage in some cold, dark ballroom, running A/V support for meetings?<br>
Do you wonder what the weather is like outside?<br>
Display the current weather information on a Companion button!<br>

This module requires an active internet connection and you will need your own API key from here: <a href="https://openweathermap.org/home/sign_up" title="OpenWeather">OpenWeather.com</a>.<br>

--------
Contributions for maintenance and development of this open source module are always welcome
https://github.com/sponsors/istnv

--------
## Configuration
**Setting** | **Description**
-----------------|---------------
Expand Down Expand Up @@ -65,4 +73,5 @@ Canada postal code | G2J

<sup>*3</sup> City name options:
- Add a comma "," and a 2 character country code to specify which City: "Paris,FR"
- Add a comma "," and a 2 character state code followed by another comma and a 2 character country code to specify which City: "Paris,TX,US"
- Add a comma "," and a 2 character state code followed by another comma and a 2 character country code to specify which City: "Paris,TX,US"

122 changes: 65 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import { BASE_URL, C_DEGREE, C_WINDIR, VARIABLE_LIST } from './constants.js'
* @author John A Knight, Jr <[email protected]>
*/
class OWInstance extends InstanceBase {
/**
* add leading zeros to num, trim to len
* -- this will truncate num if it has more than 'len' digits
*
* @param {*} num
* @param {*} len
* @returns
*/

pad0(num, len = 2) {
const zeros = '0'.repeat(len)
return (zeros + num).slice(-len)
}

/**
* Create an instance of the openweather-api module
*
Expand Down Expand Up @@ -164,10 +178,9 @@ class OWInstance extends InstanceBase {
/**
* Generate the feedbacks available
*
* @param {Object} self - 'this' from the module's context
* @since 2.0.0
*/
init_feedbacks(self) {
init_feedbacks() {
// only one, replace button 'background' with
// the recommended Icon from OpenWeather
const feedbacks = {
Expand All @@ -176,12 +189,12 @@ class OWInstance extends InstanceBase {
name: 'Current Condition Icon',
description: 'Change background to icon of current weather',
options: [],
callback: function (feedback, bank) {
callback: async (feedback, bank) => {
let ret
if (self.icons[self.iconID]) {
ret = { png64: self.icons[self.iconID] }
ret.bgcolor = self.isDay ? combineRgb(200, 200, 200) : combineRgb(16, 16, 16)
ret.color = self.isDay ? combineRgb(32, 32, 32) : combineRgb(168, 168, 168)
if (this.icons[this.iconID]) {
ret = { png64: this.icons[this.iconID] }
ret.bgcolor = this.isDay ? combineRgb(200, 200, 200) : combineRgb(16, 16, 16)
ret.color = this.isDay ? combineRgb(32, 32, 32) : combineRgb(168, 168, 168)
}
if (ret) {
return ret
Expand Down Expand Up @@ -260,8 +273,6 @@ class OWInstance extends InstanceBase {
* @since 2.0.0
*/
init_connection() {
let self = this

if (this.client) {
delete this.client
}
Expand All @@ -285,8 +296,8 @@ class OWInstance extends InstanceBase {
})

// check every minute
this.heartbeat = setInterval(function () {
self.pulse(self)
this.heartbeat = setInterval(() => {
this.pulse()
}, 60000)
// starting now :)
this.refresh()
Expand All @@ -297,11 +308,11 @@ class OWInstance extends InstanceBase {
*
* @since 2.0.0
*/
pulse(self) {
let short = self.lastPolled + self.update - Date.now()
pulse() {
let short = this.lastPolled + this.update - Date.now()
// if over 20 minutes then refresh
if (short <= 0) {
self.refresh()
this.refresh()
}
}

Expand All @@ -311,33 +322,31 @@ class OWInstance extends InstanceBase {
* @since 2.0.0
*/
refresh() {
let self = this

// Only query if more than 1 minute since last poll
if (!self.hasError && self.lastPolled + 60000 <= Date.now()) {
let url = `${BASE_URL}?q=${self.config.location}&units=${self.units}&appid=${self.config.apikey}`
self.lastPolled = Date.now()
self.client
.get(url, function (data, response) {
if (!this.hasError && this.lastPolled + 60000 <= Date.now()) {
let url = `${BASE_URL}?q=${this.config.location}&units=${this.units}&appid=${this.config.apikey}`
this.lastPolled = Date.now()
this.client
.get(url, (data, response) => {
if (data.error) {
self.log('error', data.error.message)
self.updateStatus(InstanceStatus.UnknownError, data.error.message)
self.hasError = true
this.log('error', data.error.message)
this.updateStatus(InstanceStatus.UnknownError, data.error.message)
this.hasError = true
} else if (response.statusCode == 200) {
self.updateStatus(InstanceStatus.Ok, 'Connected')
//self.log('info','Weather data updated')
self.update_variables(data)
this.updateStatus(InstanceStatus.Ok, 'Connected')
//this.log('info','Weather data updated')
this.update_variables(data)
} else {
self.log('error', data.message)
self.updateStatus(InstanceStatus.UnknownError, data.message)
self.init_vars()
self.setVariableValues({ l_name: data.message })
this.log('error', data.message)
this.updateStatus(InstanceStatus.UnknownError, data.message)
this.init_vars()
this.setVariableValues({ l_name: data.message })
}
})
.on('error', function (err) {
.on('error', (err) => {
let emsg = err.message
self.log('error', emsg)
self.updateStatus(InstanceStatus.Error, emsg)
this.log('error', emsg)
this.updateStatus(InstanceStatus.Error, emsg)
})
}
}
Expand All @@ -348,23 +357,22 @@ class OWInstance extends InstanceBase {
* @param {Object} data - information returned from the API
* @since 2.0.0
*/
update_variables = function (data) {
let self = this

update_variables (data) {
let v = VARIABLE_LIST
let dv = ''
let dt = data.dt
let tz = data.timezone
const p0 = this.pad0

self.weather = data
this.weather = data

// Additional 'date' formatting funcitons
// Additional 'date' formatting functions
Date.prototype.toHHMM = function () {
return ('00' + this.getHours()).slice(-2) + ':' + ('00' + this.getMinutes()).slice(-2)
return p0(this.getHours()) + ':' + p0(this.getMinutes())
}

Date.prototype.toMMDD_HHMM = function () {
return ('00' + (this.getMonth() + 1)).slice(-2) + '-' + ('00' + this.getDate()).slice(-2) + ' ' + this.toHHMM()
return p0(this.getMonth() + 1) + '-' + p0(this.getDate()) + ' ' + this.toHHMM()
}

for (let i in v) {
Expand All @@ -376,7 +384,8 @@ class OWInstance extends InstanceBase {
case 'main':
switch (i) {
case 'c_press':
if (this.mph) { // inHg
if (this.mph) {
// inHg
dv = Math.floor((data.main[v[i].data] / 33.863886666667) * 100)
} else {
// mmHg
Expand Down Expand Up @@ -428,7 +437,7 @@ class OWInstance extends InstanceBase {
case 'forecast':
break
}
self.setVariableValues({ [i]: dv })
this.setVariableValues({ [i]: dv })
}
}

Expand All @@ -439,32 +448,31 @@ class OWInstance extends InstanceBase {
* @since 2.0.0
*/

update_graphic = function (cond) {
update_graphic (cond) {
const code = cond[0].icon
let self = this

if (code != self.iconID) {
self.iconID = code
if (code != this.iconID) {
this.iconID = code
// cached?
if (self.icons[code]) {
self.checkFeedbacks('icon')
if (this.icons[code]) {
this.checkFeedbacks('icon')
} else {
// retrieve icon
self.client
.get(`http://openweathermap.org/img/wn/${code}@2x.png`, async function (data, response) {
this.client
.get(`http://openweathermap.org/img/wn/${code}@2x.png`, async (data, response) => {
if (response.statusCode == 200) {
const image = await Jimp.read(Buffer.from(data))
const png = await image.scaleToFit(72, 72).getBase64Async(Jimp.MIME_PNG)
self.icons[code] = png
self.checkFeedbacks('icon')
this.icons[code] = png
this.checkFeedbacks('icon')
}
// self.icons[code] = data.toString('base64');
// self.checkFeedbacks('icon');}
// this.icons[code] = data.toString('base64');
// this.checkFeedbacks('icon');}
})
.on('error', function (err) {
let emsg = err.message
self.log('error', emsg)
self.updateStatus(InstanceStatus.Error, emsg)
this.log('error', emsg)
this.updateStatus(InstanceStatus.Error, emsg)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openweather-rest",
"version": "2.1.1",
"version": "2.1.2",
"type": "module",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 72da505

Please sign in to comment.