-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
148 lines (123 loc) · 3.87 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import _ from 'lodash';
import { AgentBase, BaseAgentData, Schedule } from '../../agent';
import axios from 'axios';
import dayjs from 'dayjs';
import { sleep } from '@arken/node/util/time';
interface InitParams {
cerebro: any;
model: any;
}
export async function init({ cerebro, model }: InitParams) {
const agent = new Agent(cerebro, model);
log('Initializing');
await agent.init();
log('Initialized');
return agent;
}
let log: (...msgs: any[]) => void;
let app: any;
interface EnelData extends BaseAgentData {
personality: string[];
schedule: {
getWeather: Schedule;
getAirQuality: Schedule;
getPeriodicAirQualityScreenshot: Schedule;
};
}
class Agent extends AgentBase<EnelData> {
constructor(cerebro: any, model: any) {
super('Enel', cerebro, model, {
personality: ['analytical', 'observant', 'detailed'],
schedule: {
getWeather: {
delay: '1h',
interval: '1d',
processedDate: null,
},
getAirQuality: {
delay: '2h',
interval: '1d',
processedDate: null,
},
getPeriodicAirQualityScreenshot: {
delay: '1m',
interval: '1h',
processedDate: null,
},
},
} as Partial<EnelData>);
log = this.log.bind(this);
app = this.app;
}
async init() {
super.init();
app.channel.request.subscribe(this.onRequest.bind(this));
}
async onRequest({ req, res }: { req: any; res: any }) {
// Implement onRequest logic if needed
}
async getWeather() {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${process.env.WEATHER_CITY},CA&appid=${process.env.OPENWEATHERMAP_API_KEY}&units=metric`;
try {
const response = await axios(url);
if (!response.data) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = response.data;
const description = `${Math.round(data.main.temp)} degrees ${data.weather[0].description} (${
data.clouds.all
}% clouds) (${data.main.humidity}% humidity)`;
return {
description,
data,
};
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
async getPeriodicAirQualityScreenshot() {
log('Get periodic air quality screenshot');
const url = 'https://map.purpleair.com/1/mAQI/a60/p604800/cC0#1.8/42.3/166.1';
const browser = await app.util.browser.open();
try {
const page = await app.util.browser.newPage(browser, url, {
log,
});
await sleep(15 * 1000);
await page.click('#gdpr-cookie-accept');
await page.click('.sensorsCloseButton');
await sleep(1 * 1000);
await page.screenshot({
path: 'saved/sites/purpleair/' + dayjs().format('YYYY-MM-DD_HH-mm-ss') + '.png',
fullPage: true,
});
log('Screenshot air quality');
} catch (e) {
log('Error', ['alert', 'p10'], e);
} finally {
await browser.close();
}
}
async getAirQuality() {
const url = `https://api.purpleair.com/v1/sensors?location_type=0&nwlng=${process.env.WEATHER_NW_LNG}&selng=${process.env.WEATHER_SE_LNG}&nwlat=${process.env.WEATHER_NW_LAT}&selat=${process.env.WEATHER_SE_LAT}&api_key=${process.env.PURPLEAIR_API_KEY}&fields=pm2.5_atm,temperature,humidity`;
try {
const response = await axios(url);
if (!response.data) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = response.data;
// Extract and log relevant air quality data
if (data.data && data.data.length > 0) {
const pm25 = app.util.math.average(...data.data.map((s: any) => s[3]));
return {
pm25,
data: data.data,
};
} else {
console.log('No sensor data available.');
}
} catch (error) {
console.error('Error fetching air quality data:', error);
}
}
}