Skip to content

Commit

Permalink
Nodeclient (#7)
Browse files Browse the repository at this point in the history
* client test

* add test client

* add client with auth

* use station2 for the test

* add hello message

* Update SVG dimensions in Architecture.svg
  • Loading branch information
jmservera authored Jul 24, 2024
1 parent 3651d9e commit 1e52c90
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 19 deletions.
8 changes: 7 additions & 1 deletion ocpp-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ build: $(wildcard api/**/*.cs)
test:
@echo "Testing"
dotnet test api/api.sln
test-client:
@echo "Testing a simple node client"
node client/index.js wss://wss.jmservera.online station2 goodpwd
test-client-badauth:
@echo "Testing a simple node client"
node client/index.js wss://wss.jmservera.online station1 badpwd
clean:
@echo "Cleaning"
dotnet clean api/api.sln
Expand Down Expand Up @@ -61,4 +67,4 @@ deploy:
@echo "Waiting for infra to be ready"
sleep 60
@$(MAKE) -f $(THIS_FILE) publish
.PHONY: test clean watch start secrets
.PHONY: test clean watch start secrets test-client
12 changes: 11 additions & 1 deletion ocpp-server/api/OcppServer/PubSub/OcppService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq.Expressions;
using Azure.Core;
using Microsoft.Azure.WebPubSub.AspNetCore;
using Microsoft.Azure.WebPubSub.Common;
Expand All @@ -14,7 +15,16 @@ public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventReque
_logger.LogInformation("[SYSTEM] new user connecting.");
if (request.Query.TryGetValue("id", out var id))
{
_logger.LogInformation("[SYSTEM] new user found {userId} connecting.", id);
if(request.Query.TryGetValue("auth", out var auth)){
_logger.LogInformation("[SYSTEM] new user found {userId} connecting with auth {auth}.", id, auth);
if(auth.FirstOrDefault()!="c3RhdGlvbjI6Z29vZHB3ZA==") //station2:goodpwd
{
_logger.LogError("[SYSTEM] auth failed.");
throw new UnauthorizedAccessException();
}
} else {
_logger.LogInformation("[SYSTEM] new user found {userId} connecting without auth.", id);
}
if (request.Subprotocols.Count > 0)
{
_logger.LogInformation("[SYSTEM] connecting with subprotocol {subprotocol}.", request.Subprotocols[0]);
Expand Down
130 changes: 130 additions & 0 deletions ocpp-server/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
60 changes: 60 additions & 0 deletions ocpp-server/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const WebSocket = require('ws');

// get server address from the console
const serverAddress = process.argv[2];
// get station name from the console
const stationName = process.argv[3];
// get station password from the console
const stationPassword = process.argv[4];

const wsOptions= {
rejectUnauthorized: false,
headers: {
'Authorization': 'Basic ' + Buffer.from(stationName + ':' + stationPassword).toString('base64')
}
};
// use websocket to connect to the server
const ws = new WebSocket(serverAddress+`/OCPP/${stationName}`, 'ocpp1.6', wsOptions);
// read user input from the console
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});



// when the connection is established
ws.on('open', function open() {
// send the station name and password to the server
console.log("Connected to the server");
ws.send(`Hello! I'm ${stationName}.`);
});

// when the server sends a message
ws.on('message', function incoming(data) {
//convert binary data to string
const message = data.toString('utf8');
// print the message to the console
console.log(message);
});

// when the user types a message
readline.on('line', (input) => {
// send the message to the server
ws.send(input);
});

// when the connection is closed
ws.on('close', function close() {
console.log('disconnected');
// close the console
readline.close();
});

// wait for the user to close the console
readline.on('close', () => {
// close the websocket connection
ws.close();
});


36 changes: 36 additions & 0 deletions ocpp-server/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions ocpp-server/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "ocpp-client",
"version": "0.0.1-alpha",
"description": "A test OCPP protocol client",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/jmservera/miscdemos.git"
},
"keywords": [
"OCPP",
"Azure",
"Web",
"PubSub"
],
"author": "jmservera",
"license": "MIT",
"bugs": {
"url": "https://github.com/jmservera/miscdemos/issues"
},
"homepage": "https://github.com/jmservera/miscdemos#readme",
"dependencies": {
"ws": "^8.18.0"
}
}
Loading

0 comments on commit 1e52c90

Please sign in to comment.