gRPC Client Examples
gRPC Client¶
ResIOT provides a gRPC Server, a technology that allows the clients to call methods with their parameters directly, making the communications easier. The server implements this interface and runs a gRPC server to handle client calls, other side, the client provides the same methods as the server.
Here there are many examples of gRPC call in Golang with Applications, Connectors, Gateways, Nodes, Scenes and Variables.
////////////////////////////////////////////////////////////// APPLICATIONS //////////////////////////////////////////////////////////////
<br>
// Set up a connection to the server.
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println("Can't connect:", err)
}
defer conn.Close()
c := api.NewApplicationClient(conn)
<br>
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", TOKEN))
<br>
// GET ALL THE APPLICATION OF YOUR ORGANIZATION BASED ON LIMIT AND OFFSET
resp, err := c.List(ctx, &api.ListApplicationRequest{Limit: 40, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
"totalCount": "21",
"result": [
{
"appEUI": "18Ad679284CC9182",
"name": "APP 18Ad679284CC9182"
},
{
"appEUI": "aa172839Dfa82910",
"name": "APP aa172839Dfa82910"
},
...
]
}
*/
<br>
// CREATE AN APPLICATION
application := &api.CreateApplicationRequest{
AppEUI: "aFF8320982Dca231",
Name: "New Application",
}
<br>
resp, err := c.Create(ctx, application)
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/* resp = {} */
<br>
application_update := &api.UpdateApplicationRequest {
AppEUI: "aFF8320982Dca231",
Name: "Old Application",
}
<br>
// UPDATE APPLICATION
resp, err := c.Update(ctx, application_update)
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/* resp = {} */
<br>
// GET SINGLE APPLICATION
resp, err := c.Get(ctx, &api.GetApplicationRequest{AppEUI: "aFF8320982Dca231"})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
"appEUI": "aFF8320982Dca231",
"name": "Old Application"
}
*/
<br>
// DELETE APPLICATION
resp, err := c.Delete(ctx, &api.DeleteApplicationRequest{AppEUI: "aFF8320982Dca231"})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/* resp = {} */
<br>
////////////////////////////////////////////////////////////// CONNECTORS //////////////////////////////////////////////////////////////
<br>
// Set up a connection to the server.
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println("Can't connect:", err)
}
<br>
defer conn.Close()
c := api.NewConnectorClient(conn)
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", TOKEN))
<br>
// GET SINGLE CONNECTOR
resp, err := c.GetConnector(ctx, &api.GetConnectorRequest{HexId: "986aBe922316"})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
{
"enabled": "Enabled",
"hexId": "986aBe922316",
"loRaWanServerType": "LoRaWan server",
"name": "Main LoRaWan Server",
"status": "Connected",
"type": "MQTT Broker"
}
}
*/
<br>
// GET THE CONNECTORS OF YOUR ORGANIZATION
resp, err := c.ListConnector(ctx, &api.ListConnectorRequest{Limit: 40, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
"totalCount": "5",
"result": [
{
"enabled": "Enabled",
"hexId": "aC0922FF2615",
"loRaWanServerType": "MQTT Broker",
"name": "(OLD) Broker",
"status": "Not connected",
},
...
]
}
*/
<br>
////////////////////////////////////////////////////////////// GATEWAYS //////////////////////////////////////////////////////////////
<br>
// Set up a connection to the server.
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println("Can't connect:", err)
}
defer conn.Close()
c := api.NewGatewayClient(conn)
<br>
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", TOKEN))
<br>
// LIST THE GATEWAYS
resp, err := c.List(ctx, &api.ListGatewayRequest{Limit: 20, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"totalCount": "12",
"result": [
{
"name": "Gateway 1",
"macAddress": "1aF1252c982D",
"gwEUI": "a82c3129320012AD",
"timeout": 129600,
"hexIdLoRaWanServer": "69aF3d31Dd",
"enabled": true,
"latitude": 20.173181,
"longitude": 15.172273
},
...
]
}
*/
<br>
// CREATE GATEWAY
gateway := &api.CreateGatewayRequest {
Enabled: true,
GwEUI: "982251aF32a3254",
HexIdLoRaWanServer: "23dd38C1a8",
Latitude: 71.472838,
Longitude: -43.832019,
MacAddress: "A22d821AcFd0",
Name: "New Gateway",
Timeout: 129000,
}
resp, err := c.Create(ctx, gateway)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// UPDATE GATEWAY
gateway_update := &api.UpdateGatewayRequest {
Enabled: true,
GwEUI: "982251aF32a3254",
HexIdLoRaWanServer: "23dd38C1a8",
Latitude: 19.129371,
Longitude: -21.182931,
MacAddress: "A22d821AcFd0",
Name: "(OLD) Gateway",
Timeout: 129000,
}
resp, err := c.Update(ctx, gateway_update)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET GATEWAY
resp, err := c.Get(ctx, &api.GetGatewayRequest{GwEUI: "982251aF32a3254"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"name": "(OLD) Gateway",
"macAddress": "A22d821AcFd0",
"gwEUI": "982251aF32a3254",
"hexIdLoRaWanServer": "69643d3139",
"enabled": true,
"latitude": 13.456709,
"longitude": 28.192837
}
*/
<br>
// GET LAST ALIVE MESSAGE
resp, err := c.GetGatewayLMRequest(ctx, &api.GetGatewayRequest{GwEUI: "982251aF32a3254"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"seconds": 25
}
*/
<br>
// DELETE GATEWAY
resp, err := c.Delete(ctx, &api.DeleteGatewayRequest{GwEUI: "982251aF32a3254"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
////////////////////////////////////////////////////////////// NODES //////////////////////////////////////////////////////////////
// Set up a connection to the server.
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println("Can't connect:", err)
}
defer conn.Close()
c := api.NewNodeClient(conn)
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", TOKEN))
// CREATE A NODE
Create_Node := &api.CreateNodeRequest{
Adr: false,
AdrFrameCalculate: 1,
AppEUI: "DC0c0AB204151802",
AppSKey: "a12713298dacdf7810123aCFDD123182",
Auth: api.Authentication_ABP,
ChartsonDashboard: true,
Class: api.CLS_A,
DevAddr: "aA1234aF",
DevEUI: "dDeaa618289310aA",
Enabled: true,
HexIdLoRaWanServer: "auto",
Latitude: 21.945,
Longitude: -30.8981,
Name: "Device A",
NwkSKey: "FC1283120AAd231827CA1aa238919cf1",
RelaxFrameC: true,
Rx1Droffset: 1,
Rxwin: api.RXWindow_RX1,
Tag: "",
Timeout: 1200,
}
<br>
resp, err := c.Create(ctx, Create_Node)
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/* resp = {} */
<br>
// GET ALL THE NODES OF YOUR ORGANIZATION
resp, err := c.ListNodes(ctx, &api.ListNodesRequest{Limit: 40, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
"totalCount": "39",
"result": [
{
"appEUI": "0022113344665577",
"devEUI": "001bc5067010187a",
"timeout": 129600,
"enabled": true,
"name": "DEVICE 001bc5067010187a",
"relaxFrameC": true,
"hexId": "63Af6e123335",
"adrFrameCalculate": 150
},
...
]
}
*/
<br>
// DELETE A NODE
resp, err := c.Delete(ctx, &api.DeleteNodeRequest{AppEUI: "0fA0112204159902", DevEUI: "Af73238cb7AF1509"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET A SINGLE NODE
resp, err := c.Get(ctx, &api.GetNodeRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"appEUI": "DC0c0AB204151802",
"devEUI": "dDeaa618289310aA",
"devAddr": "aA1234aF",
"appSKey": "a12713298dacdf7810123aCFDD123182",
"nwkSKey": "FC1283120AAd231827CA1aa238919cf1",
"timeout": 1200,
"enabled": true,
"name": "Device A",
"relaxFrameC": true,
"auth": "ABP",
"hexIdLoRaWanServer": "69643d34",
"hexId": "6e6f64353934",
"chartsonDashboard": true,
"adrFrameCalculate": 1
}
*/
<br>
// EDIT A NODE
Update_Node := &api.UpdateNodeRequest{
Adr: true,
AdrFrameCalculate: 200,
AppEUI: "DC0c0AB204151802",
AppKey: "aaD1238101Fca8899123AA12319997D0",
Auth: api.Authentication_OTAA,
ChartsonDashboard: false,
Class: api.CLS_C,
DevEUI: "dDeaa618289310aA",
Enabled: true,
HexIdLoRaWanServer: "636f6e313335",
Latitude: 21.945,
Longitude: -30.8981,
Name: "New Device A",
RelaxFrameC: true,
Rx1Droffset: 4,
Rxwin: api.RXWindow_RX1,
Tag: "",
Timeout: 1200,
}
<br>
resp, err := c.Update(ctx, Update_Node)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET ALL THE NODE OF AN APPLICATION
resp, err := c.ListByApplicationID(ctx, &api.ListNodeByApplicationIDRequest{AppEUI: "DC0c0AB204151802", Limit: 20, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"totalCount": "16",
"result": [
{
Adr: true,
AdrFrameCalculate: 200,
AppEUI: "DC0c0AB204151802",
AppKey: "aaD1238101Fca8899123AA12319997D0",
Auth: api.Authentication_OTAA,
ChartsonDashboard: false,
Class: api.CLS_C,
DevEUI: "dDeaa618289310aA",
Enabled: true,
HexIdLoRaWanServer: "636f6e313335",
Latitude: 21.945,
Longitude: -30.8981,
Name: "New Device A",
RelaxFrameC: true,
Rx1Droffset: 4,
Rxwin: api.RXWindow_RX1,
Tag: "",
Timeout: 1200,
},
...
]
}
*/
<br>
// GET THE DOWNLINK QUEUE OF A NODE
resp, err := c.GetDL(ctx, &api.GetDLRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"totalCount": 3,
"items": [
{
"appEUI": "DC0c0AB204151802",
"askConfirmation": true,
"command": "",
"devEUI": "dDeaa618289310aA",
"hexIdConnector": "636f6e313335",
"id": 204,
"payload": "3C190A",
"port": 50,
"raw": true,
"reference": "Reference to Downlink 1"
}
]
...
}
*/
<br>
// CREATE A DOWNLINK MESSAGE AND QUEUE IT
Downlink := &api.CreateDLRequest {
Command: "",
AppEUI: "DC0c0AB204151802",
AskConfirmation: true,
DevEUI: "dDeaa618289310aA",
HexIdConnector: "636f6e313335",
Payload: "A1627F",
Port: 50,
Raw: true,
Reference: "Refer to this",
}
<br>
resp, err := c.CreateDL(ctx, Downlink)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// DELETE A QUEUED DOWNLINK MESSAGE
resp, err := c.DeleteDL(ctx, &api.DeleteDLRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", DownlinkID: 204}))
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET THE FRAME COUNTER OF A NODE
resp, err := c.GetFC(ctx, &api.GetFCRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA"}))
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"fcntdown": 210,
"fcntup": 210
}
*/
<br>
// UPDATE THE FRAME COUNTER OF A NODE
resp, err := c.UpdateFC(ctx, &api.UpdateFCRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", Fcntdown: 200, Fcntup: 200}))
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET THE LATITUDE AND THE LONGITUDE OF A NODE
resp, err := c.GetPosition(ctx, &api.GetPositionRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA"}))
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"lastTimeEdit": "2017-06-08 15:57:42 +0200 CEST",
"latitude": 21.945,
"longitude": -30.8981
}
*/
<br>
// UPDATE THE LATITUDE AND THE LONGITUDE OF A NODE
resp, err := c.UpdatePosition(ctx, &api.UpdatePositionRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", Latitude: 13.456, Longitude: 145.235}))
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET A PAYLOAD FIELD
resp, err := c.GetTag(ctx, &api.GetTagRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", Name: "Temperature"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"lastTimeEdit": "2017-05-26 10:41:31 +0200 CEST",
"name": "Temperature",
"type": "Numeric",
"value": "25"
}
*/
<br>
// UPDATE A PAYLOAD FIELD
resp5, err := c.UpdateTag(ctx, &api.UpdateTagRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", Name: "Temperature", Value: "28"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET ALL THE PAYLOAD FIELD OF A NODE
resp, err := c.ListTags(ctx, &api.ListTagRequest{AppEUI: "DC0c0AB204151802", DevEUI: "dDeaa618289310aA", Limit: 20, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err))}
/*
resp = {
"totalCount": 0,
"result": [
{
"lastTimeEdit": "2017-05-26 10:41:31 +0200 CEST",
"name": "Temperature",
"type": "Numeric",
"value": "28"
},
{
"lastTimeEdit": "2017-05-20 12:00:00 +0200 CEST",
"name": "Alarm",
"type": "Numeric",
"value": "1"
}
]
...
}
*/
<br>
////////////////////////////////////////////////////////////// SCENE //////////////////////////////////////////////////////////////
<br>
// RUN SCENE
resp, err := c.RunScene(ctx, &api.RunSceneRequest{HexId: "7aF362624921"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET ALL SCENE OF YOUR ORGANIZATION
resp, err := c.ListScene(ctx, &api.ListSceneRequest{Limit: 20, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"totalCount": "9",
"result": [
{
"hexId": "7aF362624921",
"name": "My first smart Scene",
"type": "Smart",
"enabled": "Enabled"
},
{
"hexId": "11b767254f06",
"name": "My first advanced Scene",
"type": "Advanced",
"enabled": "Disabled"
},
...
]
}
*/
<br>
// GET THE SINGLE SCENE
resp, err := c.GetScene(ctx, &api.GetSceneRequest{HexId: "7aF362624921"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"hexId": "7aF362624921",
"name": "My first smart Scene",
"type": "Smart",
"enabled": "Enabled"
}
*/
<br>
////////////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////////
<br>
// Set up a connection to the server.
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println("Can't connect:", err)
}
defer conn.Close()
c := api.NewVariableClient(conn)
<br>
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", TOKEN))
<br>
// GET ALL VARIABLES OF YOUR ORGANIZATION
resp, err := c.List(ctx, &api.ListVariableRequest{Limit: 20, Offset: 0})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"totalCount": "15",
"result": [
{
"name": "Variable",
"type": "Numeric",
"value": "100",
"valueOnServerReset": "100",
"unitsOfMeasurament": "Cm",
"timeout": 129600,
"lastTimeEdit": "2017-05-17 09:07:17 +0200 CEST"
},
...
]
}
*/
<br>
// CREATE A VARIABLE
variable := &api.InsertVariableRequest{
Enabled: true,
Name: "NewVariable",
ResetOnRestart: true,
Timeout: 12900,
Type: "String",
UnitsOfMeasurament: "Measurament",
Value: "4X",
ValueOnServerReset: "0X",
}
resp, err := c.Create(ctx, variable)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// UPDATE A VARIABLE
variable_update := &api.InsertVariableRequest{
Enabled: true,
Name: "NewVariable",
ResetOnRestart: true,
Timeout: 12900,
Type: "String",
UnitsOfMeasurament: "Measurament2",
Value: "8X",
ValueOnServerReset: "4X",
}
resp, err := c.Update(ctx, variable_update)
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// GET SINGLE VARIABLE
resp, err := c.Get(ctx, &api.GetVariableRequest{Name: "NewVariable"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"name": "NewVariable",
"type": "String",
"value": "8X",
"valueOnServerReset": "4X",
"unitsOfMeasurament": "Measurament2",
"timeout": 129600,
"lastTimeEdit": "2017-20-17 17:42:25 +0200 CEST"
}
*/
<br>
// GET LAST ALIVE MESSAGE
resp, err := c.GetVariableLastMessage(ctx, &api.GetVariableLMRequest{Name: "NewVariable"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"seconds": 25
}
*/
<br>
// GET VARIABLE VALUE
resp, err := c.GetVarValue(ctx, &api.GetVarValueRequest{Name: "NewVariable"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/*
resp = {
"lastTimeEdit": "2017-20-17 17:42:25 +0200 CEST",
"type": "String",
"value": "8X"
}
*/
<br>
// UPDATE VARIABLE VALUE
resp, err := c.UpdateVarValue(ctx, &api.UpdateVarValueRequest{Name: "NewVariable", Value: "12X"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */
<br>
// DELETE VARIABLE VALUE
resp, err := c.Delete(ctx, &api.DeleteVariableRequest{Name: "NewVariable"})
if err != nil { fmt.Println(grpc.ErrorDesc(err)) }
/* resp = {} */