Skip to content

Instantly share code, notes, and snippets.

@TiagoNunesDeveloper
Created August 5, 2018 14:08
Show Gist options
  • Save TiagoNunesDeveloper/47e941dccfc88aef4703ad2ae909fe44 to your computer and use it in GitHub Desktop.
Save TiagoNunesDeveloper/47e941dccfc88aef4703ad2ae909fe44 to your computer and use it in GitHub Desktop.
gRPC PLC
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/kardianos/service"
"github.com/robinson/gos7"
)
var logger service.Logger
var handler *gos7.TCPClientHandler
var client *gos7.Client
var (
// APIPort apiPort
APIPort = "8888"
// TCPDevice tcpDevice
TCPDevice = "192.168.1.8"
// Rack rack
Rack = 0
// Slot slot
Slot = 2
)
type app struct{}
func (p *app) Start(s service.Service) error {
go p.run()
return nil
}
func (p *app) run() {
handler = gos7.NewTCPClientHandler(TCPDevice, Rack, Slot)
handler.Timeout = 200 * time.Second
handler.IdleTimeout = 200 * time.Second
handler.Logger = log.New(os.Stdout, "tcp: ", log.LstdFlags)
handler.Connect()
SetupHTTPServer()
err := StartHTTPServer(APIPort)
if err != nil {
defer handler.Close()
panic(err)
}
}
func (p *app) Stop(s service.Service) error {
return nil
}
func main() {
svcFlag := flag.String("service", "", "Service de controle do sistema")
flag.Parse()
svcConfig := &service.Config{
Name: "Wine Test",
DisplayName: "Wine Dashboard App",
Description: "Sistema de monitoramento dos ativos da engenharia",
}
prg := &app{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
if *svcFlag != "" {
err = service.Control(s, *svcFlag)
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Fatal(err)
}
return
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}
// SetupHTTPServer setupHTTPServer
func SetupHTTPServer() {
http.HandleFunc("/", resultEndpoint)
http.HandleFunc("/clp/btn_start_on", resultStartON)
http.HandleFunc("/clp/btn_start_off", resultStartOFF)
http.HandleFunc("/clp/btn_stop_on", resultStopON)
http.HandleFunc("/clp/btn_stop_off", resultStopOFF)
}
// StartHTTPServer startHTTPServer
func StartHTTPServer(port string) error {
return http.ListenAndServe(":"+port, nil)
}
func resultEndpoint(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "Servidor OK!")
}
func resultStartON(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "I0.0 TRUE!")
execStartON()
}
func resultStartOFF(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "I0.0 FALSE!")
execStartOFF()
}
func resultStopON(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "I0.1 TRUE!")
execStopON()
}
func resultStopOFF(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "I0.1 FALSE!")
execStopOFF()
}
func execStartON() {
client := gos7.NewClient(handler)
data1 := make([]byte, 2)
data1[0] = 0x01
err := client.AGWriteEB(0, 1, data1)
if err != nil {
fmt.Println(err)
}
}
func execStartOFF() {
client := gos7.NewClient(handler)
data1 := make([]byte, 2)
data1[1] = 0x01
err := client.AGWriteEB(0, 1, data1)
if err != nil {
fmt.Println(err)
}
}
func execStopON() {
client := gos7.NewClient(handler)
data1 := make([]byte, 2)
data1[0] = 0x02
err := client.AGWriteEB(0, 1, data1)
if err != nil {
fmt.Println(err)
}
}
func execStopOFF() {
client := gos7.NewClient(handler)
data1 := make([]byte, 2)
data1[1] = 0x02
err := client.AGWriteEB(0, 1, data1)
if err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment