[PS]: initial
This commit is contained in:
parent
7c9ccb7c80
commit
b2254aaaab
11
.gitignore
vendored
11
.gitignore
vendored
@ -1,8 +1,11 @@
|
||||
# Emacs temp files
|
||||
*~
|
||||
|
||||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
**/debug/
|
||||
**/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
@ -10,7 +13,3 @@ Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
|
8
homework4/Cargo.toml
Normal file
8
homework4/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "homework4"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
159
homework4/src/main.rs
Normal file
159
homework4/src/main.rs
Normal file
@ -0,0 +1,159 @@
|
||||
enum PowerState {
|
||||
On,
|
||||
Off,
|
||||
}
|
||||
|
||||
struct PowerSocket {
|
||||
state: PowerState,
|
||||
consumption: u32,
|
||||
}
|
||||
|
||||
impl PowerSocket {
|
||||
fn new() -> Self {
|
||||
PowerSocket {
|
||||
state: PowerState::Off,
|
||||
consumption: 42,
|
||||
}
|
||||
}
|
||||
|
||||
// Распечатывает текущий статус розетки на STDOUT
|
||||
fn print_status(&self) {
|
||||
let r = self.report();
|
||||
println!("{r}");
|
||||
}
|
||||
|
||||
fn report(&self) -> String {
|
||||
let s: String = self.get_string_state();
|
||||
let c = self.get_consumption();
|
||||
|
||||
format!("---\ninfo:\n status: {}\n consumption: {}", s, c)
|
||||
}
|
||||
|
||||
// Получить текущий статус розетки
|
||||
fn get_state(&self) -> &PowerState {
|
||||
&self.state
|
||||
}
|
||||
|
||||
fn get_string_state(&self) -> String {
|
||||
let s = match self.get_state() {
|
||||
PowerState::Off => String::from("OFF"),
|
||||
PowerState::On => String::from("ON"),
|
||||
};
|
||||
s
|
||||
}
|
||||
|
||||
// Получить данные с аппаратного счетчика
|
||||
fn get_sensor_data(&self) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// получить текущее потребление
|
||||
fn get_consumption(&self) -> u32 {
|
||||
self.consumption
|
||||
}
|
||||
|
||||
// Установить текущее потребление
|
||||
fn set_consumption(&mut self, c: u32) {
|
||||
self.consumption = c;
|
||||
}
|
||||
|
||||
// Включить розетку
|
||||
fn on(&mut self) {
|
||||
// power on code
|
||||
self.state = PowerState::On;
|
||||
}
|
||||
|
||||
// Выключить розетку
|
||||
fn off(&mut self) {
|
||||
// power off code
|
||||
self.state = PowerState::Off;
|
||||
}
|
||||
|
||||
// Должен пускаться тред
|
||||
fn watch_consumption(&mut self) {
|
||||
loop {
|
||||
let d = self.get_sensor_data();
|
||||
if d != self.consumption {
|
||||
self.set_consumption(d)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Command {
|
||||
On,
|
||||
Off,
|
||||
GetStatus,
|
||||
GetConsumption,
|
||||
Report,
|
||||
Exit,
|
||||
}
|
||||
|
||||
struct Server();
|
||||
|
||||
impl Server {
|
||||
fn recv(&self) -> Command {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn send(&self, msg: String) {
|
||||
todo!("{msg}")
|
||||
}
|
||||
}
|
||||
|
||||
struct IpcServer {
|
||||
ipc_server: Server,
|
||||
addr: String,
|
||||
}
|
||||
|
||||
impl IpcServer {
|
||||
fn new(addr: String) -> Self {
|
||||
Self {
|
||||
ipc_server: Server(),
|
||||
addr,
|
||||
}
|
||||
}
|
||||
|
||||
fn listen_for_commands(&mut self) {
|
||||
// Тут должен запускаться тред
|
||||
println!("Listen to commands on {}", self.addr);
|
||||
self._run();
|
||||
}
|
||||
|
||||
fn recv(&self) -> Command {
|
||||
self.ipc_server.recv()
|
||||
}
|
||||
|
||||
pub fn send(&self, msg: String) {
|
||||
self.ipc_server.send(msg)
|
||||
}
|
||||
|
||||
fn _run(&mut self) {
|
||||
self.ipc_server = Server();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut s = PowerSocket::new();
|
||||
let mut ipc: IpcServer = IpcServer::new(String::from("unix:///run/pw.socket"));
|
||||
s.print_status();
|
||||
|
||||
s.watch_consumption();
|
||||
ipc.listen_for_commands();
|
||||
loop {
|
||||
// Ждем пока не прилетит комманда в unix сокет.
|
||||
// Скорее всего в rust это выглядит немного по другому
|
||||
let cmd = ipc.recv();
|
||||
match cmd {
|
||||
Command::Exit => {
|
||||
s.off();
|
||||
std::process::exit(0x0);
|
||||
}
|
||||
Command::On => s.on(),
|
||||
Command::Off => s.off(),
|
||||
Command::GetStatus => ipc.send(s.get_string_state()),
|
||||
Command::GetConsumption => ipc.send(s.get_consumption().to_string()),
|
||||
Command::Report => ipc.send(s.report()),
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user