// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
+import { randomInt } from 'node:crypto'
+
import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns'
import { BaseError } from '../exception/index.js'
Constants,
convertToDate,
formatDurationMilliSeconds,
- getRandomInteger,
isValidDate,
logger,
logPrefix,
break
}
const wait = secondsToMilliseconds(
- getRandomInteger(
+ randomInt(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
- ?.maxDelayBetweenTwoTransactions,
+ ?.minDelayBetweenTwoTransactions,
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
- ?.minDelayBetweenTwoTransactions
+ ?.maxDelayBetweenTwoTransactions
)
)
logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`)
if (startResponse?.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
// Wait until end of transaction
const waitTrxEnd = secondsToMilliseconds(
- getRandomInteger(
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration,
- this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration
+ randomInt(
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration,
+ this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration
)
)
logger.info(
// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
-import { createHash } from 'node:crypto'
+import { createHash, randomInt } from 'node:crypto'
import { EventEmitter } from 'node:events'
import { existsSync, type FSWatcher, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
exponentialDelay,
formatDurationMilliSeconds,
formatDurationSeconds,
- getRandomInteger,
getWebSocketCloseEventStatusString,
handleFileException,
isNotEmptyArray,
}
const templateConnectorId =
connectorId > 0 && stationTemplate.randomConnectors === true
- ? getRandomInteger(templateMaxAvailableConnectors, 1)
+ ? randomInt(1, templateMaxAvailableConnectors)
: connectorId
const connectorStatus = stationTemplate.Connectors[templateConnectorId]
checkStationInfoConnectorStatus(
// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
+import { randomInt } from 'node:crypto'
import { createWriteStream, readdirSync } from 'node:fs'
import { dirname, extname, join, resolve } from 'node:path'
import { fileURLToPath, URL } from 'node:url'
convertToDate,
convertToInt,
formatDurationMilliSeconds,
- getRandomInteger,
isAsyncFunction,
isNotEmptyArray,
isNotEmptyString,
chargingStation.stationInfo?.firmwareUpgrade?.failureStatus ===
OCPP16FirmwareStatus.DownloadFailed
) {
- await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)))
+ await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay)))
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
chargingStation.stationInfo.firmwareUpgrade.failureStatus
return
}
- await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)))
+ await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay)))
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
transactionsStarted = false
}
} while (transactionsStarted)
- !wasTransactionsStarted &&
- (await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay))))
+ !wasTransactionsStarted && (await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay))))
if (!checkChargingStation(chargingStation, chargingStation.logPrefix())) {
return
}
chargingStation.stationInfo?.firmwareUpgrade?.failureStatus ===
OCPP16FirmwareStatus.InstallationFailed
) {
- await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)))
+ await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay)))
await chargingStation.ocppRequestService.requestHandler<
OCPP16FirmwareStatusNotificationRequest,
OCPP16FirmwareStatusNotificationResponse
return
}
if (chargingStation.stationInfo?.firmwareUpgrade?.reset === true) {
- await sleep(secondsToMilliseconds(getRandomInteger(maxDelay, minDelay)))
+ await sleep(secondsToMilliseconds(randomInt(minDelay, maxDelay)))
await chargingStation.reset(OCPP16StopTransactionReason.REBOOT)
}
}
+import { randomInt } from 'node:crypto'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
DCElectricUtils,
getRandomFloatFluctuatedRounded,
getRandomFloatRounded,
- getRandomInteger,
handleFileException,
isNotEmptyArray,
isNotEmptyString,
parseInt(socSampledValueTemplate.value),
socSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT
)
- : getRandomInteger(socMaximumValue, socMinimumValue)
+ : randomInt(socMinimumValue, socMaximumValue)
meterValue.sampledValue.push(
buildSampledValue(socSampledValueTemplate, socSampledValueTemplateValue)
)
| RequestCommand
| IncomingRequestCommand
- static readonly MAX_RANDOM_INTEGER = 281474976710654
+ static readonly MAX_RANDOM_INTEGER = 281474976710655
static readonly STOP_CHARGING_STATIONS_TIMEOUT = 60000 // Ms
-import { getRandomValues, randomBytes, randomInt, randomUUID } from 'node:crypto'
+import { getRandomValues, randomBytes, randomUUID } from 'node:crypto'
import { env, nextTick } from 'node:process'
import {
type TimestampedData,
WebSocketCloseEventStatusString
} from '../types/index.js'
-import { Constants } from './Constants.js'
export const logPrefix = (prefixString = ''): string => {
return `${new Date().toLocaleString()}${prefixString}`
return (randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min
}
-export const getRandomInteger = (max = Constants.MAX_RANDOM_INTEGER, min = 0): number => {
- max = Math.floor(max)
- if (min !== 0) {
- min = Math.ceil(min)
- return Math.floor(randomInt(min, max + 1))
- }
- return Math.floor(randomInt(max + 1))
-}
-
/**
* Rounds the given number to the given scale.
* The rounding is done using the "round half away from zero" method.
generateUUID,
getRandomFloatFluctuatedRounded,
getRandomFloatRounded,
- getRandomInteger,
getWebSocketCloseEventStatusString,
isArraySorted,
isAsyncFunction,
+import { randomInt } from 'node:crypto'
import { version } from 'node:process'
import { describe, it } from 'node:test'
formatDurationSeconds,
generateUUID,
getRandomFloat,
- getRandomInteger,
hasOwnProp,
isArraySorted,
isAsyncFunction,
expect(convertToInt(undefined)).toBe(0)
expect(convertToInt(null)).toBe(0)
expect(convertToInt(0)).toBe(0)
- const randomInteger = getRandomInteger()
+ const randomInteger = randomInt(Constants.MAX_RANDOM_INTEGER)
expect(convertToInt(randomInteger)).toEqual(randomInteger)
expect(convertToInt('-1')).toBe(-1)
expect(convertToInt('1')).toBe(1)
expect(random).toBeLessThan(1)
})
- await it('Verify getRandomInteger()', () => {
- let randomInteger = getRandomInteger()
- expect(Number.isSafeInteger(randomInteger)).toBe(true)
- expect(randomInteger).toBeGreaterThanOrEqual(0)
- expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER)
- expect(randomInteger).not.toEqual(getRandomInteger())
- randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER)
- expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER)
- expect(randomInteger).toBeLessThanOrEqual(0)
- expect(() => getRandomInteger(0, 1)).toThrow(
- 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1'
- )
- expect(() => getRandomInteger(-1)).toThrow(
- 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0'
- )
- expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrow(
- `The value of "max" is out of range. It must be <= ${
- Constants.MAX_RANDOM_INTEGER + 1
- }. Received 281_474_976_710_656`
- )
- randomInteger = getRandomInteger(2, 1)
- expect(randomInteger).toBeGreaterThanOrEqual(1)
- expect(randomInteger).toBeLessThanOrEqual(2)
- const maximum = 2.2
- const minimum = 1.1
- randomInteger = getRandomInteger(maximum, minimum)
- expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum))
- expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum))
- })
-
await it('Verify roundTo()', () => {
expect(roundTo(0, 2)).toBe(0)
expect(roundTo(0.5, 0)).toBe(1)