Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit 172e274

Browse files
authored
Merge pull request #5 from basvanrooten/develop
Add Dockerfile
2 parents 548ef73 + a7413dd commit 172e274

File tree

4 files changed

+96
-11
lines changed

4 files changed

+96
-11
lines changed

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:10
2+
3+
# Create directory
4+
WORKDIR /usr/src/app
5+
6+
# Copy Package.json
7+
COPY package*.json ./
8+
9+
RUN npm install
10+
11+
# Copy app source
12+
COPY . .
13+
14+
# !! MAKE SURE THIS IS THE RIGHT PORT YOU WANT TO USE !!
15+
EXPOSE 3000
16+
17+
CMD ["node", "index.js"]

controllers/plug_controller.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,53 @@ module.exports = {
152152
"is_active": localStorage.getItem(req.params.plugID)
153153
});
154154
}
155+
},
156+
157+
getSmartPlug(req, res, next) {
158+
// Return Smartplug details
159+
160+
AuthenticationManager.getSessionKey().then(sessionkey => {
161+
162+
// Check if session key is valid
163+
if (sessionkey != "ERROR") {
164+
165+
axios({
166+
method: 'get',
167+
url: 'https://plug.homewizard.com/plugs',
168+
headers: {
169+
"x-session-token": sessionkey
170+
}
171+
})
172+
.then(response => {
173+
response.data.map((smartplug) => {
174+
175+
// Check if no smartplugs exist
176+
if (smartplug.length == 0) {
177+
res.status(200).send(new ApiResponse("No smartplugs found", 200));
178+
} else {
179+
180+
// Create custom response based on response data
181+
res.status(200).send({
182+
"id": smartplug.id,
183+
"name": smartplug.name,
184+
"online": smartplug.online,
185+
"latitude": smartplug.latitude,
186+
"longtitude": smartplug.longtitude,
187+
"timeZone": smartplug.timeZone,
188+
"firmwareUpdateAvailable": smartplug.firmwareUpdateAvailable
189+
});
190+
}
191+
})
192+
})
193+
.catch(e => {
194+
// Cannot communicate with HWL, returning error
195+
logger.error("Failed to communicate with HWL! ERROR: ", e.message);
196+
res.status(503).send(new ApiResponse(e.message, 503));
197+
});
198+
} else {
199+
// Session key is invalid
200+
res.status(503).send(new ApiResponse("Invalid session key! Check the logs for more details about this problem ", 503));
201+
}
202+
});
155203
}
156204
}

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/plug_routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const PlugController = require('../controllers/plug_controller');
33

44
// Plug routes
55
routes.get('/plug', PlugController.getAllPlugs);
6+
routes.get('/smartplug', PlugController.getSmartPlug);
67
routes.post('/act/smartplug/:smartPlugID/plug/:plugID', PlugController.switchPlug);
78
routes.get('/act/smartplug/:smartPlugID/plug/:plugID', PlugController.plugState);
89

0 commit comments

Comments
 (0)