Commit fd05639c956e2a90bcbee7ee546bcd74cd7c2378

Authored by 김태훈
1 parent b13e23ee41
Exists in master and in 2 other branches fhd, fhd-demo

웹으로 모드버스 테이블 보여주는 서비스 프로그램 추가

app/modbus-data-viewer/.gitignore
... ... @@ -0,0 +1,4 @@
  1 +bin/
  2 +pkg/
  3 +src/github.com/
  4 +release/
... ...
app/modbus-data-viewer/build.sh
... ... @@ -0,0 +1,4 @@
  1 +#!/bin/bash
  2 +
  3 +GOPATH=$PWD go get -v service-web
  4 +GOPATH=$PWD GOARCH=arm GOOS=linux go install service-web
... ...
app/modbus-data-viewer/release.sh
... ... @@ -0,0 +1,12 @@
  1 +#!/bin/bash
  2 +
  3 +./build.sh
  4 +
  5 +if [ ! -d release ]; then
  6 + mkdir release
  7 +else
  8 + rm -rf release/*
  9 +fi
  10 +
  11 +cp bin/linux_arm/service-web release/service-web
  12 +cp -rf src/service-web/templates release/
... ...
app/modbus-data-viewer/src/service-web/main.go
... ... @@ -0,0 +1,104 @@
  1 +package main
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/binary"
  6 + "fmt"
  7 + "html/template"
  8 + "log"
  9 + "net"
  10 + "sync"
  11 +
  12 + "github.com/codegangsta/martini"
  13 + "github.com/codegangsta/martini-contrib/render"
  14 +)
  15 +
  16 +type Header int32
  17 +type Packet [50]uint16
  18 +
  19 +var mutex sync.Mutex
  20 +var templateMap = make(map[string]interface{})
  21 +var controlTable, stateTable string
  22 +
  23 +func main() {
  24 + go ListenUDP()
  25 +
  26 + m := martini.Classic()
  27 +
  28 + m.Use(render.Renderer())
  29 +
  30 + m.Get("/", func(r render.Render) {
  31 + mutex.Lock()
  32 + defer mutex.Unlock()
  33 +
  34 + r.HTML(200, "main", templateMap)
  35 + })
  36 +
  37 + m.Run()
  38 +}
  39 +
  40 +func ListenUDP() {
  41 + addr, err := net.ResolveUDPAddr("udp", ":40001")
  42 + if err != nil {
  43 + log.Fatal(err)
  44 + }
  45 +
  46 + conn, err := net.ListenUDP("udp", addr)
  47 + if err != nil {
  48 + log.Fatal(err)
  49 + }
  50 + defer conn.Close()
  51 +
  52 + for {
  53 + b := make([]byte, 1024)
  54 + _, _, err := conn.ReadFromUDP(b)
  55 + if err != nil {
  56 + log.Fatal(err)
  57 + }
  58 +
  59 + buf := bytes.NewReader(b)
  60 +
  61 + var header Header
  62 + err = binary.Read(buf, binary.LittleEndian, &header)
  63 + if err != nil {
  64 + log.Fatal(err)
  65 + }
  66 +
  67 + var packet Packet
  68 + err = binary.Read(buf, binary.LittleEndian, &packet)
  69 + if err != nil {
  70 + log.Fatal(err)
  71 + }
  72 +
  73 + switch header {
  74 + case 0:
  75 + mapControl(packet)
  76 + case 1:
  77 + mapState(packet)
  78 + }
  79 + }
  80 +}
  81 +
  82 +func mapControl(packet Packet) {
  83 + mutex.Lock()
  84 + defer mutex.Unlock()
  85 +
  86 + controlTable = ""
  87 + for i, v := range packet {
  88 + controlTable += fmt.Sprintf("<tr><td>%v</td><td>0x%04X</td><td>0x%04X</td><td>%d</td></tr>\n", 30001+i, i, v, v)
  89 + }
  90 +
  91 + templateMap["Table_Control"] = template.HTML(controlTable)
  92 +}
  93 +
  94 +func mapState(packet Packet) {
  95 + mutex.Lock()
  96 + defer mutex.Unlock()
  97 +
  98 + stateTable = ""
  99 + for i, v := range packet {
  100 + stateTable += fmt.Sprintf("<tr><td>%v</td><td>0x%04X</td><td>0x%04X</td><td>%d</td></tr>\n", 40001+i, i, v, v)
  101 + }
  102 +
  103 + templateMap["Table_State"] = template.HTML(stateTable)
  104 +}
... ...
app/modbus-data-viewer/src/service-web/templates/main.tmpl
... ... @@ -0,0 +1,48 @@
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 + <head>
  4 + <style>
  5 +th, td { padding: 0.5em }
  6 +table.data
  7 +{
  8 + display: inline-block;
  9 + text-align: right;
  10 + font-family: monospace;
  11 + font-size: 1.2em;
  12 + border: 1px solid black;
  13 +}
  14 + </style>
  15 + </head>
  16 + <body>
  17 + <table class="data">
  18 + <tr>
  19 + <th>Address</th>
  20 + <th>Address</th>
  21 + <th>Value</th>
  22 + <th>Value</th>
  23 + </tr>
  24 + <tr>
  25 + <th>(Dec)</th>
  26 + <th>(Hex)</th>
  27 + <th>(Hex)</th>
  28 + <th>(Dec)</th>
  29 + </tr>
  30 +{{.Table_Control}}
  31 + </table>
  32 + <table class="data">
  33 + <tr>
  34 + <th>Address</th>
  35 + <th>Address</th>
  36 + <th>Value</th>
  37 + <th>Value</th>
  38 + </tr>
  39 + <tr>
  40 + <th>(Dec)</th>
  41 + <th>(Hex)</th>
  42 + <th>(Hex)</th>
  43 + <th>(Dec)</th>
  44 + </tr>
  45 +{{.Table_State}}
  46 + </table>
  47 + </body>
  48 +</html>
... ...