-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (58 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const pm25 = require('pm25in'),
axios = require('axios'),
time = require("time"),
querystring = require('querystring'),
schedule = require('node-schedule'),
config = require('./config.json');
pm25.token = config.pm25InKey
time.tzset("Asia/Shanghai");
const now = new time.Date()
// 计划执行设置
let rule = new schedule.RecurrenceRule();
rule.dayOfWeek = config.scheduleTime.days;
rule.hour = config.scheduleTime.hours;
rule.minute = config.scheduleTime.minutes;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
const app = {
init() {
this.checkAQI() // 立刻运行一次
this.watcher()
pm25.city_stations({city: 'beijing'}, (err, data) => {
console.log(data)
})
},
checkAQI() {
pm25.station_aqi({station_code: config.stationCode}, (err, data) => {
if (err) throw err;
const title = `空气质量为${data[0].quality} AQI为${data[0].aqi}, O3为${data[0].o3}`
const body = `监测点为${data[0].area}${data[0].position_name},数据来自国家环保部
${now.toLocaleString('zh-CN')}
`
console.log('Title: ', title)
console.log('Body: ', body)
if (data[0].aqi > config.conditions.aqi || data[0].o3 > config.conditions.o3 || data[0].pm2_5 > config.conditions.pm25) {
this.severChan(title, body)
}
});
},
severChan(text, desp) {
return axios.post(`http://sc.ftqq.com/${config.serverChanKey}.send`,
querystring.stringify({
text: text,
desp: desp
}))
.then((response) => {
if (response.status === 200) return console.log('serverChan: send success')
console.warn(response.status)
})
.catch((error) => {
console.error(error);
});
},
watcher() {
return schedule.scheduleJob(rule, () => {
this.checkAQI()
})
}
}
app.init()