Skip to content

Commit

Permalink
Streamlining
Browse files Browse the repository at this point in the history
  • Loading branch information
jvik committed Dec 22, 2024
1 parent 724048e commit 910eaef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
49 changes: 23 additions & 26 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
FROM node:slim

# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*

# Set the working directory inside the container
# Stage: Chrome-base (Bygges én gang og gjenbrukes)
FROM node:slim AS chrome-base
RUN apt-get update && apt-get install curl gnupg -y \
&& curl --location --silent https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install google-chrome-stable -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

# Stage: builder (Bygg appen)
FROM chrome-base AS builder
WORKDIR /app

# Copy the package.json and package-lock.json files to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install
COPY tsconfig.json ./
COPY src/ ./src
RUN npm run build

# Copy the rest of the application code to the working directory
COPY . .
# Stage: runtime (Bruker chrome-base som grunnlag)
FROM chrome-base
WORKDIR /app

# Build the TypeScript project
RUN npm run build
# Installer kun prod-avhengigheter
COPY package*.json ./
RUN npm install --only=production

# Expose port 3000
EXPOSE 3000
# Kopierer kun nødvendige filer fra builder
COPY --from=builder /app/dist ./dist

# Command to run the application
EXPOSE 3000
CMD ["node", "dist/src/main.js"]
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const oauthScope = "openid profile email";
const state = "randomstate";
const redirectUri = "io.elaway.no.app://auth.elaway.io/ios/io.elaway.no.app/callback";
const elawayAuthorizationUrl = "https://auth.elaway.io/authorize";
const ampecoApiUrl = "https://no.eu-elaway.charge.ampeco.tech/api/v1/app/oauth/token";
const ampecoApiUrl = `${config.ampecoApiUrl}/oauth/token`;
const elawayTokenUrl = "https://auth.elaway.io/oauth/token";
const tokenFilePath = path.resolve(__dirname, 'tokens.json');

Expand Down
17 changes: 8 additions & 9 deletions src/charger/charger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";

const pollingInterval = Number(process.env.POLLING_INTERVAL) || 60000;
import config from "../config.js";

class Charger {
private static instance: Charger;
Expand All @@ -16,7 +15,7 @@ class Charger {

static async getInstance(): Promise<Charger> {
if (!Charger.instance) {
const response = await axios.get("https://no.eu-elaway.charge.ampeco.tech/api/v1/app/personal/charge-points");
const response = await axios.get(`${config.ampecoApiUrl}/personal/charge-points`);

const chargerData = response.data.data[0];

Expand All @@ -28,7 +27,7 @@ class Charger {

public async checkSessionStatus() {
try {
const response = await axios.get(`https://no.eu-elaway.charge.ampeco.tech/api/v1/app/personal/charge-points/${this.chargerId}`);
const response = await axios.get(`${config.ampecoApiUrl}/personal/charge-points/${this.chargerId}`);
const currentSessionId = response?.data?.data?.evses[0]?.session?.id;

if (this.activeSessionId && !currentSessionId) {
Expand All @@ -50,17 +49,17 @@ class Charger {
// Sett opp et intervall for periodiske sjekker
setInterval(async () => {
await this.checkSessionStatus();
}, pollingInterval); // Sjekk hvert minutt (60000 ms)
}, config.pollingInterval); // Sjekk hvert minutt (60000 ms)
}

public async get() {
const response = await axios.get(`https://no.eu-elaway.charge.ampeco.tech/api/v1/app/personal/charge-points/${this.chargerId}`);
const response = await axios.get(`${config.ampecoApiUrl}/personal/charge-points/${this.chargerId}`);

return response.data;
}

public async startCharging() {
const response = await axios.post('https://no.eu-elaway.charge.ampeco.tech/api/v1/app/session/start', {
const response = await axios.post(`${config.ampecoApiUrl}/session/start`, {
evseId: this.evseId
});
this.activeSessionId = response.data.session.id;
Expand All @@ -69,15 +68,15 @@ class Charger {
}

public async stopCharging() {
const charger = await axios.get('https://no.eu-elaway.charge.ampeco.tech/api/v1/app/personal/charge-points');
const charger = await axios.get(`${config.ampecoApiUrl}/personal/charge-points`);
const currentSessionId = charger?.data?.data[0]?.evses[0]?.session?.id

if (!currentSessionId) {
throw new Error('No active session');
}

try {
const response = await axios.post(`https://no.eu-elaway.charge.ampeco.tech/api/v1/app/session/${currentSessionId}/end`);
const response = await axios.post(`${config.ampecoApiUrl}/session/${currentSessionId}/end`);
this.activeSessionId = undefined;
return response.data.data
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const envVarsSchema = Joi.object({
ELAWAY_CLIENT_SECRET: Joi.string().required(),
ELAWAY_PASSWORD: Joi.string().required(),
POLLING_INTERVAL: Joi.number().default(60000),
CLIENT_ID: Joi.string().required()
CLIENT_ID: Joi.string().required(),
AMPECO_API_URL: Joi.string().uri().default("https://no.eu-elaway.charge.ampeco.tech/api/v1/app")
}).unknown();

// Validate environment variables
Expand All @@ -32,6 +33,7 @@ const config = {
elawayClientSecret: envVars.ELAWAY_CLIENT_SECRET,
pollingInterval: envVars.POLLING_INTERVAL,
clientId: envVars.CLIENT_ID,
ampecoApiUrl: envVars.AMPECO_API_URL
};

export default config;

0 comments on commit 910eaef

Please sign in to comment.