Skip to content

Commit cf9f8ed

Browse files
authored
Merge pull request #1 from ai91/dev
Dev
2 parents 959b2ca + 57b3733 commit cf9f8ed

File tree

6 files changed

+92
-14
lines changed

6 files changed

+92
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ By default, logging is performed to console. Optionally it's possible to configu
7878
The basic configuration is responsible for communication and application itself.
7979

8080
Meanwhile, there is an extended configuration, which describes your smart home configuration, rules, and value mappings between your home automation system and Alisa Smart Home Skill.
81-
The extended configuration is takes place in the [config/devices.json](config/devices.json).
81+
The extended configuration takes place in the [config/devices.json](config/devices.json).
8282

8383
The **devices.json** configuration is a bit sophisticated, therefore I provide it in the dedicated [document](config/)
8484

alisamqttbridge/src/main/java/by/ibn/alisamqttbridge/JWTSecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class JWTSecurityConfig extends WebSecurityConfigurerAdapter {
2929
protected void configure(HttpSecurity http) throws Exception {
3030
http
3131
.authorizeRequests(authz -> authz
32+
.antMatchers("/").permitAll()
3233
.antMatchers("/user/**").hasAuthority("SCOPE_" + scope)
3334
.anyRequest().authenticated())
3435
.oauth2ResourceServer(oauth2 -> oauth2.jwt());
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package by.ibn.alisamqttbridge.controllers;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import by.ibn.alisamqttbridge.resources.Response;
12+
13+
/**
14+
* Entry point liveness check
15+
*
16+
* @author Anar Ibragimoff
17+
*
18+
*/
19+
@RestController
20+
public class LivenessController {
21+
22+
private Logger log = LoggerFactory.getLogger(LivenessController.class);
23+
24+
@RequestMapping(
25+
path = "/",
26+
method = RequestMethod.HEAD)
27+
public ResponseEntity<Response> head() {
28+
29+
log.trace("Processing request");
30+
31+
return new ResponseEntity<Response>(HttpStatus.OK);
32+
33+
}
34+
35+
}

alisamqttbridge/src/main/java/by/ibn/alisamqttbridge/service/DevicesService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package by.ibn.alisamqttbridge.service;
22

3-
import java.net.InetAddress;
4-
import java.net.UnknownHostException;
5-
import java.util.UUID;
6-
73
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
85
import org.springframework.stereotype.Service;
96

107
import by.ibn.alisamqttbridge.resources.Payload;
@@ -15,18 +12,17 @@ public class DevicesService {
1512

1613
@Autowired
1714
private DeviceRepository devicesRepository;
15+
16+
@Value("${devices.payload.userId:66211404}")
17+
private String userId;
1818

1919
public Response getDevices(String requestId) {
2020

2121
Response response = new Response();
2222

2323
response.requestId = requestId;
2424
response.payload = new Payload();
25-
try {
26-
response.payload.userId = InetAddress.getLocalHost().getHostName();
27-
} catch (UnknownHostException e) {
28-
response.payload.userId = UUID.randomUUID().toString();
29-
}
25+
response.payload.userId = userId;
3026

3127
response.payload.devices = devicesRepository.getDeviceResources();
3228

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package by.ibn.alisamqttbridge.controllers;
2+
3+
import org.eclipse.paho.client.mqttv3.IMqttClient;
4+
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.mock.mockito.MockBean;
10+
import org.springframework.test.context.ActiveProfiles;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
14+
15+
import by.ibn.alisamqttbridge.MQTTConfiguration;
16+
17+
@SpringBootTest
18+
@AutoConfigureMockMvc
19+
@ActiveProfiles("test")
20+
class LivenessControllerTest {
21+
22+
// just to disable
23+
@MockBean MQTTConfiguration mqtt;
24+
@MockBean IMqttClient mqttClient;
25+
@MockBean MqttConnectOptions mqttConnectOptions;
26+
27+
@Autowired
28+
private MockMvc mvc;
29+
30+
@Test
31+
void testHead() throws Exception {
32+
33+
mvc
34+
.perform(MockMvcRequestBuilders.head("/"))
35+
.andExpect(MockMvcResultMatchers.status().isOk());
36+
37+
}
38+
39+
}

config/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Each `rule` object may contain following nodes:
2020
- `valueMapsToMqtt` - mapping rules, when it's necessary to convert a value received from `devices/action` before publish to MQTT
2121

2222
#### rule.alexa
23-
This node may contain following nodes:
24-
- `alisa.instance` - is an Alisa controller instance under `state` node.
25-
- `alisa.subvalue` - when reporting state to Alisa, the `state` should be a complex node, rather than a simple value. It defines a subnode name under `state` for providing state. For example for `devices.capabilities.color_setting` with `color_model`=`hsv`, the expected report value must be like that:
23+
This node may contain following subnodes:
24+
- `alisa.instance` - is an `instance` under `state` node in communication with Alisa.
25+
- `alisa.subvalue` - when reporting state to Alisa, the `value` should be a complex node, rather than a simple value. It defines a subnode name under `state.value` for providing state. For example for `devices.capabilities.color_setting` with `color_model`=`hsv`, the expected report value must be like that:
2626

2727
```json
2828
...
@@ -74,7 +74,7 @@ to achieve that, it's necessary to define 3 rules, one per subvalue:
7474
```
7575

7676
#### rule.mqtt
77-
This node may contain following nodes:
77+
This node may contain following subnodes:
7878
- `mqtt.state` - topic name with values received from Home Automation System. Used as a value-source for `devices/query` responses.
7979
- `mqtt.commands` - topic name for commands to be sent to Home Automation System. Used as target-topic for `devices/action` commands received from Alexa.
8080

@@ -192,6 +192,13 @@ in this case, one can use <code>h</code>, <code>s</code>, and <code>v</code> as
192192

193193
When rule contains multiple mappings, they are attempted to be applied in order from first to last. If some mapping can't be applied (for example `value` mapping doesn't match `from` field), then this mapping is skipped, and verified next one. Iteration stopped after first successful conversion.
194194

195+
Though most of mapping types (except `linearRange`) are having output definitions as strings, when sent to Alisa as a query-response, they get casted to appropriate type automatically. For example:
196+
- `{ "type": "static", "value": "abc" }`, produces string: `{ "state": { "value": "abc" } }`
197+
- `{ "type": "static", "value": "1" }`, produces integer: `{ "state": { "value": 1 } }`
198+
- `{ "type": "static", "value": "1.0" }`, produces float: `{ "state": { "value": 1.0 } }`
199+
- `{ "type": "static", "value": "true" }`, produces boolean: `{ "state": { "value": true } }`
200+
- `{ "type": "static", "value": "{\"h\":255,\"s\":50,\"v\":100}" }`, produces json: `{ "state": { "value": { "h": 255, "s": 50, "v": 100 } } }`
201+
195202
# Troubleshooting
196203
Potential misconfigurations may cause either stop of the whole application, or some devices or rules may be ignored.
197204

0 commit comments

Comments
 (0)