-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermo.js
52 lines (48 loc) · 1.45 KB
/
thermo.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
const { Board, Thermometer } = require("johnny-five");
const gql = require('graphql-tag');
const ApolloClient = require('apollo-boost').ApolloClient;
const fetch = require('cross-fetch/polyfill').fetch;
const createHttpLink = require('apollo-link-http').createHttpLink;
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
const board = new Board();
const client = new ApolloClient({
link: createHttpLink({
uri: [MY_HASURA_GRAPHQL_ENDPOINT],
fetch: fetch
}),
cache: new InMemoryCache()
});
board.on("ready", () => {
board.samplingInterval(5000);
let temperature = new Thermometer(
{
controller: "LM35",
pin: "A0",
freq: 60000
});
temperature.on("data", () => {
const {celsius, fahrenheit, kelvin} = temperature;
console.log("Thermometer");
console.log(" celsius : ", celsius);
console.log(" fahrenheit : ", fahrenheit);
console.log(" kelvin : ", kelvin);
//GQL mutation stuff with ApolloClient
client.mutate({
mutation: gql`
mutation insertTemperature($tempc: Int, $tempf: numeric, $tempk: numeric) {
insert_schema1_daily_temperature(objects:
{temp_celsius: $tempc, temp_fahrenheit: $tempf, temp_kelvin: $tempk}){
affected_rows
}
}`,
variables: {
tempc: celsius,
tempf: fahrenheit,
tempk: kelvin
}
}).then(res => {
console.log(res.data)
})
//GQL stuff ends
});
});