-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (71 loc) · 1.86 KB
/
app.js
File metadata and controls
78 lines (71 loc) · 1.86 KB
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
'use strict';
const axios = require('axios');
const BASE_PATH = 'https://us.wio.seeed.io/v1/node'; // 自身のものをアプリで確認
const ACCESS_TOKEN = process.argv[2] || '';
const INTERVAL = 3000;
/**
* 温度を取得
*
* @return {object}
*/
const getTemp = () => {
const ENDPOINT = `/GroveTempA0/temp`;
const BASE_URL = `${BASE_PATH}${ENDPOINT}?access_token=${ACCESS_TOKEN}`;
return axios.request({method: 'GET', baseURL: BASE_URL});
}
/**
* 取得した温度情報を表示する文字列に変換
*
* @param {number} temp
* @return {string}
*/
const tempToDigit = (temp) => {
let temp_str = `${temp}`;
temp_str = temp_str.replace('.', '');
if (temp_str.length >= 4) {
temp_str = temp_str.substr(0, temp_str.length-1);
}
return `${temp_str}C`;
}
/**
* ディスプレイに値を表示
*
* @param {number} start_pos
* @param {strng} chars
* @return {object}
*/
const fourDigit = (start_pos, chars) => {
const ENDPOINT = `/Grove4DigitUART0/display_digits/${start_pos}/${chars}`;
const BASE_URL = `${BASE_PATH}${ENDPOINT}?access_token=${ACCESS_TOKEN}`;
return axios.request({method: 'POST', baseURL: BASE_URL})
}
/**
* : を表示/非表示
* @param {number} display 0|1
* @return {object}
*/
const fourDigitPoint = (display) => {
const ENDPOINT = `/Grove4DigitUART0/display_point/${display}`;
const BASE_URL = `${BASE_PATH}${ENDPOINT}?access_token=${ACCESS_TOKEN}`;
return axios.request({method: 'POST', baseURL: BASE_URL});
}
/**
* メインの処理(同期)
*/
async function main() {
let res = await getTemp();
const temp = res.data.temperature;
const text = tempToDigit(temp);
console.log(text);
res = await fourDigit(0, text);
res = await fourDigitPoint(1);
}
/**
* 繰り返し実行する
*/
setInterval(() => {
main()
.catch((e) => {
if (e.response) console.log(e.response);
})
}, INTERVAL);