Innovenergy_trunk/rust/VrmGrabberOxidised/src/main.rs

168 lines
4.5 KiB
Rust

use rocket::serde;
use handlebars::Handlebars;
// use rocket::serde::__private::de::TagContentOtherField::Content;
use urlencoding::encode;
mod installation;
mod vrm_account;
struct InstallationToHtmlInterface<'a> {
Name: &'a str,
Ip: i64,
Vrm: &'a str,
Identifier: &'a str,
Serial: &'a str,
EscapedName: &'a str,
Online: &'a str,
LastSeen: &'a str,
NumBatteries: &'a str,
BatteryVersion: &'a str,
ServerIp : &'a str,
FirmwareVersion: &'a str,
}
// impl Default for InstallationToHtmlInterface{
// fn default() -> Self {
// Self{
// Name: "",
// Ip: 0,
// Vrm: "",
// Identifier: "",
// Serial: "",
// EscapedName: "",
// Online: "",
// LastSeen: "",
// NumBatteries: "",
// BatteryVersion: "",
// ServerIp : "10.2.0.1",
// FirmwareVersion: "AF09",
// }
// }
// }
#[macro_use] extern crate rocket;
#[get("/")]
async fn index() -> String{
let res = vrm_account::all_installations_request().await;
let json = rocket::serde::Deserialize(res.unwrap().text().await.unwrap());
let source = "<head>
<style>
tbody {
background-color: #e4f0f5;
}
tbody tr:nth-child(odd) {
background-color: #ECE9E9;
}
th, td { /* cell */
padding: 0.75rem;
font-size: 0.9375rem;
}
th { /* header cell */
font-weight: 700;
text-align: left;
color: #272838;
border-bottom: 2px solid #EB9486;
position: sticky;
top: 0;
background-color: #F9F8F8;
}
table {
border-collapse: collapse;
width: 100%;
border: 2px solid rgb(200, 200, 200);
letter-spacing: 1px;
font-family: sans-serif;
font-size: 0.8rem;
position: absolute; top: 0; bottom: 0; left: 0; right: 0;
}
thead th {
border: 1px solid rgb(190, 190, 190);
padding: 5px 10px;
position: sticky;
position: -webkit-sticky;
top: 0px;
background: white;
z-index: 999;
}
td {
text-align: left;
}
#managerTable {
overflow: hidden;
}</style></head>
<div id='managerTable'>
<table>
<tbody>
<tr>
<th>Name</th>
<th>Gui</th>
<th>VRM</th>
<th>Grafana</th>
<th>Identifier</th>
<th>Last Seen</th>
<th>Serial</th>
<th>#Batteries</th>
<th>Firmware-Version</th>
<th>Update</th>
</tr>
{{#inst}}
{{> installations}}
{{/inst}}
</tbody>
</table>
<div id='managerTable'>";
let partialSource= "<tr><td>{{Name}}</td>
<td><a target='_blank' href=http://{{Ip}}>{{online}} {{Ip}}</a></td>
<td><a target='_blank' href=https://vrm.victronenergy.com/installation/{{Vrm}}/dashboard>VRM</a></td>
<td><a target='_blank' href='https://salidomo.innovenergy.ch/d/ENkNRQXmk/installation?refresh=5s&orgId=1&var-Installation={{EscapedName}}&kiosk=tv'>Grafana</a></td>
<td>{{Identifier}}</td>
<td>{{LastSeen}}</td>
<td>{{Serial}}</td>
<td>{{NumBatteries}}</td>
<td>{{BatteryVersion}}</td>
<td><a target='_blank' href=https://{{ServerIp}}/UpdateBatteryFirmware/{{Ip}}/{{NumBatteries}}>⬆️{{FirmwareVersion}}</a></td>
</tr>";
let mut handlebars = Handlebars::new();
handlebars.register_template_string("installations", partialSource);
let mut installsForHtml = Vec::<InstallationToHtmlInterface>::new();
for inst in json["records"] {
let mut installation = InstallationToHtmlInterface {
Name: inst["name"],
Ip: 0, // TODO
Vrm: inst["idSite"].parse::<i64>(),
Identifier: inst["identifier"],
Serial: "", //Todo Grab and parse Details
EscapedName: &encode(inst["name"]).to_string(),
Online: "",
LastSeen: "",
NumBatteries: "",
BatteryVersion: "",
ServerIp : "10.2.0.1",
FirmwareVersion: "AF09",
};
installsForHtml.push(installation)
}
let mut data = installsForHtml;
let result = handlebars.render(source, &data);
return result.unwrap();
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}