pyjson.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2021 EMQ Technologies Co., Ltd.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # Licensed under the Apache License, Version 2.0 (the "License");
  16. # you may not use this file except in compliance with the License.
  17. # You may obtain a copy of the License at
  18. #
  19. # http://www.apache.org/licenses/LICENSE-2.0
  20. #
  21. # Unless required by applicable law or agreed to in writing, software
  22. # distributed under the License is distributed on an "AS IS" BASIS,
  23. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. # See the License for the specific language governing permissions and
  25. # limitations under the License.
  26. import logging
  27. import time
  28. from ekuiper import Source, Context
  29. class PyJson(Source):
  30. def __init__(self):
  31. self.data = {"name": "pyjson", "value": 2021}
  32. self.running = True
  33. def configure(self, datasource: str, conf: dict):
  34. logging.info("configuring with datasource {} and conf {}".format(datasource, conf))
  35. # noinspection PyTypeChecker
  36. def open(self, ctx: Context):
  37. print("opening")
  38. while self.running:
  39. ctx.emit(self.data, None)
  40. time.sleep(0.2)
  41. print("closed")
  42. def close(self, ctx: Context):
  43. print("closing")
  44. self.running = False