Browse Source

Auto stash before revert of " Add Athlete model and correlation to trainer"

main
Haris Razis 4 years ago
parent
commit
7977cd4428
  1. 16
      .idea/inspectionProfiles/Project_Default.xml
  2. 6
      client/Dockerfile
  3. 3
      client/index.js
  4. 1988
      client/package-lock.json
  5. 2
      client/package.json
  6. 6
      client/services/socket.js
  7. 3
      docker-compose.yml
  8. 19
      server/actions/mongo_actions.js
  9. 11
      server/models/Athlete.js
  10. 11
      server/package-lock.json
  11. 1
      server/package.json
  12. 41
      server/routes/athletes.js
  13. 7
      server/routes/data.js
  14. 21
      server/services/socket.js
  15. 1
      web/src/views/Dashboard.vue

16
.idea/inspectionProfiles/Project_Default.xml

@ -1,6 +1,22 @@
<component name="InspectionProjectProfileManager"> <component name="InspectionProjectProfileManager">
<profile version="1.0"> <profile version="1.0">
<option name="myName" value="Project Default" /> <option name="myName" value="Project Default" />
<inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
<option name="myValues">
<value>
<list size="7">
<item index="0" class="java.lang.String" itemvalue="nobr" />
<item index="1" class="java.lang.String" itemvalue="noembed" />
<item index="2" class="java.lang.String" itemvalue="comment" />
<item index="3" class="java.lang.String" itemvalue="noscript" />
<item index="4" class="java.lang.String" itemvalue="embed" />
<item index="5" class="java.lang.String" itemvalue="script" />
<item index="6" class="java.lang.String" itemvalue="shapedividertop" />
</list>
</value>
</option>
<option name="myCustomValuesEnabled" value="true" />
</inspection_tool>
<inspection_tool class="JSUnresolvedFunction" enabled="false" level="WEAK WARNING" enabled_by_default="false" /> <inspection_tool class="JSUnresolvedFunction" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="JSValidateTypes" enabled="false" level="WEAK WARNING" enabled_by_default="false" /> <inspection_tool class="JSValidateTypes" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile> </profile>

6
client/Dockerfile

@ -1,11 +1,11 @@
FROM node:latest FROM node:latest
WORKDIR /usr/src/app WORKDIR /usr/src/app/client
COPY package*.json ./ COPY package*.json ./
RUN npm install RUN npm install
COPY . .
EXPOSE 6000 EXPOSE 6000
CMD [ "npm", "start" ]
CMD [ "npm", "run", "dev" ]

3
client/index.js

@ -1,4 +1,3 @@
require('./services/socket') require('./services/socket')
console.log('Client running...')
console.log('hello world')

1988
client/package-lock.json

File diff suppressed because it is too large

2
client/package.json

@ -11,6 +11,8 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"chalk": "^4.1.0", "chalk": "^4.1.0",
"getmac": "^5.17.0",
"nodemon": "^2.0.6",
"socket.io-client": "^3.0.4" "socket.io-client": "^3.0.4"
} }
} }

6
client/services/socket.js

@ -1,12 +1,15 @@
const io = require('socket.io-client'); const io = require('socket.io-client');
const chalk = require('chalk') const chalk = require('chalk')
const {server_url} = require('../config/keys'); const {server_url} = require('../config/keys');
const getMAC = require('getmac').default
const socket = io(server_url); const socket = io(server_url);
const mac = getMAC();
socket.on('connect', () => { socket.on('connect', () => {
console.log(chalk.green('Connected to server!')); console.log(chalk.green('Connected to server!'));
socket.emit('subscribe', 'pi-iot');
socket.emit('subscribe', JSON.stringify({subscribe: 'clients', mac}));
}); });
socket.on('disconnect', (reason) => { socket.on('disconnect', (reason) => {
@ -29,3 +32,4 @@ socket.on('closeConn', () => {
setInterval(() => { setInterval(() => {
socket.emit('data', {measurement: 123, pointName: 'hey-ho'}); socket.emit('data', {measurement: 123, pointName: 'hey-ho'});
}, 3 * 1000); }, 3 * 1000);

3
docker-compose.yml

@ -69,6 +69,9 @@ services:
test_client: test_client:
build: ./client build: ./client
container_name: "test_client" container_name: "test_client"
volumes:
- ./client:/usr/src/app/client
- /usr/src/app/client/node_modules
networks: networks:
- backend - backend
depends_on: depends_on:

19
server/actions/mongo_actions.js

@ -0,0 +1,19 @@
const mongoose = require('mongoose');
const faker = require('faker')
const Athlete = mongoose.model('Athlete');
saveAthlete = async (mac, socketID) => {
const newAthlete = new Athlete({
id: mac,
socketID: socketID,
name: faker.name.findName()
})
try {
await newAthlete.save();
} catch (err) {
console.log(err);
}
}
module.exports = {saveAthlete}

11
server/models/Athlete.js

@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const {Schema} = mongoose;
const AthleteSchema = new Schema({
id: String,
socketID: String,
name: String,
_trainer: {type: Schema.Types.ObjectId, ref: 'User'}
});
mongoose.model('Athlete', AthleteSchema);

11
server/package-lock.json

@ -20,6 +20,7 @@
"express-rate-limit": "^5.2.3", "express-rate-limit": "^5.2.3",
"express-session": "^1.17.1", "express-session": "^1.17.1",
"express-validator": "^6.8.0", "express-validator": "^6.8.0",
"faker": "^5.1.0",
"mongoose": "^5.11.8", "mongoose": "^5.11.8",
"nodemon": "^2.0.6", "nodemon": "^2.0.6",
"passport": "^0.4.1", "passport": "^0.4.1",
@ -923,6 +924,11 @@
"node": ">= 8.0.0" "node": ">= 8.0.0"
} }
}, },
"node_modules/faker": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/faker/-/faker-5.1.0.tgz",
"integrity": "sha512-RrWKFSSA/aNLP0g3o2WW1Zez7/MnMr7xkiZmoCfAGZmdkDQZ6l2KtuXHN5XjdvpRjDl8+3vf+Rrtl06Z352+Mw=="
},
"node_modules/fill-range": { "node_modules/fill-range": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -3509,6 +3515,11 @@
"validator": "^13.5.1" "validator": "^13.5.1"
} }
}, },
"faker": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/faker/-/faker-5.1.0.tgz",
"integrity": "sha512-RrWKFSSA/aNLP0g3o2WW1Zez7/MnMr7xkiZmoCfAGZmdkDQZ6l2KtuXHN5XjdvpRjDl8+3vf+Rrtl06Z352+Mw=="
},
"fill-range": { "fill-range": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",

1
server/package.json

@ -22,6 +22,7 @@
"express-rate-limit": "^5.2.3", "express-rate-limit": "^5.2.3",
"express-session": "^1.17.1", "express-session": "^1.17.1",
"express-validator": "^6.8.0", "express-validator": "^6.8.0",
"faker": "^5.1.0",
"mongoose": "^5.11.8", "mongoose": "^5.11.8",
"nodemon": "^2.0.6", "nodemon": "^2.0.6",
"passport": "^0.4.1", "passport": "^0.4.1",

41
server/routes/athletes.js

@ -0,0 +1,41 @@
const express = require('express')
const router = express.Router();
const mongoose = require('mongoose');
const {requireAuth} = require('../middlewares/middleware');
const Athlete = mongoose.model('Athlete');
router.get('/api/athletes', async (req, res) => {
const athletes = await Athlete.find();
res.send(athletes);
});
//add a new athlete
router.get('/api/athletes/new', requireAuth, (req, res) => {
});
//add a new athlete
router.post('/api/athletes', requireAuth, (req, res) => {
});
router.get('/api/athletes/:id', requireAuth, async (req, res) => {
const athlete = await Athlete.findById(req.params.id);
res.send(athlete)
});
router.get('/api/athletes/:id/edit', requireAuth, async (req, res) => {
const athlete = await Athlete.findById(req.params.id)
res.send(athlete)
});
router.put('/api/athletes/:id', requireAuth, async (req, res) => {
const {id, user} = req.params
await Athlete.findByIdAndUpdate(id, user)
});
router.delete('/api/athlete/:id', requireAuth, async (req, res) => {
await Athlete.findByIdAndDelete(req.params.id)
});
module.exports = router;

7
server/routes/data.js

@ -1,7 +1,8 @@
const express = require('express'), const express = require('express')
router = express.Router(); const router = express.Router();
const {requireAuth} = require('../middlewares/middleware');
router.get('/yoda', (req, res) => { router.get('/yoda', requireAuth, (req, res) => {
res.send('Become powerful you have, the dark side in you I sense. Yrsssss.'); res.send('Become powerful you have, the dark side in you I sense. Yrsssss.');
}) })

21
server/services/socket.js

@ -1,9 +1,12 @@
const socket = require('socket.io'); const socket = require('socket.io');
const redisAdapter = require('socket.io-redis'); const redisAdapter = require('socket.io-redis');
const mongoose = require('mongoose');
const chalk = require('chalk'); const chalk = require('chalk');
const {pub, sub} = require('../connections/redis_conn') const {pub, sub} = require('../connections/redis_conn')
const {saveAthlete} = require('../actions/mongo_actions')
const {iWrite, closeWrite, iQuery} = require('../actions/influx_actions') const {iWrite, closeWrite, iQuery} = require('../actions/influx_actions')
const Athlete = mongoose.model('Athlete');
module.exports = (server) => { module.exports = (server) => {
const io = socket(server) const io = socket(server)
@ -16,6 +19,24 @@ module.exports = (server) => {
console.log(chalk.red('Client disconnected!')); console.log(chalk.red('Client disconnected!'));
}); });
socket.on('subscribe', async (room) => {
const {subscribe, mac} = JSON.parse(room)
socket.join(room);
console.log(chalk.magenta(`Client with id: ${socket.id} joined room "${subscribe}"`));
if (subscribe === 'clients') {
const socketID = socket.id.toString();
if (await Athlete.findOne({id: mac})) {
await Athlete.findOneAndUpdate({id: mac}, {socketID})
return;
}
await saveAthlete(mac, socketID)
}
});
socket.on('data', (data) => { socket.on('data', (data) => {
const {measurement, pointName} = data; const {measurement, pointName} = data;

1
web/src/views/Dashboard.vue

@ -19,6 +19,7 @@ export default class Dashboard extends Vue {
this.socket.on('connect', () => { this.socket.on('connect', () => {
console.log(('Connected to server!')); console.log(('Connected to server!'));
this.socket.emit('subscribe', 'web');
}); });
this.socket.on('disconnect', (reason: string) => { this.socket.on('disconnect', (reason: string) => {

Loading…
Cancel
Save