Skip to content

Commit

Permalink
#104 HTTP Request
Browse files Browse the repository at this point in the history
  • Loading branch information
MiRo1310 committed Jan 26, 2024
1 parent d9c4ed1 commit 585d43d
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 65 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ You can create different groups with separate menus, and then assign users to th

### **WORK IN PROGRESS**

- #104 HTTP Request
- #111 status Value in dynamicSwitch
- #112 change value
- **_Change!!!_** menu:deleteAll removes automatic Request Ids which are older than 48h from list. The list is completely deleted once during the update
Expand Down
2 changes: 1 addition & 1 deletion admin/build/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions admin/build/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion admin/src/components/popupCards/RowEditPopupCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class RowEditPopupCard extends Component {
<TableCell align="left" key={i}>
<Input
width={entry.search ? "calc(100% - 50px)" : "100%"}
value={row[entry.name].replace(/&amp;/g, "&")}
value={typeof row[entry.name] === "string" ? row[entry.name].replace(/&amp;/g, "&") : ""}
margin="0px 2px 0 5px"
id={entry.name}
index={index}
Expand Down
2 changes: 1 addition & 1 deletion admin/src/lib/actionUtilis.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tabValues } from "./entrys.mjs";
function createData(element, index, rowElements) {
const obj = {};
rowElements.forEach((entry) => {
obj[entry.name] = element[entry.name][index];
obj[entry.name] = element[entry.name] ? (element[entry.name][index] ? element[entry.name][index] : null) : null;
});
return obj;
}
Expand Down
77 changes: 46 additions & 31 deletions admin/src/lib/entrys.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
export const tabValues = [
// Danach noch generateActions anpassen in action.js
{
label: "SetState",
value: "set",
trigger: true, // Wenn Trigger true ist wird hierfür UsedTrigger gesucht
entrys: [
{ name: "trigger", val: "", headline: "Trigger", elementGetRows: "IDs" },
{ name: "IDs", val: "", headline: "ID", width: "20%" },
{ name: "values", val: "", headline: "Value", width: "10%" },
{ name: "returnText", val: "", headline: "Return text", width: "40%" },
{ name: "ack", val: "false", headline: "Ack", title: "Set Value with ack-Flag", checkbox: true, width: "3%" },
{ name: "confirm", val: "false", headline: "Con", title: "Confirm with message", checkbox: true, width: "3%" },
{ name: "switch_checkbox", val: "false", headline: "Swi", title: "Switch", checkbox: true, width: "3%" },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%" },
{ name: "trigger", val: "", headline: "Trigger", elementGetRows: "IDs", required: true },
{ name: "IDs", val: "", headline: "ID", width: "20%", required: true },
{ name: "values", val: "", headline: "Value", width: "10%", required: true },
{ name: "returnText", val: "", headline: "Return text", width: "40%", required: true },
{ name: "ack", val: "false", headline: "Ack", title: "Set Value with ack-Flag", checkbox: true, width: "3%", required: true },
{ name: "confirm", val: "false", headline: "Con", title: "Confirm with message", checkbox: true, width: "3%", required: true },
{ name: "switch_checkbox", val: "false", headline: "Swi", title: "Switch", checkbox: true, width: "3%", required: true },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
Expand All @@ -20,11 +21,11 @@ export const tabValues = [
value: "get",
trigger: true,
entrys: [
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "IDs" },
{ name: "IDs", val: "", headline: "ID", width: "40%" },
{ name: "text", val: "", headline: "Text", width: "40%" },
{ name: "newline_checkbox", val: "true", headline: "New", title: "Newline", checkbox: true, width: "3%" },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%" },
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "IDs", required: true },
{ name: "IDs", val: "", headline: "ID", width: "40%", required: true },
{ name: "text", val: "", headline: "Text", width: "40%", required: true },
{ name: "newline_checkbox", val: "true", headline: "New", title: "Newline", checkbox: true, width: "3%", required: true },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
Expand All @@ -34,10 +35,10 @@ export const tabValues = [
value: "pic",
trigger: true,
entrys: [
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "IDs" },
{ name: "IDs", val: "", headline: "ID", width: "40%" },
{ name: "fileName", val: "", headline: "Filename", width: "40%" },
{ name: "picSendDelay", val: "", headline: "Delay", width: "40%", type: "number" },
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "IDs", required: true },
{ name: "IDs", val: "", headline: "ID", width: "40%", required: true },
{ name: "fileName", val: "", headline: "Filename", width: "40%", required: true },
{ name: "picSendDelay", val: "", headline: "Delay (ms)", width: "40%", type: "number", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
Expand All @@ -46,10 +47,10 @@ export const tabValues = [
value: "loc",
trigger: true,
entrys: [
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "latitude" },
{ name: "latitude", val: "", headline: "Latitude", width: "20%", search: true },
{ name: "longitude", val: "", headline: "Longitude", width: "20%", search: true },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%" },
{ name: "trigger", val: "", headline: "Trigger", width: "20%", elementGetRows: "latitude", required: true },
{ name: "latitude", val: "", headline: "Latitude", width: "20%", search: true, required: true },
{ name: "longitude", val: "", headline: "Longitude", width: "20%", search: true, required: true },
{ name: "parse_mode", val: "false", headline: "Par", title: "Parse Mode HTML", checkbox: true, width: "3%", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
Expand All @@ -58,10 +59,10 @@ export const tabValues = [
value: "events",
trigger: false,
entrys: [
{ name: "ID", val: "", headline: "ID", width: "40%", search: true },
{ name: "menu", val: "", headline: "Executed Menu", width: "20%", elementGetRows: "menu" },
{ name: "condition", val: "", headline: "Condition to open Menu", width: "20%", noIcon: true },
{ name: "ack", val: "false", headline: "Ack", title: "Ack-Flag to open Menu", checkbox: true, width: "3%" },
{ name: "ID", val: "", headline: "ID", width: "40%", search: true, required: true },
{ name: "menu", val: "", headline: "Executed Menu", width: "20%", elementGetRows: "menu", required: true },
{ name: "condition", val: "", headline: "Condition to open Menu", width: "20%", noIcon: true, required: true },
{ name: "ack", val: "false", headline: "Ack", title: "Ack-Flag to open Menu", checkbox: true, width: "3%", required: true },
],
popupCard: { buttons: { add: false, remove: false }, width: "99%", height: "40%" },
},
Expand All @@ -71,16 +72,30 @@ export const tabValues = [
searchRoot: { root: "echarts", type: ["chart"] }, // Search Root for SelectID
trigger: true,
entrys: [
{ name: "trigger", val: "", headline: "Trigger", width: "20%" },
// { name: "echartInstance", val: "echarts.0", headline: "Echart Instance", width: "40%" },
{ name: "preset", val: "", headline: "Preset", width: "40%", elementGetRows: "preset", search: true },
{ name: "background", val: "#FFFFFF", headline: "Background", width: "10%" },
{ name: "theme", val: "light", headline: "Theme", width: "10%" },
{ name: "filename", val: "echarts-temp-photo.jpg", headline: "Filename", width: "20%" },
{ name: "trigger", val: "", headline: "Trigger", width: "20%", required: true },
{ name: "preset", val: "", headline: "Preset", width: "40%", elementGetRows: "preset", search: true, required: true },
{ name: "background", val: "#FFFFFF", headline: "Background", width: "10%", required: true },
{ name: "theme", val: "light", headline: "Theme", width: "10%", required: true },
{ name: "filename", val: "echarts-temp-photo.jpg", headline: "Filename", width: "20%", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
{
label: "Http Request",
value: "httpRequest",
trigger: true,
entrys: [
{ name: "trigger", val: "", headline: "Trigger", width: "20%", required: true },
{ name: "url", val: "", headline: "URL", width: "20%", elementGetRows: "url", search: true, required: true },
{ name: "user", val: "", headline: "User", width: "20%" },
{ name: "password", val: "", headline: "Password", password: true, width: "20%" },
{ name: "filename", val: "http-request-photo.jpg", headline: "Filename", width: "20%", required: true },
{ name: "delay", val: 5000, headline: "Delay (ms)", width: "20%", type: "number", required: true },
],
popupCard: { buttons: { add: true, remove: true }, width: "99%", height: "70%" },
},
];
// Danach noch generateActions anpassen in action.js

export const navEntrys = [
{ name: "call", val: "", headline: "Trigger", width: "25%", editWidth: "98%" },
Expand Down
11 changes: 5 additions & 6 deletions admin/src/pages/TabAction/Action/ActionCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ class ActionCard extends Component {
let valueRowValuesAndSwitch = true;
let row = this.state.newRow;
this.props.entrys.forEach((entry) => {
if (!entry.checkbox) {
console.log("test");
if (!entry.checkbox && entry.required) {
console.log("entry", entry);
// Wenn der Wert nicht vorhanden ist, dadurch das evtl eine neues Element in entrys hinzugefügt wurde
if (!row[entry.name]) row[entry.name] = [""];
row[entry.name].forEach((val, index) => {
// console.log("row ", row);
// console.log("name " + entry.name, "val " + val, "index " + index);
if (value && entry.name === "values") {
if ((val !== "" && val !== undefined && val !== null) || row.switch_checkbox[index] === "true") {
valueRowValuesAndSwitch = true;
} else {
// console.log(entry.name, val, index);
valueRowValuesAndSwitch = false;
}
} else if (value && val == "") {
// console.log(entry.name, val, index);
// console.log("false", entry.name, val, index);
value = false;
}
});
Expand Down
6 changes: 6 additions & 0 deletions lib/js/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ function generateActions(_this, action, userObject) {
{ name: "parse_mode", key: 0 },
],
},
{
objName: "httpRequest",
name: "httpRequest",
loop: "url",
elements: [{ name: "url" }, { name: "user" }, { name: "password" }, { name: "filename" }, { name: "delay" }],
},
];

_this.log.debug("action : " + JSON.stringify(action));
Expand Down
53 changes: 53 additions & 0 deletions lib/js/httpRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const axios = require("axios");
const { sendToTelegram } = require("./telegram");
const path = require("path");
const fs = require("fs");
async function httpRequest(_this, parts, userToSend, instanceTelegram, resize_keyboard, one_time_keyboard, userListWithChatID, directoryPicture) {
parts.httpRequest.forEach((part) => {
const url = part.url;
const user = part.user;
const password = part.password;
const method = "get";

axios(
user
? {
method: method,
url: url,
responseType: "arraybuffer",
auth: {
username: user,
password: password,
},
}
: {
method: method,
url: url,
responseType: "arraybuffer",
},
)
.then(function (response) {
const imagePath = path.join(directoryPicture, part.filename);
try {
fs.writeFileSync(imagePath, Buffer.from(response.data), "binary");
_this.log.debug("Bild erfolgreich gespeichert:", imagePath);
} catch (error) {
_this.log.error("Fehler beim Speichern des Bildes:", error);
}

// Hier könntest du weitere Aktionen durchführen, z.B. iobroker-Objekt aktualisieren
setTimeout(() => {
sendToTelegram(_this, user, imagePath, [], instanceTelegram, resize_keyboard, one_time_keyboard, userListWithChatID, "");
}, part.delay);
})
.catch(function (response) {
//handle error
console.log(response);
// sendToTelegram(_this, userToSend, response, [], instanceTelegram, resize_keyboard, one_time_keyboard, userListWithChatID, "");
});
});
return true;
}
module.exports = {
httpRequest,
};
17 changes: 16 additions & 1 deletion lib/js/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { getDynamicValue, removeUserFromDynamicValue } = require("./dynamicValue"
const { adjustValueType } = require("./action");
const { _subscribeAndUnSubscribeForeignStatesAsync } = require("./subscribeStates");
const { getChart } = require("./echarts");
const { httpRequest } = require("./httpRequest");

/**
*
Expand Down Expand Up @@ -141,7 +142,18 @@ async function processData(
removeUserFromDynamicValue(userToSend);
const result = await switchBack(_this, userToSend, allMenusWithData, menus, true);
console.log("Result: " + JSON.stringify(result));
if (result) sendToTelegram(_this, userToSend, result["texttosend"], result["menuToSend"], instanceTelegram, resize_keyboard, one_time_keyboard, userListWithChatID, result["parseMode"]);
if (result)
sendToTelegram(
_this,
userToSend,
result["texttosend"],
result["menuToSend"],
instanceTelegram,
resize_keyboard,
one_time_keyboard,
userListWithChatID,
result["parseMode"],
);
else sendNav(_this, part, userToSend, instanceTelegram, userListWithChatID, resize_keyboard, one_time_keyboard);
return true;
}
Expand Down Expand Up @@ -228,6 +240,9 @@ async function processData(
_this.log.debug("Echarts");
await getChart(_this, part.echarts, directoryPicture, userToSend, instanceTelegram, userListWithChatID, resize_keyboard, one_time_keyboard);
return true;
} else if (part.httpRequest) {
const result = await httpRequest(_this, part, userToSend, instanceTelegram, resize_keyboard, one_time_keyboard, userListWithChatID, directoryPicture);
if (result) return true;
}
} else if ((calledValue.startsWith("menu") || calledValue.startsWith("submenu")) && menuData[call]) {
_this.log.debug("Call Submenu");
Expand Down
30 changes: 11 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 585d43d

Please sign in to comment.