| 69 | } |
| 70 | |
| 71 | async fn handle(&self, req: &Request) -> Result<Response> { |
| 72 | let rt = rquickjs::Runtime::new()?; |
| 73 | let ctx = rquickjs::Context::full(&rt)?; |
| 74 | |
| 75 | ctx.with::<_, Result<Response>>(|ctx| { |
| 76 | let m = rquickjs::Module::declare(ctx.clone(), "script", self.script.clone()) |
| 77 | .catch(&ctx) |
| 78 | .map_err(|e| anyhow!("Declare script: JS error: {}", e))?; |
| 79 | let (m, m_promise) = m |
| 80 | .eval() |
| 81 | .catch(&ctx) |
| 82 | .map_err(|e| anyhow!("Eval script: JS error: {}", e))?; |
| 83 | () = m_promise.finish()?; |
| 84 | let func: rquickjs::Function = m |
| 85 | .get("handle") |
| 86 | .catch(&ctx) |
| 87 | .map_err(|e| anyhow!("Get handle function: JS error: {}", e))?; |
| 88 | |
| 89 | let device_variables = rquickjs::Object::new(ctx.clone())?; |
| 90 | for (k, v) in &req.device_variables { |
| 91 | device_variables.set(k, v)?; |
| 92 | } |
| 93 | |
| 94 | let input = rquickjs::Object::new(ctx.clone())?; |
| 95 | input.set("regionConfigId", req.region_config_id.clone())?; |
| 96 | input.set("regionCommonName", req.region_common_name.to_string())?; |
| 97 | input.set("devEui", req.dev_eui.to_string())?; |
| 98 | input.set("macVersion", req.mac_version.to_string())?; |
| 99 | input.set("regParamsRevision", req.reg_params_revision.to_string())?; |
| 100 | input.set("adr", req.adr)?; |
| 101 | input.set("dr", req.dr)?; |
| 102 | input.set("txPowerIndex", req.tx_power_index)?; |
| 103 | input.set("nbTrans", req.nb_trans)?; |
| 104 | input.set("maxTxPowerIndex", req.max_tx_power_index)?; |
| 105 | input.set("requiredSnrForDr", req.required_snr_for_dr)?; |
| 106 | input.set("installationMargin", req.installation_margin)?; |
| 107 | input.set("minDr", req.min_dr)?; |
| 108 | input.set("maxDr", req.max_dr)?; |
| 109 | input.set("skipFCntCheck", req.skip_f_cnt_check)?; |
| 110 | input.set("deviceVariables", device_variables)?; |
| 111 | |
| 112 | let mut uplink_history: Vec<rquickjs::Object> = Vec::new(); |
| 113 | |
| 114 | for uh in &req.uplink_history { |
| 115 | let obj = rquickjs::Object::new(ctx.clone())?; |
| 116 | obj.set("fCnt", uh.f_cnt)?; |
| 117 | obj.set("maxSnr", uh.max_snr)?; |
| 118 | obj.set("maxRssi", uh.max_rssi)?; |
| 119 | obj.set("txPowerIndex", uh.tx_power_index)?; |
| 120 | obj.set("gatewayCount", uh.gateway_count)?; |
| 121 | uplink_history.push(obj); |
| 122 | } |
| 123 | |
| 124 | input.set("uplinkHistory", uplink_history)?; |
| 125 | |
| 126 | let res: rquickjs::Object = func |
| 127 | .call((input,)) |
| 128 | .catch(&ctx) |