1use std::any::Any;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use log::{info, error};
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 info!("BaseService: Running action: {:?}", action);
55 let action_str = action.get("ACTION").and_then(|v| v.as_str()).unwrap_or("UNKNOWN");
56 match action_str {
57 "CHECK" => {
58 info!("BaseService: Performing check action");
59 (0, "check ok".to_string())
60 }
61 "PING" => {
62 info!("BaseService: Performing ping action");
63 (0, "pong".to_string()) }
65 _ => {
66 info!("BaseService: Unknown action");
67 error!("BaseService: Unknown action");
68 (1, "unknown action".to_string())
69 }
70 }
71 }
72
73 fn as_any(&self) -> &dyn Any {
75 self
76 }
77
78 fn stop_service(&self) {
80 info!("BaseService: Stopping service");
81 }
82
83 fn get_version(&self) -> String {
85 BASE_VERSION.to_string()
86 }
87}
88
89impl Default for BaseAction {
90 fn default() -> Self {
92 BaseAction::Unknown
93 }
94}