from onboardapipython import M_Device_Service, M_Device_IService, M_Device_IClient, M_Device_Client
from onboardapipython import M_Device_DeviceStatusType, M_Common_OperationModeType
import time
# Callbacks of a service to receive messages from client(s).
class CallbacksDeviceService(M_Device_IService):
def CmdIsDeviceAlive(self):
print("Received CmdIsDeviceAlive")
def CmdEnableDevice(self, IsEnabled):
print(f"Received CmdEnableDevice {str(IsEnabled)}")
def CmdShutdownDevice(self):
print("Received CmdShutdownDevice")
def CmdSetDeviceMode(self, Mode):
print("Received CmdSetDeviceMode")
def CmdSetValue(self, Id, Value):
print("Received CmdSetValue")
def CmdSetValueIncrementally(self, Id, Value):
print("Received CmdSetValueIncrementally")
def RequestValue(self, Id):
print("Received RequestValue")
# Callback class of a client to receive messages from the service.
class CallbacksDeviceClient(M_Device_IClient):
def ReportDeviceSettings(self, Settings):
print("Received ReportDeviceSettings")
def ReportDeviceStatus(self, Status, IsRemoved):
print("Received ReportDeviceStatus")
def ReportValue(self, KeyId, Value, IsRemoved):
print("Received ReportValue")
def ResponseValue(self, Id, Value):
print("Received ResponseValue")
if __name__ == "__main__":
print("start program")
domainId = 6
serviceName = "MyExampleServiceName"
# Service
serviceCallbacks = CallbacksDeviceService()
deviceService: M_Device_Service = M_Device_Service.create(domainId, serviceName, serviceCallbacks)
# Client
clientCallbacks = CallbacksDeviceClient()
deviceClient: M_Device_Client = M_Device_Client.create(domainId, serviceName, clientCallbacks)
while True:
time.sleep(1)
# Service sends a message to its client(s).
try:
status: M_Device_DeviceStatusType = M_Device_DeviceStatusType()
status.IsEnabled = True
deviceService.send().ReportDeviceStatus(status, False)
# Client sends a message to the service.
mode: M_Common_OperationModeType = M_Common_OperationModeType()
mode.Name = "HelloWorld"
deviceClient.send().CmdSetDeviceMode(mode)
except Exception:
print("Error")