Skip to content

Commit 96e4dbc

Browse files
committed
refactoring
1 parent 63bc776 commit 96e4dbc

File tree

19 files changed

+464
-20
lines changed

19 files changed

+464
-20
lines changed

.licenses/go/github.com/arduino/iot-client-go/v2.dep.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: github.com/arduino/iot-client-go/v2
3-
version: v2.0.2
3+
version: v2.0.3
44
type: go
55
summary:
66
homepage: https://pkg.go.dev/github.com/arduino/iot-client-go/v2
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <ArduinoIoTCloud.h>
2+
3+
#include "thingProperties.h"
4+
5+
void setup() {
6+
// Initialize serial and wait for port to open:
7+
Serial.begin(9600);
8+
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
9+
delay(1500);
10+
11+
// Defined in thingProperties.h
12+
initProperties();
13+
14+
// Connect to Arduino IoT Cloud
15+
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false, "mqtts-sa.iot.oniudra.cc");
16+
17+
setDebugMessageLevel(4);
18+
ArduinoCloud.printDebugInfo();
19+
}
20+
21+
unsigned long previousMillis = 0;
22+
const long interval = 5000; //ms
23+
bool increase = true;
24+
25+
// the loop function runs over and over again forever
26+
void loop() {
27+
ArduinoCloud.update();
28+
29+
unsigned long currentMillis = millis();
30+
if (currentMillis - previousMillis >= interval) {
31+
previousMillis = currentMillis;
32+
increase = !increase;
33+
34+
if(pressure < 2){
35+
pressure = 8;
36+
}
37+
if(temperature < 2){
38+
temperature = 25;
39+
}
40+
41+
int randNumber = random(10.0, 70.0);
42+
if(!increase){
43+
randNumber = -randNumber;
44+
}
45+
float diff = (float)randNumber / 100.0;
46+
47+
pressure = pressure + diff;
48+
temperature = temperature + diff;
49+
50+
Serial.println(AIOT_CONFIG_LIB_VERSION);
51+
Serial.println("2.1");
52+
}
53+
54+
}
55+
56+
/*
57+
Since Temperature is READ_WRITE variable, onTemperatureChange() is
58+
executed every time a new value is received from IoT Cloud.
59+
*/
60+
void onTemperatureChange() {
61+
// Add your code here to act upon Temperature change
62+
}
63+
64+
/*
65+
Since Pressure is READ_WRITE variable, onPressureChange() is
66+
executed every time a new value is received from IoT Cloud.
67+
*/
68+
void onPressureChange() {
69+
// Add your code here to act upon Pressure change
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"cpu": {
3+
"fqbn": "arduino:mbed_nano:nanorp2040connect",
4+
"name": "",
5+
"type": "serial"
6+
},
7+
"secrets": [
8+
{
9+
"name": "SECRET_SSID",
10+
"value": ""
11+
},
12+
{
13+
"name": "SECRET_OPTIONAL_PASS",
14+
"value": ""
15+
}
16+
],
17+
"included_libs": []
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
2+
3+
#include <ArduinoIoTCloud.h>
4+
#include <Arduino_ConnectionHandler.h>
5+
6+
const char SSID[] = SECRET_SSID; // Network SSID (name)
7+
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
8+
9+
void onPressureChange();
10+
void onTemperatureChange();
11+
12+
float pressure;
13+
CloudTemperature temperature;
14+
15+
void initProperties(){
16+
17+
ArduinoCloud.addProperty(pressure, READWRITE, 30 * SECONDS, onPressureChange);
18+
ArduinoCloud.addProperty(temperature, READWRITE, 30 * SECONDS, onTemperatureChange);
19+
20+
}
21+
22+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
23+

TT/resources/thing_1.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
id: tositewise-3-2547e
2+
name: ToSiteWise 3
3+
sketch_template: ToSiteWise 3
4+
sketch_name: ToSiteWise_3_may08a
5+
timezone: ""
6+
tags:
7+
- key: integration
8+
value: sitewise
9+
variables:
10+
- id: pressure
11+
name: pressure
12+
type: FLOAT
13+
permission: READ_WRITE
14+
update_parameter: 30
15+
update_strategy: TIMED
16+
variable_name: pressure
17+
- id: temperature
18+
name: temperature
19+
type: TEMPERATURE
20+
permission: READ_WRITE
21+
update_parameter: 30
22+
update_strategy: TIMED
23+
variable_name: temperature

TT/template.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"title":"OK - RP with binaries","name":"OK - RP with binaries","description":"\u003cp\u003eBIN CONTENT INCLUDED\u003c/p\u003e","template":{"things":["thing_1.yaml"],"dashboards":null,"triggers":null},"resources":{"things":1,"dashboards":0,"triggers":0,"max_variables":2},"compatible_boards":[{"fqbn":"arduino:mbed_nano:nanorp2040connect"}],"images":[],"assets":{"thing_template_names":["ToSiteWise 3"],"dashboard_template_names":[],"trigger_template_names":[]},"source_template_id":"af076825-c0d2-4a27-a1cd-042df3604bae"}

TT/template_ok-rp-with-binaries.tino

248 KB
Binary file not shown.

cli/device/device.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCommand() *cobra.Command {
3131

3232
deviceCommand.AddCommand(initCreateCommand())
3333
deviceCommand.AddCommand(initListCommand())
34+
deviceCommand.AddCommand(initShowCommand())
3435
deviceCommand.AddCommand(initDeleteCommand())
3536
deviceCommand.AddCommand(tag.InitCreateTagsCommand())
3637
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())

cli/device/show.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"context"
22+
"fmt"
23+
"os"
24+
"strings"
25+
26+
"github.com/arduino/arduino-cli/cli/errorcodes"
27+
"github.com/arduino/arduino-cli/cli/feedback"
28+
"github.com/arduino/arduino-cli/table"
29+
"github.com/arduino/arduino-cloud-cli/command/device"
30+
"github.com/arduino/arduino-cloud-cli/config"
31+
"github.com/sirupsen/logrus"
32+
"github.com/spf13/cobra"
33+
)
34+
35+
type showFlags struct {
36+
deviceId string
37+
}
38+
39+
func initShowCommand() *cobra.Command {
40+
flags := &showFlags{}
41+
showCommand := &cobra.Command{
42+
Use: "show",
43+
Short: "Show device properties",
44+
Long: "Show device properties on Arduino IoT Cloud",
45+
Run: func(cmd *cobra.Command, args []string) {
46+
if err := runShowCommand(flags); err != nil {
47+
feedback.Errorf("Error during device show: %v", err)
48+
os.Exit(errorcodes.ErrGeneric)
49+
}
50+
},
51+
}
52+
showCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "device ID")
53+
54+
showCommand.MarkFlagRequired("device-id")
55+
56+
return showCommand
57+
}
58+
59+
func runShowCommand(flags *showFlags) error {
60+
logrus.Info("Show device")
61+
62+
cred, err := config.RetrieveCredentials()
63+
if err != nil {
64+
return fmt.Errorf("retrieving credentials: %w", err)
65+
}
66+
67+
dev, _, err := device.Show(context.TODO(), flags.deviceId, cred)
68+
if err != nil {
69+
return err
70+
}
71+
72+
feedback.PrintResult(showResult{dev})
73+
return nil
74+
}
75+
76+
type showResult struct {
77+
device *device.DeviceInfo
78+
}
79+
80+
func (r showResult) Data() interface{} {
81+
return r.device
82+
}
83+
84+
func (r showResult) String() string {
85+
if r.device == nil {
86+
return "No device found."
87+
}
88+
t := table.New()
89+
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Connection type", "Thing", "Tags")
90+
t.AddRow(
91+
r.device.Name,
92+
r.device.ID,
93+
r.device.Board,
94+
r.device.FQBN,
95+
r.device.Serial,
96+
dereferenceString(r.device.Status),
97+
dereferenceString(r.device.ConnectionType),
98+
dereferenceString(r.device.ThingID),
99+
strings.Join(r.device.Tags, ","),
100+
)
101+
return t.Render()
102+
}

cli/template/apply.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type applyFlags struct {
3434
templatePrefix string
3535
deviceId string
3636
netCredentials string
37+
applyOta bool
3738
}
3839

3940
func initTemplateApplyCommand() *cobra.Command {
@@ -53,12 +54,14 @@ func initTemplateApplyCommand() *cobra.Command {
5354
applyCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template ID")
5455
applyCommand.Flags().StringVarP(&flags.templatePrefix, "prefix", "p", "", "Prefix to apply to the name of created resources")
5556
applyCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID")
56-
applyCommand.Flags().StringVarP(&flags.netCredentials, "network-credentials", "n", "", "Network credentials")
57+
applyCommand.Flags().StringVarP(&flags.netCredentials, "network-credentials", "n", "", "Network credentials used to configure device (e.g. WiFi credentials). Supported values: SECRET_SSID | SECRET_OPTIONAL_PASS | SECRET_DEVICE_KEY")
5758

5859
applyCommand.MarkFlagRequired("template-id")
5960
applyCommand.MarkFlagRequired("prefix")
6061
applyCommand.MarkFlagRequired("device-id")
6162

63+
flags.applyOta = false
64+
6265
return applyCommand
6366
}
6467

@@ -72,8 +75,8 @@ func runTemplateApplyCommand(flags *applyFlags) error {
7275
if err != nil {
7376
return fmt.Errorf("parsing network credentials: %w", err)
7477
}
75-
76-
return template.ApplyCustomTemplates(cred, flags.templateId, flags.deviceId, flags.templatePrefix, deviceNetCredentials)
78+
79+
return template.ApplyCustomTemplates(cred, flags.templateId, flags.deviceId, flags.templatePrefix, deviceNetCredentials, flags.applyOta)
7780
}
7881

7982
func parseCredentials(credentials string) (map[string]string, error) {

command/device/device.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ import (
2525
// DeviceInfo contains the most interesting
2626
// parameters of an Arduino IoT Cloud device.
2727
type DeviceInfo struct {
28-
Name string `json:"name"`
29-
ID string `json:"id"`
30-
Board string `json:"board"`
31-
Serial string `json:"serial_number"`
32-
FQBN string `json:"fqbn"`
33-
Tags []string `json:"tags,omitempty"`
34-
Status *string `json:"status,omitempty"`
28+
Name string `json:"name"`
29+
ID string `json:"id"`
30+
Board string `json:"board"`
31+
Serial string `json:"serial_number"`
32+
FQBN string `json:"fqbn"`
33+
Tags []string `json:"tags,omitempty"`
34+
Status *string `json:"status,omitempty"`
35+
Type string `json:"type,omitempty"`
36+
ConnectionType *string `json:"connection_type,omitempty"`
37+
ThingID *string `json:"thing_id,omitempty"`
3538
}
3639

3740
func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
@@ -42,13 +45,18 @@ func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
4245
}
4346

4447
dev := &DeviceInfo{
45-
Name: device.Name,
46-
ID: device.Id,
47-
Board: device.Type,
48-
Serial: device.Serial,
49-
FQBN: dereferenceString(device.Fqbn),
50-
Tags: tags,
51-
Status: device.DeviceStatus,
48+
Name: device.Name,
49+
ID: device.Id,
50+
Board: device.Type,
51+
Serial: device.Serial,
52+
FQBN: dereferenceString(device.Fqbn),
53+
Tags: tags,
54+
Status: device.DeviceStatus,
55+
Type: device.Type,
56+
ConnectionType: device.ConnectionType,
57+
}
58+
if device.Thing != nil {
59+
dev.ThingID = &device.Thing.Id
5260
}
5361
return dev, nil
5462
}

0 commit comments

Comments
 (0)