iobroker.bosch-ebike
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"common": { | ||
"name": "bosch-ebike", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"news": { | ||
"0.0.3": { | ||
"en": "fix login" | ||
}, | ||
"0.0.2": { | ||
@@ -19,3 +22,2 @@ "en": "initial release", | ||
}, | ||
"title": "Bosch eBike", | ||
"titleLang": { | ||
@@ -22,0 +24,0 @@ "en": "Bosch eBike", |
@@ -13,280 +13,280 @@ //v1.5 | ||
module.exports = class Json2iob { | ||
constructor(adapter) { | ||
this.adapter = adapter; | ||
this.alreadyCreatedObjects = {}; | ||
} | ||
constructor(adapter) { | ||
this.adapter = adapter; | ||
this.alreadyCreatedObjects = {}; | ||
} | ||
async parse(path, element, options) { | ||
try { | ||
if (element === null || element === undefined) { | ||
this.adapter.log.debug("Cannot extract empty: " + path); | ||
return; | ||
} | ||
async parse(path, element, options) { | ||
try { | ||
if (element === null || element === undefined) { | ||
this.adapter.log.debug("Cannot extract empty: " + path); | ||
return; | ||
} | ||
const objectKeys = Object.keys(element); | ||
const objectKeys = Object.keys(element); | ||
if (!options || !options.write) { | ||
if (!options) { | ||
options = { write: false }; | ||
} else { | ||
options["write"] = false; | ||
} | ||
} | ||
if (!options || !options.write) { | ||
if (!options) { | ||
options = { write: false }; | ||
} else { | ||
options["write"] = false; | ||
} | ||
} | ||
if (typeof element === "string" || typeof element === "number") { | ||
let name = element; | ||
if (typeof element === "number") { | ||
name = element.toString(); | ||
} | ||
if (!this.alreadyCreatedObjects[path]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path, { | ||
type: "state", | ||
common: { | ||
name: name, | ||
role: this.getRole(element, options.write), | ||
type: element !== null ? typeof element : "mixed", | ||
write: options.write, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
// this.alreadyCreatedObjects[path] = true; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
if (typeof element === "string" || typeof element === "number") { | ||
let name = element; | ||
if (typeof element === "number") { | ||
name = element.toString(); | ||
} | ||
if (!this.alreadyCreatedObjects[path]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path, { | ||
type: "state", | ||
common: { | ||
name: name, | ||
role: this.getRole(element, options.write), | ||
type: element !== null ? typeof element : "mixed", | ||
write: options.write, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
this.alreadyCreatedObjects[path] = true; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
this.adapter.setState(path, element, true); | ||
this.adapter.setState(path, element, true); | ||
return; | ||
} | ||
if (!this.alreadyCreatedObjects[path]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path, { | ||
type: "channel", | ||
common: { | ||
name: options.channelName || "", | ||
write: false, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
// this.alreadyCreatedObjects[path] = true; | ||
options.channelName = undefined; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
if (Array.isArray(element)) { | ||
await this.extractArray(element, "", path, options); | ||
return; | ||
} | ||
return; | ||
} | ||
if (!this.alreadyCreatedObjects[path]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path, { | ||
type: "channel", | ||
common: { | ||
name: options.channelName || "", | ||
write: false, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
this.alreadyCreatedObjects[path] = true; | ||
options.channelName = undefined; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
if (Array.isArray(element)) { | ||
await this.extractArray(element, "", path, options); | ||
return; | ||
} | ||
for (const key of objectKeys) { | ||
if (this.isJsonString(element[key]) && options.autoCast) { | ||
element[key] = JSONbig.parse(element[key]); | ||
} | ||
for (const key of objectKeys) { | ||
if (this.isJsonString(element[key]) && options.autoCast) { | ||
element[key] = JSONbig.parse(element[key]); | ||
} | ||
if (Array.isArray(element[key])) { | ||
await this.extractArray(element, key, path, options); | ||
} else if (element[key] !== null && typeof element[key] === "object") { | ||
await this.parse(path + "." + key, element[key], options); | ||
} else { | ||
if (!this.alreadyCreatedObjects[path + "." + key]) { | ||
let objectName = key; | ||
if (options.descriptions && options.descriptions[key]) { | ||
objectName = options.descriptions[key]; | ||
} | ||
const type = element[key] !== null ? typeof element[key] : "mixed"; | ||
const common = { | ||
name: objectName, | ||
role: this.getRole(element[key], options.write), | ||
type: type, | ||
write: options.write, | ||
read: true, | ||
}; | ||
if (Array.isArray(element[key])) { | ||
await this.extractArray(element, key, path, options); | ||
} else if (element[key] !== null && typeof element[key] === "object") { | ||
await this.parse(path + "." + key, element[key], options); | ||
} else { | ||
if (!this.alreadyCreatedObjects[path + "." + key]) { | ||
let objectName = key; | ||
if (options.descriptions && options.descriptions[key]) { | ||
objectName = options.descriptions[key]; | ||
} | ||
const type = element[key] !== null ? typeof element[key] : "mixed"; | ||
const common = { | ||
name: objectName, | ||
role: this.getRole(element[key], options.write), | ||
type: type, | ||
write: options.write, | ||
read: true, | ||
}; | ||
await this.adapter | ||
.setObjectNotExistsAsync(path + "." + key, { | ||
type: "state", | ||
common: common, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
// this.alreadyCreatedObjects[path + "." + key] = true; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
this.adapter.setState(path + "." + key, element[key], true); | ||
} | ||
} | ||
} catch (error) { | ||
this.adapter.log.error("Error extract keys: " + path + " " + JSON.stringify(element)); | ||
this.adapter.log.error(error); | ||
await this.adapter | ||
.setObjectNotExistsAsync(path + "." + key, { | ||
type: "state", | ||
common: common, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
this.alreadyCreatedObjects[path + "." + key] = true; | ||
}) | ||
.catch((error) => { | ||
this.adapter.log.error(error); | ||
}); | ||
} | ||
this.adapter.setState(path + "." + key, element[key], true); | ||
} | ||
} | ||
} catch (error) { | ||
this.adapter.log.error("Error extract keys: " + path + " " + JSON.stringify(element)); | ||
this.adapter.log.error(error); | ||
} | ||
async extractArray(element, key, path, options) { | ||
try { | ||
if (key) { | ||
element = element[key]; | ||
} | ||
async extractArray(element, key, path, options) { | ||
try { | ||
if (key) { | ||
element = element[key]; | ||
} | ||
for (let index in element) { | ||
const arrayElement = element[index]; | ||
index = parseInt(index) + 1; | ||
if (index < 10) { | ||
index = "0" + index; | ||
} | ||
let arrayPath = key + index; | ||
if (typeof arrayElement === "string") { | ||
await this.parse(path + "." + key + "." + arrayElement, arrayElement, options); | ||
continue; | ||
} | ||
if (typeof arrayElement[Object.keys(arrayElement)[0]] === "string") { | ||
arrayPath = arrayElement[Object.keys(arrayElement)[0]]; | ||
} | ||
for (const keyName of Object.keys(arrayElement)) { | ||
if (keyName.endsWith("Id") && arrayElement[keyName] !== null) { | ||
if (arrayElement[keyName] && arrayElement[keyName].replace) { | ||
arrayPath = arrayElement[keyName].replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement[keyName]; | ||
} | ||
for (let index in element) { | ||
const arrayElement = element[index]; | ||
index = parseInt(index) + 1; | ||
if (index < 10) { | ||
index = "0" + index; | ||
} | ||
let arrayPath = key + index; | ||
if (typeof arrayElement === "string") { | ||
await this.parse(path + "." + key + "." + arrayElement, arrayElement, options); | ||
continue; | ||
} | ||
if (typeof arrayElement[Object.keys(arrayElement)[0]] === "string") { | ||
arrayPath = arrayElement[Object.keys(arrayElement)[0]]; | ||
} | ||
for (const keyName of Object.keys(arrayElement)) { | ||
if (keyName.endsWith("Id") && arrayElement[keyName] !== null) { | ||
if (arrayElement[keyName] && arrayElement[keyName].replace) { | ||
arrayPath = arrayElement[keyName].replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement[keyName]; | ||
} | ||
} | ||
} | ||
for (const keyName in Object.keys(arrayElement)) { | ||
if (keyName.endsWith("Name")) { | ||
if (arrayElement[keyName] && arrayElement[keyName].replace) { | ||
arrayPath = arrayElement[keyName].replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement[keyName]; | ||
} | ||
} | ||
} | ||
if (arrayElement.id) { | ||
if (arrayElement.id.replace) { | ||
arrayPath = arrayElement.id.replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement.id; | ||
} | ||
} | ||
if (arrayElement.name) { | ||
arrayPath = arrayElement.name.replace(/\./g, ""); | ||
} | ||
if (arrayElement.label) { | ||
arrayPath = arrayElement.label.replace(/\./g, ""); | ||
} | ||
if (arrayElement.labelText) { | ||
arrayPath = arrayElement.labelText.replace(/\./g, ""); | ||
} | ||
if (arrayElement.start_date_time) { | ||
arrayPath = arrayElement.start_date_time.replace(/\./g, ""); | ||
} | ||
if (options.preferedArrayName && options.preferedArrayName.indexOf("+") !== -1) { | ||
const preferedArrayNameArray = options.preferedArrayName.split("+"); | ||
if (arrayElement[preferedArrayNameArray[0]]) { | ||
const element0 = arrayElement[preferedArrayNameArray[0]].replace(/\./g, "").replace(/\ /g, ""); | ||
let element1 = ""; | ||
if (preferedArrayNameArray[1].indexOf("/") !== -1) { | ||
const subArray = preferedArrayNameArray[1].split("/"); | ||
const subElement = arrayElement[subArray[0]]; | ||
if (subElement && subElement[subArray[1]] !== undefined) { | ||
element1 = subElement[subArray[1]]; | ||
} else if (arrayElement[subArray[1]] !== undefined) { | ||
element1 = arrayElement[subArray[1]]; | ||
} | ||
} else { | ||
element1 = arrayElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/\ /g, ""); | ||
} | ||
arrayPath = element0 + "-" + element1; | ||
} | ||
} else if (options.preferedArrayName && options.preferedArrayName.indexOf("/") !== -1) { | ||
const preferedArrayNameArray = options.preferedArrayName.split("/"); | ||
const subElement = arrayElement[preferedArrayNameArray[0]]; | ||
if (subElement) { | ||
arrayPath = subElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/\ /g, ""); | ||
} | ||
} else if (options.preferedArrayName && arrayElement[options.preferedArrayName]) { | ||
arrayPath = arrayElement[options.preferedArrayName].replace(/\./g, ""); | ||
} | ||
if (options.forceIndex) { | ||
arrayPath = key + index; | ||
} | ||
//special case array with 2 string objects | ||
if ( | ||
!options.forceIndex && | ||
Object.keys(arrayElement).length === 2 && | ||
typeof Object.keys(arrayElement)[0] === "string" && | ||
typeof Object.keys(arrayElement)[1] === "string" && | ||
typeof arrayElement[Object.keys(arrayElement)[0]] !== "object" && | ||
typeof arrayElement[Object.keys(arrayElement)[1]] !== "object" && | ||
arrayElement[Object.keys(arrayElement)[0]] !== "null" | ||
) { | ||
let subKey = arrayElement[Object.keys(arrayElement)[0]]; | ||
const subValue = arrayElement[Object.keys(arrayElement)[1]]; | ||
const subName = Object.keys(arrayElement)[0] + " " + Object.keys(arrayElement)[1]; | ||
if (key) { | ||
subKey = key + "." + subKey; | ||
} | ||
if (!this.alreadyCreatedObjects[path + "." + subKey]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path + "." + subKey, { | ||
type: "state", | ||
common: { | ||
name: subName, | ||
role: this.getRole(subValue, options.write), | ||
type: subValue !== null ? typeof subValue : "mixed", | ||
write: options.write, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
// this.alreadyCreatedObjects[path + "." + subKey] = true; | ||
}); | ||
} | ||
this.adapter.setState(path + "." + subKey, subValue, true); | ||
continue; | ||
} | ||
await this.parse(path + "." + arrayPath, arrayElement, options); | ||
} | ||
} | ||
for (const keyName in Object.keys(arrayElement)) { | ||
if (keyName.endsWith("Name")) { | ||
if (arrayElement[keyName] && arrayElement[keyName].replace) { | ||
arrayPath = arrayElement[keyName].replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement[keyName]; | ||
} | ||
} catch (error) { | ||
this.adapter.log.error("Cannot extract array " + path); | ||
this.adapter.log.error(error); | ||
} | ||
} | ||
} | ||
isJsonString(str) { | ||
try { | ||
JSON.parse(str); | ||
} catch (e) { | ||
return false; | ||
if (arrayElement.id) { | ||
if (arrayElement.id.replace) { | ||
arrayPath = arrayElement.id.replace(/\./g, ""); | ||
} else { | ||
arrayPath = arrayElement.id; | ||
} | ||
} | ||
return true; | ||
} | ||
getRole(element, write) { | ||
if (typeof element === "boolean" && !write) { | ||
return "indicator"; | ||
if (arrayElement.name) { | ||
arrayPath = arrayElement.name.replace(/\./g, ""); | ||
} | ||
if (typeof element === "boolean" && write) { | ||
return "switch"; | ||
if (arrayElement.label) { | ||
arrayPath = arrayElement.label.replace(/\./g, ""); | ||
} | ||
if (typeof element === "number" && !write) { | ||
return "value"; | ||
if (arrayElement.labelText) { | ||
arrayPath = arrayElement.labelText.replace(/\./g, ""); | ||
} | ||
if (typeof element === "number" && write) { | ||
return "level"; | ||
if (arrayElement.start_date_time) { | ||
arrayPath = arrayElement.start_date_time.replace(/\./g, ""); | ||
} | ||
if (typeof element === "string") { | ||
return "text"; | ||
if (options.preferedArrayName && options.preferedArrayName.indexOf("+") !== -1) { | ||
const preferedArrayNameArray = options.preferedArrayName.split("+"); | ||
if (arrayElement[preferedArrayNameArray[0]]) { | ||
const element0 = arrayElement[preferedArrayNameArray[0]].replace(/\./g, "").replace(/\ /g, ""); | ||
let element1 = ""; | ||
if (preferedArrayNameArray[1].indexOf("/") !== -1) { | ||
const subArray = preferedArrayNameArray[1].split("/"); | ||
const subElement = arrayElement[subArray[0]]; | ||
if (subElement && subElement[subArray[1]] !== undefined) { | ||
element1 = subElement[subArray[1]]; | ||
} else if (arrayElement[subArray[1]] !== undefined) { | ||
element1 = arrayElement[subArray[1]]; | ||
} | ||
} else { | ||
element1 = arrayElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/\ /g, ""); | ||
} | ||
arrayPath = element0 + "-" + element1; | ||
} | ||
} else if (options.preferedArrayName && options.preferedArrayName.indexOf("/") !== -1) { | ||
const preferedArrayNameArray = options.preferedArrayName.split("/"); | ||
const subElement = arrayElement[preferedArrayNameArray[0]]; | ||
if (subElement) { | ||
arrayPath = subElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/\ /g, ""); | ||
} | ||
} else if (options.preferedArrayName && arrayElement[options.preferedArrayName]) { | ||
arrayPath = arrayElement[options.preferedArrayName].replace(/\./g, ""); | ||
} | ||
return "state"; | ||
if (options.forceIndex) { | ||
arrayPath = key + index; | ||
} | ||
//special case array with 2 string objects | ||
if ( | ||
!options.forceIndex && | ||
Object.keys(arrayElement).length === 2 && | ||
typeof Object.keys(arrayElement)[0] === "string" && | ||
typeof Object.keys(arrayElement)[1] === "string" && | ||
typeof arrayElement[Object.keys(arrayElement)[0]] !== "object" && | ||
typeof arrayElement[Object.keys(arrayElement)[1]] !== "object" && | ||
arrayElement[Object.keys(arrayElement)[0]] !== "null" | ||
) { | ||
let subKey = arrayElement[Object.keys(arrayElement)[0]]; | ||
const subValue = arrayElement[Object.keys(arrayElement)[1]]; | ||
const subName = Object.keys(arrayElement)[0] + " " + Object.keys(arrayElement)[1]; | ||
if (key) { | ||
subKey = key + "." + subKey; | ||
} | ||
if (!this.alreadyCreatedObjects[path + "." + subKey]) { | ||
await this.adapter | ||
.setObjectNotExistsAsync(path + "." + subKey, { | ||
type: "state", | ||
common: { | ||
name: subName, | ||
role: this.getRole(subValue, options.write), | ||
type: subValue !== null ? typeof subValue : "mixed", | ||
write: options.write, | ||
read: true, | ||
}, | ||
native: {}, | ||
}) | ||
.then(() => { | ||
this.alreadyCreatedObjects[path + "." + subKey] = true; | ||
}); | ||
} | ||
this.adapter.setState(path + "." + subKey, subValue, true); | ||
continue; | ||
} | ||
await this.parse(path + "." + arrayPath, arrayElement, options); | ||
} | ||
} catch (error) { | ||
this.adapter.log.error("Cannot extract array " + path); | ||
this.adapter.log.error(error); | ||
} | ||
} | ||
isJsonString(str) { | ||
try { | ||
JSON.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
getRole(element, write) { | ||
if (typeof element === "boolean" && !write) { | ||
return "indicator"; | ||
} | ||
if (typeof element === "boolean" && write) { | ||
return "switch"; | ||
} | ||
if (typeof element === "number" && !write) { | ||
return "value"; | ||
} | ||
if (typeof element === "number" && write) { | ||
return "level"; | ||
} | ||
if (typeof element === "string") { | ||
return "text"; | ||
} | ||
return "state"; | ||
} | ||
}; |
@@ -209,3 +209,5 @@ "use strict"; | ||
for (const match of matches) { | ||
returnObject[match[1]] = match[2]; | ||
if (match[2] != null) { | ||
returnObject[match[1]] = match[2]; | ||
} | ||
} | ||
@@ -212,0 +214,0 @@ return returnObject; |
{ | ||
"name": "iobroker.bosch-ebike", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Adapter for Bosch eBike", | ||
@@ -21,3 +21,3 @@ "author": { | ||
"dependencies": { | ||
"@iobroker/adapter-core": "^2.6.2", | ||
"@iobroker/adapter-core": "^2.6.6", | ||
"axios": "^0.27.2", | ||
@@ -30,3 +30,3 @@ "http-cookie-agent": "^4.0.2", | ||
"devDependencies": { | ||
"@iobroker/adapter-dev": "^1.0.1", | ||
"@iobroker/adapter-dev": "^1.1.0", | ||
"@iobroker/testing": "^4.1.0", | ||
@@ -36,3 +36,3 @@ "@types/chai": "^4.3.3", | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^18.7.16", | ||
"@types/node": "^18.8.3", | ||
"@types/proxyquire": "^1.3.28", | ||
@@ -43,8 +43,8 @@ "@types/sinon": "^10.0.13", | ||
"chai-as-promised": "^7.1.1", | ||
"eslint": "^8.23.0", | ||
"eslint": "^8.24.0", | ||
"mocha": "^10.0.0", | ||
"proxyquire": "^2.1.3", | ||
"sinon": "^14.0.0", | ||
"sinon": "^14.0.1", | ||
"sinon-chai": "^3.7.0", | ||
"typescript": "~4.8.3" | ||
"typescript": "~4.8.4" | ||
}, | ||
@@ -51,0 +51,0 @@ "engines": { |
@@ -33,3 +33,6 @@ ![Logo](admin/bosch-ebike.png) | ||
### 0.0.1 | ||
### 0.0.3 | ||
* (TA2k) login fix | ||
### 0.0.2 | ||
* (TA2k) initial release | ||
@@ -58,2 +61,2 @@ | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
SOFTWARE. |
Telemetry
Supply chain riskThis package contains telemetry which tracks how it is used.
Found 1 instance in 1 package
886
61
85299
1