def main(args): global engine # opening the sap file, and creating the SEPA instance engine = YSAPEngine("./cocktail_sap.ysap") if "clear" in args: engine.clear() # # IDENTIFICATION OF INTERACTION PATTERNS # # Setup the Threshold Action threshold_Action = Action( engine, { "td": ThermostatTD, "action": "<http://MyThermostat.swot/ThresholdAction>", "newName": "ThresholdAction", "ids": ds_threshold }, threshold_update) # Setup the Event temperature_Event = Event( engine, { "td": ThermostatTD, "event": "<http://MyThermostat.swot/TemperatureEvent>", "eName": "TemperatureEvent", "ods": ds_lambda }) # # POSTING TRIPLES TO THE SWTE # # Setup and post the WebThing thermostat = Thing( engine, { "thing": ThermostatURI, "newName": "SmartThermostat", "newTD": ThermostatTD }).post(interaction_patterns=[threshold_Action, temperature_Event]) local_engine = YSAPEngine("./example.ysap") # # OPTIONAL THING DESCRIPTION JSON-LD # thermostat.tdServer_start("localhost", 8321) # adding context triples local_engine.update("ADD_THERMOSTAT_CONTEXT_TRIPLES", forcedBindings={"th": ThermostatURI}) # enabling threshold Action threshold_Action.enable() # main thermostat triggering logic local_engine.subscribe("THERMOSTAT_SMART_DISCOVERY", "thermostat_subscription", forcedBindings={"ds": ds_psi}, handler=available_actuators) # # DEVICE LOOP # # temperature Event triggering logic event_bindings = {"event": temperature_Event.uri, "newDS": ds_lambda} try: while True: sleep(2) unique_id = uuid4() event_bindings[ "newEInstance"] = "<http://MyThermostat.swot/TemperatureEvent/Instance_{}>".format( unique_id) event_bindings[ "newOData"] = "<http://MyThermostat.swot/TemperatureEvent/Data_{}>".format( unique_id) # in the real world this simulate() call would be a read to a temperature sensor! event_bindings["newValue"] = str(simulate()) temperature_Event.notify(event_bindings) with thresholdLock: if float(event_bindings["newValue"]) < T_low: message = '{{"target": {}, "now": "warming"}}'.format( T_low) trigger_action(message) elif float(event_bindings["newValue"]) > T_high: message = '{{"target": {}, "now": "cooling"}}'.format( T_high) trigger_action(message) except KeyboardInterrupt: print("Got KeyboardInterrupt!") except Exception as ex: print("Temperature simulation failed! Check the simulation server: {}". format(ex)) threshold_Action.disable() thermostat.tdServer_stop() return 0
def main(args): engine = YSAPEngine("./cocktail_sap.ysap") if "clear" in args: engine.clear() # # IDENTIFICATION AND DESCRIPTION OF INTERACTION PATTERNS # DEFINITION OF ACTIONS' BEHAVIOUR # def timeActionHandler(added, removed): print(added) if added != []: whatTimeIsIt.post_output( {"instance": added[0]["aInstance"]["value"], "oData": "<http://SmartClock.swot/TimeAction/Data_{}>".format(uuid4()), "oValue": datetime.fromtimestamp(time()).strftime('%Y-%m-%dT%H:%M:%S.%fZ'), "oDS": ds_datetime}) # Setup the Hot/Cold Action whatTimeIsIt = Action( engine, {"td": ClockTD, "action": TimeAction, "newName": "WhatTimeIsIt", "ods": ds_datetime}, timeActionHandler) def temperatureActionHandler(added, removed): print(added) if added != []: whatTimeIsIt.post_output( {"instance": added[0]["aInstance"]["value"], "oData": "<http://SmartClock.swot/TemperatureAction/Data_{}>".format(uuid4()), "oValue": str(simulate()), "oDS": ds_lambda}) whatsTheTemperature = Action( engine, {"td": ClockTD, "action": TemperatureAction, "newName": "WhatsTheTemperature", "ods": ds_lambda}, temperatureActionHandler) # # POSTING THE TRIPLES TO THE SWTE # # Setup and post the WebThing smartClock = Thing( engine, {"thing": ClockURI, "newName": "SmartClock", "newTD": ClockTD}).post(interaction_patterns=[whatTimeIsIt, whatsTheTemperature]) whatTimeIsIt.enable() whatsTheTemperature.enable() local_engine = YSAPEngine("./example.ysap") # adding context triples local_engine.update("ADD_THERMOSTAT_CONTEXT_TRIPLES", forcedBindings={"th": ClockURI}) # # DEVICE LOOP # while True: try: sleep(10) except KeyboardInterrupt: print("Got KeyboardInterrupt!") whatTimeIsIt.disable() whatsTheTemperature.disable() break return 0
def main(args): global engine global HC_Property_bindings global HC_Property # opening the sap file, and creating the SEPA instance engine = YSAPEngine("./cocktail_sap.ysap") if "clear" in args: engine.clear() # # IDENTIFICATION AND DESCRIPTION OF INTERACTION PATTERNS # # Setup the Hot/Cold Action mainHC_Action = Action( engine, {"td": HotColdTD, "action": HotColdActionURI, "newName": "MainHotColdAction", "ids": ds_psi}, mainHotColdActionLogic) HC_Property_bindings = {"td": HotColdTD, "property": HotColdPropertyURI, "newName": "InternalStatusHotColdProperty", "newStability": "0", "newWritability": "false", "newDS": ds_psi, "newPD": "<http://HotCold.swot/MainHotColdProperty/Data>", "newValue": '{"now": "off", "target": "15"}'} HC_Property = Property(engine, HC_Property_bindings) # # POSTING TRIPLES TO THE SWTE # # Setup and post the WebThing thermostat = Thing( engine, {"thing": HotColdURI, "newName": "HotCold", "newTD": HotColdTD}).post(interaction_patterns=[mainHC_Action, HC_Property]) mainHC_Action.enable() local_engine = YSAPEngine("./example.ysap") # adding context triples local_engine.update("ADD_HOTCOLD_CONTEXT_TRIPLES", forcedBindings={"hc": HotColdURI}) # main hotcold event search logic local_engine.subscribe( "HOTCOLD_SMART_DISCOVERY", "hotcold_subscription", forcedBindings={"ds": ds_lambda}, handler=available_sensors) # # DEVICE LOOP # try: while True: sleep(10) print("Device status is currently {}".format(HC_Property.value)) except KeyboardInterrupt: print("Got KeyboardInterrupt!") mainHC_Action.disable() return 0