Set up manifest and basic mqtt connection to adafruit io
This commit is contained in:
parent
f3f8012ce3
commit
4f38af0a13
7 changed files with 7381 additions and 1 deletions
|
|
@ -1,6 +1,18 @@
|
||||||
{
|
{
|
||||||
"name": "DII Hackathon",
|
"name": "DII Hackathon",
|
||||||
"version": "0.1",
|
"version": "0.1",
|
||||||
"description": "About Chrome extension that communicates with Adafruit.IO for the Design of Innovative Interactions course at University of Salzburg",
|
"description": "A Chrome extension that communicates with Adafruit.IO for the Design of Innovative Interactions course at University of Salzburg",
|
||||||
|
"permissions": ["storage"],
|
||||||
|
"background": {
|
||||||
|
"scripts": [
|
||||||
|
"dist/background/index.js"
|
||||||
|
],
|
||||||
|
"persistent": false
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"scripts": [
|
||||||
|
"dist/frontend/index.js"
|
||||||
|
]
|
||||||
|
},
|
||||||
"manifest_version": 2
|
"manifest_version": 2
|
||||||
}
|
}
|
||||||
7290
package-lock.json
generated
Normal file
7290
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
30
package.json
Normal file
30
package.json
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name": "dii-hackathon-extension",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Chrome extension that communicates with Adafruit.IO for the Design of Innovative Interactions course at University of Salzburg",
|
||||||
|
"main": "background.js",
|
||||||
|
"scripts": {
|
||||||
|
"clean": "rm -rf dist/background & rm -rf dist/frontend",
|
||||||
|
"serve": "parcel watch src/background/index.js --out-dir dist/background & parcel watch src/frontend/index.js --out-dir dist/frontend",
|
||||||
|
"build:back": "parcel build src/background/index.js --out-dir dist/background",
|
||||||
|
"build:front": "parcel build src/frontend/index.js --out-dir dist/frontend",
|
||||||
|
"build": "npm run clean && npm run build:back & npm run build:front",
|
||||||
|
"package": "npm run build && zip -r dii-hackathon-extension.zip dist/ manifest.json"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/dennisschoepf/dii-hackathon-extension.git"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/dennisschoepf/dii-hackathon-extension/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/dennisschoepf/dii-hackathon-extension#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"dotenv": "^8.2.0",
|
||||||
|
"mqtt": "4.1.0",
|
||||||
|
"parcel": "^1.12.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/background/api.js
Normal file
36
src/background/api.js
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import mqtt from "mqtt";
|
||||||
|
|
||||||
|
const mqttHeartbeatTopic = "dennissc/feeds/heartbeat/json";
|
||||||
|
const mqttTiltTopic = "dennissc/feeds/tilt-sensor/json";
|
||||||
|
const mqttEdnpoint = process.env.AIO_ENDPOINT;
|
||||||
|
const mqttOptions = {
|
||||||
|
port: Number(process.env.AIO_PORT),
|
||||||
|
username: process.env.AIO_USERNAME,
|
||||||
|
password: process.env.AIO_KEY,
|
||||||
|
};
|
||||||
|
|
||||||
|
const client = mqtt.connect("mqtt://io.adafruit.com", mqttOptions);
|
||||||
|
|
||||||
|
export function subscribeToAllSensors({ onHeartbeat, onTilt }) {
|
||||||
|
client.on("connect", () => {
|
||||||
|
client.subscribe(mqttHeartbeatTopic);
|
||||||
|
client.subscribe(mqttTiltTopic);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("error", (error) => {
|
||||||
|
console.log("MQTT Client Errored");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("message", (topic, message) => {
|
||||||
|
if (topic === mqttHeartbeatTopic) {
|
||||||
|
const parsedMessage = JSON.parse(message.toString());
|
||||||
|
onHeartbeat(parsedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topic === mqttTiltTopic) {
|
||||||
|
const parsedMessage = JSON.parse(message.toString());
|
||||||
|
onTilt(parsedMessage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
11
src/background/index.js
Normal file
11
src/background/index.js
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { subscribeToAllSensors } from "./api";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
chrome.runtime.onInstalled.addListener(() => {
|
||||||
|
subscribeToAllSensors({
|
||||||
|
onHeartbeat: (heartbeatData) => console.log(heartbeatData),
|
||||||
|
onTilt: (tiltData) => console.log(tiltData),
|
||||||
|
});
|
||||||
|
});
|
||||||
0
src/background/storage.js
Normal file
0
src/background/storage.js
Normal file
1
src/frontend/index.js
Normal file
1
src/frontend/index.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
console.log("Frontend registered");
|
||||||
Reference in a new issue