def Start(): # Make the "modbus" file as executable st = os.stat(EXECUTABLE_FILENAME) os.chmod(EXECUTABLE_FILENAME, st.st_mode | stat.S_IEXEC) # Loading devices configuration lastQuery = {} try: if Constellation.GetSetting("Devices"): for device in json.loads(Constellation.GetSetting("Devices")): Constellation.WriteInfo( "Registering '%s' (interval: %d ms)" % (device["Name"], device["RequestInterval"])) lastQuery[device["Name"]] = 0 typeDescriptor = [] for prop in device["Properties"]: typeDescriptor.append({ "Name": prop["Name"], "Type": prop["Type"] if "Type" in prop else "", "Description": prop["Description"] if "Description" in prop else "" }) Constellation.DescribeStateObjectType( "Modbus.%s" % device["StateObjectTypeName"], "%s's datas" % device["StateObjectTypeName"], typeDescriptor) Constellation.DeclarePackageDescriptor() except Exception, e: Constellation.WriteError( "Error while loading the device's configuration : %s" % str(e))
def Start(): # Make the "GetTSL2561" file as executable st = os.stat(EXECUTABLE_FILENAME) os.chmod(EXECUTABLE_FILENAME, st.st_mode | stat.S_IEXEC) # Describe StateObject Constellation.DescribeStateObjectType("LightSensor.Lux", "Lux data informations", [{ 'Name': 'Broadband', 'Type': 'int' }, { 'Name': 'IR', 'Type': 'int' }, { 'Name': 'Lux', 'Type': 'int' }]) Constellation.DeclarePackageDescriptor() # Main loop if (bool(Constellation.GetSetting("EnableTSL2561"))): Constellation.WriteInfo("LuxSensor is ready !") lastSend = 0 while Constellation.IsRunning: if (bool(Constellation.GetSetting("EnableTSL2561"))): ts = int(round(time.time())) if ts - lastSend >= int(Constellation.GetSetting("Interval")): DoMeasure() lastSend = ts time.sleep(1)
def OnStart(): #declaration du StateObject Constellation.DescribeStateObjectType( "Sensors", "StateObject qui récupère données des capteurs ", [{ 'Name': 'CO2', 'Type': 'int', 'Description': 'Temperature en °C' }, { 'Name': 'Soundlevel', 'Type': 'int', 'Description': ' intensite sonore en dB' }, { 'Name': 'Temperature', 'Type': 'float', 'Description': 'niveau de co2(en ppm)' }, { 'Name': 'Ledstate', 'Type': 'bool', 'Description': 'niveau de co2(en ppm)' }]) #ouverture de port serie Constellation.WriteInfo( "Hi I'm '%s' and I currently running on %s and %s to Constellation" % (Constellation.PackageName, Constellation.SentinelName if not Constellation.IsStandAlone else 'local sandbox', "connected" if Constellation.IsConnected else "disconnected")) connected = [] ser = 0 while len(connected) <= 0 and ser == 0: ports_series = serial.tools.list_ports.comports() for element in ports_series: connected.append(element.device) if len( connected ) > 0: # on test si plusieurs ports ont été trouvée, si oui on utilise le 1er ser = serial.Serial(connected[0], 9600) ser.flushInput() while 1: #on lit le json sorti du port serie data = ser.readline() #on lui explique que c'est un json pushMe = json.loads(data) #Et on l'envoi a constellation Constellation.PushStateObject("Sensors", pushMe) time.sleep(1)
def OnStart(): # Register callback on package shutdown Constellation.OnExitCallback = OnExit # Write log & common properties Constellation.WriteInfo("Hi I'm '%s' and I currently running on %s and %s to Constellation" % (Constellation.PackageName, Constellation.SentinelName if not Constellation.IsStandAlone else 'local sandbox', "connected" if Constellation.IsConnected else "disconnected")) # GetSetting Constellation.WriteInfo("Demo1 = " + str(Constellation.GetSetting("Demo1"))) # If setting not exist, return None ! # Send message without parameter Constellation.SendMessage("DemoPackage", "HelloWorld", [], Constellation.MessageScope.package) # Send message with 2 parameter Constellation.SendMessage("DemoPackage", "SendMessage", [ "+33123456789", "Hi" ], Constellation.MessageScope.package) # Send message with complex parameter Constellation.SendMessage("DemoPackage", "DemoComplex", { "A": "This is a string", "B": 123, "C": True }, Constellation.MessageScope.package) # Send message "MessageCallbackWithComplexeResponse" with "123" as parameter to "Demo" pasckage and write the "A" property of the response Constellation.SendMessageWithSaga(lambda response: Constellation.WriteInfo("A=%s" % response.A), "Demo", "MessageCallbackWithComplexResponse", 123) # Push basic StateObject Constellation.PushStateObject("DemoStr", "Demo de seb") # StateObject with "String" as value Constellation.PushStateObject("DemoBool", False, lifetime=10) # StateObject with "Bool" as value and a lifetime (10 sec here) Constellation.PushStateObject("DemoInt", 123, metadatas={ "DeviceId":"RPi", "Serial":"123" }) # StateObject with "Int" as value and metadatas Constellation.PushStateObject("DemoFloat", 12.45, metadatas={ "DeviceId":"RPi", "Serial":"123" }, lifetime=10) # StateObject with "Float" as value, metadatas and lifetime # Custom type descriptions Constellation.DescribeMessageCallbackType("CredentialInfo", "Credential information", [ { 'Name':'Username', 'Type':'string', 'Description': 'The username' }, { 'Name':'Password', 'Type':'string', 'Description': 'The password' }, ]) Constellation.DescribeStateObjectType("DemoType", "Demo type", [ { 'Name':'Sender', 'Type':'string', 'Description': 'The demo' }, { 'Name':'Credential', 'Type':'CredentialInfo', 'Description': 'The credential nested demo' } ]) Constellation.DeclarePackageDescriptor() # Push complex StateObject Constellation.PushStateObject("Demo", { "Sender": "DemoPython", "Credential": { "Username": "******", "Password":"******" } }, "DemoType", { "DeviceId": "RPi", "Serial":"123" }) # StateObject with custom type # WriteInfo, WriteWarn & WriteError Constellation.WriteInfo("Hello world from Python !") Constellation.WriteWarn("This is a warning !") Constellation.WriteError("This is an error !") # Last StateObjects of the previous instance if Constellation.LastStateObjects: for so in Constellation.LastStateObjects: Constellation.WriteInfo(" + %s @ %s" % (so.Name, so.LastUpdate))