@@ -19,12 +19,15 @@ const core_1 = require("@node-wot/core");
1919const binding_http_1 = require ( "@node-wot/binding-http" ) ;
2020// create Servient add HTTP binding with port configuration
2121const servient = new core_1 . Servient ( ) ;
22+ // const staticAddress = "plugfest.thingweb.io";
23+ const staticAddress = "localhost" ; // use this for testing locally
24+ const httpPort = 8081 ;
2225servient . addServer (
2326 new binding_http_1 . HttpServer ( {
24- port : 8081 ,
27+ port : httpPort ,
2528 } )
2629) ;
27- core_1 . Helpers . setStaticAddress ( "plugfest.thingweb.io" ) ; // comment this out if you are testing locally
30+ core_1 . Helpers . setStaticAddress ( staticAddress ) ;
2831let waterAmount = 1000 ;
2932let beansAmount = 1000 ;
3033let milkAmount = 1000 ;
@@ -73,8 +76,22 @@ servient.start().then((WoT) => {
7376 refill : {
7477 synchronous : true ,
7578 input : {
76- type : "string" ,
77- enum : [ "water" , "beans" , "milk" ] ,
79+ type : "array" ,
80+ items : {
81+ type : "string" ,
82+ enum : [ "water" , "beans" , "milk" ] ,
83+ } ,
84+ } ,
85+ } ,
86+ } ,
87+ events : {
88+ resourceEmpty : {
89+ data : {
90+ type : "array" ,
91+ items : {
92+ type : "string" ,
93+ enum : [ "water" , "beans" , "milk" ] ,
94+ } ,
7895 } ,
7996 } ,
8097 } ,
@@ -99,6 +116,16 @@ servient.start().then((WoT) => {
99116 waterAmount = waterAmount - 10 ;
100117 beansAmount = beansAmount - 10 ;
101118 thing . emitPropertyChange ( "resources" ) ;
119+ const resourceEvent = [ ] ;
120+ if ( waterAmount <= 10 ) {
121+ resourceEvent . push ( "water" ) ;
122+ }
123+ if ( beansAmount <= 10 ) {
124+ resourceEvent . push ( "beans" ) ;
125+ }
126+ if ( resourceEvent . length > 0 ) {
127+ thing . emitEvent ( "resourceEmpty" , resourceEvent ) ;
128+ }
102129 return undefined ;
103130 }
104131 } else if ( coffeeType === "cappuccino" ) {
@@ -110,6 +137,19 @@ servient.start().then((WoT) => {
110137 beansAmount = beansAmount - 20 ;
111138 milkAmount = milkAmount - 10 ;
112139 thing . emitPropertyChange ( "resources" ) ;
140+ const resourceEvent = [ ] ;
141+ if ( waterAmount <= 10 ) {
142+ resourceEvent . push ( "water" ) ;
143+ }
144+ if ( beansAmount <= 10 ) {
145+ resourceEvent . push ( "beans" ) ;
146+ }
147+ if ( milkAmount <= 10 ) {
148+ resourceEvent . push ( "milk" ) ;
149+ }
150+ if ( resourceEvent . length > 0 ) {
151+ thing . emitEvent ( "resourceEmpty" , resourceEvent ) ;
152+ }
113153 return undefined ;
114154 }
115155 } else if ( coffeeType === "americano" ) {
@@ -120,6 +160,16 @@ servient.start().then((WoT) => {
120160 waterAmount = waterAmount - 30 ;
121161 beansAmount = beansAmount - 10 ;
122162 thing . emitPropertyChange ( "resources" ) ;
163+ const resourceEvent = [ ] ;
164+ if ( waterAmount <= 10 ) {
165+ resourceEvent . push ( "water" ) ;
166+ }
167+ if ( beansAmount <= 10 ) {
168+ resourceEvent . push ( "beans" ) ;
169+ }
170+ if ( resourceEvent . length > 0 ) {
171+ thing . emitEvent ( "resourceEmpty" , resourceEvent ) ;
172+ }
123173 return undefined ;
124174 }
125175 } else {
@@ -129,25 +179,22 @@ servient.start().then((WoT) => {
129179 thing . setActionHandler ( "refill" , async ( params , options ) => {
130180 const selectedResource = await params . value ( ) ;
131181 console . info ( "received refill order of " , selectedResource ) ;
132- switch ( selectedResource ) {
133- case "water" :
134- waterAmount = 1000 ;
135- break ;
136- case "beans" :
137- beansAmount = 1000 ;
138- break ;
139- case "milk" :
140- milkAmount = 1000 ;
141- break ;
142- default :
143- throw new Error ( "Wrong refill input" ) ;
182+ if ( selectedResource . indexOf ( "water" ) !== - 1 ) {
183+ waterAmount = 1000 ;
184+ }
185+ if ( selectedResource . indexOf ( "beans" ) !== - 1 ) {
186+ beansAmount = 1000 ;
187+ }
188+ if ( selectedResource . indexOf ( "milk" ) !== - 1 ) {
189+ milkAmount = 1000 ;
144190 }
145191 thing . emitPropertyChange ( "resources" ) ;
146192 return undefined ;
147193 } ) ;
148194 // expose the thing
149195 thing . expose ( ) . then ( ( ) => {
150196 console . info ( thing . getThingDescription ( ) . title + " ready" ) ;
197+ console . info ( "TD available at http://" + staticAddress + ":" + httpPort ) ;
151198 } ) ;
152199 } )
153200 . catch ( ( e ) => {
0 commit comments