harvesterMessageDistributer.py 1.26 KB
Newer Older
lenekaethe's avatar
lenekaethe committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from harvesterServices import HarvesterServices
from harvesterSecretary import HarvesterSecretary
import json

import sys, os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
from digitalTwin import MessageDistributer

class HarvesterMessageDistributer(MessageDistributer):
    def __init__ (self, idp):
        super().__init__(idp, HarvesterServices, HarvesterSecretary)

    def on_receive(self, body):
        """This function is called when the Message Distributer gets something told. In this case, whenever the harvester receives a message.
        """
        body_str = body.decode('utf8').replace("'", '"').replace(
        "True", "true"). replace("False", "false")  # convert bytes to str
        body_json = json.loads(body_str)
        print("Message received: ", body_json["messageType"], ":", body_json["serviceType"])
        if body_json["messageType"] == "serviceRequest":
            if body_json["serviceType"] == "fml40::FellingJob":
                self.secretary_proxy.acceptsFellingJobs()
        return "I'm the Message Distribution Actor and I distributed your message to one of my highly qualified actors"