乐趣区

关于程序员:Shifu基础功能设备控制

设施管制

与数据采集相似,在 deviceshifu_configmap.yaml 中设置好设施的指令后,咱们能够通过 HTTP/gRPC 与 deviceshifu 进行通信,deviceshifu会将咱们发送的指令转换成设施所反对协定的模式,并发送给设施。设施承受到指令之后,能够通过指令执行相应的操作,从而实现设施管制。

联合数据采集实现设施的自动化管制

1. 这里,咱们再创立一个虚构设施 PLC(如果您未试玩过PLC 设施,您能够点击查看)。

$ kubectl get pods -n deviceshifu
NAME                                            READY   STATUS    RESTARTS   AGE
deviceshifu-opcua-deployment-765b77cfcf-dnhjh   1/1     Running   0          14m
deviceshifu-plc-deployment-7f96585f7c-6t48g     1/1     Running   0          7m8s

此时咱们启动了两个 deviceshifu,它们别离与设施建设了连贯。咱们能够将两个deviceshifu 进行联动,即当温度计温度超过阈值时,将 PLC 的 Q 区的最低地位为 1,当温度计温度低于阈值时则置回 0。

2. 编写与管制设施相干的程序。

package main  

import (  
   "io/ioutil"  
   "log"
   "net/http"
   "strconv"
   "time"
)  

func main() {  
   targetUrl := "http://deviceshifu-thermometer.deviceshifu.svc.cluster.local/read_value"  
   req, _ := http.NewRequest("GET", targetUrl, nil)  
   var isHigh bool  
   for {res, _ := http.DefaultClient.Do(req)  
      body, _ := ioutil.ReadAll(res.Body)  
      temperature, _ := strconv.Atoi(string(body))  
      if temperature > 20 && isHigh == false {setPLCBit("1")  
         isHigh = true  
      } else if temperature <= 20 && isHigh == true {setPLCBit("0")  
         isHigh = false  
      }  
      log.Printf("Now remperature is: %d", temperature)  
      res.Body.Close()  
      time.Sleep(5 * time.Second)  
   }  
}  

func setPLCBit(value string) {  
   targetUrl := "http://deviceshifu-plc/sendsinglebit?rootaddress=Q&address=0&start=0&digit=0&value=" + value  
   req, _ := http.NewRequest("GET", targetUrl, nil)  
   res, _ := http.DefaultClient.Do(req)  
   defer res.Body.Close()}

3. 对于上述程序,咱们能够将其打包成 docker image 并加载到集群中,以便其能更好的与 deviceshifu 进行通信。创立以下 Dockerfile 文件:

# syntax=docker/dockerfile:1  
FROM golang:1.17-alpine  
WORKDIR /app  
COPY go.mod ./  
RUN go mod download  
COPY *.go ./  
RUN go build -o /high-temperature-control-plc 
EXPOSE 11111  
CMD ["/high-temperature-control-plc"]

4. 应用 Dockerfile 文件生成docker image

docker build --tag high-temperature-control-plc:v0.0.1

5. 之后咱们将 docker image 加载到集群中。

kind load docker-image high-temperature-control-plc:v0.0.1

6. 运行咱们编写的数据采集程序。

kubectl run high-temperature-control-plc --image=high-temperature-control-plc:v0.0.1

7. 同时为了便于咱们察看 PLC 设施的值,咱们再载入一个 nginx 镜像。

kubectl run nginx --image=nginx:1.21 -n deviceshifu

8. 此时咱们有了如下的 pod,且均处于Running 状态。

$ kubectl get pods -n deviceshifu
NAME                                                  READY   STATUS    RESTARTS   AGE
deviceshifu-plc-deployment-7f96585f7c-87zb4           1/1     Running   0          20m
deviceshifu-thermometer-deployment-7b69b89b88-crwzx   1/1     Running   0          67m
high-temperature-control-plc                          1/1     Running   0          8m54s
nginx                                                 1/1     Running   0          61m

9. 咱们编写的自动化设施控制程序正处于运行中,能够通过查看实时日志的形式查看程序获取的数据。

$ kubectl logs high-temperature-control-plc -n deviceshifu -f 
2022/07/07 03:05:07 Now remperature is: 29
2022/07/07 03:05:12 Now remperature is: 10
2022/07/07 03:05:17 Now remperature is: 23
2022/07/07 03:05:22 Now remperature is: 30

10. 为了不便察看数据,咱们将程序中的 time.Sleep(5 * time.Second) 调高 (为进步采集精度,可将其调低,以进步采集频率)。此时咱们再输出一条命令进入nginx 的容器中。

kubectl exec -it nginx -n deviceshifu -- bash

11. 程序获取的温度超过阈值时咱们通过 curl 获取 PLC 数值。

$ curl "http://deviceshifu-plc/getcontent?rootaddress=Q&address=0&start=0"; echo
0b0000000000000001

12. 当程序获取的温度低于阈值时咱们再次通过 cur l 获取PLC 数值

$ curl "http://deviceshifu-plc/getcontent?rootaddress=Q&address=0&start=0"; echo
0b0000000000000000

自此,咱们通过对虚构温度计采集实时数据,实现了对 PLC 设施的自动化管制。

本文由边无际受权公布

退出移动版