1use std::any::Any;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use log::{info, error, debug};
5use async_trait::async_trait;
6use crate::service::{Service, WebSocketWrite};
7
8pub const BASE_VERSION: &str = "1.0.0";
10
11#[derive(Deserialize, Serialize, Debug, Clone)]
13#[serde(rename_all = "UPPERCASE")]
14pub enum BaseAction {
15 Check,
17 Print { action: String },
19 Ping,
21 Unknown,
23}
24
25impl From<&str> for BaseAction {
26 fn from(action: &str) -> Self {
28 match action {
29 "CHECK" => BaseAction::Check,
30 "PRINT" => BaseAction::Print { action: "print".to_string() },
31 "PING" => BaseAction::Ping,
32 _ => BaseAction::Unknown,
33 }
34 }
35}
36
37#[derive(Clone)]
39pub struct BaseService;
40
41impl BaseService {
42 pub fn new() -> Self {
44 info!("BaseService initialized (version: {})", BASE_VERSION);
45 BaseService
46 }
47}
48
49#[async_trait]
51impl Service for BaseService {
52 async fn run(&self, action: Value, _write: WebSocketWrite) -> (i32, String) {
54 let action_str = action.get("ACTION").and_then(|v| v.as_str()).unwrap_or("UNKNOWN");
55 if action_str == "PING" {
58 debug!("BaseService: Running action: {:?}", action);
59 } else {
60 info!("BaseService: Running action: {:?}", action);
61 }
62 match action_str {
63 "CHECK" => {
64 info!("BaseService: Performing check action");
65 (0, "check ok".to_string())
66 }
67 "PING" => {
68 debug!("BaseService: Performing ping action");
69 (0, "pong".to_string()) }
71 _ => {
72 error!("BaseService: Unknown action");
73 (1, "unknown action".to_string())
74 }
75 }
76 }
77
78 fn as_any(&self) -> &dyn Any {
80 self
81 }
82
83 fn stop_service(&self) {
85 info!("BaseService: Stopping service");
86 }
87
88 fn get_version(&self) -> String {
90 BASE_VERSION.to_string()
91 }
92}
93
94impl Default for BaseAction {
95 fn default() -> Self {
97 BaseAction::Unknown
98 }
99}