Shifu基础功能:数据采集

文章介绍了一种通过HTTP/gRPC与deviceShifu交互,从设备收集数据的方法。编写了一个Go程序,周期性地获取edgedevice-thermometer的温度数据,并根据温度范围进行分类打印。程序被构建为Docker镜像,部署在Kubernetes集群中,以实现自动化数据采集和处理。
摘要由CSDN通过智能技术生成

数据采集

我们可以通过HTTP/gRPC与deviceShifu进行通信,deviceShifu会将我们发送的请求转换成设备所支持协议的形式,并发送给设备。

当设备接收到指令之后,数据会传输到deviceShifu中,之后deviceShifu将数据作为我们请求的返回值进行返回,从而实现数据的采集。

实现自动化数据采集

1.首先,我们可编写以下程序来实现自动采集数据。该程序用于对之前运行的edgedevice-thermometer设备进行实时数据采集,将温度数据进行解析并输出。该程序可以通过任意语言、任意形式进行编写,您可以将数据存入您的数据库中,或者存入文件中。

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)
   }
}

2.使用go mod init high-temperature-detecto生成go.mod文件。

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

4.使用Dockerfile文件生成docker image,需执行以下命令:

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

5.之后我们将docker image加载到集群中,需执行以下命令:

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

6.运行我们编写的数据采集程序 ,需执行以下命令:

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

7.最后我们查看该程序的日志信息获取数据,需执行以下命令:

kubectl logs 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

本文由边无际授权发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值