Skip to content

Commit e8df54e

Browse files
authored
add akri vscode extension samples (#164)
## Purpose <!-- Describe the intention of the changes being proposed. What problem does it solve or functionality does it add? --> * ... This PR adds the akri samples needed to test the akri connectors vscode extension. ## Does this introduce a breaking change? <!-- Mark one with an "x". --> ``` [ ] Yes [x] No ``` ## Pull Request Type What kind of change does this Pull Request introduce? <!-- Please check the one that applies to this PR using "x". --> ``` [ ] Bugfix [ ] Feature [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [x] Documentation content changes [ ] Other... Please describe: ``` ## How to Test * Get the code ``` git clone [repo-address] cd [repo-name] git checkout [branch-name] npm install ``` * Test the code <!-- Add steps to run the tests suite and/or manually test --> ``` ``` ## What to Check Verify that the following are valid * ... ## Other Information <!-- Add any other helpful information that may be needed here. -->
1 parent a882f40 commit e8df54e

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: namespaces.deviceregistry.microsoft.com/v1
2+
kind: Asset
3+
metadata:
4+
name: my-rest-thermostat-asset1
5+
namespace: azure-iot-operations
6+
spec:
7+
attributes:
8+
assetId: my-rest-thermostat-asset1
9+
assetType: rest-thermostat-asset
10+
datasets:
11+
- name: thermostat_status
12+
dataPoints:
13+
- dataSource: /api/thermostat/current
14+
name: currentTemperature
15+
dataPointConfiguration: |-
16+
{
17+
"HttpRequestMethod": "GET",
18+
}
19+
- dataSource: /api/thermostat/desired
20+
name: desiredTemperature
21+
dataPointConfiguration: |-
22+
{
23+
"HttpRequestMethod": "GET",
24+
}
25+
dataSource: /thermostat
26+
destinations:
27+
- configuration:
28+
topic: mqtt/machine/asset1/status
29+
target: Mqtt
30+
deviceRef:
31+
deviceName: my-rest-thermostat-device-name
32+
endpointName: my-rest-thermostat-endpoint-name
33+
version: 1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: namespaces.deviceregistry.microsoft.com/v1
2+
kind: Asset
3+
metadata:
4+
name: my-rest-thermostat-asset2
5+
namespace: azure-iot-operations
6+
spec:
7+
attributes:
8+
assetId: my-rest-thermostat-asset2
9+
assetType: rest-thermostat-asset
10+
datasets:
11+
- name: thermostat_status
12+
dataPoints:
13+
- dataSource: /api/thermostat/current
14+
name: currentTemperature
15+
dataPointConfiguration: |-
16+
{
17+
"HttpRequestMethod": "GET",
18+
}
19+
- dataSource: /api/thermostat/desired
20+
name: desiredTemperature
21+
dataPointConfiguration: |-
22+
{
23+
"HttpRequestMethod": "GET",
24+
}
25+
dataSource: /thermostat
26+
destinations:
27+
- configuration:
28+
key: RestThermostatKey
29+
target: BrokerStateStore
30+
deviceRef:
31+
deviceName: my-rest-thermostat-device-name
32+
endpointName: my-rest-thermostat-endpoint-name
33+
version: 1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: namespaces.deviceregistry.microsoft.com/v1
2+
kind: Device
3+
metadata:
4+
name: my-rest-thermostat-device-name
5+
namespace: azure-iot-operations
6+
spec:
7+
attributes:
8+
deviceId: my-thermostat
9+
deviceType: thermostat-device-type
10+
enabled: true
11+
endpoints:
12+
inbound:
13+
my-rest-thermostat-endpoint-name:
14+
additionalConfiguration: '{}'
15+
address: http://restserver:3000
16+
authentication:
17+
method: Anonymous
18+
endpointType: rest-thermostat-endpoint-type
19+
uuid: 1234-5678-9012-3456
20+
version: 2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Dockerfile
2+
FROM node:18
3+
4+
WORKDIR /usr/src/app
5+
COPY package*.json ./
6+
RUN npm install
7+
COPY . .
8+
9+
EXPOSE 3000
10+
CMD ["node", "server.js"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Sample Rest Server
2+
3+
Run the following commands to build a sample rest server docker image:
4+
5+
```bash
6+
cd preview/akri-connectors-extension/SampleRestServer
7+
docker build -t rest-server:latest .
8+
```
9+
10+
**NOTE:**: This rest server is unauthenticated and it should be accessible at port 3000 during container run.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "rest-server",
3+
"version": "1.0.0",
4+
"description": "Test REST Service",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"auth": "^1.1.1",
14+
"basic": "^2.0.3",
15+
"basic-auth": "^2.0.1",
16+
"express": "^4.19.2"
17+
}
18+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// server.js
2+
const express = require("express");
3+
const app = express();
4+
5+
const port = process.env.PORT || 3000;
6+
7+
// Function to generate random temperature values in Fahrenheit
8+
function getRandomTemperature(min, max) {
9+
return (Math.random() * (max - min) + min).toFixed(2);
10+
}
11+
12+
let desiredTemperature = getRandomTemperature(68, 77); // Approx 20-25�C in Fahrenheit
13+
let currentTemperature = getRandomTemperature(68, 77);
14+
let thermostatPower = "on";
15+
16+
// Get Current Temperature
17+
app.get("/api/thermostat/current", (req, res) => {
18+
currentTemperature = getRandomTemperature(68, 77);
19+
res.json({ currentTemperature: parseFloat(currentTemperature) });
20+
});
21+
22+
// Get Desired Temperature
23+
app.get("/api/thermostat/desired", (req, res) => {
24+
res.json({ desiredTemperature: parseFloat(desiredTemperature) });
25+
});
26+
27+
// Set Desired Temperature
28+
app.post("/api/thermostat/desired", express.json(), (req, res) => {
29+
if (req.body.desiredTemperature) {
30+
desiredTemperature = req.body.desiredTemperature;
31+
res.json({ message: "Desired temperature set successfully" });
32+
} else {
33+
res.status(400).json({ message: "Desired temperature is required" });
34+
}
35+
});
36+
37+
// Get Thermostat Status
38+
app.get("/api/thermostat/status", (req, res) => {
39+
currentTemperature = getRandomTemperature(68, 77);
40+
let status = desiredTemperature > currentTemperature ? "heating" : "cooling";
41+
res.json({
42+
status: status,
43+
currentTemperature: parseFloat(currentTemperature),
44+
desiredTemperature: parseFloat(desiredTemperature),
45+
});
46+
});
47+
48+
// Toggle Thermostat Power
49+
app.post("/api/thermostat/power", express.json(), (req, res) => {
50+
if (req.body.power === "on" || req.body.power === "off") {
51+
thermostatPower = req.body.power;
52+
res.json({ message: `Thermostat power turned ${thermostatPower}` });
53+
} else {
54+
res.status(400).json({ message: "Power state must be 'on' or 'off'" });
55+
}
56+
});
57+
58+
app.listen(port, () => {
59+
console.log(`Thermostat API server running on port ${port}`);
60+
});

0 commit comments

Comments
 (0)