Skip to content

Instantly share code, notes, and snippets.

View oleh-zaporozhets's full-sized avatar

Oleh Zaporozhets oleh-zaporozhets

View GitHub Profile
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define WIFI_NAME ""
#define WIFI_PASSWORD ""
#define HOST ""
#define PATH "/webhooks/electricity/"
#define PORT 443
#define CHAT_ID ""
#define TIMER_DELAY 60000
import { RequestHandler } from 'express';
import { ITelegramBot } from '@/application/telegram-bot';
import { Timer } from '@/application/timer';
import { ITimerStore } from '@/application/timer-store';
import { Controller } from './controller';
const FIVE_MINUTES = 1000 * 60 * 5;
import { ITimer } from './timer';
interface ITimerStore {
getTimerById(id: string): ITimer | null;
addTimer(id: string, timer: ITimer): void;
}
class TimerStore implements ITimerStore {
private timers: Map<string, ITimer>;
type Callback = () => Promise<void> | void;
interface ITimer {
reset(): void;
refresh(): void;
isTimerRunning(): boolean;
}
class Timer implements ITimer {
private callbacks: Callback[];
import axios, { AxiosInstance } from 'axios';
interface ITelegramBot {
sendMessage(chatId: string, message: string): Promise<void>;
}
class TelegramBot implements ITelegramBot {
private axiosInstance: AxiosInstance;
public constructor(botToken: string) {
class TestHttp extends Http {
constructor() {
super('http://localhost:5000/');
}
public test() {
return this.instance.get('/test');
}
}
const express = require('express');
const app = express();
let counter = 0;
app.get('/test', (_, res) => {
counter++;
console.log(`Incoming request №${counter}`);
class CircuitBreakerWithEmitter implements ICircuitBreakerWithEmitter {
private readonly http: IHttp;
private readonly timeout: number;
private isOpen: boolean = false;
private readonly errorHandler: (error: any) => boolean;
private readonly eventEmitter: IEventEmitter;
constructor(http: IHttp, option: ICircuitBreakerOptions) {
this.http = http;
this.timeout = option.timeout;
class EventEmitter implements IEventEmitter {
private readonly events: Record<string, Function[]>;
public constructor() {
this.events = {};
}
public on(name: string, listener: Function) {
if (!this.events[name]) {
this.events[name] = [];
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
class CircuitBreaker implements ICircuitBreaker {
private readonly http: IHttp;
private readonly timeout: number;
private isOpen = false;
private errorHandler: (error: any) => boolean;
constructor(http: IHttp, options: ICircuitBreakerOptions) {
this.http = http;