为了账号安全,请及时绑定邮箱和手机立即绑定

用户手册:与 deviceShifu 交互的应用

Shifu会对每一个连接的设备创建一个deviceShifudeviceShifu是物理设备的数字孪生并负责控制,收集设备指标。

这个教程会创建一个简单的温度检测程序,通过和一个温度计的deviceShifu交互来演示如何使用应用来和deviceShifu交互。

前提

本示例需要安装Go, Docker, kind, kubectlkubebuilder

1. 运行 Shifu 并连接一个简单的温度计

shifu/examples/deviceshifu/demo_device路径中已经有一个演示温度计的deployment配置。该温度计会上报一个整数代表当前温度,它拥有一个read_value API来汇报这个数值。

shifu根目录下,运行下面两条命令来运行Shifu和演示温度计的deviceShifu

./test/scripts/deviceshifu-setup.sh apply # setup and start shifu services for this demo
kubectl apply -f examples/deviceshifu/demo_device/edgedevice-thermometer # connect mock thermometer to shifu

2. 温度检测程序

本应用会通过HTTP请求来和deviceShifu交互,每两秒检测read_value节点来获取温度计deviceShifu的读数。

应用示例如下:

high-temperature-detector.go

package main

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

func main() {
    targetUrl := "http://edgedevice-thermometer/read_value"
    req, _ := http.NewRequest("GET", targetUrl, nil)
    for {
        res, _ := http.DefaultClient.Do(req)
        body, _ := ioutil.ReadAll(res.Body)
        temperature, _ := strconv.Atoi(string(body))
        if temperature > 20 {
            log.Println("High temperature:", temperature)
        } else if temperature > 15 {
            log.Println("Normal temperature:", temperature)
        } else {
            log.Println("Low temperature:", temperature)
        }
        res.Body.Close()
        time.Sleep(2 * time.Second)
    }
}

生成go.mod:

go mod init high-temperature-detector

3. 容器化应用

需要一个应用程序的Dockerfile

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-detector
EXPOSE 11111
CMD [ "/high-temperature-detector" ] 

之后,创建应用:

docker build --tag high-temperature-detector:v0.0.1 .

现在温度检测应用的镜像已经构建完成。

4. 加载应用镜像并启动应用 Pod

首先将应用镜像加载到kind中:

kind load docker-image high-temperature-detector:v0.0.1

之后运行容器Pod

kubectl run high-temperature-detector --image=high-temperature-detector:v0.0.1 -n deviceshifu

5. 检查应用输出

温度检测应用会每两秒钟通过温度计的deviceShifu获取当前数值。

一切准备就绪,通过log来查看程序输出:

kubectl logs -n default high-temperature-detector -f

输出示例:

kubectl logs -n default high-temperature-detector -f

2021/10/18 10:35:35 High temperature: 24
2021/10/18 10:35:37 High temperature: 23
2021/10/18 10:35:39 Low temperature: 15
2021/10/18 10:35:41 Low temperature: 11
2021/10/18 10:35:43 Low temperature: 12
2021/10/18 10:35:45 High temperature: 28
2021/10/18 10:35:47 Low temperature: 15
2021/10/18 10:35:49 High temperature: 30
2021/10/18 10:35:51 High temperature: 30
2021/10/18 10:35:53 Low temperature: 15

本文由边无际授权发布

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消