解决方案
Product
Services
开发者
工厂客户
Register
Login
首页
文章
详情
怎么用Shell脚本控制智能配电箱
2024-05-01 发布
浏览:808 次
本文介绍了如何用
Shell
对接 智能配电箱 的方法: 可以通过接口实现。
智能配电箱
更多...
支持命令
开关
power
先通后断
point
先断后通
reset
产品手册
版本
对应产品手册
HTTP接口控制
以设备控制(向设备下发命令)为例
Shell-cURL
Shell-cURL-Windows
Shell-Httpie
Shell-wget
Shell-PowerShell
# First you need to prepare the following values # 1. AppID (in your console development settings) # 2. AppSecret (in your console development settings) # 3. ts (timestamp of the current time, seconds) # 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) # In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string # Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; # Request needs to pass two parameters device and order: # device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface # order[json string]: the command to be issued, for example: # {"power":1}, usually to connect the circuit of the switch # {"power3":0}, usually to close the third line of the switch or controller # {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content # The same type of product, the command is the same, different product types of commands, please view the product manual page of each product # Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command #!/bin/bash # 定义变量 AppId="开发者ID" # 替换为实际的 AppID AppSecret="开发者密码" # 替换为实际的 AppSecret ts=$(date +%s) sign=$(echo -n $(echo -n $AppSecret | md5sum | awk '{print $1}')$ts | md5sum | awk '{print $1}') url="https://api.thingboot.com/${AppId}/device/control/?sign=${sign}&ts=${ts}" # 请求体数据 device="1878" # 替换为实际的设备 ID ;可传多个[用,间隔] order='{"power1":1}' # 替换为实际的命令 # 构建请求体 postData="{\"device\": \"$device\", \"order\": $order}" # 发送请求并获取响应 response=$(curl -s -X POST "$url" \ -H "Content-Type: application/json" \ -d "$postData") # 输出响应 echo "$response"
# First you need to prepare the following values # 1. AppID (in your console development settings) # 2. AppSecret (in your console development settings) # 3. ts (timestamp of the current time, seconds) # 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) # In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string # Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; # Request needs to pass two parameters device and order: # device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface # order[json string]: the command to be issued, for example: # {"power":1}, usually to connect the circuit of the switch # {"power3":0}, usually to close the third line of the switch or controller # {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content # The same type of product, the command is the same, different product types of commands, please view the product manual page of each product # Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command #在 Windows 环境中,可以使用 curl 命令行工具来发送 HTTP 请求。在 Windows 上,curl 通常是通过命令行工具(如 CMD 或 PowerShell)运行的。以下是如何将你的 Shell 脚本改写为在 Windows 上使用 curl 的示例: REM 在 CMD 中使用 curl @echo off set "AppId=开发者ID" & REM 替换为实际的 AppID set "AppSecret=开发者密码" & REM 替换为实际的 AppSecret REM 获取当前时间戳 for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set "datetime=%%a" set "ts=%datetime:~0,10%" REM 计算签名 setlocal enabledelayedexpansion set "sign=" for /f %%a in ('echo %AppSecret%%ts% | md5sum') do set "sign=%%a" endlocal & set "sign=%sign%" set "url=https://api.thingboot.com/%AppId%/device/control/?sign=%sign%&ts=%ts%" set "device=1878" & REM 替换为实际的设备 ID ;可传多个[用,间隔] set "order={""power1"":1}" & REM 替换为实际的命令 set "postData={""device"": ""%device%"", ""order"": %order%}" REM 发送请求并获取响应 for /f "delims=" %%a in ('curl -s -X POST "%url%" -H "Content-Type: application/json" -d "%postData%"') do set "response=%%a" REM 输出响应 echo %response% #在 PowerShell 中使用 curl $AppID = "开发者ID" # 替换为实际的 AppID $AppSecret = "开发者密码" # 替换为实际的 AppSecret # 获取当前时间戳 $ts = [DateTimeOffset]::Now.ToUnixTimeSeconds() # 计算签名 $sign = (Get-FileHash -InputStream ([System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($AppSecret + $ts))) -Algorithm MD5).Hash.ToLower() $url = "https://api.thingboot.com/$AppID/device/control/?sign=$sign&ts=$ts" $device = "1878" # 替换为实际的设备 ID ;可传多个[用,间隔] $order = "{""power1"":1}" # 替换为实际的命令 $postData = "{""device"": ""$device"", ""order"": $order}" # 发送请求并获取响应 $response = curl -s -X POST $url -H "Content-Type: application/json" -d $postData # 输出响应 Write-Output $response
#!/bin/bash # First you need to prepare the following values # 1. AppID (in your console development settings) # 2. AppSecret (in your console development settings) # 3. ts (timestamp of the current time, seconds) # 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) # In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string # Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; # Request needs to pass two parameters device and order: # device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface # order[json string]: the command to be issued, for example: # {"power":1}, usually to connect the circuit of the switch # {"power3":0}, usually to close the third line of the switch or controller # {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content # The same type of product, the command is the same, different product types of commands, please view the product manual page of each product # Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command # 定义变量 AppID="开发者ID" # 替换为实际的 AppID AppSecret="开发者密码" # 替换为实际的 AppSecret ts=$(date +%s) sign=$(echo -n $(echo -n $AppSecret | md5sum | awk '{print $1}')$ts | md5sum | awk '{print $1}') url="https://api.thingboot.com/${AppID}/device/control/" # 请求体数据 device="1878" # 替换为实际的设备 ID;可传多个[用,间隔] order='{"power1":1}' # 替换为实际的命令 # 构建请求体 postData="{\"device\": \"$device\", \"order\": $order}" # 发送请求并获取响应 response=$(http POST "$url" \ "sign=$sign" \ "ts=$ts" \ Content-Type:application/json \ "$postData") # 输出响应 echo "$response"
#!/bin/bash # First you need to prepare the following values # 1. AppID (in your console development settings) # 2. AppSecret (in your console development settings) # 3. ts (timestamp of the current time, seconds) # 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) # In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string # Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; # Request needs to pass two parameters device and order: # device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface # order[json string]: the command to be issued, for example: # {"power":1}, usually to connect the circuit of the switch # {"power3":0}, usually to close the third line of the switch or controller # {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content # The same type of product, the command is the same, different product types of commands, please view the product manual page of each product # Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command # 定义变量 AppID="开发者ID" # 替换为实际的 AppID AppSecret="开发者密码" # 替换为实际的 AppSecret ts=$(date +%s) sign=$(echo -n "$(echo -n "$AppSecret" | md5sum | awk '{print $1}')$ts" | md5sum | awk '{print $1}') # 构建 URL url="https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}" # 请求体数据 device="1878" # 替换为实际的设备ID;可传多个[用,间隔] order='{"power1":1}' # 替换为实际的命令 # 构建请求体 postData="{\"device\": \"$device\", \"order\": $order}" # 发送请求并获取响应 response=$(wget --no-check-certificate \ --method POST \ --timeout=0 \ --header 'Content-Type: application/json' \ --body-data="$postData" \ -O - "$url") # 输出响应 echo "$response"
# First you need to prepare the following values # 1. AppID (in your console development settings) # 2. AppSecret (in your console development settings) # 3. ts (timestamp of the current time, seconds) # 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) # In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string # Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; # Request needs to pass two parameters device and order: # device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface # order[json string]: the command to be issued, for example: # {"power":1}, usually to connect the circuit of the switch # {"power3":0}, usually to close the third line of the switch or controller # {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content # The same type of product, the command is the same, different product types of commands, please view the product manual page of each product # Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command # 定义变量 $AppID = "开发者ID" # 替换为实际的 AppID $AppSecret = "开发者密码" # 替换为实际的 AppSecret $ts = [int][double]::Parse((Get-Date -UFormat %s)) #获取签名 md5(md5(开发者密码)拼接上面的ts参数) $sign = [System.BitConverter]::ToString(( [System.Security.Cryptography.MD5]::Create().ComputeHash( [System.Text.Encoding]::UTF8.GetBytes($AppSecret + $ts.ToString()) ) )).Replace("-", "").ToLower() # 构建 URL $url = "https://api.thingboot.com/$AppID/device/control/?sign=$sign&ts=$ts" # 创建请求头 $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" # 创建 multipart/form-data 内容 $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() # 添加设备 ID $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") $stringHeader.Name = "device" $stringContent = [System.Net.Http.StringContent]::new("1878") $stringContent.Headers.ContentDisposition = $stringHeader $multipartContent.Add($stringContent) # 添加命令 $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") $stringHeader.Name = "order" $stringContent = [System.Net.Http.StringContent]::new("{`"power1`":1}") $stringContent.Headers.ContentDisposition = $stringHeader $multipartContent.Add($stringContent) # 发送请求并获取响应 $response = Invoke-RestMethod -Uri $url -Method 'POST' -Headers $headers -Body $multipartContent # 输出响应 $response | ConvertTo-Json
文章:
怎么用Shell脚本控制智能配电箱
相关技术文章:
怎样用Shell脚本对接8路远程照明开关
查看
如何用Shell脚本控制40W 壁挂 HTTP 接口语音音箱
查看
怎样用Shell脚本对接智能PDU[分控]8位
查看
怎么用Shell脚本对接40A带计量数显智能空开
查看
怎么用Shell脚本对接HTTP接口密码门禁器
查看