Skip to content

Exercise in creating a smart home HTTP backend for OTUS Rust Developer

Notifications You must be signed in to change notification settings

mihsamusev/otus_smart_home_backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart home backend

Exercise in creating a smart home HTTP backend for OTUS Rust Developer

API routes

  • room
    • POST /room/{room_id}
    • GET /room
    • GET /room/{room_id}
    • DELETE /room/{room_id}
  • device
    • POST /device/{room_id}
    • GET /device/{room_id}/{device_id}
    • DELETE /device/{room_id}/{device_id}
  • status
    • GET /status/{room_id}
    • GET /status/{room_id}/{device_id}

Example

Turn on TCP socket device server to communicate with it device through HTTP API. You can run multiple servers on different ports to emulate more than one device.

cargo run --example net_socket_emulator -- --address 127.0.0.1:8080
cargo run --example net_socket_emulator -- --address 127.0.0.1:8090

Start the smart home HTTP server

cargo run

Interact with an api using imported Postman collection from the following JSON link, or try the following curl commands in your terminal:

# create a kitchen nad bathroom
curl -X POST "127.0.0.1:8888/room/kitchen"
curl -X POST "127.0.0.1:8888/room/bathroom"
curl -X GET "127.0.0.1:8888/room"
curl -X GET "127.0.0.1:8888/room/kitchen"

# add 2 devices to kitchen
curl -X POST "127.0.0.1:8888/device/kitchen" -H 'Content-Type: application/json' -d '{"device_name": "socket_1", "address": "127.0.0.1:8080", "device_type": "tcp_socket"}'

# add 1 device to bathroom
curl -X POST "127.0.0.1:8888/device/bathroom" -H 'Content-Type: application/json' -d '{"device_name": "socket_1", "address": "127.0.0.1:8090", "device_type": "tcp_socket"}'

curl -X POST "127.0.0.1:8888/device/bathroom" -H 'Content-Type: application/json' -d '{"device_name": "socket_2", "address": "127.0.0.1:8091", "device_type": "tcp_socket"}'

# see the rooms layout with devices
curl -X GET "127.0.0.1:8888/room"

# ask devices for their statuses
curl -X GET "127.0.0.1:8888/status/bathroom"
curl -X GET "127.0.0.1:8888/status/kitchen"

# delete a device and see how many are left
curl -X DELETE "127.0.0.1:8888/device/bathroom/socket_1"
curl -X GET "127.0.0.1:8888/room"

About

Exercise in creating a smart home HTTP backend for OTUS Rust Developer

Resources

Stars

Watchers

Forks

Languages