UNPKG

iobroker.go-e-charger

Version:
231 lines 8.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProjectUtils = void 0; class ProjectUtils { adapter; constructor(adapter) { this.adapter = adapter; } async getStateValue(stateName) { try { const stateObject = await this.getState(stateName); return stateObject?.val ?? null; } catch (error) { this.adapter.log.error(`[getStateValue](${stateName}): ${error}`); return null; } } async getState(stateName) { try { if (await this.verifyStateAvailable(stateName)) { const stateValueObject = await this.adapter.getStateAsync(stateName); if (!this.isLikeEmpty(stateValueObject)) { return stateValueObject; } throw new Error(`Unable to retrieve info from state '${stateName}'.`); } } catch (error) { this.adapter.log.error(`[asyncGetState](${stateName}): ${error}`); return null; } } async verifyStateAvailable(stateName) { const stateObject = await this.adapter.getObjectAsync(stateName); if (!stateObject) { this.adapter.log.debug(`[verifyStateAvailable](${stateName}): State does not exist.`); return false; } return true; } async asyncGetForeignStateVal(stateName) { try { const stateObject = await this.asyncGetForeignState(stateName); if (stateObject == null) { return null; } return stateObject.val; } catch (error) { this.adapter.log.error(`[asyncGetForeignStateValue](${stateName}): ${error}`); return null; } } async asyncGetForeignState(stateName) { try { const stateObject = await this.adapter.getForeignObjectAsync(stateName); if (!stateObject) { throw new Error(`State '${stateName}' does not exist.`); } else { const stateValueObject = await this.adapter.getForeignStateAsync(stateName); if (!this.isLikeEmpty(stateValueObject)) { return stateValueObject ?? null; } throw new Error(`Unable to retrieve info from state '${stateName}'.`); } } catch (error) { this.adapter.log.error(`[asyncGetForeignState](${stateName}): ${error}`); return null; } } isLikeEmpty(inputVar) { if (typeof inputVar !== "undefined" && inputVar !== null) { const sTemp = JSON.stringify(inputVar).replace(/[\s"'[\]{}]/g, ""); if (sTemp !== "") { return false; } return true; } return true; } async checkAndSetValue(stateName, value, description = "-", role = "text", writeable = false, dontUpdate = false, forceMode = false) { if (value?.trim()?.length) { const commonObj = { name: stateName.split(".").pop() ?? stateName, type: "string", role: role, desc: description, read: true, write: writeable, }; await (forceMode ? this.adapter.setObject(stateName, { type: "state", common: commonObj, native: {} }) : this.adapter.setObjectNotExistsAsync(stateName, { type: "state", common: commonObj, native: {} })); if (!dontUpdate || !(await this.adapter.getStateAsync(stateName))) { await this.adapter.setState(stateName, { val: value, ack: true }); } } } async checkAndSetValueNumber(stateName, value, description = "-", unit, role = "value", writeable = false, dontUpdate = false, forceMode = false, min, max, step) { if (value !== undefined) { const commonObj = { name: stateName.split(".").pop() ?? stateName, type: "number", role: role, desc: description, read: true, write: writeable, ...((unit ?? undefined) ? { unit } : {}), ...((min ?? undefined) ? { min } : {}), ...((max ?? undefined) ? { max } : {}), ...((step ?? undefined) ? { step } : {}), }; if (unit != null) { commonObj.unit = unit; } await (forceMode ? this.adapter.setObject(stateName, { type: "state", common: commonObj, native: {} }) : this.adapter.setObjectNotExistsAsync(stateName, { type: "state", common: commonObj, native: {} })); if (!dontUpdate || !(await this.adapter.getStateAsync(stateName))) { await this.adapter.setState(stateName, { val: value, ack: true }); } } } async checkAndSetValueBoolean(stateName, value, description = "-", role = "indicator", writeable = false, dontUpdate = false, forceMode = false) { if (value !== undefined && value !== null) { const commonObj = { name: stateName.split(".").pop() ?? stateName, type: "boolean", role: role, desc: description, read: true, write: writeable, }; await (forceMode ? this.adapter.setObject(stateName, { type: "state", common: commonObj, native: {} }) : this.adapter.setObjectNotExistsAsync(stateName, { type: "state", common: commonObj, native: {} })); if (!dontUpdate || !(await this.adapter.getStateAsync(stateName))) { await this.adapter.setState(stateName, { val: value, ack: true }); } } } async checkAndSetFolder(folderObjectName, name, icon = "", forceMode = false) { const commonObj = { type: "meta.folder", name: name ?? folderObjectName.split(".").pop() ?? folderObjectName, desc: "", }; if (icon) { commonObj.icon = icon; } await (forceMode ? this.adapter.setObject(folderObjectName, { type: "folder", common: commonObj, native: {}, }) : this.adapter.setObjectNotExistsAsync(folderObjectName, { type: "folder", common: commonObj, native: {}, })); } async checkAndSetDevice(deviceObjectName, name, onlineId = "", icon = "", forceMode = false) { const commonObj = { name: name ?? deviceObjectName.split(".").pop() ?? deviceObjectName, desc: "", }; if (onlineId) { commonObj.statusStates = { onlineId, }; } if (icon) { commonObj.icon = icon; } await (forceMode ? this.adapter.setObject(deviceObjectName, { type: "device", common: commonObj, native: {}, }) : this.adapter.setObjectNotExistsAsync(deviceObjectName, { type: "device", common: commonObj, native: {}, })); } async checkAndSetChannel(channelObjectName, name, icon = "", forceMode = false) { const commonObj = { name: name ?? channelObjectName.split(".").pop() ?? channelObjectName, desc: "", }; if (icon) { commonObj.icon = icon; } await (forceMode ? this.adapter.setObject(channelObjectName, { type: "channel", common: commonObj, native: {}, }) : this.adapter.setObjectNotExistsAsync(channelObjectName, { type: "channel", common: commonObj, native: {}, })); } generateErrorMessage(error, context) { let errorMessages = ""; if (error.errors && Array.isArray(error.errors)) { for (const err of error.errors) { if (errorMessages) { errorMessages += ", "; } errorMessages += err.message; } } else if (error.message) { errorMessages = error.message; } else { errorMessages = "Unknown error"; } return `Error (${error.statusMessage || error.statusText || "Unknown Status"}) occurred during: -${context}- : ${errorMessages}`; } } exports.ProjectUtils = ProjectUtils; //# sourceMappingURL=projectUtils.js.map