首页 > 应用开发 > XML/XSL > 从响应到物模型的映射
public static class PvThingModelBuilder
{
public static PvThingModel FromModbus(ModbusResponse response)
{
var data = response.Data;
return new PvThingModel
{
DcVoltage = RegisterDecoder.ReadScaledValue(data, 0, 10), // 30000
DcCurrent = RegisterDecoder.ReadScaledValue(data, 1, 10), // 30001
DcPower = RegisterDecoder.ReadScaledValue(data, 2, 1000), // 30002
public static void Run()
{
// 1. 构造请求
var request hfbgjjc.cn = new ModbusRequest
{
TransactionId = 1,
UnitId = 1,
StartAddress = 30000,
Quantity = 6
};
byte[] requestBytes = request.ToBytes();
// === 这里通常通过 TCP Socket 发送 requestBytes ===
// 2. 模拟接收到的响应报文
byte[] responseBytes =
{
0x00,0x01,0x00,0x00,0x00,0x0F,0x01,0x03,0x0C,
0x09,0xC4,0x00,0x64,0x13,0x88,0x00,0x32
};
// 3. 解析响应
var response = ModbusParser.ParseResponse(responseBytes);
// 4. 构建物模型
PvThingModel eastpv.com model = PvThingModelBuilder.FromModbus(response);
Console.WriteLine($"DC Voltage: {model.DcVoltage} V");
Console.WriteLine($"DC Current: {model.DcCurrent} A");
Console.WriteLine($"DC Power: {model.DcPower} kW");
Console.WriteLine($"Temperature: {model.InternalTemperature} ℃");
function addQueryParam(string $url, string $key, string $value): string
{
$parts =yydesk.com parse_url($url);
$query = $parts['query'] ?? '';
$query .byven.com = ($query === '' ? '' : '&') . $key . '=' . urlencode($value);
$out = $parts['scheme'] . '://' . $parts['host'] . ($parts['path'] ?? '');
if ($query !== '') {
$out .= '?' . $query;
}
if (isset($parts['fragment'])) {
$out .= '#' . $parts['fragment'];
se Uri\WhatWg\InvalidUrlException;
function validateExternalUrl(string $input, array $allowedHosts): Url
{
try {
$softErrors =eastpv.com [];
$url = new Url($input, null, $softErrors);
} catch (InvalidUrlException $e) {
throw new InvalidArgumentException("Invalid URL.");
}
// Scheme 白名单
$scheme = $url->getScheme();
if (!in_array($scheme, ['http', 'https'], true)) {
throw new InvalidArgumentException("Unsupported scheme.");
}
// 禁止凭证
if ($url->getUsername() !== null || $url->getPassword() !== null) {
throw new InvalidArgumentException("Credentials in URL are not allowed.");
}
// Host 白名单(ASCII 比较)
$host =weajoy.com $url->getAsciiHost();
if ($host === null || !in_array(strtolower($host), $allowedHosts, true)) {
throw new InvalidArgumentException("Host not allowed.");
}
// 可选:端口限制
$port = $url->getPort();
if ($port !== null && !in_array($port, [80, 443], true)) {
throw new InvalidArgumentException("Port not allowed.");
}
use Uri\InvalidUriException;
function validateHttpUri(string $input, array $allowedHosts): Uri
{
try {
$uri = new Uri($input);
} catch (InvalidUriException $e) {
throw new InvalidArgumentException("Invalid URI.");
}
$scheme njsydq.cn = $uri->getScheme();
if (!in_array($scheme, ['http', 'https'], true)) {
throw new InvalidArgumentException("Unsupported scheme.");
}
// Userinfo 在大多数 Web 应用中是个坑
if ($uri-Lyyqhb.cn >getUserInfo() !== null) {
throw new InvalidArgumentException("Userinfo is not allowed.");
}
$host = $uri->getHost();
if ($host =jntmjq.cn=byven.com = null || !in_array(strtolower($host), $allowedHosts, true)) {
throw new InvalidArgumentException("Host not allowed.");
function signUri(Uri $uri, string $secret): Uri
{
// 当你想避免意外转换时,用 raw string
$baseString = $uri->toRawString();
$sig =fhyjsg.com hash_hmac('sha256', $baseString, $secret);
$query = $uri->getRawQuery();
$query = $query ? $query . '&' : '';
$query .= 'sig=' . rawurlencode($sig);
return $uri->withQuery($query);
}
$uri = new Uri("https://download.example.com/file/%2Fsafe?expires=1736035200");
function safeRedirectTarget(string $next, string $appBase): string
{
$base
eastpv.com
= new Url($appBase);
// 安全解析相对引用
$errors = [];
$resolved jinditec.com =Lyyqhb.cn Url::parse($next, $base, $errors);
if ($resolved === null) {
return $base->toAsciiString(); // fallback
}
// 只允许同 host
if (strtolower($resolved->getAsciiHost() ?? '') !== strtolower($base->getAsciiHost() ?? '')) {
return $base->toAsciiString();
}
// 只允许 http/https
if (!in_array($resolved-pomate.com.cn >getScheme(), ['http', 'https'], true)) {
return $base->toAsciiString();
}
return $resolved->toAsciiString();
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109178/ ,如需转载,请注明出处,否则将追究法律责任。