Skip to content

Commit

Permalink
UPdating
Browse files Browse the repository at this point in the history
  • Loading branch information
terafin committed Aug 12, 2020
1 parent 2e30524 commit 1aac53f
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 720 deletions.
58 changes: 30 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Requirements
const mqtt = require('mqtt')
const _ = require('lodash')
const repeat = require('repeat')
const interval = require('interval-promise')
const logging = require('homeautomation-js-lib/logging.js')
const Davis = require('davis-weather')
const mqtt_helpers = require('homeautomation-js-lib/mqtt_helpers.js')
Expand All @@ -15,57 +15,59 @@ var weatherstationPort = process.env.DAVIS_PORT
const client = mqtt_helpers.setupClient(null, null)

if (_.isNil(topic_prefix)) {
logging.warn('TOPIC_PREFIX not set, not starting')
process.abort()
logging.warn('TOPIC_PREFIX not set, not starting')
process.abort()
}

if (_.isNil(weatherstationIP)) {
logging.warn('DAVIS_IP not set, not starting')
process.abort()
logging.warn('DAVIS_IP not set, not starting')
process.abort()
}

if (_.isNil(weatherstationPort)) {
weatherstationPort = 22222
weatherstationPort = 22222
}

var mqttOptions = {}

var shouldRetain = process.env.MQTT_RETAIN

if (_.isNil(shouldRetain)) {
shouldRetain = true
shouldRetain = true
}

if (!_.isNil(shouldRetain)) {
mqttOptions['retain'] = shouldRetain
mqttOptions['retain'] = shouldRetain
}

var davisClient = new Davis(weatherstationIP, weatherstationPort)

const check_measurements = function() {
davisClient.getCurrentConditions(function(err, data) {
if( err ) {
logging.info(err)
} else {
Object.keys(data).forEach(measurement => {
const value = data[measurement]
if ( !_.isNil(value) ) {
var stringValue = value.toString()
davisClient.getCurrentConditions(function(err, data) {
if (err) {
logging.info(err)
} else {
Object.keys(data).forEach(measurement => {
const value = data[measurement]
if (!_.isNil(value)) {
var stringValue = value.toString()

if ( stringValue != 'NaN' ) {
client.smartPublish(topic_prefix + '/' + measurement, stringValue)
}
} else {
logging.debug('missing value for key: ' + measurement)
}
})
}
})
if (stringValue != 'NaN') {
client.smartPublish(topic_prefix + '/' + measurement, stringValue)
}
} else {
logging.debug('missing value for key: ' + measurement)
}
})
}
})
}

const startMonitoring = function() {
logging.info('Starting to monitor: ' + weatherstationIP)
repeat(check_measurements).every(30, 's').start.in(1, 'sec')
logging.info('Starting to monitor: ' + weatherstationIP)
interval(async() => {
check_measurements()
}, 30 * 1000)
}

startMonitoring()
startMonitoring()
Loading

0 comments on commit 1aac53f

Please sign in to comment.