Skip to content

Commit 806899b

Browse files
author
Sakif Surur
committed
Initial commit
0 parents  commit 806899b

File tree

9 files changed

+1196
-0
lines changed

9 files changed

+1196
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
# ViriCiti Vue Assignment
3+
---
4+
![](https://imgs.xkcd.com/comics/self_description.png)
5+
6+
This repository holds the ViriCiti Vue assignment. In this project you will find the description regarding the assignment for you to do. This assignment represent what we do on a day to day basis. We receive raw data from vehicles store it on database then send it to browser app via web socket.
7+
8+
---
9+
10+
## Getting Started
11+
First of all, fork the repository at:
12+
13+
`https://github.com/viriciti/vue-assignment`
14+
15+
Then open up your terminal and clone the forked repository
16+
17+
<sup>Replace [YOUR_USERNAME] with your name</sup>
18+
19+
`git clone https://github.com/[YOUR_USERNAME]/vue-assignment.git`
20+
21+
Enter the directory
22+
23+
`cd vue-assignment`
24+
25+
Install all the dependencies
26+
27+
`npm i`
28+
29+
Run the app
30+
31+
`npm start`
32+
33+
---
34+
35+
## The Assignment
36+
We have provided you with a starter kit that broadcast vehicle data via web socket. You can find the HTTP server on `src/server/app.js`, it's a bare minimum setup containing an express server to serve HTML page and ws server that broadcast raw data. What you need to build is a front end application that should look like below:
37+
38+
![](https://github.com/viriciti/vue-assignment/raw/master/sketch.png)
39+
40+
The whole front end application should be build with [Vue.js](https://vuejs.org/), but you're free to choose how it's build and composed. Data that are needed to render all those component is provided via the web socket connection.
41+
42+
As you can see in this repository there are no webpack configuration and frontend server. You're free to create your own configuration, maybe you want different config for production and development.
43+
44+
### Get Creative!
45+
You can also extend the functionality for both front end and back end of the application. For example, making a more informative front end or incident reporting on that alert the user when the vehicle went to fast on the road (this can be done both in the front end and back end, double points for back end implementation ;) )
46+
47+
### The data
48+
On websocket stream the data should look like this:
49+
50+
```JS
51+
{
52+
time: 1511512585495,
53+
energy: 85.14600000000002,
54+
gps: ["52.08940124511719","5.105764865875244"],
55+
odo: 5.381999999997788,
56+
speed: 12,
57+
soc: 88.00000000000007
58+
}
59+
```
60+
61+
* time - Unix timestamp of the moment the datapoint was recorder
62+
* energy - Energy used in kWh
63+
* gps - Latitude and longitude where the datapoint was recorded
64+
* odo - The distance driven in km
65+
* speed - The speed the vehicle was going in km/h
66+
* soc - The state of charge (battery) of the vehicle in %
67+
68+
## Read up material
69+
Looking to level up your knowledge and skills? These are some good articles/courses that you can check out.
70+
* [Vue JS](https://vuejs.org/)
71+
* [Chartjs](https://www.chartjs.org/)
72+
* [Node.js WebSocket](https://flaviocopes.com/node-websockets/)
73+
### General
74+
* Learn [Node.js and it's modules](http://nodeschool.io/#workshoppers)
75+
76+
## Questions
77+
If you have any questions about the assignment or project setup feel free to contact us at <a href='mailto:e.tamames@viriciti.com'>e.tamames@viriciti.com</a> or <a href='mailto:s.surur@viriciti.com'>s.surur@viriciti.com</a>. You can also come by the office. We're always ready to help.
78+
79+
Good luck with the assignment!

meta/route.csv

+1,000
Large diffs are not rendered by default.

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "vue-assignment",
3+
"version": "1.0.0",
4+
"description": "![ ](https://imgs.xkcd.com/comics/code_quality_3.png)",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node src/server/app.js"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"csv-parse": "^4.0.1",
13+
"express": "^4.16.4",
14+
"pug": "^2.0.3",
15+
"ws": "^6.1.2"
16+
}
17+
}

sketch.png

95.9 KB
Loading

src/client/app.js

Whitespace-only changes.

src/server/Broadcaster.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const EventEmitter = require("events").EventEmitter
2+
const csvParse = require ( "csv-parse")
3+
const fs = require ( "fs")
4+
const Writable = require ("stream").Writable
5+
6+
class Broadcaster extends EventEmitter {
7+
constructor() {
8+
super()
9+
}
10+
11+
start() {
12+
this.broadcasting = true
13+
const broadcast = () => {
14+
console.log("Broadcasting...")
15+
const fileStream = fs.createReadStream("./meta/route.csv")
16+
17+
fileStream
18+
// Filestream piped to csvParse which accept nodejs readablestreams and parses each line to a JSON object
19+
.pipe(csvParse({ delimiter: ",", columns: true, cast: true }))
20+
// Then it is piped to a writable streams that will push it into nats
21+
.pipe(new Writable({
22+
objectMode: true,
23+
write: (obj, enc, cb) => {
24+
if(!this.broadcasting)
25+
return cb()
26+
27+
// setTimeout in this case is there to emulate real life situation
28+
// data that came out of the vehicle came in with irregular interval
29+
// Hence the Math.random() on the second parameter
30+
setTimeout(() => {
31+
this.emit("data", obj)
32+
cb()
33+
}, Math.ceil(Math.random() * 150))
34+
}
35+
}))
36+
.once("finish", () => {
37+
console.log("Finished broadcasting")
38+
if(this.broadcasting) {
39+
console.log("Re-broadcast")
40+
broadcast()
41+
} else {
42+
console.log("Stopped broadcast")
43+
return
44+
}
45+
})
46+
47+
}
48+
broadcast()
49+
}
50+
51+
end() {
52+
this.broadcasting = false
53+
}
54+
}
55+
56+
module.exports = Broadcaster

src/server/app.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require("express")
2+
const http = require("http")
3+
const WebSocket = require("ws")
4+
const Broadcaster = require ("./Broadcaster")
5+
6+
const app = express()
7+
8+
const httpServer = http.createServer(app)
9+
10+
app
11+
.set("views", `${process.cwd()}/src/server/views`)
12+
.set("view engine", "pug")
13+
.use(express.static(`${process.cwd()}/src/client`))
14+
15+
app
16+
.get("/", (req, res) => {
17+
res.render("index")
18+
})
19+
20+
21+
const wss = new WebSocket.Server({ server: httpServer })
22+
23+
const broadcaster = new Broadcaster()
24+
25+
broadcaster.start()
26+
broadcaster.on("data", (data) => {
27+
wss.clients.forEach((socket) => {
28+
socket.send(JSON.stringify(data))
29+
})
30+
})
31+
32+
33+
httpServer.listen(3000, () => {
34+
console.log("HTTP server listening on port 3000")
35+
})
36+
37+

src/server/views/index.pug

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
html
2+
head
3+
title ViriCiti Vue App
4+
link(rel="stylesheet", href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css")
5+
body
6+
h1 HELLO WORLD!

0 commit comments

Comments
 (0)