WebThingsIO / mozilla-iot.github.io

Mozilla IoT Website
20 stars 12 forks source link

Add PHP library. #70

Closed mrstegeman closed 4 years ago

maliknaik16 commented 4 years ago

Web Thing PHP test results using webthing-tester:

$ python3.7 test-client.py --protocol http --host localhost --port 8888 --skip-websocket --debug
Request:  GET http://localhost:8888
Response: 200 {"id":"urn:dev:ops:my-lamp-1234","title":"My Lamp","@context":"https:\/\/iot.mozilla.org\/schemas","properties":{"on":{"@type":"OnOffProperty","title":"On\/Off","type":"boolean","description":"Whether the lamp is turned on","links":[{"rel":"property","href":"\/properties\/on"}]},"brightness":{"@type":"BrightnessProperty","title":"Brightness","type":"integer","description":"The level of light from 0-100","minimum":0,"maximum":100,"unit":"percent","links":[{"rel":"property","href":"\/properties\/brightness"}]}},"actions":{"fade":{"title":"Fade","description":"Fade the lamp to a given level","input":{"type":"object","required":["brightness","duration"],"properties":{"brightness":{"type":"integer","minimum":0,"maximum":100,"unit":"percent"},"duration":{"type":"integer","minimum":1,"unit":"milliseconds"}}},"links":[{"rel":"action","href":"\/actions\/fade"}]}},"events":{"overheated":{"description":"The lamp has exceeded its safe operating temperature","type":"number","unit":"degree celsius","links":[{"rel":"event","href":"\/events\/overheated"}]}},"links":[{"rel":"properties","href":"\/properties"},{"rel":"actions","href":"\/actions"},{"rel":"events","href":"\/events"},{"rel":"alternate","href":"http\/"}],"description":"A web connected lamp","@type":["OnOffSwitch","Light"],"base":"http:\/\/localhost:8888\/","securityDefinitions":{"nosec_sc":{"scheme":"nosec"}},"security":"nosec_sc"}

Request:  GET http://localhost:8888/properties
Response: 200 {"on":true,"brightness":50}

Request:  GET http://localhost:8888/properties/brightness
Response: 200 {"brightness":50}

Request:  PUT http://localhost:8888/properties/brightness
          {'brightness': 25}
Response: 200 {"brightness":25}

Request:  GET http://localhost:8888/properties/brightness
Response: 200 {"brightness":25}

Request:  GET http://localhost:8888/events
Response: 200 []

Request:  GET http://localhost:8888/actions
Response: 200 []

Request:  POST http://localhost:8888/actions
          {'fade': {'input': {'brightness': 50, 'duration': 2000}}}
Response: 201 {"fade":{"href":"\/actions\/fade\/25e61afc-b7de-4cd1-82e5-fc8ffced3d81","timeRequested":"2020-01-14T15:29:48+05:30","status":"created","input":{"brightness":50,"duration":2000}}}

Request:  GET http://localhost:8888/actions
Response: 200 [{"fade":{"href":"\/actions\/fade\/25e61afc-b7de-4cd1-82e5-fc8ffced3d81","timeRequested":"2020-01-14T15:29:48+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-14T15:29:50+05:30"}}]

Request:  GET http://localhost:8888/actions/fade
Response: 200 [{"fade":{"href":"\/actions\/fade\/25e61afc-b7de-4cd1-82e5-fc8ffced3d81","timeRequested":"2020-01-14T15:29:48+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-14T15:29:50+05:30"}}]

Request:  DELETE http://localhost:8888/actions/fade/25e61afc-b7de-4cd1-82e5-fc8ffced3d81
Response: 204

Request:  GET http://localhost:8888/events
Response: 200 [{"overheated":{"timestamp":"2020-01-14T15:29:50+05:30","data":102}}]

Request:  GET http://localhost:8888/events/overheated
Response: 200 [{"overheated":{"timestamp":"2020-01-14T15:29:50+05:30","data":102}}]

Request:  POST http://localhost:8888/actions/fade
          {'fade': {'input': {'brightness': 50, 'duration': 2000}}}
Response: 201 {"fade":{"href":"\/actions\/fade\/051c33ed-fa6d-4317-9c2e-75c2e7c54dc1","timeRequested":"2020-01-14T15:29:53+05:30","status":"created","input":{"brightness":50,"duration":2000}}}

Request:  GET http://localhost:8888/actions
Response: 200 [{"fade":{"href":"\/actions\/fade\/051c33ed-fa6d-4317-9c2e-75c2e7c54dc1","timeRequested":"2020-01-14T15:29:53+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-14T15:29:55+05:30"}}]

Request:  GET http://localhost:8888/actions/fade
Response: 200 [{"fade":{"href":"\/actions\/fade\/051c33ed-fa6d-4317-9c2e-75c2e7c54dc1","timeRequested":"2020-01-14T15:29:53+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-14T15:29:55+05:30"}}]

Request:  DELETE http://localhost:8888/actions/fade/051c33ed-fa6d-4317-9c2e-75c2e7c54dc1
Response: 204
maliknaik16 commented 4 years ago

All the HTTP tests are passing and for the WebSocket support I'll try to implement that by the end of this week.

Note: The HTTP and WebSocket run on different ports for webthing-php.

maliknaik16 commented 4 years ago

I've successfully implemented the support for the WebSocket in the Web Thing PHP. The following are the test results from the webthing-tester:

$ python3.7 test-client.py --protocol http --host localhost --port 8888 --debug
Request:  GET http://localhost:8888
Response: 200 {"id":"urn:dev:ops:my-lamp-1234","title":"My Lamp","@context":"https:\/\/iot.mozilla.org\/schemas","properties":{"on":{"@type":"OnOffProperty","title":"On\/Off","type":"boolean","description":"Whether the lamp is turned on","links":[{"rel":"property","href":"\/properties\/on"}]},"brightness":{"@type":"BrightnessProperty","title":"Brightness","type":"integer","description":"The level of light from 0-100","minimum":0,"maximum":100,"unit":"percent","links":[{"rel":"property","href":"\/properties\/brightness"}]}},"actions":{"fade":{"title":"Fade","description":"Fade the lamp to a given level","input":{"type":"object","required":["brightness","duration"],"properties":{"brightness":{"type":"integer","minimum":0,"maximum":100,"unit":"percent"},"duration":{"type":"integer","minimum":1,"unit":"milliseconds"}}},"links":[{"rel":"action","href":"\/actions\/fade"}]}},"events":{"overheated":{"description":"The lamp has exceeded its safe operating temperature","type":"number","unit":"degree celsius","links":[{"rel":"event","href":"\/events\/overheated"}]}},"links":[{"rel":"properties","href":"\/properties"},{"rel":"actions","href":"\/actions"},{"rel":"events","href":"\/events"},{"rel":"alternate","href":"ws:\/\/localhost:8081\/"}],"description":"A web connected lamp","@type":["OnOffSwitch","Light"],"base":"http:\/\/localhost:8888\/","securityDefinitions":{"nosec_sc":{"scheme":"nosec"}},"security":"nosec_sc"}

Request:  GET http://localhost:8888/properties
Response: 200 {"on":true,"brightness":50}

Request:  GET http://localhost:8888/properties/brightness
Response: 200 {"brightness":50}

Request:  PUT http://localhost:8888/properties/brightness
          {'brightness': 25}
Response: 200 {"brightness":25}

Request:  GET http://localhost:8888/properties/brightness
Response: 200 {"brightness":25}

Request:  GET http://localhost:8888/events
Response: 200 []

Request:  GET http://localhost:8888/actions
Response: 200 []

Request:  POST http://localhost:8888/actions
          {'fade': {'input': {'brightness': 50, 'duration': 2000}}}
Response: 201 {"fade":{"href":"\/actions\/fade\/2ad1d191-0e9f-434b-8ca3-3ae92485ddd5","timeRequested":"2020-01-15T00:41:49+05:30","status":"created","input":{"brightness":50,"duration":2000}}}

Request:  GET http://localhost:8888/actions
Response: 200 [{"fade":{"href":"\/actions\/fade\/2ad1d191-0e9f-434b-8ca3-3ae92485ddd5","timeRequested":"2020-01-15T00:41:49+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-15T00:41:51+05:30"}}]

Request:  GET http://localhost:8888/actions/fade
Response: 200 [{"fade":{"href":"\/actions\/fade\/2ad1d191-0e9f-434b-8ca3-3ae92485ddd5","timeRequested":"2020-01-15T00:41:49+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-15T00:41:51+05:30"}}]

Request:  DELETE http://localhost:8888/actions/fade/2ad1d191-0e9f-434b-8ca3-3ae92485ddd5
Response: 204

Request:  GET http://localhost:8888/events
Response: 200 [{"overheated":{"timestamp":"2020-01-15T00:41:51+05:30","data":102}}]

Request:  GET http://localhost:8888/events/overheated
Response: 200 [{"overheated":{"timestamp":"2020-01-15T00:41:51+05:30","data":102}}]

Request:  POST http://localhost:8888/actions/fade
          {'fade': {'input': {'brightness': 50, 'duration': 2000}}}
Response: 201 {"fade":{"href":"\/actions\/fade\/91e342ea-b3f7-4c9e-a134-df4e47447fc1","timeRequested":"2020-01-15T00:41:54+05:30","status":"created","input":{"brightness":50,"duration":2000}}}

Request:  GET http://localhost:8888/actions
Response: 200 [{"fade":{"href":"\/actions\/fade\/91e342ea-b3f7-4c9e-a134-df4e47447fc1","timeRequested":"2020-01-15T00:41:54+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-15T00:41:56+05:30"}}]

Request:  GET http://localhost:8888/actions/fade
Response: 200 [{"fade":{"href":"\/actions\/fade\/91e342ea-b3f7-4c9e-a134-df4e47447fc1","timeRequested":"2020-01-15T00:41:54+05:30","status":"completed","input":{"brightness":50,"duration":2000},"timeCompleted":"2020-01-15T00:41:56+05:30"}}]

Request:  DELETE http://localhost:8888/actions/fade/91e342ea-b3f7-4c9e-a134-df4e47447fc1
Response: 204

WS Send: {"messageType": "setProperty", "data": {"brightness": 10}}
WS Recv: {"messageType":"propertyStatus","data":{"brightness":10}}
Request:  GET http://localhost:8888/properties/brightness
Response: 200 {"brightness":10}

WS Send: {"messageType": "requestAction", "data": {"fade": {"input": {"brightness": 90, "duration": 1000}}}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/2c34c923-0794-4a83-82de-dc901d52dcc9","timeRequested":"2020-01-15T00:41:58+05:30","status":"created","input":{"brightness":90,"duration":1000}}}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/2c34c923-0794-4a83-82de-dc901d52dcc9","timeRequested":"2020-01-15T00:41:58+05:30","status":"pending","input":{"brightness":90,"duration":1000}}}}
WS Recv: {"messageType":"propertyStatus","data":{"brightness":90}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/2c34c923-0794-4a83-82de-dc901d52dcc9","timeRequested":"2020-01-15T00:41:58+05:30","status":"completed","input":{"brightness":90,"duration":1000},"timeCompleted":"2020-01-15T00:41:59+05:30"}}}
Request:  GET http://localhost:8888/actions
Response: 200 [{"fade":{"href":"\/actions\/fade\/2c34c923-0794-4a83-82de-dc901d52dcc9","timeRequested":"2020-01-15T00:41:58+05:30","status":"completed","input":{"brightness":90,"duration":1000},"timeCompleted":"2020-01-15T00:41:59+05:30"}}]

Request:  GET http://localhost:8888/actions/fade/2c34c923-0794-4a83-82de-dc901d52dcc9
Response: 200 {"fade":{"href":"\/actions\/fade\/2c34c923-0794-4a83-82de-dc901d52dcc9","timeRequested":"2020-01-15T00:41:58+05:30","status":"completed","input":{"brightness":90,"duration":1000},"timeCompleted":"2020-01-15T00:41:59+05:30"}}

Request:  GET http://localhost:8888/events
Response: 200 [{"overheated":{"timestamp":"2020-01-15T00:41:51+05:30","data":102}},{"overheated":{"timestamp":"2020-01-15T00:41:56+05:30","data":102}},{"overheated":{"timestamp":"2020-01-15T00:41:59+05:30","data":102}}]

WS Send: {"messageType": "addEventSubscription", "data": {"overheated": {}}}
WS Send: {"messageType": "requestAction", "data": {"fade": {"input": {"brightness": 100, "duration": 500}}}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/c3b525ba-9497-4369-962f-ed9547b14a58","timeRequested":"2020-01-15T00:41:59+05:30","status":"created","input":{"brightness":100,"duration":500}}}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/c3b525ba-9497-4369-962f-ed9547b14a58","timeRequested":"2020-01-15T00:41:59+05:30","status":"pending","input":{"brightness":100,"duration":500}}}}
WS Recv: {"messageType":"propertyStatus","data":{"brightness":100}}
WS Recv: {"messageType":"event","data":{"overheated":{"timestamp":"2020-01-15T00:41:59+05:30","data":102}}}
WS Recv: {"messageType":"actionStatus","data":{"fade":{"href":"\/actions\/fade\/c3b525ba-9497-4369-962f-ed9547b14a58","timeRequested":"2020-01-15T00:41:59+05:30","status":"completed","input":{"brightness":100,"duration":500},"timeCompleted":"2020-01-15T00:41:59+05:30"}}}
mrstegeman commented 4 years ago

Great work!!

maliknaik16 commented 4 years ago

Thanks

maliknaik16 commented 4 years ago

Can I copy the explanation of the Example implementation from webthing-python into my README.md by replacing the python's code with PHP?

mrstegeman commented 4 years ago

@maliknaik16 I don't see why not.