-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (92 loc) · 3.51 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
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
const axios = require('axios');
var clientTransactionId = 1;
exports.getStatus = async function(config) {
return axios.all([getStat(config, 'cansetshutter'), getStat(config, 'shutterstatus')])
.then(axios.spread((canSetShutter, shutterStatus) => {
let dome = {};
let validateResponse = (response, valueName, handleValid) => {
dome[valueName] = null;
let baseErrorMessage = `\t\tError occurred in "${valueName}" call to dome equipment`;
if (!response) {
console.error(baseErrorMessage);
} else if (response.status !== 200) {
console.error(`${baseErrorMessage}: Error status ${response.status} - ${response.data}`);
} else if (response.data.ErrorNumber !== 0) {
console.error(`${baseErrorMessage}: Error ${response.data.ErrorNumber} - ${response.data.ErrorMessage}`);
} else {
handleValid();
}
};
validateResponse(canSetShutter, 'canSetShutter', () => {
dome.canSetShutter = canSetShutter.data.Value;
});
validateResponse(shutterStatus, 'shutterStatus', () => {
switch (shutterStatus.data.Value) {
case 0:
dome.shutterStatus = 'open'; break;
case 1:
dome.shutterStatus = 'closed'; break;
case 2:
dome.shutterStatus = 'opening'; break;
case 3:
dome.shutterStatus = 'closing'; break;
default:
dome.shutterStatus = 'error';
}
});
return dome;
}))
.catch((error) => {
if (error.message) {
error.message = `Error occurred in "getStatus" call to dome equipment`;
}
console.error(error);
throw error;
});
};
exports.executeCommand = async function(command, config) {
// Assert that the type is correct?
// The request body must be a string in this format or Alpaca will not accept it
let requestBody = `ClientTransactionID=${clientTransactionId}`;
if (config.clientId) {
requestBody += `&ClientId=${config.clientId}`;
}
// Valid actions are "openShutter", "closeShutter", "abortSlew" (in uppercase or lowercase)
let url = `${config.baseUrl}/${config.deviceNumber}/${command.action.toLowerCase()}`;
await axios.put(url, requestBody)
.then(response => {
// Send response somewhere?
if (!response) {
let error = new Error();
error.response = 'null response';
error.message = 'Null response was returned';
throw error;
} else if (response.data.ErrorNumber !== 0) {
let error = new Error();
let baseErrorMessage = `Error occurred in "${command.action.toLowerCase()}" call to dome equipment`;
error.message = `\t\t${baseErrorMessage}: Error ${response.data.ErrorNumber} - ${response.data.ErrorMessage}`;
error.response = response;
throw error;
}
})
.catch(error => {
console.error(`\t\tError occurred in "${command.action.toLowerCase()}" command to dome equipment`, error.response);
throw error;
});
};
function getStat(config, stat) {
let url = new URL(`${config.baseUrl}/${config.deviceNumber}/${stat}`);
let searchParams = new URLSearchParams();
searchParams.append('clienttransactionid', clientTransactionId);
if (config.clientId) {
searchParams.append('clientid', config.clientId);
}
url.search = searchParams;
let response = axios.get(url.toString())
.catch((error) => {
// TODO: Do more checking for different cases?
return error.response;
});
clientTransactionId++;
return response;
};