-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-ICA.js
146 lines (126 loc) · 4.76 KB
/
MMM-ICA.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Module.register("MMM-ICA", {
defaults: {
username: "",
password: "",
apiUrl: "",
storeApiUrl: "",
updateNotification: "UPDATE_ICA_MODULE",
updateInterval: 10 * 60 * 1000, // Update every 10 minutes
retryDelay: 5 * 60 * 1000, // Retry delay
settings: {
Saldo: true,
AccountName: true,
FavoriteStores: true,
DisplayStoreID: true,
},
offersStoreId: "",
predictedSaldo: null, // Added for predicted saldo
},
start: function() {
console.log("ICA Module config:", this.config);
Log.info(`Starting module: ${this.name}`);
if (!this.config.username || !this.config.password) {
console.error("ICA Error: username or password not provided in module config.");
this.authTicket = "";
this.updateDom();
return;
}
this.sendSocketNotification("GET_AUTH_TICKET", this.config);
if (this.cardAccountsInterval) {
clearInterval(this.cardAccountsInterval);
}
this.cardAccountsInterval = setInterval(() => {
this.getCardAccounts();
}, this.config.updateInterval);
},
getCardAccounts: function() {
// Logic to get card accounts, if any
},
getDom: function() {
const wrapper = document.createElement("div");
wrapper.className = "small bright";
if (this.cardAccounts) {
if (this.config.settings.Saldo) {
const saldoDiv = document.createElement("div");
saldoDiv.innerHTML = `Tillgängligt Saldo: ${this.cardAccounts.Cards[0].Accounts[0].Available}`;
wrapper.appendChild(saldoDiv);
}
if (this.config.settings.AccountName) {
const accountNameDiv = document.createElement("div");
accountNameDiv.innerHTML = `Account Name: ${this.cardAccounts.Cards[0].Accounts[0].AccountName}`;
wrapper.appendChild(accountNameDiv);
}
if (this.config.predictedSaldo !== null) {
this.displayPredictedSaldo(wrapper);
}
if (this.config.settings.FavoriteStores && this.favoriteStores) {
const favoriteStoresDiv = document.createElement("div");
const favoriteStores = this.favoriteStores.FavoriteStores.join(", ");
favoriteStoresDiv.innerHTML = `Favorite Stores: ${favoriteStores}`;
wrapper.appendChild(favoriteStoresDiv);
}
if (this.config.settings.DisplayStoreID) {
const storeIDDiv = document.createElement("div");
storeIDDiv.innerHTML = `Store ID: ${this.config.offersStoreId}`;
wrapper.appendChild(storeIDDiv);
}
} else {
wrapper.innerHTML = "<span class='small fa fa-refresh fa-spin fa-fw'></span>";
wrapper.className = "small dimmed";
}
return wrapper;
},
displayPredictedSaldo: function(wrapper) {
const predictedSaldoDiv = document.createElement("div");
predictedSaldoDiv.innerHTML = `Gissad saldo den sista: ${this.config.predictedSaldo}`;
wrapper.appendChild(predictedSaldoDiv);
},
socketNotificationReceived: function(notification, payload) {
console.log("Received socket notification:", notification, "with payload:", payload);
if (notification === "AUTH_TICKET_RESULT") {
if (payload.error) {
console.error(`ICA Error getting authentication ticket: ${payload.error}`);
this.authTicket = "";
this.updateDom();
setTimeout(() => {
this.sendSocketNotification("GET_AUTH_TICKET", this.config);
}, this.config.retryDelay);
return;
}
const authTicket = payload.authenticationTicket;
if (!authTicket) {
console.error("ICA Error: Unable to retrieve authentication ticket.");
this.authTicket = "";
this.updateDom();
setTimeout(() => {
this.sendSocketNotification("GET_AUTH_TICKET", this.config);
}, this.config.retryDelay);
return;
}
console.log(`ICA Got authentication ticket: ${authTicket}`);
this.authTicket = authTicket;
this.updateDom();
this.getCardAccounts(); // Call to fetch card accounts
} else if (notification === "CARD_ACCOUNTS_RESULT") {
if (payload.error) {
console.error(`ICA Error getting card accounts: ${payload.error}`);
this.updateDom();
return;
}
const cardAccounts = payload.cardAccounts;
if (!cardAccounts) {
console.error("ICA Error: Unable to retrieve card accounts.");
this.updateDom();
return;
}
console.log("ICA Got card accounts:", cardAccounts);
this.cardAccounts = cardAccounts;
this.updateDom(1000);
console.error("ICA Error:Exporting: Unable to retrieve card information.");
} else if (notification === "PREDICTION_RESULT") {
console.log(`Exporting: ${payload}`);
this.config.predictedSaldo = payload; // Assuming payload is the predicted saldo
this.updateDom();
}
},
});